Schema syncing examples and options

For naming clarity in the follow examples, we'll use a and b as the two databases.

a might also be thought of as the initial or current state of the database, and b the desired/target/future state. They also match the convention of other diff tools such as the unix diff command itself.

It's important to get there the right way around - reversing the order will result in the inverse operation(s).

We can get the diff as a single combined SQL string, or a list of statements.

import databaseci

a = databaseci.db('a')
b = databaseci.db('b')

diff_sql = a.schemadiff_as_sql(b)
diff_statements = a.schemadiff_as_statements(b)

For even more succinctness, there's a one-liner available:

schema = "create table ...; create view ..."

a.sync_schema_to(schema)

Note that this requires PostgreSQL running locally (in order to create a temporary DB in which to temporily load the schema definition).

For safety reasons, this method has a prompt that requires an input of "yes" before applying any changes.

To disable this, set prompt=False. Or set prompt to a string to change the prompt text.

Command line usage

Just specify the two databases by name or URL as follows:

databaseci dbdiff a b
# or 
databaseci dbdiff postgresql:///a postgresql:///b

This command conforms to the same conventions of the standard diff command. In particular, with return codes: 0 ("success") on no differences, and non-zero on differences ("failure").

All parameters/command line options