Skip to main content

Diffable SQL

This is a small tool that outputs SQL from a number of databases in a sorted, diff-friendly order.

Install

The tool supports any database that SQLAlchemy supports. You must install the correct connector for your database:

$ pip install diffable-sql
$ pip install psycopg2-binary # postgres
$ pip install mysqlclient # mysql

Usage

You can pass any number of DSNs as arguments, for example postgresql:///db-name. The tool will output normalised SQL DDL statements for each DSN.

$ diffable-sql sqlite:///db.sqlite3
CREATE TABLE sometable (
	foo VARCHAR,
	id BIGINT NOT NULL
);

ALTER TABLE sometable ADD CONSTRAINT c PRIMARY KEY (id);
ALTER TABLE sometable ADD CONSTRAINT c UNIQUE (foo);
CREATE INDEX i ON sometable (foo);

Specifically:

  • The tables and columns will be sorted by name
  • Indexes and constraints will be renamed as i and c
  • Deferrable constraint information will be hidden
  • Identity columns will be hidden

These allow schemas to be more easily diffed.

JSON output

Passing --output-format=json will output a json structure for each table, containing information on the column types, SQL, nullability, indexes and constraints:

$ poetry run diffable-sql sqlite:///tools/db.sqlite3 --output-format=json | jq

Outputs:

{
  "name": "sometable",
  "database": "tools/db.sqlite3",
  "columns": {
    "id": {
      "type": "BIGINT",
      "sql": "id BIGINT NOT NULL",
      "nullable": false
    },
    "foo": {
      "type": "VARCHAR",
      "sql": "foo VARCHAR",
      "nullable": true
    }
  },
  "indexes": [
    {
      "name": "foobar",
      "type": "Index",
      "sql": "CREATE INDEX foobar ON sometable (foo)",
      "columns": [
        "foo"
      ],
      "expressions": []
    }
  ],
  "constraints": [
    {
      "name": null,
      "type": "PrimaryKeyConstraint",
      "sql": "ALTER TABLE sometable ADD PRIMARY KEY (id)",
      "columns": [
        "id"
      ],
      "expression": null
    },
    {
      "name": null,
      "type": "UniqueConstraint",
      "sql": "ALTER TABLE sometable ADD UNIQUE (foo)",
      "columns": [
        "foo"
      ],
      "expression": null
    }
  ]
}

Args

$ diffable-sql --help
Usage: diffable-sql [OPTIONS] [dsn]...

Options:
  --no-rename-indexes TEXT        Don't rename indexes
  --no-rename-constraints TEXT    Don't rename constraints
  --no-ignore-deferrable-constraints TEXT
                                  Include deferrable constraint information
  --no-ignore-identity-columns TEXT
                                  Don't ignore identity column information
  --output-format [sql|json]
  --help                          Show this message and exit.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffable_sql-0.2.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

diffable_sql-0.2.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file diffable_sql-0.2.0.tar.gz.

File metadata

  • Download URL: diffable_sql-0.2.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for diffable_sql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f1023f3e023a0ad5cbf5e2ad183939a513bef10c7ab32e21fe99ab38ffe2369d
MD5 0857a64b0d22ed7050db3fd12ef9e644
BLAKE2b-256 433fbade6b922c25b1e00c2850865ccfe63f94a61f7a108d9727d4952e4e8e8f

See more details on using hashes here.

File details

Details for the file diffable_sql-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: diffable_sql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for diffable_sql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d898942fde9b198420ec3f9127682614bd3f048c568506f68171121c5812cf2c
MD5 2ddcbe080a49b9a97a454fb67bdc4f41
BLAKE2b-256 0c47b114e60eac52a8880da2f09c64c8029435a1c2f4746d46122b7185a0366e

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page