Skip to main content

Postgres version control system

Project description

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.

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-0.1.2.tar.gz (9.8 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-0.1.2-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file psql_vcs-0.1.2.tar.gz.

File metadata

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

File hashes

Hashes for psql_vcs-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7325d7e9c1deece058336a9ecc3a1e0e83a3b2942c55adb1567e1feba7ebf08a
MD5 20dcdb555ed73bafa1e4ec5bf08a6583
BLAKE2b-256 c849efa36820ee0599bd0f5cedd606b1f1dacd2c170ea15693489eaa92d803f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for psql_vcs-0.1.2.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-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: psql_vcs-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.4 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-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8cf503397c65562eb1002b4df221e526e7d49628d6fa8b945cdc434571fd5006
MD5 17212a61ff40862d9c9d70ebb443d500
BLAKE2b-256 4398b95afffb2dcb97fb090f2ad17e84e3e4c91d91ed338a8599b5a8bba36d26

See more details on using hashes here.

Provenance

The following attestation bundles were made for psql_vcs-0.1.2-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