Skip to main content

Database Migration Tool

Project description

dbupgrade

Database Migration Tool

Python MIT License GitHub pypi GitHub Actions

Basic Usage

Usage: dbupgrade [OPTIONS] [-l API_LEVEL|-L] DBNAME SCHEMA DIRECTORY

Upgrade the given SCHEMA in the database specified as DBNAME with SQL scripts from DIRECTORY. DIRECTORY is searched for all files with the .sql suffix. These files are SQL scripts with a special header sections:

-- Schema: my-db-schema
-- Version: 25
-- API-Level: 3
-- Dialect: postgres

CREATE TABLE ...

The following headers are required:

  • Schema
    Name of the schema to update.
  • Dialect
    Database dialect of this script. Use SQLalchemy's database URL scheme identifier, e.g. postgres or sqlite.
  • Version
    The new version of the schema after this script was applied. It is an error if two scripts have the same schema, dialect, and version.
  • API-Level
    The new API level of the schema after this script was applied. For a given schema, the API level of a subsequent version must either be equal or higher by one than the API level of the preceding version. For example, if script version 44 has API level 3, script version 45 must have API level 3 or 4.
  • Transaction (optional)
    Possible values are yes (default) and no. When this header is yes, all statements of a single upgrade file and the corresponding version upgrade statements are executed within a single transaction. Otherwise each statement is executed separately. The former is usually preferable so that all changes will be rolled back if a script fails to apply, but the latter is required in some cases.

The database must contain a table db_config with three columns: schema, version, and api_level. If this table does not exist, it is created. This table must contain exactly one row for the given schema. If this row does not exist, it is created with version and api_level initially set to 0.

The current version and API level of the schema are requested from the database and all scripts with a higher version number are applied, in order. If there are any version numbers missing, the script will stop after the last version before the missing version.

Unless the -l or -L option is supplied, only scripts that do not increase the API level will be applied. If the -l option is given, all scripts up to the given API level will be applied. -L will apply all scripts without regard to the API level.

Each script is executed in a seperate transaction. If a script fails, all changes in that script will be rolled back and the script will stop with an error message and a non-zero return status.

JSON Output

When supplying the --json option, dbupgrade will information about the applied scripts as JSON to the standard output. Sample output:

{
  "success": true,
  "oldVersion": {
    "version": 123,
    "apiLevel": 15
  },
  "newVersion": {
    "version": 125,
    "apiLevel": 16
  },
  "appliedScripts": [
    {
      "filename": "0124-create-foo.sql",
      "version": 124,
      "apiLevel": 15
    },
    {
      "filename": "0125-delete-bar-sql",
      "version": 125,
      "apiLevel": 16
    }
  ],
  "failedScript": {
    "filename": "0126-change-stuff.sql",
    "version": 126,
    "apiLevel": 16
  }
}

success is true if all scripts were applied successfully or no scripts were to be applied. In this case, the failedScript key is not defined. The appliedScripts key is always defined. In case no scripts were applied, it's an empty array.

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

dbupgrade-2023.2.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

dbupgrade-2023.2.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file dbupgrade-2023.2.0.tar.gz.

File metadata

  • Download URL: dbupgrade-2023.2.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.7 Linux/5.19.0-29-generic

File hashes

Hashes for dbupgrade-2023.2.0.tar.gz
Algorithm Hash digest
SHA256 7c9e2638c013dc1e6d473bc95b3c161f6e32e2e4c29ccd9ebaeb98f546eb979f
MD5 cfeb8ddc2c65cf9e0b27cbab939bafd1
BLAKE2b-256 d96c508029c73478e1cd4a5927c78e95a5f6020719443e710415776a81035d98

See more details on using hashes here.

File details

Details for the file dbupgrade-2023.2.0-py3-none-any.whl.

File metadata

  • Download URL: dbupgrade-2023.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.7 Linux/5.19.0-29-generic

File hashes

Hashes for dbupgrade-2023.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1895662b92697cec142679ebe9c19bdfe3382c30386c4ea3544f0c47d856eba6
MD5 b846f94e45f007f23c7c30c4a121f401
BLAKE2b-256 c4e0f7d57f02f941a36b0b72868293e1fbb7f71ea1fb270d6f4256adbb382310

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