Example #1
0
 def test_create_index_no_expr_allowed(self):
     op_fixture()
     assert_raises_message(
         ValueError,
         "String or text\(\) construct expected",
         op.create_index, 'name', 'tname', [func.foo(column('x'))]
     )
Example #2
0
 def test_drop_check(self):
     op_fixture('mysql')
     assert_raises_message(
         NotImplementedError,
         "MySQL does not support CHECK constraints.",
         op.drop_constraint, "f1", "t1", "check"
     )
Example #3
0
 def test_add_foreign_key_dialect_kw(self):
     op_fixture()
     with mock.patch("sqlalchemy.schema.ForeignKeyConstraint") as fkc:
         op.create_foreign_key("fk_test", "t1", "t2", ["foo", "bar"], ["bat", "hoho"], foobar_arg="xyz")
         if config.requirements.foreign_key_match.enabled:
             eq_(
                 fkc.mock_calls[0],
                 mock.call(
                     ["foo", "bar"],
                     ["t2.bat", "t2.hoho"],
                     onupdate=None,
                     ondelete=None,
                     name="fk_test",
                     foobar_arg="xyz",
                     deferrable=None,
                     initially=None,
                     match=None,
                 ),
             )
         else:
             eq_(
                 fkc.mock_calls[0],
                 mock.call(
                     ["foo", "bar"],
                     ["t2.bat", "t2.hoho"],
                     onupdate=None,
                     ondelete=None,
                     name="fk_test",
                     foobar_arg="xyz",
                     deferrable=None,
                     initially=None,
                 ),
             )
    def test_add_fk_constraint_separate_colkeys(self):
        m = MetaData()
        Table('a', m, Column('id', Integer, key='aid', primary_key=True))
        b = Table('b', m, Column('a_id', Integer, key='baid'))
        fk = ForeignKeyConstraint(['baid'], ['a.aid'], name='fk_a_id')
        b.append_constraint(fk)

        py_code = autogenerate.render._add_table(b, self.autogen_context)

        eq_ignore_whitespace(
            py_code,
            "op.create_table('b',"
            "sa.Column('a_id', sa.Integer(), nullable=True),"
            "sa.ForeignKeyConstraint(['a_id'], ['a.id'], name='fk_a_id'))"
        )

        context = op_fixture()
        eval(py_code)
        context.assert_(
            "CREATE TABLE b (a_id INTEGER, CONSTRAINT "
            "fk_a_id FOREIGN KEY(a_id) REFERENCES a (id))")

        context = op_fixture()
        py_code = autogenerate.render._add_fk_constraint(
            fk, self.autogen_context)

        eq_ignore_whitespace(
            autogenerate.render._add_fk_constraint(fk, self.autogen_context),
            "op.create_foreign_key('fk_a_id', 'b', 'a', ['a_id'], ['id'])"
        )

        eval(py_code)
        context.assert_(
            "ALTER TABLE b ADD CONSTRAINT fk_a_id "
            "FOREIGN KEY(a_id) REFERENCES a (id)")
Example #5
0
 def test_col_alter_type_required(self):
     op_fixture('mysql')
     assert_raises_message(
         util.CommandError,
         "MySQL CHANGE/MODIFY COLUMN operations require the existing type.",
         op.alter_column, 't1', 'c1', nullable=False, server_default="q"
     )
Example #6
0
 def test_drop_unknown(self):
     op_fixture('mysql')
     assert_raises_message(
         TypeError,
         "'type' can be one of 'check', 'foreignkey', "
         "'primary', 'unique', None",
         op.drop_constraint, "f1", "t1", "typo"
     )
Example #7
0
 def test_drop_generic_constraint(self):
     op_fixture('mysql')
     assert_raises_message(
         NotImplementedError,
         "No generic 'DROP CONSTRAINT' in MySQL - please "
         "specify constraint type",
         op.drop_constraint, "f1", "t1"
     )
Example #8
0
 def test_drop_explicit_constraint(self):
     op_fixture('sqlite')
     assert_raises_message(
         NotImplementedError,
         "No support for ALTER of constraints in SQLite dialect",
         op.drop_constraint,
         "foo",
         "sometable",
     )
Example #9
0
 def test_create_index_no_expr_allowed(self):
     op_fixture()
     assert_raises_message(
         ValueError,
         r"String or text\(\) construct expected",
         op.create_index,
         "name",
         "tname",
         [func.foo(column("x"))],
     )
Example #10
0
 def test_add_explicit_constraint(self):
     op_fixture('sqlite')
     assert_raises_message(
         NotImplementedError,
         "No support for ALTER of constraints in SQLite dialect",
         op.create_check_constraint,
         "foo",
         "sometable",
         column('name') > 5
     )
Example #11
0
    def test_legacy_kwarg_catches_arg_missing(self):
        op_fixture()

        assert_raises_message(
            TypeError,
            "missing required positional argument: columns",
            op.create_primary_key,
            name=None,
            table_name="sometable",
            wrong_cols=["router_id", "l3_agent_id"],
        )
Example #12
0
 def test_alter_column_nullable_type_required(self):
     op_fixture("mssql")
     assert_raises_message(
         util.CommandError,
         "MS-SQL ALTER COLUMN operations with NULL or "
         "NOT NULL require the existing_type or a new "
         "type_ be passed.",
         op.alter_column,
         "t",
         "c",
         nullable=False,
     )
Example #13
0
    def test_create_index_table_col_event(self):
        context = op_fixture()

        op.create_index('ik_test', 'tbl_with_auto_appended_column', ['foo', 'bar'])
        context.assert_(
            "CREATE INDEX ik_test ON tbl_with_auto_appended_column (foo, bar)"
        )
Example #14
0
 def test_alter_column_multi_alter_w_drop_default(self):
     context = op_fixture('mysql')
     op.alter_column(
         't1', 'c1', nullable=False, server_default=None, type_=Integer)
     context.assert_(
         "ALTER TABLE t1 MODIFY c1 INTEGER NOT NULL"
     )
Example #15
0
 def test_col_multi_alter(self):
     context = op_fixture('mysql')
     op.alter_column(
         't1', 'c1', nullable=False, server_default="q", type_=Integer)
     context.assert_(
         "ALTER TABLE t1 MODIFY c1 INTEGER NOT NULL DEFAULT 'q'"
     )
Example #16
0
 def test_col_not_nullable_existing_serv_default(self):
     context = op_fixture('mysql')
     op.alter_column('t1', 'c1', nullable=False, existing_type=Integer,
                     existing_server_default='5')
     context.assert_(
         "ALTER TABLE t1 MODIFY c1 INTEGER NOT NULL DEFAULT '5'"
     )
Example #17
0
 def test_alter_column_modify_default(self):
     context = op_fixture('mysql')
     # notice we dont need the existing type on this one...
     op.alter_column("t", "c", server_default='1')
     context.assert_(
         "ALTER TABLE t ALTER COLUMN c SET DEFAULT '1'"
     )
Example #18
0
 def test_alter_replace_server_default(self):
     context = op_fixture('oracle')
     op.alter_column(
         "t", "c", server_default="5", existing_server_default="6")
     context.assert_(
         "ALTER TABLE t MODIFY c DEFAULT '5'"
     )
Example #19
0
 def test_alter_column_not_nullable_w_new_type(self):
     context = op_fixture('oracle')
     op.alter_column("t", "c", nullable=False, type_=Integer)
     context.assert_(
         "ALTER TABLE t MODIFY c NOT NULL",
         "ALTER TABLE t MODIFY c INTEGER"
     )
Example #20
0
 def test_alter_column_dont_touch_constraints(self):
     context = op_fixture('mssql')
     from sqlalchemy import Boolean
     op.alter_column('tests', 'col',
                     existing_type=Boolean(),
                     nullable=False)
     context.assert_('ALTER TABLE tests ALTER COLUMN col BIT NOT NULL')
Example #21
0
 def test_drop_column_w_fk_in_batch(self):
     context = op_fixture('mssql')
     with op.batch_alter_table('t1', schema=None) as batch_op:
         batch_op.drop_column('c1', mssql_drop_foreign_key=True)
     context.assert_contains(
         "exec('alter table t1 drop constraint ' + @const_name)")
     context.assert_contains("ALTER TABLE t1 DROP COLUMN c1")
Example #22
0
    def test_custom_op(self):
        from alembic.operations import Operations, MigrateOperation

        @Operations.register_operation("create_sequence")
        class CreateSequenceOp(MigrateOperation):
            """Create a SEQUENCE."""

            def __init__(self, sequence_name, **kw):
                self.sequence_name = sequence_name
                self.kw = kw

            @classmethod
            def create_sequence(cls, operations, sequence_name, **kw):
                """Issue a "CREATE SEQUENCE" instruction."""

                op = CreateSequenceOp(sequence_name, **kw)
                return operations.invoke(op)

        @Operations.implementation_for(CreateSequenceOp)
        def create_sequence(operations, operation):
            operations.execute("CREATE SEQUENCE %s" % operation.sequence_name)

        context = op_fixture()
        op.create_sequence('foob')
        context.assert_("CREATE SEQUENCE foob")
Example #23
0
    def test_naming_changes(self):
        context = op_fixture()
        op.alter_column("t", "c", name="x")
        context.assert_("ALTER TABLE t RENAME c TO x")

        context = op_fixture()
        op.alter_column("t", "c", new_column_name="x")
        context.assert_("ALTER TABLE t RENAME c TO x")

        context = op_fixture('mysql')
        op.drop_constraint("f1", "t1", type="foreignkey")
        context.assert_("ALTER TABLE t1 DROP FOREIGN KEY f1")

        context = op_fixture('mysql')
        op.drop_constraint("f1", "t1", type_="foreignkey")
        context.assert_("ALTER TABLE t1 DROP FOREIGN KEY f1")
Example #24
0
 def test_rename_column(self):
     context = op_fixture('mysql')
     op.alter_column(
         't1', 'c1', new_column_name="c2", existing_type=Integer)
     context.assert_(
         'ALTER TABLE t1 CHANGE c1 c2 INTEGER NULL'
     )
Example #25
0
 def test_add_foreign_key_self_referential(self):
     context = op_fixture()
     op.create_foreign_key("fk_test", "t1", "t1", ["foo"], ["bar"])
     context.assert_(
         "ALTER TABLE t1 ADD CONSTRAINT fk_test "
         "FOREIGN KEY(foo) REFERENCES t1 (bar)"
     )
Example #26
0
 def test_alter_column_add_comment_quotes(self):
     context = op_fixture("oracle")
     op.alter_column("t", "c", type_=Integer, comment="c 'comment'")
     context.assert_(
         "ALTER TABLE t MODIFY c INTEGER",
         "COMMENT ON COLUMN t.c IS 'c ''comment'''",
     )
Example #27
0
    def test_col_dont_remove_server_default(self):
        context = op_fixture('mysql')
        op.alter_column('t1', 'c1', existing_type=Integer,
                        existing_server_default='1',
                        server_default=False)

        context.assert_()
Example #28
0
 def test_drop_column_w_check(self):
     context = op_fixture('mssql')
     op.drop_column('t1', 'c1', mssql_drop_check=True)
     op.drop_column('t1', 'c2', mssql_drop_check=True)
     context.assert_contains(
         "exec('alter table t1 drop constraint ' + @const_name)")
     context.assert_contains("ALTER TABLE t1 DROP COLUMN c1")
Example #29
0
 def test_col_add_autoincrement(self):
     context = op_fixture('mysql')
     op.alter_column('t1', 'c1', existing_type=Integer,
                     autoincrement=True)
     context.assert_(
         'ALTER TABLE t1 MODIFY c1 INTEGER NULL AUTO_INCREMENT'
     )
Example #30
0
 def test_rename_column_quotes_needed_one(self):
     context = op_fixture('mysql')
     op.alter_column('MyTable', 'ColumnOne', new_column_name="ColumnTwo",
                     existing_type=Integer)
     context.assert_(
         'ALTER TABLE `MyTable` CHANGE `ColumnOne` `ColumnTwo` INTEGER NULL'
     )
Example #31
0
 def test_drop_index(self):
     context = op_fixture("oracle")
     op.drop_index("my_idx", "my_table")
     context.assert_contains("DROP INDEX my_idx")
Example #32
0
 def test_drop_column_w_check(self):
     context = op_fixture("oracle")
     op.drop_column("t1", "c1")
     context.assert_("ALTER TABLE t1 DROP COLUMN c1")
Example #33
0
 def test_alter_column_not_nullable_w_existing_type(self):
     context = op_fixture("oracle")
     op.alter_column("t", "c", nullable=False, existing_type=Integer)
     context.assert_("ALTER TABLE t MODIFY c NOT NULL")