Commits

bbinet  committed 945977a

always pass schema name as a separate parameter

  • Participants
  • Parent commits 8d5ef85

Comments (0)

Files changed (2)

File alembic/operations.py

 
     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,
 
         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
         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
 
 
     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.
 
          ``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
         :param ondelete: Optional string. If set, emit ON DELETE <value> 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.
 
          issuing DDL for this constraint.
         :param initially: optional string. If set, emit INITIALLY <value> 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.
 
          ``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 <value> 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):

File tests/test_op.py

 
 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():
     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 "
 
 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)"
     )