]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- use new sa. import in tutorial examples
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 28 Nov 2011 01:29:40 +0000 (20:29 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 28 Nov 2011 01:29:40 +0000 (20:29 -0500)
- add an alter column autogen test with an FK constraint

docs/build/tutorial.rst
tests/test_autogenerate.py

index c621769ab42e9879ff74db3f397b0cdd9605df54..bdf28f7aa7a4a9949df084008ab004e9823b245d 100644 (file)
@@ -236,14 +236,12 @@ the ability to build the current database fully).
 
 We can then add some directives to our script, suppose adding a new table ``account``::
 
-    from sqlalchemy import Integer, String, Unicode, Column
-
     def upgrade():
         create_table(
             'account',
-            Column('id', Integer, primary_key=True),
-            Column('name', String(50), nullable=False),
-            Column('description', Unicode(200)),
+            sa.Column('id', sa.Integer, primary_key=True),
+            sa.Column('name', sa.String(50), nullable=False),
+            sa.Column('description', sa.Unicode(200)),
         )
 
     def downgrade():
@@ -307,10 +305,9 @@ Let's edit this file and add a new column to the ``account`` table::
 
     from alembic.op import *
     import sqlalchemy as sa
-    from sqlalchemy import DateTime, Column
 
     def upgrade():
-        add_column('account', Column('last_transaction_date', DateTime))
+        add_column('account', sa.Column('last_transaction_date', sa.DateTime))
 
     def downgrade():
         drop_column('account', 'last_transaction_date')
index 05335cb40f9e1321c06141dc3a9d8107169b12b4..8ae983b7a399f34008dffd02ab0d08715a46709a 100644 (file)
@@ -21,7 +21,7 @@ def _model_one():
 
     Table('order', m,
         Column('order_id', Integer, primary_key=True),
-        Column("amount", Numeric(8, 2), nullable=False)
+        Column("amount", Numeric(8, 2), nullable=False),
     )
 
     Table('extra', m,
@@ -47,12 +47,14 @@ def _model_two():
 
     Table('order', m,
         Column('order_id', Integer, primary_key=True),
-        Column("amount", Numeric(10, 2), nullable=True)
+        Column("amount", Numeric(10, 2), nullable=True),
+        Column('user_id', Integer, ForeignKey('user.id')),
     )
 
     Table('item', m, 
         Column('id', Integer, primary_key=True),
-        Column('description', String(100))
+        Column('description', String(100)),
+        Column('order_id', Integer, ForeignKey('order.order_id')),
     )
     return m
 
@@ -86,14 +88,16 @@ class AutogenerateDiffTest(TestCase):
         eq_(dropcol.type._type_affinity, String)
         eq_(dropcol.type.length, 50)
 
-        eq_(repr(diffs[2][3]), "NUMERIC(precision=8, scale=2)")
-        eq_(repr(diffs[2][4]), "Numeric(precision=10, scale=2)")
-        del diffs[2]
+
+        eq_(repr(diffs[3][3]), "NUMERIC(precision=8, scale=2)")
+        eq_(repr(diffs[3][4]), "Numeric(precision=10, scale=2)")
+        del diffs[3]
         eq_(
             diffs,
             [
                 ('add_table', metadata.tables['item']), 
                 ('modify_nullable', 'user', 'name', True, False), 
+                ('add_column', 'order', metadata.tables['order'].c.user_id),
                 ('modify_nullable', 'order', u'amount', False, True), 
                 ('add_column', 'address', 
                         metadata.tables['address'].c.street)
@@ -115,15 +119,19 @@ class AutogenerateDiffTest(TestCase):
     create_table('item',
     sa.Column('id', sa.Integer(), nullable=False),
     sa.Column('description', sa.String(length=100), nullable=True),
+    sa.Column('order_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint([order_id], ['order.order_id'], ),
     sa.PrimaryKeyConstraint('id')
     )
     drop_table(u'extra')
     drop_column('user', u'pw')
     alter_column('user', 'name', nullable=False)
+    add_column('order', sa.Column('user_id', sa.Integer(), nullable=True))
     alter_column('order', u'amount', type_=sa.Numeric(precision=10, scale=2), old_type=sa.NUMERIC(precision=8, scale=2))
     alter_column('order', u'amount', nullable=True)
     add_column('address', sa.Column('street', sa.String(length=50), nullable=True))
     ### end Alembic commands ###""")
+
         eq_(template_args['downgrades'],
 """### commands auto generated by Alembic - please adjust! ###
     drop_table('item')
@@ -133,6 +141,7 @@ class AutogenerateDiffTest(TestCase):
     )
     add_column('user', sa.Column(u'pw', sa.VARCHAR(length=50), nullable=True))
     alter_column('user', 'name', nullable=True)
+    drop_column('order', 'user_id')
     alter_column('order', u'amount', type_=sa.NUMERIC(precision=8, scale=2), old_type=sa.Numeric(precision=10, scale=2))
     alter_column('order', u'amount', nullable=False)
     drop_column('address', 'street')