API Documentation

Module migrate.changeset

This module extends SQLAlchemy and provides additional DDL [1] support.

[1]SQL Data Definition Language

Module ansisql

Extensions to SQLAlchemy for altering existing tables.

At the moment, this isn’t so much based off of ANSI as much as things that just happen to work with multiple databases.

class migrate.changeset.ansisql.ANSIColumnDropper(connection)

Extends ANSI SQL dropper for column dropping (ALTER TABLE DROP COLUMN).

visit_column(column)

Drop a column from its table.

Parameter:column (sqlalchemy.Column) – the column object
class migrate.changeset.ansisql.ANSIColumnGenerator(dialect, connection, checkfirst=False, tables=None, **kwargs)

Extends ansisql generator for column creation (alter table add col)

visit_column(column)

Create a column (table already exists).

Parameter:column (sqlalchemy.Column) – column object
visit_table(table)

Default table visitor, does nothing.

Parameter:table (sqlalchemy.Table) – table object
class migrate.changeset.ansisql.ANSIConstraintCommon(connection)

Migrate’s constraints require a separate creation function from SA’s: Migrate’s constraints are created independently of a table; SA’s are created at the same time as the table.

get_constraint_name(cons)

Gets a name for the given constraint.

If the name is already set it will be used otherwise the constraint’s autoname method is used.

Parameter:cons (migrate.changeset.constraint.ConstraintChangeset) – constraint object
class migrate.changeset.ansisql.ANSIFKGenerator(*args, **kwargs)

Extends ansisql generator for column creation (alter table add col)

visit_column(column)
Create foreign keys for a column (table already exists); #32
class migrate.changeset.ansisql.ANSISchemaChanger(dialect, connection, checkfirst=False, tables=None, **kwargs)

Manages changes to existing schema elements.

Note that columns are schema elements; ALTER TABLE ADD COLUMN is in SchemaGenerator.

All items may be renamed. Columns can also have many of their properties - type, for example - changed.

Each function is passed a tuple, containing (object,name); where object is a type of object you’d expect for that function (ie. table for visit_table) and name is the object’s new name. NONE means the name is unchanged.

visit_column(delta)
Rename/change a column.
visit_index(param)
Rename an index; #36
visit_table(param)
Rename a table. Other ops aren’t supported.
class migrate.changeset.ansisql.AlterTableVisitor(connection)
Common operations for ALTER TABLE statements
class migrate.changeset.ansisql.RawAlterTableVisitor

Common operations for ALTER TABLE statements.

start_alter_table(param)

Returns the start of an ALTER TABLE SQL-Statement.

Use the param object to determine the table name and use it for building the SQL statement.

Parameter:param (sqlalchemy.Column, sqlalchemy.Index, sqlalchemy.schema.Constraint, sqlalchemy.Table, or string (table name)) – object to determine the table from

Module constraint

This module defines standalone schema constraint classes.

class migrate.changeset.constraint.CheckConstraint(sqltext, *args, **kwargs)
Check constraint class.
class migrate.changeset.constraint.ConstraintChangeset

Base class for Constraint classes.

accept_schema_visitor(visitor, *p, **k)
Raises:NotImplementedError if this method is not overridden by a subclass
autoname()

Automatically generate a name for the constraint instance.

Subclasses must implement this method.

Raises:NotImplementedError if this method is not overridden by a subclass
create(engine=None)

Create the constraint in the database.

Parameter:engine (sqlalchemy.engine.base.Engine) – the database engine to use. If this is None the instance’s engine will be used
drop(engine=None)

Drop the constraint from the database.

Parameter:engine (sqlalchemy.engine.base.Engine) – the database engine to use. If this is None the instance’s engine will be used
class migrate.changeset.constraint.ForeignKeyConstraint(columns, refcolumns, *p, **k)

Foreign key constraint class.

autoname()
Mimic the database’s automatic constraint names
class migrate.changeset.constraint.PrimaryKeyConstraint(*cols, **kwargs)

Primary key constraint class.

autoname()
Mimic the database’s automatic constraint names

Module databases

This module contains database dialect specific changeset implementations.

Module mysql

MySQL database specific implementations of changeset classes.

Module oracle

Oracle database specific implementations of changeset classes.

Module postgres

PostgreSQL database specific implementations of changeset classes.

class migrate.changeset.databases.postgres.PGColumnDropper(connection)
PostgreSQL column dropper implementation.
class migrate.changeset.databases.postgres.PGColumnGenerator(dialect, connection, checkfirst=False, tables=None, **kwargs)
PostgreSQL column generator implementation.
class migrate.changeset.databases.postgres.PGConstraintDropper(connection)
PostgreSQL constaint dropper implementation.
class migrate.changeset.databases.postgres.PGConstraintGenerator(connection)
PostgreSQL constraint generator implementation.
class migrate.changeset.databases.postgres.PGSchemaChanger(dialect, connection, checkfirst=False, tables=None, **kwargs)
PostgreSQL schema changer implementation.
class migrate.changeset.databases.postgres.PGSchemaGeneratorMixin
Common code used by the PostgreSQL specific classes.

Module sqlite

SQLite database specific implementations of changeset classes.

Module visitor

Module for visitor class mapping.

migrate.changeset.databases.visitor.get_dialect_visitor(sa_dialect, name)

Get the visitor implementation for the given dialect.

Finds the visitor implementation based on the dialect class and returns and instance initialized with the given name.

migrate.changeset.databases.visitor.get_engine_visitor(engine, name)
Get the visitor implementation for the given database engine.

Module exceptions

This module provides exception classes.

exception migrate.changeset.exceptions.Error
Changeset error.
exception migrate.changeset.exceptions.InvalidConstraintError
Invalid constraint error.
exception migrate.changeset.exceptions.NotSupportedError
Not supported error.

Module schema

Schema module providing common schema operations.

migrate.changeset.schema.alter_column(*p, **k)

Alter a column.

Parameters: column name, table name, an engine, and the properties of that column to change

migrate.changeset.schema.rename_table(table, name, engine=None)
Rename a table, given the table’s current name and the new name.
migrate.changeset.schema.rename_index(index, name, table=None, engine=None)

Rename an index.

Takes an index name/object, a table name/object, and an engine. Engine and table aren’t required if an index object is given.

Module migrate.versioning

This package provides functionality to create and manage repositories of database schema changesets and to apply these changesets to databases.

Module api

This module provides an external API to the versioning system.

Changed in version 0.4.5: --preview_sql displays source file when using SQL scripts. If Python script is used, it runs the action with mocked engine and returns captured SQL statements.

Changed in version 0.4.5: Deprecated --echo parameter in favour of new migrate.versioning.util.construct_engine() behavior.

migrate.versioning.api.help(cmd=None, **opts)

%prog help COMMAND

Displays help on a given command.

migrate.versioning.api.create(repository, name, **opts)

%prog create REPOSITORY_PATH NAME [–table=TABLE]

Create an empty repository at the specified path.

You can specify the version_table to be used; by default, it is ‘migrate_version’. This table is created in all version-controlled databases.

migrate.versioning.api.script(description, repository, **opts)

%prog script [–repository=REPOSITORY_PATH] DESCRIPTION

Create an empty change script using the next unused version number appended with the given description.

For instance, manage.py script “Add initial tables” creates: repository/versions/001_Add_initial_tables.py

migrate.versioning.api.script_sql(database, repository, **opts)

%prog script_sql [–repository=REPOSITORY_PATH] DATABASE

Create empty change SQL scripts for given DATABASE, where DATABASE is either specific (‘postgres’, ‘mysql’, ‘oracle’, ‘sqlite’, etc.) or generic (‘default’).

For instance, manage.py script_sql postgres creates: repository/versions/001_postgres_upgrade.sql and repository/versions/001_postgres_postgres.sql

migrate.versioning.api.make_update_script_for_model(url, oldmodel, model, repository, **opts)

%prog make_update_script_for_model URL OLDMODEL MODEL REPOSITORY_PATH

Create a script changing the old Python model to the new (current) Python model, sending to stdout.

NOTE: This is EXPERIMENTAL.

migrate.versioning.api.version(repository, **opts)

%prog version REPOSITORY_PATH

Display the latest version available in a repository.

migrate.versioning.api.source(version, dest=None, repository=None, **opts)

%prog source VERSION [DESTINATION] –repository=REPOSITORY_PATH

Display the Python code for a particular version in this repository. Save it to the file at DESTINATION or, if omitted, send to stdout.

migrate.versioning.api.version_control(url, repository, version=None, **opts)

%prog version_control URL REPOSITORY_PATH [VERSION]

Mark a database as under this repository’s version control.

Once a database is under version control, schema changes should only be done via change scripts in this repository.

This creates the table version_table in the database.

The url should be any valid SQLAlchemy connection string.

By default, the database begins at version 0 and is assumed to be empty. If the database is not empty, you may specify a version at which to begin instead. No attempt is made to verify this version’s correctness - the database schema is expected to be identical to what it would be if the database were created from scratch.

migrate.versioning.api.db_version(url, repository, **opts)

%prog db_version URL REPOSITORY_PATH

Show the current version of the repository with the given connection string, under version control of the specified repository.

The url should be any valid SQLAlchemy connection string.

migrate.versioning.api.upgrade(url, repository, version=None, **opts)

%prog upgrade URL REPOSITORY_PATH [VERSION] [–preview_py|–preview_sql]

Upgrade a database to a later version.

This runs the upgrade() function defined in your change scripts.

By default, the database is updated to the latest available version. You may specify a version instead, if you wish.

You may preview the Python or SQL code to be executed, rather than actually executing it, using the appropriate ‘preview’ option.

migrate.versioning.api.downgrade(url, repository, version, **opts)

%prog downgrade URL REPOSITORY_PATH VERSION [–preview_py|–preview_sql]

Downgrade a database to an earlier version.

This is the reverse of upgrade; this runs the downgrade() function defined in your change scripts.

You may preview the Python or SQL code to be executed, rather than actually executing it, using the appropriate ‘preview’ option.

migrate.versioning.api.drop_version_control(url, repository, **opts)

%prog drop_version_control URL REPOSITORY_PATH

Removes version control from a database.

migrate.versioning.api.manage(file, **opts)

%prog manage FILENAME VARIABLES...

Creates a script that runs Migrate with a set of default values.

For example:

%prog manage manage.py --repository=/path/to/repository --url=sqlite:///project.db

would create the script manage.py. The following two commands would then have exactly the same results:

python manage.py version
%prog version --repository=/path/to/repository
migrate.versioning.api.test(repository, url=None, **opts)

%prog test REPOSITORY_PATH URL [VERSION]

Performs the upgrade and downgrade option on the given database. This is not a real test and may leave the database in a bad state. You should therefore better run the test on a copy of your database.

migrate.versioning.api.compare_model_to_db(url, model, repository, **opts)

%prog compare_model_to_db URL MODEL REPOSITORY_PATH

Compare the current model (assumed to be a module level variable of type sqlalchemy.MetaData) against the current database.

NOTE: This is EXPERIMENTAL.

migrate.versioning.api.create_model(url, repository, **opts)

%prog create_model URL REPOSITORY_PATH

Dump the current database as a Python model to stdout.

NOTE: This is EXPERIMENTAL.

migrate.versioning.api.update_db_from_model(url, model, repository, **opts)

%prog update_db_from_model URL MODEL REPOSITORY_PATH

Modify the database to match the structure of the current Python model. This also sets the db_version number to the latest in the repository.

NOTE: This is EXPERIMENTAL.

Module exceptions

Provide exception classes for migrate.versioning

exception migrate.versioning.exceptions.ApiError
Base class for API errors.
exception migrate.versioning.exceptions.ControlledSchemaError
Base class for controlled schema errors.
exception migrate.versioning.exceptions.DatabaseAlreadyControlledError
Database shouldn’t be under version control, but it is
exception migrate.versioning.exceptions.DatabaseNotControlledError
Database should be under version control, but it’s not.
exception migrate.versioning.exceptions.Error
Error base class.
exception migrate.versioning.exceptions.InvalidRepositoryError
Invalid repository error.
exception migrate.versioning.exceptions.InvalidScriptError
Invalid script error.
exception migrate.versioning.exceptions.InvalidVersionError
Invalid version error.
exception migrate.versioning.exceptions.KnownError
A known error condition.
exception migrate.versioning.exceptions.NoSuchTableError
The table does not exist.
exception migrate.versioning.exceptions.PathError
Base class for path errors.
exception migrate.versioning.exceptions.PathFoundError
A path with a file was required; found no file.
exception migrate.versioning.exceptions.PathNotFoundError
A path with no file was required; found a file.
exception migrate.versioning.exceptions.RepositoryError
Base class for repository errors.
exception migrate.versioning.exceptions.ScriptError
Base class for script errors.
exception migrate.versioning.exceptions.UsageError
A known error condition where help should be displayed.
exception migrate.versioning.exceptions.WrongRepositoryError
This database is under version control by another repository.

Module genmodel

Code to generate a Python model from a database or differences between a model and database.

Some of this is borrowed heavily from the AutoCode project at: http://code.google.com/p/sqlautocode/

Module pathed

A path/directory class.

class migrate.versioning.pathed.Pathed(path)

A class associated with a path/directory tree.

Only one instance of this class may exist for a particular file; __new__ will return an existing instance if possible

classmethod require_found(path)
Ensures a given path already exists
classmethod require_notfound(path)
Ensures a given path does not already exist

Module repository

SQLAlchemy migrate repository management.

class migrate.versioning.repository.Changeset(start, *changes, **k)

A collection of changes to be applied to a database.

Changesets are bound to a repository and manage a set of logsql scripts from that repository.

Behaves like a dict, for the most part. Keys are ordered based on start/end.

keys()
In a series of upgrades x -> y, keys are version x. Sorted.
class migrate.versioning.repository.Repository(path)

A project’s change script repository

changeset(database, start, end=None)
Create a changeset to migrate this dbms from ver. start to end/latest.
classmethod create(path, name, **opts)
Create a repository at a specified path
id
db_settings
classmethod prepare_config(pkg, rsrc, name, **opts)
Prepare a project configuration file for a new project.
classmethod verify(path)

Ensure the target path is a valid repository.

Raises:InvalidRepositoryError if not valid
version_table
db_settings
migrate.versioning.repository.manage(file, **opts)
Create a project management script

Module schema

Database schema version management.

class migrate.versioning.schema.ControlledSchema(engine, repository)

A database under version control

classmethod compare_model_to_db(engine, model, repository)
Compare the current model against the current database.
classmethod create(engine, repository, version=None)
Declare a database to be under a repository’s version control.
classmethod create_model(engine, repository, declarative=False)
Dump the current database as a Python model.
drop()
Remove version control from a database.
update_db_from_model(model)
Modify the database to match the structure of the current Python model.
upgrade(version=None)
Upgrade (or downgrade) to a specified version, or latest version.

Module shell

The migrate command-line tool.

migrate.versioning.shell.main(argv=None, **kwargs)
kwargs are default options that can be overriden with passing –some_option to cmdline

Module util

migrate.versioning.util.asbool(obj)
Do everything to use object as bool
migrate.versioning.util.catch_known_errors(f)
Decorator that catches known api errors
migrate.versioning.util.construct_engine(url, **opts)

New in version 0.5.4.

Constructs and returns SQLAlchemy engine.

Currently, there are 2 ways to pass create_engine options to migrate.versioning.api functions:

Parameters:

Note

keyword parameters override engine_dict values.

migrate.versioning.util.guess_obj_type(obj)

Do everything to guess object type from string

Tries to convert to int, bool and finally returns if not succeded.

migrate.versioning.util.load_model(dotted_name)

Import module and use module-level variable”.

Parameter:dotted_name – path to model in form of string: some.python.module:Class

Changed in version 0.5.4.

Module schemadiff

Schema differencing support.

class migrate.versioning.schemadiff.SchemaDiff(model, conn, excludeTables=None, oldmodel=None)

Differences of model against database.

compareModelToDatabase()
Do actual comparison.
migrate.versioning.schemadiff.getDiffOfModelAgainstDatabase(model, conn, excludeTables=None)

Return differences of model against database.

Returns:object which will evaluate to True if there are differences else False.
migrate.versioning.schemadiff.getDiffOfModelAgainstModel(oldmodel, model, conn, excludeTables=None)

Return differences of model against another model.

Returns:object which will evaluate to True if there are differences else False.