From: Mike Bayer Date: Mon, 28 Nov 2011 01:29:40 +0000 (-0500) Subject: - use new sa. import in tutorial examples X-Git-Tag: rel_0_1_0~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fee327e67822bc96b1b90e4ad065ba4b0d2033d6;p=thirdparty%2Fsqlalchemy%2Falembic.git - use new sa. import in tutorial examples - add an alter column autogen test with an FK constraint --- diff --git a/docs/build/tutorial.rst b/docs/build/tutorial.rst index c621769a..bdf28f7a 100644 --- a/docs/build/tutorial.rst +++ b/docs/build/tutorial.rst @@ -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') diff --git a/tests/test_autogenerate.py b/tests/test_autogenerate.py index 05335cb4..8ae983b7 100644 --- a/tests/test_autogenerate.py +++ b/tests/test_autogenerate.py @@ -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')