From: Bruno Binet Date: Tue, 25 Sep 2012 21:11:17 +0000 (+0200) Subject: always pass schema name as a separate parameter X-Git-Tag: rel_0_4_0~4^2^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a909e8d0e8def2fc274250de2ac53400923b8ebb;p=thirdparty%2Fsqlalchemy%2Falembic.git always pass schema name as a separate parameter --- diff --git a/alembic/operations.py b/alembic/operations.py index 21e2fb33..01d29e95 100644 --- a/alembic/operations.py +++ b/alembic/operations.py @@ -52,24 +52,25 @@ class Operations(object): def _foreign_key_constraint(self, name, source, referent, local_cols, remote_cols, - onupdate=None, ondelete=None): + onupdate=None, ondelete=None, + source_schema=None, referent_schema=None): m = sa_schema.MetaData() if source == referent: t1_cols = local_cols + remote_cols else: t1_cols = local_cols - sname, tname = self._parse_table_key(referent) - sa_schema.Table(tname, m, + sa_schema.Table(referent, m, *[sa_schema.Column(n, NULLTYPE) for n in remote_cols], - schema=sname) + schema=referent_schema) - sname, tname = self._parse_table_key(source) - t1 = sa_schema.Table(tname, m, + t1 = sa_schema.Table(source, m, *[sa_schema.Column(n, NULLTYPE) for n in t1_cols], - schema=sname) + schema=source_schema) + tname = "%s.%s" % (referent_schema, referent) if referent_schema \ + else referent f = sa_schema.ForeignKeyConstraint(local_cols, - ["%s.%s" % (referent, n) + ["%s.%s" % (tname, n) for n in remote_cols], name=name, onupdate=onupdate, @@ -79,11 +80,10 @@ class Operations(object): return f - def _unique_constraint(self, name, source, local_cols, **kw): - sname, tname = self._parse_table_key(source) - t = sa_schema.Table(tname, sa_schema.MetaData(), + def _unique_constraint(self, name, source, local_cols, schema=None, **kw): + t = sa_schema.Table(source, sa_schema.MetaData(), *[sa_schema.Column(n, NULLTYPE) for n in local_cols], - schema=sname) + schema=schema) kw['name'] = name uq = sa_schema.UniqueConstraint(*[t.c[n] for n in local_cols], **kw) # TODO: need event tests to ensure the event @@ -91,10 +91,9 @@ class Operations(object): t.append_constraint(uq) return uq - def _check_constraint(self, name, source, condition, **kw): - sname, tname = self._parse_table_key(source) - t = sa_schema.Table(tname, sa_schema.MetaData(), - sa_schema.Column('x', Integer), schema=sname) + def _check_constraint(self, name, source, condition, schema=None, **kw): + t = sa_schema.Table(source, sa_schema.MetaData(), + sa_schema.Column('x', Integer), schema=schema) ck = sa_schema.CheckConstraint(condition, name=name, **kw) t.append_constraint(ck) return ck @@ -374,7 +373,8 @@ class Operations(object): def create_foreign_key(self, name, source, referent, local_cols, - remote_cols, onupdate=None, ondelete=None): + remote_cols, onupdate=None, ondelete=None, + source_schema=None, referent_schema=None): """Issue a "create foreign key" instruction using the current migration context. @@ -400,10 +400,8 @@ class Operations(object): ``name`` here can be ``None``, as the event listener will apply the name to the constraint object when it is associated with the table. - :param source: String name of the source table. Dotted schema names - are supported. - :param referent: String name of the destination table. Dotted schema - names are supported. + :param source: String name of the source table. + :param referent: String name of the destination table. :param local_cols: a list of string column names in the source table. :param remote_cols: a list of string column names in the @@ -414,16 +412,21 @@ class Operations(object): :param ondelete: Optional string. If set, emit ON DELETE when issuing DDL for this constraint. Typical values include CASCADE, DELETE and RESTRICT. + :param source_schema: Optional schema name of the source table. + :param referent_schema: Optional schema name of the destination table. """ self.impl.add_constraint( self._foreign_key_constraint(name, source, referent, local_cols, remote_cols, - onupdate=onupdate, ondelete=ondelete) + onupdate=onupdate, ondelete=ondelete, + source_schema=source_schema, + referent_schema=referent_schema) ) - def create_unique_constraint(self, name, source, local_cols, **kw): + def create_unique_constraint(self, name, source, local_cols, + schema=None, **kw): """Issue a "create unique constraint" instruction using the current migration context. @@ -455,15 +458,17 @@ class Operations(object): issuing DDL for this constraint. :param initially: optional string. If set, emit INITIALLY when issuing DDL for this constraint. + :param schema: Optional schema name of the source table. """ self.impl.add_constraint( self._unique_constraint(name, source, local_cols, - **kw) + schema=schema, **kw) ) - def create_check_constraint(self, name, source, condition, **kw): + def create_check_constraint(self, name, source, condition, + schema=None, **kw): """Issue a "create check constraint" instruction using the current migration context. @@ -490,18 +495,18 @@ class Operations(object): ``name`` here can be ``None``, as the event listener will apply the name to the constraint object when it is associated with the table. - :param source: String name of the source table. Dotted schema names - are supported. + :param source: String name of the source table. :param condition: SQL expression that's the condition of the constraint. Can be a string or SQLAlchemy expression language structure. :param deferrable: optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when issuing DDL for this constraint. :param initially: optional string. If set, emit INITIALLY when issuing DDL for this constraint. + :param schema: Optional schema name of the source table. """ self.impl.add_constraint( - self._check_constraint(name, source, condition, **kw) + self._check_constraint(name, source, condition, schema=schema, **kw) ) def create_table(self, name, *columns, **kw): diff --git a/tests/test_op.py b/tests/test_op.py index d6722645..626c5d2e 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -331,11 +331,12 @@ def test_add_foreign_key(): def test_add_foreign_key_schema(): context = op_fixture() - op.create_foreign_key('fk_test', 'foo.t1', 'bar.t2', - ['foo', 'bar'], ['bat', 'hoho']) + op.create_foreign_key('fk_test', 't1', 't2', + ['foo', 'bar'], ['bat', 'hoho'], + source_schema='foo2', referent_schema='bar2') context.assert_( - "ALTER TABLE foo.t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) " - "REFERENCES bar.t2 (bat, hoho)" + "ALTER TABLE foo2.t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) " + "REFERENCES bar2.t2 (bat, hoho)" ) def test_add_foreign_key_onupdate(): @@ -382,8 +383,9 @@ def test_add_check_constraint_schema(): context = op_fixture() op.create_check_constraint( "ck_user_name_len", - "foo.user_table", - func.len(column('name')) > 5 + "user_table", + func.len(column('name')) > 5, + schema='foo' ) context.assert_( "ALTER TABLE foo.user_table ADD CONSTRAINT ck_user_name_len " @@ -399,7 +401,7 @@ def test_add_unique_constraint(): def test_add_unique_constraint_schema(): context = op_fixture() - op.create_unique_constraint('uk_test', 'foo.t1', ['foo', 'bar']) + op.create_unique_constraint('uk_test', 't1', ['foo', 'bar'], schema='foo') context.assert_( "ALTER TABLE foo.t1 ADD CONSTRAINT uk_test UNIQUE (foo, bar)" )