diff options
author | Jan Dittberner <jan.dittberner@googlemail.com> | 2011-10-28 10:59:10 +0200 |
---|---|---|
committer | Jan Dittberner <jan.dittberner@googlemail.com> | 2011-10-28 10:59:10 +0200 |
commit | 3619347441ac129bfa2a266714b9fe933bbb4942 (patch) | |
tree | eb805693a593d5e8550cae900375c13d78c71d2f | |
parent | e4ad6e357f62f9fe333042e9190285ad6f283ad1 (diff) | |
download | sqlalchemy-migrate-3619347441ac129bfa2a266714b9fe933bbb4942.tar.gz |
drop SQLAlchemy < 0.6 compatibility code
-rw-r--r-- | migrate/changeset/__init__.py | 1 | ||||
-rw-r--r-- | migrate/changeset/ansisql.py | 106 | ||||
-rw-r--r-- | migrate/changeset/constraint.py | 3 | ||||
-rw-r--r-- | migrate/changeset/databases/firebird.py | 12 | ||||
-rw-r--r-- | migrate/changeset/databases/mysql.py | 43 | ||||
-rw-r--r-- | migrate/changeset/databases/oracle.py | 7 | ||||
-rw-r--r-- | migrate/changeset/databases/postgres.py | 12 | ||||
-rw-r--r-- | migrate/changeset/databases/sqlite.py | 8 | ||||
-rw-r--r-- | migrate/changeset/schema.py | 10 | ||||
-rw-r--r-- | migrate/tests/changeset/test_changeset.py | 30 | ||||
-rw-r--r-- | migrate/tests/changeset/test_constraint.py | 5 | ||||
-rw-r--r-- | migrate/tests/fixture/database.py | 8 | ||||
-rw-r--r-- | migrate/tests/versioning/test_genmodel.py | 12 | ||||
-rw-r--r-- | migrate/tests/versioning/test_schemadiff.py | 1 |
14 files changed, 58 insertions, 200 deletions
diff --git a/migrate/changeset/__init__.py b/migrate/changeset/__init__.py index fa3387d..80ea152 100644 --- a/migrate/changeset/__init__.py +++ b/migrate/changeset/__init__.py @@ -13,7 +13,6 @@ from sqlalchemy import __version__ as _sa_version warnings.simplefilter('always', DeprecationWarning) _sa_version = tuple(int(re.match("\d+", x).group(0)) for x in _sa_version.split(".")) -SQLA_06 = _sa_version >= (0, 6) SQLA_07 = _sa_version >= (0, 7) del re diff --git a/migrate/changeset/ansisql.py b/migrate/changeset/ansisql.py index 67d0b0e..9ded560 100644 --- a/migrate/changeset/ansisql.py +++ b/migrate/changeset/ansisql.py @@ -17,23 +17,19 @@ from sqlalchemy.schema import (ForeignKeyConstraint, Index) from migrate import exceptions -from migrate.changeset import constraint, SQLA_06 +from migrate.changeset import constraint -if not SQLA_06: - from sqlalchemy.sql.compiler import SchemaGenerator, SchemaDropper -else: - from sqlalchemy.schema import AddConstraint, DropConstraint - from sqlalchemy.sql.compiler import DDLCompiler - SchemaGenerator = SchemaDropper = DDLCompiler +from sqlalchemy.schema import AddConstraint, DropConstraint +from sqlalchemy.sql.compiler import DDLCompiler +SchemaGenerator = SchemaDropper = DDLCompiler class AlterTableVisitor(SchemaVisitor): """Common operations for ``ALTER TABLE`` statements.""" - if SQLA_06: - # engine.Compiler looks for .statement - # when it spawns off a new compiler - statement = ClauseElement() + # engine.Compiler looks for .statement + # when it spawns off a new compiler + statement = ClauseElement() def append(self, s): """Append content to the SchemaIterator's query buffer.""" @@ -123,9 +119,8 @@ class ANSIColumnGenerator(AlterTableVisitor, SchemaGenerator): name=column.primary_key_name) cons.create() - if SQLA_06: - def add_foreignkey(self, fk): - self.connection.execute(AddConstraint(fk)) + def add_foreignkey(self, fk): + self.connection.execute(AddConstraint(fk)) class ANSIColumnDropper(AlterTableVisitor, SchemaDropper): """Extends ANSI SQL dropper for column dropping (``ALTER TABLE @@ -232,10 +227,7 @@ class ANSISchemaChanger(AlterTableVisitor, SchemaGenerator): def _visit_column_type(self, table, column, delta): type_ = delta['type'] - if SQLA_06: - type_text = str(type_.compile(dialect=self.dialect)) - else: - type_text = type_.dialect_impl(self.dialect).get_col_spec() + type_text = str(type_.compile(dialect=self.dialect)) self.append("TYPE %s" % type_text) def _visit_column_name(self, table, column, delta): @@ -279,75 +271,17 @@ class ANSIConstraintCommon(AlterTableVisitor): def visit_migrate_unique_constraint(self, *p, **k): self._visit_constraint(*p, **k) -if SQLA_06: - class ANSIConstraintGenerator(ANSIConstraintCommon, SchemaGenerator): - def _visit_constraint(self, constraint): - constraint.name = self.get_constraint_name(constraint) - self.append(self.process(AddConstraint(constraint))) - self.execute() - - class ANSIConstraintDropper(ANSIConstraintCommon, SchemaDropper): - def _visit_constraint(self, constraint): - constraint.name = self.get_constraint_name(constraint) - self.append(self.process(DropConstraint(constraint, cascade=constraint.cascade))) - self.execute() - -else: - class ANSIConstraintGenerator(ANSIConstraintCommon, SchemaGenerator): +class ANSIConstraintGenerator(ANSIConstraintCommon, SchemaGenerator): + def _visit_constraint(self, constraint): + constraint.name = self.get_constraint_name(constraint) + self.append(self.process(AddConstraint(constraint))) + self.execute() - def get_constraint_specification(self, cons, **kwargs): - """Constaint SQL generators. - - We cannot use SA visitors because they append comma. - """ - - if isinstance(cons, PrimaryKeyConstraint): - if cons.name is not None: - self.append("CONSTRAINT %s " % self.preparer.format_constraint(cons)) - self.append("PRIMARY KEY ") - self.append("(%s)" % ', '.join(self.preparer.quote(c.name, c.quote) - for c in cons)) - self.define_constraint_deferrability(cons) - elif isinstance(cons, ForeignKeyConstraint): - self.define_foreign_key(cons) - elif isinstance(cons, CheckConstraint): - if cons.name is not None: - self.append("CONSTRAINT %s " % - self.preparer.format_constraint(cons)) - self.append("CHECK (%s)" % cons.sqltext) - self.define_constraint_deferrability(cons) - elif isinstance(cons, UniqueConstraint): - if cons.name is not None: - self.append("CONSTRAINT %s " % - self.preparer.format_constraint(cons)) - self.append("UNIQUE (%s)" % \ - (', '.join(self.preparer.quote(c.name, c.quote) for c in cons))) - self.define_constraint_deferrability(cons) - else: - raise exceptions.InvalidConstraintError(cons) - - def _visit_constraint(self, constraint): - - table = self.start_alter_table(constraint) - constraint.name = self.get_constraint_name(constraint) - self.append("ADD ") - self.get_constraint_specification(constraint) - self.execute() - - - class ANSIConstraintDropper(ANSIConstraintCommon, SchemaDropper): - - def _visit_constraint(self, constraint): - self.start_alter_table(constraint) - self.append("DROP CONSTRAINT ") - constraint.name = self.get_constraint_name(constraint) - self.append(self.preparer.format_constraint(constraint)) - if constraint.cascade: - self.cascade_constraint(constraint) - self.execute() - - def cascade_constraint(self, constraint): - self.append(" CASCADE") +class ANSIConstraintDropper(ANSIConstraintCommon, SchemaDropper): + def _visit_constraint(self, constraint): + constraint.name = self.get_constraint_name(constraint) + self.append(self.process(DropConstraint(constraint, cascade=constraint.cascade))) + self.execute() class ANSIDialect(DefaultDialect): diff --git a/migrate/changeset/constraint.py b/migrate/changeset/constraint.py index 476a456..96407bd 100644 --- a/migrate/changeset/constraint.py +++ b/migrate/changeset/constraint.py @@ -4,7 +4,6 @@ from sqlalchemy import schema from migrate.exceptions import * -from migrate.changeset import SQLA_06 class ConstraintChangeset(object): """Base class for Constraint classes.""" @@ -165,8 +164,6 @@ class CheckConstraint(ConstraintChangeset, schema.CheckConstraint): table = kwargs.pop('table', table) schema.CheckConstraint.__init__(self, sqltext, *args, **kwargs) if table is not None: - if not SQLA_06: - self.table = table self._set_parent(table) self.colnames = colnames diff --git a/migrate/changeset/databases/firebird.py b/migrate/changeset/databases/firebird.py index 675666c..226728b 100644 --- a/migrate/changeset/databases/firebird.py +++ b/migrate/changeset/databases/firebird.py @@ -4,13 +4,10 @@ from sqlalchemy.databases import firebird as sa_base from sqlalchemy.schema import PrimaryKeyConstraint from migrate import exceptions -from migrate.changeset import ansisql, SQLA_06 +from migrate.changeset import ansisql -if SQLA_06: - FBSchemaGenerator = sa_base.FBDDLCompiler -else: - FBSchemaGenerator = sa_base.FBSchemaGenerator +FBSchemaGenerator = sa_base.FBDDLCompiler class FBColumnGenerator(FBSchemaGenerator, ansisql.ANSIColumnGenerator): """Firebird column generator implementation.""" @@ -41,10 +38,7 @@ class FBColumnDropper(ansisql.ANSIColumnDropper): # is deleted! continue - if SQLA_06: - should_drop = column.name in cons.columns - else: - should_drop = cons.contains_column(column) and cons.name + should_drop = column.name in cons.columns if should_drop: self.start_alter_table(column) self.append("DROP CONSTRAINT ") diff --git a/migrate/changeset/databases/mysql.py b/migrate/changeset/databases/mysql.py index badd9fe..6987b4b 100644 --- a/migrate/changeset/databases/mysql.py +++ b/migrate/changeset/databases/mysql.py @@ -6,13 +6,10 @@ from sqlalchemy.databases import mysql as sa_base from sqlalchemy import types as sqltypes from migrate import exceptions -from migrate.changeset import ansisql, SQLA_06 +from migrate.changeset import ansisql -if not SQLA_06: - MySQLSchemaGenerator = sa_base.MySQLSchemaGenerator -else: - MySQLSchemaGenerator = sa_base.MySQLDDLCompiler +MySQLSchemaGenerator = sa_base.MySQLDDLCompiler class MySQLColumnGenerator(MySQLSchemaGenerator, ansisql.ANSIColumnGenerator): pass @@ -53,37 +50,11 @@ class MySQLSchemaChanger(MySQLSchemaGenerator, ansisql.ANSISchemaChanger): class MySQLConstraintGenerator(ansisql.ANSIConstraintGenerator): pass -if SQLA_06: - class MySQLConstraintDropper(MySQLSchemaGenerator, ansisql.ANSIConstraintDropper): - def visit_migrate_check_constraint(self, *p, **k): - raise exceptions.NotSupportedError("MySQL does not support CHECK" - " constraints, use triggers instead.") - -else: - class MySQLConstraintDropper(ansisql.ANSIConstraintDropper): - - def visit_migrate_primary_key_constraint(self, constraint): - self.start_alter_table(constraint) - self.append("DROP PRIMARY KEY") - self.execute() - - def visit_migrate_foreign_key_constraint(self, constraint): - self.start_alter_table(constraint) - self.append("DROP FOREIGN KEY ") - constraint.name = self.get_constraint_name(constraint) - self.append(self.preparer.format_constraint(constraint)) - self.execute() - - def visit_migrate_check_constraint(self, *p, **k): - raise exceptions.NotSupportedError("MySQL does not support CHECK" - " constraints, use triggers instead.") - - def visit_migrate_unique_constraint(self, constraint, *p, **k): - self.start_alter_table(constraint) - self.append('DROP INDEX ') - constraint.name = self.get_constraint_name(constraint) - self.append(self.preparer.format_constraint(constraint)) - self.execute() + +class MySQLConstraintDropper(MySQLSchemaGenerator, ansisql.ANSIConstraintDropper): + def visit_migrate_check_constraint(self, *p, **k): + raise exceptions.NotSupportedError("MySQL does not support CHECK" + " constraints, use triggers instead.") class MySQLDialect(ansisql.ANSIDialect): diff --git a/migrate/changeset/databases/oracle.py b/migrate/changeset/databases/oracle.py index bd761bc..2f16b5b 100644 --- a/migrate/changeset/databases/oracle.py +++ b/migrate/changeset/databases/oracle.py @@ -5,13 +5,10 @@ import sqlalchemy as sa from sqlalchemy.databases import oracle as sa_base from migrate import exceptions -from migrate.changeset import ansisql, SQLA_06 +from migrate.changeset import ansisql -if not SQLA_06: - OracleSchemaGenerator = sa_base.OracleSchemaGenerator -else: - OracleSchemaGenerator = sa_base.OracleDDLCompiler +OracleSchemaGenerator = sa_base.OracleDDLCompiler class OracleColumnGenerator(OracleSchemaGenerator, ansisql.ANSIColumnGenerator): diff --git a/migrate/changeset/databases/postgres.py b/migrate/changeset/databases/postgres.py index 015ad65..10ea094 100644 --- a/migrate/changeset/databases/postgres.py +++ b/migrate/changeset/databases/postgres.py @@ -3,14 +3,10 @@ .. _`PostgreSQL`: http://www.postgresql.org/ """ -from migrate.changeset import ansisql, SQLA_06 - -if not SQLA_06: - from sqlalchemy.databases import postgres as sa_base - PGSchemaGenerator = sa_base.PGSchemaGenerator -else: - from sqlalchemy.databases import postgresql as sa_base - PGSchemaGenerator = sa_base.PGDDLCompiler +from migrate.changeset import ansisql + +from sqlalchemy.databases import postgresql as sa_base +PGSchemaGenerator = sa_base.PGDDLCompiler class PGColumnGenerator(PGSchemaGenerator, ansisql.ANSIColumnGenerator): diff --git a/migrate/changeset/databases/sqlite.py b/migrate/changeset/databases/sqlite.py index 447412d..5a13780 100644 --- a/migrate/changeset/databases/sqlite.py +++ b/migrate/changeset/databases/sqlite.py @@ -9,13 +9,11 @@ from copy import copy from sqlalchemy.databases import sqlite as sa_base from migrate import exceptions -from migrate.changeset import ansisql, SQLA_06 +from migrate.changeset import ansisql -if not SQLA_06: - SQLiteSchemaGenerator = sa_base.SQLiteSchemaGenerator -else: - SQLiteSchemaGenerator = sa_base.SQLiteDDLCompiler +SQLiteSchemaGenerator = sa_base.SQLiteDDLCompiler + class SQLiteCommon(object): diff --git a/migrate/changeset/schema.py b/migrate/changeset/schema.py index b61ff5b..c467cc5 100644 --- a/migrate/changeset/schema.py +++ b/migrate/changeset/schema.py @@ -11,7 +11,7 @@ from sqlalchemy.schema import ForeignKeyConstraint from sqlalchemy.schema import UniqueConstraint from migrate.exceptions import * -from migrate.changeset import SQLA_06, SQLA_07 +from migrate.changeset import SQLA_07 from migrate.changeset.databases.visitor import (get_engine_visitor, run_single_visitor) @@ -349,10 +349,7 @@ class ColumnDelta(DictMixin, sqlalchemy.schema.SchemaItem): def process_column(self, column): """Processes default values for column""" # XXX: this is a snippet from SA processing of positional parameters - if not SQLA_06 and column.args: - toinit = list(column.args) - else: - toinit = list() + toinit = list() if column.server_default is not None: if isinstance(column.server_default, sqlalchemy.FetchedValue): @@ -367,9 +364,6 @@ class ColumnDelta(DictMixin, sqlalchemy.schema.SchemaItem): for_update=True)) if toinit: column._init_items(*toinit) - - if not SQLA_06: - column.args = [] def _get_table(self): return getattr(self, '_table', None) diff --git a/migrate/tests/changeset/test_changeset.py b/migrate/tests/changeset/test_changeset.py index d4dae7a..e1166f4 100644 --- a/migrate/tests/changeset/test_changeset.py +++ b/migrate/tests/changeset/test_changeset.py @@ -337,26 +337,21 @@ class TestAddDropColumn(fixture.DB): self.table.create() # paranoid check - if SQLA_06: - self.refresh_table() - self.assertEqual( - sorted([i.name for i in self.table.indexes]), - [u'ix_tmp_adddropcol_d1', u'ix_tmp_adddropcol_d2'] - ) + self.refresh_table() + self.assertEqual( + sorted([i.name for i in self.table.indexes]), + [u'ix_tmp_adddropcol_d1', u'ix_tmp_adddropcol_d2'] + ) # delete one self.table.c.d2.drop() # ensure the other index is still there - if SQLA_06: - self.refresh_table() - self.assertEqual( - sorted([i.name for i in self.table.indexes]), - [u'ix_tmp_adddropcol_d1'] - ) - else: - # a crude test for 0.5.x - Index('ix_tmp_adddropcol_d1',self.table.c.d1).drop() + self.refresh_table() + self.assertEqual( + sorted([i.name for i in self.table.indexes]), + [u'ix_tmp_adddropcol_d1'] + ) def _actual_foreign_keys(self): from sqlalchemy.schema import ForeignKeyConstraint @@ -678,10 +673,7 @@ class TestColumnChange(fixture.DB): # server_default isn't necessarily None for Oracle #self.assert_(self.table.c.data.server_default is None,self.table.c.data.server_default) self.engine.execute(self.table.insert(), id=11) - if SQLA_06: - row = self.table.select(self.table.c.id == 11).execution_options(autocommit=True).execute().fetchone() - else: - row = self.table.select(self.table.c.id == 11, autocommit=True).execute().fetchone() + row = self.table.select(self.table.c.id == 11).execution_options(autocommit=True).execute().fetchone() self.assert_(row['data'] is None, row['data']) @fixture.usedb(not_supported='firebird') diff --git a/migrate/tests/changeset/test_constraint.py b/migrate/tests/changeset/test_constraint.py index cf71d53..f36698d 100644 --- a/migrate/tests/changeset/test_constraint.py +++ b/migrate/tests/changeset/test_constraint.py @@ -211,10 +211,7 @@ class TestAutoname(CommonTestConstraint): self.refresh_table() if not self.url.startswith('sqlite'): # TODO: test for index for sqlite - if SQLA_06: - self.compare_columns_equal(cons.columns, self.table.primary_key) - else: - self.compare_columns_equal(cons.columns, self.table.primary_key, ['autoincrement']) + self.compare_columns_equal(cons.columns, self.table.primary_key) cons.name = None cons.drop() diff --git a/migrate/tests/fixture/database.py b/migrate/tests/fixture/database.py index 0095f6b..e6e410a 100644 --- a/migrate/tests/fixture/database.py +++ b/migrate/tests/fixture/database.py @@ -9,7 +9,6 @@ from sqlalchemy import create_engine, Table, MetaData from sqlalchemy.orm import create_session from sqlalchemy.pool import StaticPool -from migrate.changeset import SQLA_06 from migrate.changeset.schema import ColumnDelta from migrate.versioning.util import Memoize @@ -174,11 +173,8 @@ class DB(Base): def _select_row(self): """Select rows, used in multiple tests""" - if SQLA_06: - row = self.table.select().execution_options(autocommit=True).execute().fetchone() - else: - row = self.table.select(autocommit=True).execute().fetchone() - return row + return self.table.select().execution_options( + autocommit=True).execute().fetchone() def refresh_table(self, name=None): """Reload the table from the database diff --git a/migrate/tests/versioning/test_genmodel.py b/migrate/tests/versioning/test_genmodel.py index c12900d..eeefe8a 100644 --- a/migrate/tests/versioning/test_genmodel.py +++ b/migrate/tests/versioning/test_genmodel.py @@ -7,7 +7,7 @@ from sqlalchemy import * from nose.tools import eq_ from migrate.versioning import genmodel, schemadiff -from migrate.changeset import schema, SQLA_06 +from migrate.changeset import schema from migrate.tests import fixture @@ -103,10 +103,7 @@ class TestSchemaDiff(fixture.DB): if not self.engine.name == 'oracle': # Add data, later we'll make sure it's still present. result = self.engine.execute(self.table.insert(), id=1, name=u'mydata') - if SQLA_06: - dataId = result.inserted_primary_key[0] - else: - dataId = result.last_inserted_ids()[0] + dataId = result.inserted_primary_key[0] # Modify table in model (by removing it and adding it back to model) # Drop column data, add columns data2 and data3. @@ -162,10 +159,7 @@ class TestSchemaDiff(fixture.DB): # Add data, later we'll make sure it's still present. result = self.engine.execute(self.table.insert(), id=2, name=u'mydata2', data2=123) - if SQLA_06: - dataId2 = result.inserted_primary_key[0] - else: - dataId2 = result.last_inserted_ids()[0] + dataId2 = result.inserted_primary_key[0] # Change column type in model. self.meta.remove(self.table) diff --git a/migrate/tests/versioning/test_schemadiff.py b/migrate/tests/versioning/test_schemadiff.py index 51aaf80..0bfa902 100644 --- a/migrate/tests/versioning/test_schemadiff.py +++ b/migrate/tests/versioning/test_schemadiff.py @@ -6,7 +6,6 @@ from sqlalchemy import * from nose.tools import eq_ from migrate.versioning import schemadiff -from migrate.changeset import SQLA_06 from migrate.tests import fixture |