@@ -5401,31 +5401,9 @@ func TestJsonScripts(t *testing.T, harness Harness) {
54015401}
54025402
54035403func TestAlterTable (t * testing.T , harness Harness ) {
5404- errorTests := []queries.QueryErrorTest {
5405- {
5406- Query : "ALTER TABLE one_pk_two_idx MODIFY COLUMN v1 BIGINT DEFAULT (pk) AFTER v3" ,
5407- ExpectedErr : sql .ErrTableColumnNotFound ,
5408- },
5409- {
5410- Query : "ALTER TABLE one_pk_two_idx ADD COLUMN v4 BIGINT DEFAULT (pk) AFTER v3" ,
5411- ExpectedErr : sql .ErrTableColumnNotFound ,
5412- },
5413- {
5414- Query : "ALTER TABLE one_pk_two_idx ADD COLUMN v3 BIGINT DEFAULT 5, RENAME COLUMN v3 to v4" ,
5415- ExpectedErr : sql .ErrTableColumnNotFound ,
5416- },
5417- {
5418- Query : "ALTER TABLE one_pk_two_idx ADD COLUMN v3 BIGINT DEFAULT 5, modify column v3 bigint default null" ,
5419- ExpectedErr : sql .ErrTableColumnNotFound ,
5420- },
5421- }
5422-
54235404 harness .Setup (setup .MydbData , setup .Pk_tablesData )
54245405 e := mustNewEngine (t , harness )
54255406 defer e .Close ()
5426- for _ , tt := range errorTests {
5427- runQueryErrorTest (t , harness , tt )
5428- }
54295407
54305408 t .Run ("variety of alter column statements in a single statement" , func (t * testing.T ) {
54315409 RunQuery (t , e , harness , "CREATE TABLE t32(pk BIGINT PRIMARY KEY, v1 int, v2 int, v3 int default (v1), toRename int)" )
@@ -5503,216 +5481,9 @@ func TestAlterTable(t *testing.T, harness Harness) {
55035481 }, checks )
55045482 })
55055483
5506- t .Run ("drop column drops check constraint" , func (t * testing.T ) {
5507- RunQuery (t , e , harness , "create table t34 (i bigint primary key, s varchar(20))" )
5508- RunQuery (t , e , harness , "ALTER TABLE t34 ADD COLUMN j int" )
5509- RunQuery (t , e , harness , "ALTER TABLE t34 ADD CONSTRAINT test_check CHECK (j < 12345)" )
5510- RunQuery (t , e , harness , "ALTER TABLE t34 DROP COLUMN j" )
5511- tt := queries.QueryTest {
5512- Query : "show create table t34" ,
5513- Expected : []sql.Row {{"t34" , "CREATE TABLE `t34` (\n " +
5514- " `i` bigint NOT NULL,\n " +
5515- " `s` varchar(20),\n " +
5516- " PRIMARY KEY (`i`)\n " +
5517- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5518- }
5519- TestQueryWithEngine (t , harness , e , tt )
5520- })
5521-
5522- t .Run ("drop column drops all relevant check constraints" , func (t * testing.T ) {
5523- RunQuery (t , e , harness , "create table t42 (i bigint primary key, s varchar(20))" )
5524- RunQuery (t , e , harness , "ALTER TABLE t42 ADD COLUMN j int" )
5525- RunQuery (t , e , harness , "ALTER TABLE t42 ADD CONSTRAINT check1 CHECK (j < 12345)" )
5526- RunQuery (t , e , harness , "ALTER TABLE t42 ADD CONSTRAINT check2 CHECK (j > 0)" )
5527- RunQuery (t , e , harness , "ALTER TABLE t42 DROP COLUMN j" )
5528- tt := queries.QueryTest {
5529- Query : "show create table t42" ,
5530- Expected : []sql.Row {{"t42" , "CREATE TABLE `t42` (\n " +
5531- " `i` bigint NOT NULL,\n " +
5532- " `s` varchar(20),\n " +
5533- " PRIMARY KEY (`i`)\n " +
5534- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5535- }
5536- TestQueryWithEngine (t , harness , e , tt )
5537- })
5538-
5539- t .Run ("drop column drops correct check constraint" , func (t * testing.T ) {
5540- RunQuery (t , e , harness , "create table t41 (i bigint primary key, s varchar(20))" )
5541- RunQuery (t , e , harness , "ALTER TABLE t41 ADD COLUMN j int" )
5542- RunQuery (t , e , harness , "ALTER TABLE t41 ADD COLUMN k int" )
5543- RunQuery (t , e , harness , "ALTER TABLE t41 ADD CONSTRAINT j_check CHECK (j < 12345)" )
5544- RunQuery (t , e , harness , "ALTER TABLE t41 ADD CONSTRAINT k_check CHECK (k < 123)" )
5545- RunQuery (t , e , harness , "ALTER TABLE t41 DROP COLUMN j" )
5546- tt := queries.QueryTest {
5547- Query : "show create table t41" ,
5548- Expected : []sql.Row {{"t41" , "CREATE TABLE `t41` (\n " +
5549- " `i` bigint NOT NULL,\n " +
5550- " `s` varchar(20),\n " +
5551- " `k` int,\n " +
5552- " PRIMARY KEY (`i`),\n " +
5553- " CONSTRAINT `k_check` CHECK ((`k` < 123))\n " +
5554- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5555- }
5556- TestQueryWithEngine (t , harness , e , tt )
5557- })
5558-
5559- t .Run ("drop column does not drop when referenced in constraint with other column" , func (t * testing.T ) {
5560- RunQuery (t , e , harness , "create table t43 (i bigint primary key, s varchar(20))" )
5561- RunQuery (t , e , harness , "ALTER TABLE t43 ADD COLUMN j int" )
5562- RunQuery (t , e , harness , "ALTER TABLE t43 ADD COLUMN k int" )
5563- RunQuery (t , e , harness , "ALTER TABLE t43 ADD CONSTRAINT test_check CHECK (j < k)" )
5564- AssertErr (t , e , harness , "ALTER TABLE t43 DROP COLUMN j" , sql .ErrCheckConstraintInvalidatedByColumnAlter )
5565- tt := queries.QueryTest {
5566- Query : "show create table t43" ,
5567- Expected : []sql.Row {{"t43" , "CREATE TABLE `t43` (\n " +
5568- " `i` bigint NOT NULL,\n " +
5569- " `s` varchar(20),\n " +
5570- " `j` int,\n " +
5571- " `k` int,\n " +
5572- " PRIMARY KEY (`i`),\n " +
5573- " CONSTRAINT `test_check` CHECK ((`j` < `k`))\n " +
5574- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5575- }
5576- TestQueryWithEngine (t , harness , e , tt )
5577- })
5578-
5579- t .Run ("drop column preserves indexes" , func (t * testing.T ) {
5580- ctx := NewContext (harness )
5581- RunQuery (t , e , harness , "create table t35 (i bigint primary key, s varchar(20), s2 varchar(20))" )
5582- RunQuery (t , e , harness , "ALTER TABLE t35 ADD unique key test_key (s)" )
5583-
5584- RunQuery (t , e , harness , "ALTER TABLE t35 DROP COLUMN s2" )
5585- TestQueryWithContext (t , ctx , e , harness , "show create table t35" ,
5586- []sql.Row {{"t35" , "CREATE TABLE `t35` (\n " +
5587- " `i` bigint NOT NULL,\n " +
5588- " `s` varchar(20),\n " +
5589- " PRIMARY KEY (`i`),\n " +
5590- " UNIQUE KEY `test_key` (`s`)\n " +
5591- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5592- nil , nil )
5593- })
5594-
5595- t .Run ("drop column prevents foreign key violations" , func (t * testing.T ) {
5596- RunQuery (t , e , harness , "create table t36 (i bigint primary key, j varchar(20))" )
5597- RunQuery (t , e , harness , "create table t37 (i bigint primary key, j varchar(20))" )
5598- RunQuery (t , e , harness , "ALTER TABLE t36 ADD key (j)" )
5599- RunQuery (t , e , harness , "ALTER TABLE t37 ADD constraint fk_36 foreign key (j) references t36(j)" )
5600-
5601- AssertErr (t , e , harness , "ALTER TABLE t37 DROP COLUMN j" , sql .ErrForeignKeyDropColumn )
5602- })
5603-
5604- t .Run ("disable keys / enable keys" , func (t * testing.T ) {
5605- ctx := NewContext (harness )
5606- AssertWarningAndTestQuery (t , e , ctx , harness , "ALTER TABLE t33 DISABLE KEYS" ,
5607- []sql.Row {{types .NewOkResult (0 )}},
5608- nil , mysql .ERNotSupportedYet , 1 ,
5609- "" , false )
5610- AssertWarningAndTestQuery (t , e , ctx , harness , "ALTER TABLE t33 ENABLE KEYS" ,
5611- []sql.Row {{types .NewOkResult (0 )}}, nil , mysql .ERNotSupportedYet , 1 ,
5612- "" , false )
5613- })
5614-
5615- t .Run ("adding a unique constraint errors if violations exist" , func (t * testing.T ) {
5616- // single column unique constraint (success)
5617- RunQuery (t , e , harness , "CREATE TABLE t38 (pk int PRIMARY KEY, col1 int)" )
5618- RunQuery (t , e , harness , "INSERT INTO t38 VALUES (1, 1)" )
5619- RunQuery (t , e , harness , "INSERT INTO t38 VALUES (2, 2)" )
5620- RunQuery (t , e , harness , "INSERT INTO t38 VALUES (3, NULL)" )
5621- RunQuery (t , e , harness , "INSERT INTO t38 VALUES (4, NULL)" )
5622- RunQuery (t , e , harness , "ALTER TABLE t38 ADD UNIQUE u_col1 (col1)" )
5623-
5624- // multi column unique constraint (success)
5625- RunQuery (t , e , harness , "CREATE TABLE t39 (pk int PRIMARY KEY, col1 int, col2 int)" )
5626- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (1, 1, 1)" )
5627- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (2, 1, 2)" )
5628- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (3, 2, 1)" )
5629- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (4, 1, NULL)" )
5630- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (5, 1, NULL)" )
5631- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (6, NULL, 1)" )
5632- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (7, NULL, 1)" )
5633- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (8, NULL, NULL)" )
5634- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (9, NULL, NULL)" )
5635- RunQuery (t , e , harness , "ALTER TABLE t39 ADD UNIQUE u_col1_col2 (col1, col2)" )
5636-
5637- // single column unique constraint (failure)
5638- RunQuery (t , e , harness , "ALTER TABLE t38 DROP INDEX u_col1;" )
5639- RunQuery (t , e , harness , "INSERT INTO t38 VALUES (5, 1);" )
5640- AssertErr (t , e , harness , "ALTER TABLE t38 ADD UNIQUE u_col1 (col1)" , sql .ErrUniqueKeyViolation )
5641- tt := queries.QueryTest {
5642- Query : "show create table t38;" ,
5643- Expected : []sql.Row {{"t38" , "CREATE TABLE `t38` (\n " +
5644- " `pk` int NOT NULL,\n " +
5645- " `col1` int,\n " +
5646- " PRIMARY KEY (`pk`)\n " +
5647- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5648- }
5649- TestQueryWithEngine (t , harness , e , tt )
5650-
5651- // multi column unique constraint (failure)
5652- RunQuery (t , e , harness , "ALTER TABLE t39 DROP INDEX u_col1_col2;" )
5653- RunQuery (t , e , harness , "INSERT INTO t39 VALUES (10, 1, 1);" )
5654- AssertErr (t , e , harness , "ALTER TABLE t39 ADD UNIQUE u_col1_col2 (col1, col2)" , sql .ErrUniqueKeyViolation )
5655- tt = queries.QueryTest {
5656- Query : "show create table t39;" ,
5657- Expected : []sql.Row {{"t39" , "CREATE TABLE `t39` (\n " +
5658- " `pk` int NOT NULL,\n " +
5659- " `col1` int,\n " +
5660- " `col2` int,\n " +
5661- " PRIMARY KEY (`pk`)\n " +
5662- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5663- }
5664- TestQueryWithEngine (t , harness , e , tt )
5665- })
5666-
5667- t .Run ("ALTER TABLE remove AUTO_INCREMENT" , func (t * testing.T ) {
5668- RunQuery (t , e , harness , "CREATE TABLE t40 (pk int AUTO_INCREMENT PRIMARY KEY, val int)" )
5669- RunQuery (t , e , harness , "INSERT into t40 VALUES (1, 1), (NULL, 2), (NULL, 3)" )
5670-
5671- RunQuery (t , e , harness , "ALTER TABLE t40 MODIFY COLUMN pk int" )
5672- ctx := harness .NewContext ()
5673- TestQueryWithContext (t , ctx , e , harness , "DESCRIBE t40" , []sql.Row {
5674- {"pk" , "int" , "NO" , "PRI" , "NULL" , "" },
5675- {"val" , "int" , "YES" , "" , "NULL" , "" }},
5676- nil , nil )
5677-
5678- AssertErr (t , e , harness , "INSERT INTO t40 VALUES (NULL, 4)" , sql .ErrInsertIntoNonNullableProvidedNull )
5679- RunQuery (t , e , harness , "DROP TABLE t40" )
5680-
5681- RunQuery (t , e , harness , "CREATE TABLE t40 (pk int AUTO_INCREMENT PRIMARY KEY, val int)" )
5682- RunQuery (t , e , harness , "INSERT into t40 VALUES (NULL, 1)" )
5683-
5684- TestQueryWithContext (t , ctx , e , harness , "SELECT * FROM t40" , []sql.Row {{1 , 1 }}, nil , nil )
5685- })
5686-
5687- TestScript (t , harness , queries.ScriptTest {
5688- // https://github.com/dolthub/dolt/issues/6206
5689- Name : "alter table containing column default value expressions" ,
5690- SetUpScript : []string {
5691- "create table t (pk int primary key, col1 timestamp default current_timestamp(), col2 varchar(1000), index idx1 (pk, col1));" ,
5692- },
5693- Assertions : []queries.ScriptTestAssertion {
5694- {
5695- Query : "alter table t alter column col2 DROP DEFAULT;" ,
5696- Expected : []sql.Row {},
5697- },
5698- {
5699- Query : "show create table t;" ,
5700- Expected : []sql.Row {{"t" , "CREATE TABLE `t` (\n `pk` int NOT NULL,\n `col1` timestamp(6) DEFAULT (CURRENT_TIMESTAMP()),\n `col2` varchar(1000),\n PRIMARY KEY (`pk`),\n KEY `idx1` (`pk`,`col1`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5701- },
5702- {
5703- Query : "alter table t alter column col2 SET DEFAULT 'FOO!';" ,
5704- Expected : []sql.Row {},
5705- },
5706- {
5707- Query : "show create table t;" ,
5708- Expected : []sql.Row {{"t" , "CREATE TABLE `t` (\n `pk` int NOT NULL,\n `col1` timestamp(6) DEFAULT (CURRENT_TIMESTAMP()),\n `col2` varchar(1000) DEFAULT 'FOO!',\n PRIMARY KEY (`pk`),\n KEY `idx1` (`pk`,`col1`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin" }},
5709- },
5710- {
5711- Query : "alter table t drop index idx1;" ,
5712- Expected : []sql.Row {{types .NewOkResult (0 )}},
5713- },
5714- },
5715- })
5484+ for _ , script := range queries .AlterTableScripts {
5485+ TestScript (t , harness , script )
5486+ }
57165487}
57175488
57185489func NewColumnDefaultValue (expr sql.Expression , outType sql.Type , representsLiteral , isParenthesized , mayReturnNil bool ) * sql.ColumnDefaultValue {
0 commit comments