项目作者: srittau

项目描述 :
Database Upgrading Framework
高级语言: Python
项目地址: git://github.com/srittau/dbupgrade.git
创建时间: 2018-02-07T21:17:50Z
项目社区:https://github.com/srittau/dbupgrade

开源协议:MIT License

下载


dbupgrade

Database Migration Tool

Python Version
License MIT
GitHub Releases
pypi
GitHub Actions build

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:

  1. -- Schema: my-db-schema
  2. -- Version: 25
  3. -- API-Level: 3
  4. -- Dialect: postgresql
  5. 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 dialect identifier, e.g. postgresql or sqlite, but do not include
    the driver used to connect to the database. E.g. use postgresql instead of
    postgresql+psycopg.
  • 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 the row
does not exist, it will be created with the version and api_level columns
initially set to 0.

The current version and API level of the schema are retrieved from the
database, and all scripts with a higher version number are applied in order.
If any version numbers are 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 separate transaction. If a script fails, all
changes made by that script will be rolled back, and the script will terminate
with an error message and a non-zero return status.

JSON Output

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

  1. {
  2. "success": true,
  3. "oldVersion": {
  4. "version": 123,
  5. "apiLevel": 15
  6. },
  7. "newVersion": {
  8. "version": 125,
  9. "apiLevel": 16
  10. },
  11. "appliedScripts": [
  12. {
  13. "filename": "0124-create-foo.sql",
  14. "version": 124,
  15. "apiLevel": 15
  16. },
  17. {
  18. "filename": "0125-delete-bar-sql",
  19. "version": 125,
  20. "apiLevel": 16
  21. }
  22. ],
  23. "failedScript": {
  24. "filename": "0126-change-stuff.sql",
  25. "version": 126,
  26. "apiLevel": 16
  27. }
  28. }

The success key is true if all scripts were applied successfully or if no
scripts needed to be applied. In this case, the failedScript key is not
included. The appliedScripts key is always present and contains an array
of applied scripts. If no scripts were applied, this array is empty.