Skip to main content

A database migration tool for Neo4j that allows to apply Cypher-based and arbitrary Python-based migrations.

Project description

python version version Codecov

neo4j-python-migrations

It is a database migration tool for Neo4j written in Python that allows to apply not only Cypher migrations, but also arbitrary Python-based migrations.

This tool is inspired by Michael Simons tool for Java and works directly on neo4j-python-driver.

Features

  • Python migration support makes it possible to do any things in your migration that Python allows you to do.
  • Cypher-based migrations support.
  • It can be used either via the command line or directly in your code.
  • Multi-database support for Neo4j Enterprise Edition users.
  • The ability to separate logically independent migration chains within a single database (see the project option). May be useful for Neo4j Community Edition users.

Installation

From PyPi:

pip3 install neo4j-python-migrations

If you want to install it from sources, try this:

python3 -m pip install poetry
python3 -m pip install .
python3 -m neo4j_python_migrations 

Usage

Creating migrations

Naming Convention

Each migration will be a Cypher or Python file following the format V<sem_ver>__<migration_name>.ext.

Make sure to follow the naming convention as stated in Michael's tool documentation (except that .py files are allowed).

Cypher

Just create a Cypher file with your custom script, for example ./migrations/V0001__initial.cypher:

CREATE CONSTRAINT UniqueAuthor IF NOT EXISTS FOR (a:AUTHOR) REQUIRE a.uuid IS UNIQUE;
CREATE INDEX author_uuid_index IF NOT EXISTS FOR (a:AUTHOR) ON (a.uuid);

This script will be executed within a single transaction. Therefore, if you need both DDL and DML commands, split them into different files.

Python

Python-based migrations should have a special format, for example ./migrations/V0002__drop_index.py:

from neo4j import Transaction


# This function must be present
def up(tx: Transaction):
    tx.run("DROP CONSTRAINT UniqueAuthor")

Applying migrations

CLI

You can apply migrations or verify the status of migrations using the command line interface:

Usage: python -m neo4j_python_migrations [OPTIONS] COMMAND [ARGS]...

Options:
  --username TEXT                 The login of the user connecting to the
                                  database.  [env var: NEO4J_MIGRATIONS_USER;
                                  default: neo4j]
  --password TEXT                 The password of the user connecting to the
                                  database.  [env var: NEO4J_MIGRATIONS_PASS;
                                  default: neo4j]
  --path PATH                     The path to the directory for scanning
                                  migration files.  [env var:
                                  NEO4J_MIGRATIONS_PATH; required]
  --port INTEGER                  Port for connecting to the database  [env
                                  var: NEO4J_MIGRATIONS_PORT; default: 7687]
  --host TEXT                     Host for connecting to the database  [env
                                  var: NEO4J_MIGRATIONS_HOST; default:
                                  127.0.0.1]
  --scheme TEXT                   Scheme for connecting to the database
                                  [default: neo4j]
  --project TEXT                  The name of the project for separating
                                  logically independent migration chains
                                  within a single database.  [env var:
                                  NEO4J_MIGRATIONS_PROJECT]
  --schema-database TEXT          The database that should be used for storing
                                  information about migrations (Neo4j EE). If
                                  not specified, then the database that should
                                  be migrated is used.  [env var:
                                  NEO4J_MIGRATIONS_SCHEMA_DATABASE]
  --database TEXT                 The database that should be migrated (Neo4j
                                  EE)  [env var: NEO4J_MIGRATIONS_DATABASE]
  --install-completion [bash|zsh|fish|powershell|pwsh]
                                  Install completion for the specified shell.
  --show-completion [bash|zsh|fish|powershell|pwsh]
                                  Show completion for the specified shell, to
                                  copy it or customize the installation.
  --help                          Show this message and exit.

Commands:
  analyze  Analyze migrations, find pending and missed.
  migrate  Retrieves all pending migrations, verify and applies them.

So, to apply migrations, just run the command:

python3 -m neo4j_python_migrations --username neo4j --password test --path ./migrations migrate

Note: it is more secure to store the password in the environment variable NEO4J_MIGRATIONS_PASS.

Python Code

You can apply migrations directly into your application:

from pathlib import Path

from neo4j import GraphDatabase

from neo4j_python_migrations.executor import Executor

with GraphDatabase.driver("neo4j://localhost:7687", auth=("neo4j", "test")) as driver:
    executor = Executor(driver, migrations_path=Path("./migrations"))
    executor.migrate()

Available methods: migrate, analyze.

How migrations are tracked

Information about the applied migrations is stored in the database using the schema described in Michael's README.

Supported migration types: СYPHER, PYTHON. Other types of migrations, such as JAVA, are not supported.

Note: the project option are incompatible with this schema. When using the option, each migration nodes will have an additional property named project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neo4j_python_migrations-0.1.4.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neo4j_python_migrations-0.1.4-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file neo4j_python_migrations-0.1.4.tar.gz.

File metadata

  • Download URL: neo4j_python_migrations-0.1.4.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for neo4j_python_migrations-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e52afcc2de7c7d5dc713ffb0d25abd355c630c130c0b1a0c147c46ad051eaea3
MD5 53cd122a1746550715cd38cfe61488ef
BLAKE2b-256 f0f96e0f8ddb423a3d654ea1c75e0899461e955688948af996066985633cbe06

See more details on using hashes here.

File details

Details for the file neo4j_python_migrations-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: neo4j_python_migrations-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for neo4j_python_migrations-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a620047188745e8aed714426b88e722cbf2b2f350389eb2023514db39cc339f3
MD5 0f83ac4e6c51447345cc1224b38f1f33
BLAKE2b-256 09d9140f1abdbecdec458266b34186a017007f3ae2e1896206e5194648a0ee1b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page