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.
Parameter: | column (sqlalchemy.Column) – the column object |
---|
Extends ansisql generator for column creation (alter table add col)
Create a column (table already exists).
Parameter: | 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.
Parameter: | 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.
Common operations for ALTER TABLE statements.
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 |
---|
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 |
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: |
|
---|
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.PrimaryKeyConstraint
Construct PrimaryKeyConstraint
Migrate’s additional parameters:
Parameters: |
|
---|
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.UniqueConstraint
Construct UniqueConstraint
Migrate’s additional parameters:
Parameters: |
|
---|
New in version 0.6.0.
Create the constraint in the database.
Parameters: |
|
---|
Drop the constraint from the database.
Parameters: |
|
---|---|
Returns: | Instance with cleared columns |
This module contains database dialect specific changeset implementations.
MySQL database specific implementations of changeset classes.
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 constaint dropper implementation.
Oracle database specific implementations of changeset classes.
PostgreSQL database specific implementations of changeset classes.
SQLite database specific implementations of changeset classes.
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 |
This module provides exception classes.
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.
Direct API to ColumnDelta.
Parameters: |
|
---|---|
Returns: | Columndelta instance |
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()
Parameter: | column (Column instance or string) – Column to be created |
---|
Drop a column, given its name or definition.
API to ChangesetColumn.drop()
Parameter: | column (Column instance or string) – Column to be droped |
---|
Rename this table.
Parameters: |
|
---|
Changeset extensions to SQLAlchemy columns.
Alter a column’s definition: ALTER TABLE ALTER COLUMN.
Column name, type, server_default, and nullable may be changed here.
Direct API to alter_column()
Example:
col.alter(name='foobar', type=Integer(), server_default=text("a"))
Supported parameters: name, type, primary_key, nullable, server_onupdate, server_default, autoincrement
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: |
|
---|
Changeset extensions to SQLAlchemy Indexes.
Change the name of an index.
Parameters: |
|
---|
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. |
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 REPOSITORY_PATH
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
Provide exception classes for migrate.versioning
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/
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
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.
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 project management script (manage.py)
Parameters: |
|
---|
Prepare a project configuration file for a new project.
Parameters: |
|
---|---|
Returns: | Populated config file |
Ensure the target path is a valid repository.
Raises: | InvalidRepositoryError |
---|
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.
Declare a database to be under a repository’s version control.
Raises: | DatabaseAlreadyControlledError |
---|---|
Returns: | ControlledSchema |
Schema differencing support.
Differences of model against database.
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:
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 |
---|
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
Parameter: | path (string) – Script location |
---|---|
Raises: | InvalidScriptError |
Returns: | Python module |
Bases: migrate.versioning.script.base.BaseScript
A file containing plain SQL statements.
Create an empty migration script at specified path
Returns: | SqlScript instance |
---|
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
Parameter: | 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
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”.
Parameter: | 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
Returns: | Latest version in Collection |
---|
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