Skip to main content

Postgres version control system

Project description

psql_vcs - PostgreSQL Version Control System

English Russian

About project

The project was created to support an easy-to-develop PostgreSQL database migration system. The library allows for automatic change tracking, creation of SQL scripts for migration, and migration to the latest database version, including creating a database if it doesn't exist.

[!CAUTION] Library based on results, so generated SQL code for migration inherits problems that are or may be in this library


Installation

pip install psql_vcs

Usage

Firstly you need to create migration files.

Migrations are created in a PostgreSQL database, allowing you to freely separate the servers where the migration database is stored and the servers where these migrations are applied.

Connecting to the server is accomplished by instantiating one of two classes, depending on your preferred connection method. You can connect by specifying the host, port, username, and password using AuthArgs, or use URLArgs to connect via a URL string with the PostgreSQL schema (such as postgres://login:password@host:port/database).

Initialization example with AuthArgs

from psql_vcs import PostgresMigrator, AuthArgs

migrator = PostgresMigrator(
    AuthArgs(
        target_server_host='localhost',
        target_server_port=5432,
        target_server_username='username',
        target_server_password='password',
        target_database='my_db'
    )
)

Initialization example with URLArgs

from psql_vcs import PostgresMigrator, URLArgs

migrator = PostgresMigrator(
    URLArgs(
        target_database_url="postgres://username:password@localhost:5432/my_db"
    )
)

Additional

You can also specify connection details for the target database separately.

Also, sometimes you need to create a test database, for example, to generate a migration script (to compare the latest existing schema with the new target). By default, the server storing migration records is used for this. However, like the connection to the target database, this can be specified separately.

from psql_vcs import AuthArgs

args = AuthArgs(
    target_database = "my_db",
    target_server_host = "localhost",
    target_server_port = 5432,
    target_server_username = "username",
    target_server_password = "password",
    target_server_main_database = "postgres",
    migration_server_host = "localhost",
    migration_server_port = 5432,
    migration_server_username = "username",
    migration_server_password = "password",
    migration_server_main_database = "postgres",
    migration_server_migrations_database = "my_migrations",
    migration_server_test_database = "my_test_for_migrations",
    migration_name = "special_tag"
)
from psql_vcs import URLArgs

args = URLArgs(
    target_database_url = "postgres://username:password@localhost:5432/my_db",
    migrations_database_url = "postgres://username:password@localhost:5432/my_mirations",
    migrations_main_database_url = "postgres://username:password@localhost:5432/postgres",
    migration_server_test_database = "postgres://username:password@localhost:5432/my_migrations_test",
    target_server_main_database_url = "postgres://username:password@localhost:5432/postgres",
    migration_name = "special_tag"
)

If you don't define specific links, default values are used according to the following rules:

  • The username and password are taken from the target database.
  • The name of the "primary" database is postgres.
  • The name of the database with migrations is psql_vcs_migrations_db.
  • The name of the database for creating test schemas is psql_vcs_test_db (always deleted after use).

Additionally, each connection argument class has a migration_name argument: by default, the unique "key" linking a migration chain is determined by the name of the database for which these migrations are compiled. However, if you plan to use migrations with a single schema on multiple servers containing databases with different names but the same schema, you can define a tag for such a migration chain.


Create migrations

To create a migration, use the create_migraton method.

from psql_vcs import PostgresMigrator, URLArgs

migrator = PostgresMigrator(URLArgs("..."))

migrator.create_migration()

If this is the first method call for the selected database/tag (the migration_name argument), a "zero" file will be created, containing code for creating the current database from scratch.

Essentially, the only difference is that the current migration file will be called when creating the database (if no database exists when running migrations) and will serve as the primary one for the correct operation of subsequent migrations.


Make migrations

To migrate to the latest version, use the migrate_to_last_version method.

from psql_vcs import PostgresMigrator, URLArgs

migrator = PostgresMigrator(URLArgs("..."))

migrator.migrate_to_last_version()

The method will compare the current target_base schema with the schemas stored in the migration list. If migration_name is passed, only the corresponding migrations will be compared.

The corresponding SQL commands will then be executed to bring the database up to date.


Sync migrations between projects / servers

If you can't connect to the server storing migrations from the target server where the migration is running (or vice versa), or if you want to separate the architecture of saving and running migrations, you can save the migration history to a file and restore it on the desired server. To do this, first save the migrations to a file:

from psql_vcs import PostgresMigrator, URLArgs

migrator = PostgresMigrator(URLArgs("..."))

migrator.save_migrations('migrations.pkl')

Now you can transfer this file to the target server, sync it via the Git repository, etc.

And restore the migrations on the target server for further use:

from psql_vcs import PostgresMigrator, URLArgs

migrator = PostgresMigrator(URLArgs("..."))

migrator.load_migrations('migrations.pkl')

[!NOTE] The load_migrations method, like the migrate_to_last_version method, allows you to call them constantly, for example, when starting a project, to bring the database up to date, and will not raise an exception if migrations have already been restored or the database has already been brought to the latest version.

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

psql_vcs-1.0.0a0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

psql_vcs-1.0.0a0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file psql_vcs-1.0.0a0.tar.gz.

File metadata

  • Download URL: psql_vcs-1.0.0a0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for psql_vcs-1.0.0a0.tar.gz
Algorithm Hash digest
SHA256 4fb039dee59c56f7918a82c6bcde5267425faf8f4e098fada6cb6206053d3ab7
MD5 7261e6ca9f01bf178c5c55d79faa8d1d
BLAKE2b-256 b7fdb11d5e6a7eded4ebae988d7ddc496a4fa0ea0493c72610003de614f5125b

See more details on using hashes here.

Provenance

The following attestation bundles were made for psql_vcs-1.0.0a0.tar.gz:

Publisher: pip_publish.yml on Beloborod/psql_vcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file psql_vcs-1.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: psql_vcs-1.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for psql_vcs-1.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 4be6e3a7b81067a8910f517e815dcb08f431873b2357114e0990af7346c87730
MD5 07246ea0415c98e9c6cd5e07973fd942
BLAKE2b-256 7dbda13021c39e177fef097a4b5abb693a4b2cfe29a138b99890872a6da584c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for psql_vcs-1.0.0a0-py3-none-any.whl:

Publisher: pip_publish.yml on Beloborod/psql_vcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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