Skip to main content

Migration library for Clickhouse

Project description

ClickHouse Migrate

codecov PyPI version

Python library for creating and applying migrations in ClickHouse database.

Installation

Installation via PyPi:

pip install clickhouse-migrate

Usage

To use this tool it is required to create and provide a directory in which the migration files will be created, stored and from which they will be applied. clickhouse-migrate tool supports clustered ClickHouse setup and therefore it requires ALL clickhouse servers that are clustered as a database parameter.

Environment variables

Required parameters can be provided as environment variables

# Comma separated ClickHouse connection strings from one cluster
# Can be as a single connection string in case of standalone clickhouse instance

CLICKHOUSE_MIGRATE_DATABASES="clickhouse+native://<user>:<pass>@<host1>:<port>, ... ,clickhouse+native://<user>:<pass>@<host2>:<port>"
CLICKHOUSE_MIGRATE_DIRECTORY=<path to directory with migrations files>

Create migration file

One can create a new migration via calling clickhouse-migrate create_migration command.

Command parameters:

-n / --name <name of a new migration file> — this is a required parameter

-dir / --migration_dir — optional parameter for providing path to directory with migration files, default value ./migrations, can be replaced by CLICKHOUSE_MIGRATE_DIRECTORY environment variable

Example usage:

clickhouse-migrate create_migration -n test_migration -dir migrations

After calling the above-mentioned command a blank migration file YYYY-MM-DD-HH-mm-ss_<migration_name>.py will be created in the migrations directory which was defined in command line arguments.

The content of the created file will look like this

from clickhouse_migrate import Step

migrations = [Step(sql="")]

One should use Step dataclass to create migrations. Migrations would be applied in the same order that they are stated in the migrations list variable. All of listed changes in the migration list variable are by default applied to all databases listed in the *.ini config file, since usually changes are required for all replicated instances.

If an SQL query stated in the Step dataclass should not be executed for all DB replicas one may use Step(sql="<sql query>", replicated=False) statement.

This rule does not apply in case there is only one connection string in *.ini file.

Apply migrations

One can apply migrations created via clickhouse-migrate create_migration command by calling clickhouse-migrate migrate. This command will check for already applied migrations and will only apply new ones.

Command parameters:

-db / --databases — optional parameter for providing database connection strings, can be replaced by CLICKHOUSE_MIGRATE_DATABASES environment variable

-dir / --migration_dir — optional parameter for providing path to directory with migration files, can be replaced by CLICKHOUSE_MIGRATE_DIRECTORY environment variable

Example usage:

clickhouse-migrate migrate -dir /home/my_project/migrations -db clickhouse+native://default:@host1:9000/db -db clickhouse+native://default:@host2:9000/db

After calling this command all changes from migration files will be applied step-by-step. Changes are stored in clickhouse_migrate table.

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

clickhouse-migrate-0.0.4.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

clickhouse_migrate-0.0.4-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file clickhouse-migrate-0.0.4.tar.gz.

File metadata

  • Download URL: clickhouse-migrate-0.0.4.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.7

File hashes

Hashes for clickhouse-migrate-0.0.4.tar.gz
Algorithm Hash digest
SHA256 5abfbb8f35278e81171aed4cfbf8565715bd7acb5c7a9c94789dae609e837c25
MD5 4f5fcc5c9b87593f8835c495cb53976f
BLAKE2b-256 2404b115b6e335feae558553b37abf7b26f1c5245682435909926996fdefedc7

See more details on using hashes here.

File details

Details for the file clickhouse_migrate-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for clickhouse_migrate-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 32478f427aa9cc21b5a7bf087fd14062234480b43d4d909a45285c86add65ad9
MD5 3927800ddb25d255099cf7cc5dce9f16
BLAKE2b-256 c9d1ab66c23ff75e8fce97600cfbc8b5ce837b7161d148c41cd4ee495055059d

See more details on using hashes here.

Supported by

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