]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
create table construct, does the "metadata" thing as well.
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 1 May 2010 22:42:37 +0000 (18:42 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 1 May 2010 22:42:37 +0000 (18:42 -0400)
alembic/context.py
alembic/ddl/__init__.py
alembic/op.py
tests/__init__.py
tests/test_schema.py

index 176388bd1e318ecd184c2aca95243c1dc018070e..6978bbaad6d0d57f6e219e72c31526064fd9039d 100644 (file)
@@ -1,4 +1,3 @@
-from alembic.ddl import base
 from alembic import util
 from sqlalchemy import MetaData, Table, Column, String, literal_column, text
 from sqlalchemy.schema import CreateTable
@@ -110,6 +109,7 @@ def opts(cfg, **kw):
     
 def configure_connection(connection):
     global _context
+    from alembic.ddl import base
     _context = _context_impls.get(connection.dialect.name, DefaultContext)(connection, **_context_opts)
     
 def run_migrations(**kw):
index 20bb6d77f5291d1cc9f298f1d374b8ffbbc7f3f7..07b063dd21d20bb94afae809ad141bcdc5832763 100644 (file)
@@ -1 +1 @@
-import base, postgresql, mysql, sqlite
\ No newline at end of file
+import postgresql, mysql, sqlite
\ No newline at end of file
index dbd0de4ead7830629e2bac28aa8fba2e26e1cd15..d2b1083db2f317f03d1e94c87449a9b3e1678c9f 100644 (file)
@@ -38,13 +38,42 @@ def _foreign_key_constraint(name, source, referent, local_cols, remote_cols):
                                         name=name
                                         )
     t1.append_constraint(f)
+    
     return f
 
 def _unique_constraint(name, source, local_cols):
     t = schema.Table(source, schema.MetaData(), 
                 *[schema.Column(n, NULLTYPE) for n in local_cols])
     return schema.UniqueConstraint(*t.c, name=name)
+
+def _table(name, *columns, **kw):
+    m = schema.MetaData()
+    t = schema.Table(name, m, *columns, **kw)
+    for f in t._foreign_keys:
+        _ensure_table_for_fk(m, f)
+    return t
+
+def _ensure_table_for_fk(metadata, fk):
+    """create a placeholder Table object for the referent of a
+    ForeignKey.
     
+    """
+    if isinstance(fk._colspec, basestring):
+        table_key, cname = fk._colspec.split('.')
+        if '.' in table_key:
+            tokens = tname.split('.')
+            sname = ".".join(tokens[0:-1])
+            tname = tokens[-1]
+        else:
+            tname = table_key
+            sname = None
+        if table_key not in metadata.tables:
+            rel_t = schema.Table(tname, metadata, schema=sname)
+        else:
+            rel_t = metadata.tables[table_key]
+        if not rel_t.c.contains_column(cname):
+            rel_t.append_column(schema.Column(cname, NULLTYPE))
+
 def create_foreign_key(name, source, referent, local_cols, remote_cols):
     get_context().add_constraint(
                 _foreign_key_constraint(source, referent, local_cols, remote_cols)
@@ -55,5 +84,10 @@ def create_unique_constraint(name, source, local_cols):
                 _unique_constraint(name, source, local_cols)
             )
 
+def create_table(name, *columns, **kw):
+    get_context().create_table(
+        _table(name, *columns, **kw)
+    )
+
 def execute(sql):
     get_context().execute(sql)
\ No newline at end of file
index 5b33b99409f71ab6077c5c377386e21c2f709ce5..64ebcb5eb1f8586b2bbd9bf1a04df71d2f8d168c 100644 (file)
@@ -18,7 +18,10 @@ def _get_dialect(name):
     
 def assert_compiled(element, assert_string, dialect=None):
     dialect = _get_dialect(dialect)
-    eq_(unicode(element.compile(dialect=dialect)), assert_string)
+    eq_(
+        unicode(element.compile(dialect=dialect)).replace("\n", "").replace("\t", ""),
+        assert_string.replace("\n", "").replace("\t", "")
+    )
 
 def _testing_config():
     from alembic.config import Config
index 5ba4fc9e77e4323628e6f9cf808a428865f6ac31..84bff84dc8d7753bf79da95528ba2833edb1f18b 100644 (file)
@@ -1,6 +1,9 @@
 from tests import assert_compiled
 from alembic import op
-from sqlalchemy.schema import AddConstraint, ForeignKeyConstraint
+from sqlalchemy.schema import AddConstraint, ForeignKeyConstraint, \
+                            CreateTable, Column, ForeignKey,\
+                            MetaData, Table
+from sqlalchemy import Integer
 
 def test_foreign_key():
     fk = op._foreign_key_constraint('fk_test', 't1', 't2', ['foo', 'bar'], ['bat', 'hoho'])
@@ -15,4 +18,50 @@ def test_unique_constraint():
         AddConstraint(uc),
         "ALTER TABLE t1 ADD CONSTRAINT uk_test UNIQUE (foo, bar)"
     )
-    
\ No newline at end of file
+    
+
+def test_table():
+    tb = op._table("some_table", 
+        Column('id', Integer, primary_key=True),
+        Column('foo_id', Integer, ForeignKey('foo.id')),
+        schema='schema'
+    )
+    assert_compiled(
+        CreateTable(tb),
+        "CREATE TABLE schema.some_table ("
+            "id INTEGER NOT NULL, "
+            "foo_id INTEGER, "
+            "PRIMARY KEY (id), "
+            "FOREIGN KEY(foo_id) REFERENCES foo (id))"
+    )
+
+    tb = op._table("some_table", 
+        Column('id', Integer, primary_key=True),
+        Column('foo_id', Integer, ForeignKey('foo.id')),
+        Column('foo_bar', Integer, ForeignKey('foo.bar')),
+    )
+    assert_compiled(
+        CreateTable(tb),
+        "CREATE TABLE some_table ("
+            "id INTEGER NOT NULL, "
+            "foo_id INTEGER, "
+            "foo_bar INTEGER, "
+            "PRIMARY KEY (id), "
+            "FOREIGN KEY(foo_id) REFERENCES foo (id), "
+            "FOREIGN KEY(foo_bar) REFERENCES foo (bar))"
+    )
+    
+    m = MetaData()
+    foo = Table('foo', m, Column('id', Integer, primary_key=True))
+    tb = op._table("some_table", 
+        Column('id', Integer, primary_key=True),
+        Column('foo_id', Integer, ForeignKey('foo.id')),
+    )
+    assert_compiled(
+        CreateTable(tb),
+        "CREATE TABLE some_table ("
+            "id INTEGER NOT NULL, "
+            "foo_id INTEGER, "
+            "PRIMARY KEY (id), "
+            "FOREIGN KEY(foo_id) REFERENCES foo (id))"
+    )
\ No newline at end of file