This module extends SQLAlchemy and provides additional DDL [1] support.
[1] | SQL Data Definition Language |
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.
Extends ANSI SQL dropper for column dropping (ALTER TABLE DROP COLUMN).
Drop a column from its table.
Parameters: | column (sqlalchemy.Column) – the column object |
---|
Extends ansisql generator for column creation (alter table add col)
Create a column (table already exists).
Parameters: | column (sqlalchemy.Column instance) – column object |
---|
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.
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.
Parameters: | cons – constraint object |
---|
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.
Starts ALTER COLUMN
Rename/change a column.
Rename an index
Rename a table. Other ops aren’t supported.
Common operations for ALTER TABLE statements.
Append content to the SchemaIterator’s query buffer.
Execute the contents of the SchemaIterator’s buffer.
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.
Parameters: | param (sqlalchemy.Column, sqlalchemy.Index, sqlalchemy.schema.Constraint, sqlalchemy.Table, or string (table name)) – object to determine the table from |
---|
This module defines standalone schema constraint classes.
Bases: migrate.changeset.constraint.ConstraintChangeset, sqlalchemy.schema.CheckConstraint
Construct CheckConstraint
Migrate’s additional parameters:
Parameters: |
|
---|
Create the constraint in the database.
Parameters: |
|
---|
Drop the constraint from the database.
Parameters: |
|
---|---|
Returns: | Instance with cleared columns |
used to allow SchemaVisitor access
Bases: object
Base class for Constraint classes.
Create the constraint in the database.
Parameters: |
|
---|
Drop the constraint from the database.
Parameters: |
|
---|---|
Returns: | Instance with cleared columns |
Bases: migrate.changeset.constraint.ConstraintChangeset, sqlalchemy.schema.ForeignKeyConstraint
Construct ForeignKeyConstraint
Migrate’s additional parameters:
Parameters: |
|
---|
Mimic the database’s automatic constraint names
Create the constraint in the database.
Parameters: |
|
---|
Drop the constraint from the database.
Parameters: |
|
---|---|
Returns: | Instance with cleared columns |
used to allow SchemaVisitor access
Bases: migrate.changeset.constraint.ConstraintChangeset, sqlalchemy.schema.PrimaryKeyConstraint
Construct PrimaryKeyConstraint
Migrate’s additional parameters:
Parameters: |
|
---|
Mimic the database’s automatic constraint names
Create the constraint in the database.
Parameters: |
|
---|
Drop the constraint from the database.
Parameters: |
|
---|---|
Returns: | Instance with cleared columns |
used to allow SchemaVisitor access
Bases: migrate.changeset.constraint.ConstraintChangeset, sqlalchemy.schema.UniqueConstraint
Construct UniqueConstraint
Migrate’s additional parameters:
Parameters: |
|
---|
New in version 0.6.0.
Mimic the database’s automatic constraint names
Create the constraint in the database.
Parameters: |
|
---|
Drop the constraint from the database.
Parameters: |
|
---|---|
Returns: | Instance with cleared columns |
used to allow SchemaVisitor access
This module contains database dialect specific changeset implementations.
Firebird database specific implementations of changeset classes.
Firebird column dropper implementation.
Firebird supports ‘DROP col’ instead of ‘DROP COLUMN col’ syntax
Drop primary key and unique constraints if dropped column is referencing it.
Firebird column generator implementation.
Firebird constaint dropper implementation.
Cascading constraints is not supported
Firebird constraint generator implementation.
PostgreSQL database specific implementations of changeset classes.
PostgreSQL column dropper implementation.
PostgreSQL column generator implementation.
PostgreSQL constaint dropper implementation.
PostgreSQL constraint generator implementation.
PostgreSQL schema changer implementation.
SQLite database specific implementations of changeset classes.
SQLite ColumnDropper
SQLite ColumnGenerator
Module for visitor class mapping.
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.
Binds dialect specific preparer to visitor.
Get the visitor implementation for the given database engine.
Parameters: |
|
---|---|
Returns: | visitor |
Taken from sqlalchemy.engine.base.Engine._run_single_visitor() with support for migrate visitors.
Schema module providing common schema operations.
Create a column, given the table.
API to ChangesetColumn.create().
Drop a column, given the table.
API to ChangesetColumn.drop().
Alter a column.
This is a helper function that creates a ColumnDelta and runs it.
Parameters: |
|
---|---|
Returns: | A ColumnDelta instance representing the change. |
Rename a table.
If Table instance is given, engine is not used.
API to ChangesetTable.rename().
Parameters: |
|
---|
Rename an index.
If Index instance is given, table and engine are not used.
API to ChangesetIndex.rename().
Parameters: |
|
---|
Changeset extensions to SQLAlchemy tables.
Creates a column.
The column parameter may be a column definition or the name of a column in this table.
API to ChangesetColumn.create()
Parameters: | column (Column instance or string) – Column to be created |
---|
Remove this table from its metadata
Drop a column, given its name or definition.
API to ChangesetColumn.drop()
Parameters: | column (Column instance or string) – Column to be droped |
---|
Rename this table.
Parameters: |
|
---|
Changeset extensions to SQLAlchemy columns.
Makes a call to alter_column() for the column this method is called on.
Create a copy of this Column, with all attributes.
Create this column in the database.
Assumes the given table exists. ALTER TABLE ADD COLUMN, for most databases.
Parameters: |
|
---|---|
Returns: | self |
Drop this column from the database, leaving its table intact.
ALTER TABLE DROP COLUMN, for most databases.
Parameters: | connection (sqlalchemy.engine.base.Connection instance) – reuse connection istead of creating new one. |
---|
Changeset extensions to SQLAlchemy Indexes.
Change the name of an index.
Parameters: |
|
---|
Implements comparison between DefaultClause instances
Extracts the differences between two columns/column-parameters
May receive parameters arranged in several different ways:
Additional parameters can be specified to override column differences.
Additional parameters alter current_column. Table name is extracted from current_column object. Name is changed to current_column.name from current_name, if current_name is specified.
Table kw must specified.
Parameters: |
|
---|---|
Returns: | ColumnDelta instance provides interface for altered attributes to result_column through dict() alike object. |
Populate dict and column object with new values
Compares two types to be equal
Compares one Column object
Compares two Column objects
Compares Column objects with reflection
Processes default values for column
This package provides functionality to create and manage repositories of database schema changesets and to apply these changesets to databases.
This module provides an external API to the versioning system.
Changed in version 0.6.0: migrate.versioning.api.test() and schema diff functions changed order of positional arguments so all accept url and repository as first arguments.
Changed in version 0.5.4: --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.5.4: Deprecated --echo parameter in favour of new migrate.versioning.util.construct_engine() behavior.
%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.
%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.
%prog drop_version_control URL REPOSITORY_PATH
Removes version control from a database.
%prog help COMMAND
Displays help on a given command.
%prog script DESCRIPTION REPOSITORY_PATH
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
%prog test URL REPOSITORY_PATH [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.
%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.
%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
%prog update_db_from_model URL REPOSITORY_PATH MODEL
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.
%prog create_model URL REPOSITORY_PATH [DECLERATIVE=True]
Dump the current database as a Python model to stdout.
NOTE: This is EXPERIMENTAL.
%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.
%prog version REPOSITORY_PATH
Display the latest version available in a repository.
%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.
%prog compare_model_to_db URL REPOSITORY_PATH MODEL
Compare the current model (assumed to be a module level variable of type sqlalchemy.MetaData) against the current database.
NOTE: This is EXPERIMENTAL.
%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.
%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.
%prog script_sql DATABASE DESCRIPTION REPOSITORY_PATH
Create empty change SQL scripts for given DATABASE, where DATABASE is either specific (‘postgresql’, ‘mysql’, ‘oracle’, ‘sqlite’, etc.) or generic (‘default’).
For instance, manage.py script_sql postgresql description creates: repository/versions/001_description_postgresql_upgrade.sql and repository/versions/001_description_postgresql_downgrade.sql
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/
Various transformations from an A, B diff.
In the implementation, A tends to be called the model and B the database (although this is not true of all diffs). The diff is directionless, but transformations apply the diff in a particular direction, described in the method name.
Generate a migration from B to A.
Was: toUpgradeDowngradePython Assume model (A) is most current and database (B) is out-of-date.
Generates the source code for a definition of B.
Assumes a diff where A is empty.
Was: toPython. Assume database (B) is current and model (A) is empty.
Goes from B to A.
Was: applyModel. Apply model (A) to current database (B).
A path/directory class.
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
Ensures a given path already exists
Ensures a given path does not already exist
SQLAlchemy migrate repository management.
A collection of changes to be applied to a database.
Changesets are bound to a repository and manage a set of scripts from that repository.
Behaves like a dict, for the most part. Keys are ordered based on step value.
Add new change to changeset
In a series of upgrades x -> y, keys are version x. Sorted.
Run the changeset scripts
A project’s change script repository
Create a changeset to migrate this database from ver. start to end/latest.
Parameters: | |
---|---|
Returns: | Changeset instance |
Create a repository at a specified path
Create a project management script (manage.py)
Parameters: |
|
---|
API to migrate.versioning.version.Collection.create_new_python_version()
API to migrate.versioning.version.Collection.create_new_sql_version()
Prepare a project configuration file for a new project.
Parameters: | |
---|---|
Returns: | Populated config file |
Ensure the target path is a valid repository.
Raises : | InvalidRepositoryError |
---|
Returns repository id specified in config
Returns use_timestamp_numbering specified in config
Returns version_table name specified in config
Database schema version management.
A database under version control
API to Changeset creation.
Uses self.version for start version and engine.name to get database name.
Compare the current model against the current database.
Declare a database to be under a repository’s version control.
Raises : | DatabaseAlreadyControlledError |
---|---|
Returns: | ControlledSchema |
Dump the current database as a Python model.
Remove version control from a database.
Load controlled schema version info from DB
Modify the database to match the structure of the current Python model.
Update version_table with new information
Upgrade (or downgrade) to a specified version, or latest version.
Schema differencing support.
Container for differences in one Column between two Table instances, A and B.
Compute the difference between two MetaData objects.
The string representation of a SchemaDiff will summarise the changes found between the two MetaData objects.
The length of a SchemaDiff will give the number of changes found, enabling it to be used much like a boolean in expressions.
Parameters: |
---|
A sequence of table names that were found in B but weren’t in A.
A sequence of table names that were found in A but weren’t in B.
Container for differences in one Table between two MetaData instances, A and B.
A sequence of column names that were found in B but weren’t in A.
A sequence of column names that were found in A but weren’t in B.
Return differences of model against database.
Returns: | object which will evaluate to True if there are differences else False. |
---|
Return differences of model against another model.
Returns: | object which will evaluate to True if there are differences else False. |
---|
Base class for other types of scripts. All scripts have the following properties:
Core of each BaseScript subclass. This method executes the script.
Returns: | source code of the script. |
---|---|
Return type: | string |
Ensure this is a valid script This version simply ensures the script file’s existence
Raises : | InvalidScriptError |
---|
Bases: migrate.versioning.script.base.BaseScript
Base for Python scripts
Create an empty migration script at specified path
Returns: | PythonScript instance |
---|
Create a migration script based on difference between two SA models.
Parameters: |
|
---|---|
Returns: | Upgrade / Downgrade script |
Return type: | string |
Mocks SQLAlchemy Engine to store all executed calls in a string and runs PythonScript.run
Returns: | SQL file |
---|
Ensures a given path already exists
Ensures a given path does not already exist
Core method of Script file. Exectues update() or downgrade() functions
Parameters: |
---|
Returns: | source code of the script. |
---|---|
Return type: | string |
Ensure this is a valid script This version simply ensures the script file’s existence
Raises : | InvalidScriptError |
---|
Ensure path is a valid script
Parameters: | path (string) – Script location |
---|---|
Raises : | InvalidScriptError |
Returns: | Python module |
Calls migrate.versioning.script.py.verify_module() and returns it.
Bases: migrate.versioning.script.base.BaseScript
A file containing plain SQL statements.
Create an empty migration script at specified path
Returns: | SqlScript instance |
---|
Ensures a given path already exists
Ensures a given path does not already exist
Runs SQL script through raw dbapi execute call
Returns: | source code of the script. |
---|---|
Return type: | string |
Ensure this is a valid script This version simply ensures the script file’s existence
Raises : | InvalidScriptError |
---|
The migrate command-line tool.
Shell interface to migrate.versioning.api.
kwargs are default options that can be overriden with passing –some_option as command line option
Parameters: | disable_logging (bool) – Let migrate configure logging |
---|
Memoize(fn) - an instance which acts like fn but memoizes its arguments Will only work on functions with non-mutable arguments
ActiveState Code 52201
Do everything to use object as bool
Decorator that catches known api errors
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: |
|
---|---|
Returns: | SQLAlchemy Engine |
Note
keyword parameters override engine_dict values.
Do everything to guess object type from string
Tries to convert to int, bool and finally returns if not succeded.
Import module and use module-level variable”.
Parameters: | dotted_name – path to model in form of string: some.python.module:Class |
---|
Changed in version 0.5.4.
Decorator for migrate.versioning.api functions to safely close resources after function usage.
Passes engine parameters to construct_engine() and resulting parameter is available as kw[‘engine’].
Engine is disposed after wrapped function is executed.
A collection of versioning scripts in a repository
Create Python files for new version
Create SQL files for new version
Returns latest Version if vernum is not given. Otherwise, returns wanted version
Returns: | Latest version in Collection |
---|
A namespace for file extensions
A version number that behaves like a string and int at the same time
A single version in a collection :param vernum: Version Number :param path: Path to script files :param filelist: List of scripts :type vernum: int, VerNum :type path: string :type filelist: list
Add script to Collection/Version
Returns SQL or Python Script
Replaces spaces, (double and single) quotes and double underscores to underscores
Provide exception classes for migrate
Base class for API errors.
Base class for controlled schema errors.
Database shouldn’t be under version control, but it is
Database should be under version control, but it’s not.
Error base class.
Invalid constraint error
Invalid repository error.
Invalid script error.
Invalid version error.
A known error condition.
Warning for deprecated features in Migrate
The table does not exist.
Not supported error
Base class for path errors.
A path with a file was required; found no file.
A path with no file was required; found a file.
Base class for repository errors.
Base class for script errors.
A known error condition where help should be displayed.
This database is under version control by another repository.