Exemplo n.º 1
0
    def test_parse_alter_table_stmt(self, mysql_conn):
        table_name = self._get_random_table_name()
        stmt = 'CREATE TABLE `{}` (id int(11));'.format(table_name)
        self._apply_ddl_stmt(mysql_conn, stmt)
        stmt = 'ALTER TABLE `{}` ADD due_by timestamp null;'.format(table_name)
        self._apply_ddl_stmt(mysql_conn, stmt)

        actual = parse_mysql_statement(mysql_conn, mysql_ddl_stmt=stmt)

        expected = MySQLTable(
            db_name='test',
            table_name=table_name,
            columns=[
                MySQLColumn(
                    column_name='id', ordinal_position=1,
                    column_default=None, is_nullable='YES',
                    data_type='int', char_max_len=None,
                    numeric_precision=10, numeric_scale=0,
                    char_set_name=None, collation_name=None,
                    column_type='int(11)'
                ),
                MySQLColumn(
                    column_name='due_by', ordinal_position=2,
                    column_default=None, is_nullable='YES',
                    data_type='timestamp', char_max_len=None,
                    numeric_precision=None, numeric_scale=None,
                    char_set_name=None, collation_name=None,
                    column_type='timestamp'
                )
            ],
            primary_keys=[]
        )
        assert actual == expected

        self._apply_ddl_stmt(mysql_conn, 'DROP TABLE `{}`;'.format(table_name))
Exemplo n.º 2
0
    def test_parse_create_table_stmt_with_primary_key(self, mysql_conn):
        table_name = self._get_random_table_name()
        stmt = ('CREATE TABLE `{}` ('
                '  id int(11) not null,'
                '  pid bigint(12) not null default 10, '
                '  name varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci, '
                '  primary key (pid, id)'
                ');').format(table_name)
        self._apply_ddl_stmt(mysql_conn, stmt)

        actual = parse_mysql_statement(mysql_conn, mysql_ddl_stmt=stmt)

        expected = MySQLTable(
            db_name='test',
            table_name=table_name,
            columns=[
                MySQLColumn(
                    column_name='id', ordinal_position=1,
                    column_default=None, is_nullable='NO',
                    data_type='int', char_max_len=None,
                    numeric_precision=10, numeric_scale=0,
                    char_set_name=None, collation_name=None,
                    column_type='int(11)'
                ),
                MySQLColumn(
                    column_name='pid', ordinal_position=2,
                    column_default='10', is_nullable='NO',
                    data_type='bigint', char_max_len=None,
                    numeric_precision=19, numeric_scale=0,
                    char_set_name=None, collation_name=None,
                    column_type='bigint(12)'
                ),
                MySQLColumn(
                    column_name='name', ordinal_position=3,
                    column_default=None, is_nullable='YES',
                    data_type='varchar', char_max_len=16,
                    numeric_precision=None, numeric_scale=None,
                    char_set_name='utf8', collation_name='utf8_general_ci',
                    column_type='varchar(16)'
                )
            ],
            primary_keys=[
                MySQLKey(
                    constraint_name='PRIMARY',
                    column_name='pid',
                    ordinal_position=1
                ),
                MySQLKey(
                    constraint_name='PRIMARY',
                    column_name='id',
                    ordinal_position=2
                )
            ]
        )
        assert actual == expected

        self._apply_ddl_stmt(mysql_conn, 'DROP TABLE `{}`;'.format(table_name))
Exemplo n.º 3
0
 def test_parse_empty_mysql_stmt(self, mysql_conn):
     with pytest.raises(ValueError):
         parse_mysql_statement(mysql_conn, mysql_ddl_stmt=' ')