github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/enginetest/ddl_queries.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package enginetest 16 17 import ( 18 "github.com/dolthub/go-mysql-server/enginetest/queries" 19 "github.com/dolthub/go-mysql-server/sql" 20 "github.com/dolthub/go-mysql-server/sql/types" 21 22 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 23 ) 24 25 // Tests in this file are a grab bag of DDL queries, many of them ported from older parts of the Dolt codebase 26 // before enginetest format adoption. Typically you shouldn't add things here instead of in the enginetest package in 27 // go-mysql-server, but it's appropriate for dolt-specific tests of DDL operations. 28 29 var SimpsonsSetup = []string{ 30 `create table people (id int primary key, 31 first_name varchar(100) not null, 32 last_name varchar(100) not null, 33 is_married tinyint, 34 age int, 35 rating float, 36 uuid varchar(64), 37 num_episodes int unsigned);`, 38 `create table episodes (id int primary key, 39 name varchar(100) not null, 40 air_date datetime, 41 rating float);`, 42 `create table appearances (character_id int not null, 43 episode_id int not null, 44 comments varchar(100), 45 primary key (character_id, episode_id));`, 46 `insert into people values 47 (0, "Homer", "Simpson", 1, 40, 8.5, null, null), 48 (1, "Marge", "Simpson", 1, 38, 8, "00000000-0000-0000-0000-000000000001", 111), 49 (2, "Bart", "Simpson", 0, 10, 9, "00000000-0000-0000-0000-000000000002", 222), 50 (3, "Lisa", "Simpson", 0, 8, 10, "00000000-0000-0000-0000-000000000003", 333), 51 (4, "Moe", "Szyslak", 0, 48, 6.5, "00000000-0000-0000-0000-000000000004", 444), 52 (5, "Barney", "Gumble", 0, 40, 4, "00000000-0000-0000-0000-000000000005", 555); 53 `, 54 `insert into episodes values 55 (1, "Simpsons Roasting On an Open Fire", "1989-12-18 03:00:00", 8.0), 56 (2, "Bart the Genius", "1990-01-15 03:00:00", 9.0), 57 (3, "Homer's Odyssey", "1990-01-22 03:00:00", 7.0), 58 (4, "There's No Disgrace Like Home", "1990-01-29 03:00:00", 8.5); 59 `, 60 `insert into appearances values 61 (0, 1, "Homer is great in this one"), 62 (1, 1, "Marge is here too"), 63 (0, 2, "Homer is great in this one too"), 64 (2, 2, "This episode is named after Bart"), 65 (3, 2, "Lisa is here too"), 66 (4, 2, "I think there's a prank call scene"), 67 (0, 3, "Homer is in every episode"), 68 (1, 3, "Marge shows up a lot too"), 69 (3, 3, "Lisa is the best Simpson"), 70 (5, 3, "I'm making this all up"); 71 `, 72 } 73 74 var AllInitialSimpsonsCharacters = []sql.Row{ 75 {0, "Homer", "Simpson", 1, 40, 8.5, nil, nil}, 76 {1, "Marge", "Simpson", 1, 38, 8.0, "00000000-0000-0000-0000-000000000001", uint(111)}, 77 {2, "Bart", "Simpson", 0, 10, 9.0, "00000000-0000-0000-0000-000000000002", uint(222)}, 78 {3, "Lisa", "Simpson", 0, 8, 10.0, "00000000-0000-0000-0000-000000000003", uint(333)}, 79 {4, "Moe", "Szyslak", 0, 48, 6.5, "00000000-0000-0000-0000-000000000004", uint(444)}, 80 {5, "Barney", "Gumble", 0, 40, 4.0, "00000000-0000-0000-0000-000000000005", uint(555)}, 81 } 82 83 var ModifyAndChangeColumnScripts = []queries.ScriptTest{ 84 { 85 Name: "alter modify column reorder middle", 86 SetUpScript: SimpsonsSetup, 87 Assertions: []queries.ScriptTestAssertion{ 88 { 89 Query: "alter table people modify column first_name varchar(163) not null after last_name", 90 SkipResultsCheck: true, 91 }, 92 { 93 Query: "show create table people", 94 Expected: []sql.Row{sql.Row{"people", "CREATE TABLE `people` (\n" + 95 " `id` int NOT NULL,\n" + 96 " `last_name` varchar(100) NOT NULL,\n" + 97 " `first_name` varchar(163) NOT NULL,\n" + 98 " `is_married` tinyint,\n" + 99 " `age` int,\n" + 100 " `rating` float,\n" + 101 " `uuid` varchar(64),\n" + 102 " `num_episodes` int unsigned,\n" + 103 " PRIMARY KEY (`id`)\n" + 104 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 105 }, 106 { 107 Query: "select * from people order by 1", 108 Expected: []sql.Row{ 109 {0, "Simpson", "Homer", 1, 40, 8.5, nil, nil}, 110 {1, "Simpson", "Marge", 1, 38, 8.0, "00000000-0000-0000-0000-000000000001", uint(111)}, 111 {2, "Simpson", "Bart", 0, 10, 9.0, "00000000-0000-0000-0000-000000000002", uint(222)}, 112 {3, "Simpson", "Lisa", 0, 8, 10.0, "00000000-0000-0000-0000-000000000003", uint(333)}, 113 {4, "Szyslak", "Moe", 0, 48, 6.5, "00000000-0000-0000-0000-000000000004", uint(444)}, 114 {5, "Gumble", "Barney", 0, 40, 4.0, "00000000-0000-0000-0000-000000000005", uint(555)}, 115 }, 116 }, 117 }, 118 }, 119 { 120 Name: "alter modify column reorder first", 121 SetUpScript: SimpsonsSetup, 122 Assertions: []queries.ScriptTestAssertion{ 123 { 124 Query: "alter table people modify column first_name varchar(163) not null first", 125 SkipResultsCheck: true, 126 }, 127 { 128 Query: "show create table people", 129 Expected: []sql.Row{sql.Row{"people", "CREATE TABLE `people` (\n" + 130 " `first_name` varchar(163) NOT NULL,\n" + 131 " `id` int NOT NULL,\n" + 132 " `last_name` varchar(100) NOT NULL,\n" + 133 " `is_married` tinyint,\n" + 134 " `age` int,\n" + 135 " `rating` float,\n" + 136 " `uuid` varchar(64),\n" + 137 " `num_episodes` int unsigned,\n" + 138 " PRIMARY KEY (`id`)\n" + 139 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 140 }, 141 { 142 Query: "select * from people order by id", 143 Expected: []sql.Row{ 144 {"Homer", 0, "Simpson", 1, 40, 8.5, nil, nil}, 145 {"Marge", 1, "Simpson", 1, 38, 8.0, "00000000-0000-0000-0000-000000000001", uint(111)}, 146 {"Bart", 2, "Simpson", 0, 10, 9.0, "00000000-0000-0000-0000-000000000002", uint(222)}, 147 {"Lisa", 3, "Simpson", 0, 8, 10.0, "00000000-0000-0000-0000-000000000003", uint(333)}, 148 {"Moe", 4, "Szyslak", 0, 48, 6.5, "00000000-0000-0000-0000-000000000004", uint(444)}, 149 {"Barney", 5, "Gumble", 0, 40, 4.0, "00000000-0000-0000-0000-000000000005", uint(555)}, 150 }, 151 }, 152 }, 153 }, 154 { 155 Name: "alter modify column drop null constraint", 156 SetUpScript: SimpsonsSetup, 157 Assertions: []queries.ScriptTestAssertion{ 158 { 159 Query: "alter table people modify column first_name varchar(163) null", 160 SkipResultsCheck: true, 161 }, 162 { 163 Query: "show create table people", 164 Expected: []sql.Row{sql.Row{"people", "CREATE TABLE `people` (\n" + 165 " `id` int NOT NULL,\n" + 166 " `first_name` varchar(163),\n" + 167 " `last_name` varchar(100) NOT NULL,\n" + 168 " `is_married` tinyint,\n" + 169 " `age` int,\n" + 170 " `rating` float,\n" + 171 " `uuid` varchar(64),\n" + 172 " `num_episodes` int unsigned,\n" + 173 " PRIMARY KEY (`id`)\n" + 174 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 175 }, 176 { 177 Query: "select * from people order by id", 178 Expected: AllInitialSimpsonsCharacters, 179 }, 180 }, 181 }, 182 { 183 Name: "alter change column rename and reorder", 184 SetUpScript: SimpsonsSetup, 185 Assertions: []queries.ScriptTestAssertion{ 186 { 187 Query: "alter table people change first_name christian_name varchar(163) not null after last_name", 188 SkipResultsCheck: true, 189 }, 190 { 191 Query: "show create table people", 192 Expected: []sql.Row{sql.Row{"people", "CREATE TABLE `people` (\n" + 193 " `id` int NOT NULL,\n" + 194 " `last_name` varchar(100) NOT NULL,\n" + 195 " `christian_name` varchar(163) NOT NULL,\n" + 196 " `is_married` tinyint,\n" + 197 " `age` int,\n" + 198 " `rating` float,\n" + 199 " `uuid` varchar(64),\n" + 200 " `num_episodes` int unsigned,\n" + 201 " PRIMARY KEY (`id`)\n" + 202 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 203 }, 204 { 205 Query: "select * from people order by id", 206 Expected: []sql.Row{ 207 {0, "Simpson", "Homer", 1, 40, 8.5, nil, nil}, 208 {1, "Simpson", "Marge", 1, 38, 8.0, "00000000-0000-0000-0000-000000000001", uint(111)}, 209 {2, "Simpson", "Bart", 0, 10, 9.0, "00000000-0000-0000-0000-000000000002", uint(222)}, 210 {3, "Simpson", "Lisa", 0, 8, 10.0, "00000000-0000-0000-0000-000000000003", uint(333)}, 211 {4, "Szyslak", "Moe", 0, 48, 6.5, "00000000-0000-0000-0000-000000000004", uint(444)}, 212 {5, "Gumble", "Barney", 0, 40, 4.0, "00000000-0000-0000-0000-000000000005", uint(555)}, 213 }, 214 }, 215 }, 216 }, 217 { 218 Name: "alter change column rename and reorder first", 219 SetUpScript: SimpsonsSetup, 220 Assertions: []queries.ScriptTestAssertion{ 221 { 222 Query: "alter table people change column first_name christian_name varchar(163) not null first", 223 SkipResultsCheck: true, 224 }, 225 { 226 Query: "show create table people", 227 Expected: []sql.Row{sql.Row{"people", "CREATE TABLE `people` (\n" + 228 " `christian_name` varchar(163) NOT NULL,\n" + 229 " `id` int NOT NULL,\n" + 230 " `last_name` varchar(100) NOT NULL,\n" + 231 " `is_married` tinyint,\n" + 232 " `age` int,\n" + 233 " `rating` float,\n" + 234 " `uuid` varchar(64),\n" + 235 " `num_episodes` int unsigned,\n" + 236 " PRIMARY KEY (`id`)\n" + 237 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 238 }, 239 { 240 Query: "select * from people order by id", 241 Expected: []sql.Row{ 242 {"Homer", 0, "Simpson", 1, 40, 8.5, nil, nil}, 243 {"Marge", 1, "Simpson", 1, 38, 8.0, "00000000-0000-0000-0000-000000000001", uint(111)}, 244 {"Bart", 2, "Simpson", 0, 10, 9.0, "00000000-0000-0000-0000-000000000002", uint(222)}, 245 {"Lisa", 3, "Simpson", 0, 8, 10.0, "00000000-0000-0000-0000-000000000003", uint(333)}, 246 {"Moe", 4, "Szyslak", 0, 48, 6.5, "00000000-0000-0000-0000-000000000004", uint(444)}, 247 {"Barney", 5, "Gumble", 0, 40, 4.0, "00000000-0000-0000-0000-000000000005", uint(555)}, 248 }, 249 }, 250 }, 251 }, 252 { 253 Name: "alter change column drop null constraint", 254 SetUpScript: SimpsonsSetup, 255 Assertions: []queries.ScriptTestAssertion{ 256 { 257 Query: "alter table people change column first_name first_name varchar(163) null", 258 SkipResultsCheck: true, 259 }, 260 { 261 Query: "show create table people", 262 Expected: []sql.Row{sql.Row{"people", "CREATE TABLE `people` (\n" + 263 " `id` int NOT NULL,\n" + 264 " `first_name` varchar(163),\n" + 265 " `last_name` varchar(100) NOT NULL,\n" + 266 " `is_married` tinyint,\n" + 267 " `age` int,\n" + 268 " `rating` float,\n" + 269 " `uuid` varchar(64),\n" + 270 " `num_episodes` int unsigned,\n" + 271 " PRIMARY KEY (`id`)\n" + 272 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 273 }, 274 { 275 Query: "select * from people order by id", 276 Expected: AllInitialSimpsonsCharacters, 277 }, 278 }, 279 }, 280 { 281 Name: "alter modify column not null with type mismatch in default", 282 SetUpScript: SimpsonsSetup, 283 Assertions: []queries.ScriptTestAssertion{ 284 { 285 Query: "alter table people modify rating double default 'not a number'", 286 ExpectedErrStr: "incompatible type for default value: error: 'not a number' is not a valid value for 'double'", 287 }, 288 }, 289 }, 290 { 291 Name: "alter modify column not null, existing null values", 292 SetUpScript: SimpsonsSetup, 293 Assertions: []queries.ScriptTestAssertion{ 294 { 295 Query: "alter table people modify num_episodes bigint unsigned not null", 296 ExpectedErr: sql.ErrInsertIntoNonNullableProvidedNull, 297 }, 298 }, 299 }, 300 { 301 Name: "alter, rename primary key column", 302 SetUpScript: []string{ 303 "create table t (pk1 varchar(100), pk2 varchar(50), PRIMARY KEY (pk1, pk2))", 304 "alter table t change column pk2 pkTwo varchar(20)", 305 }, 306 Assertions: []queries.ScriptTestAssertion{ 307 { 308 Query: "show create table t", 309 Expected: []sql.Row{{"t", "CREATE TABLE `t` (\n `pk1` varchar(100) NOT NULL,\n `pkTwo` varchar(20) NOT NULL,\n PRIMARY KEY (`pk1`,`pkTwo`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 310 }, 311 }, 312 }, 313 } 314 315 var ModifyColumnTypeScripts = []queries.ScriptTest{ 316 { 317 Name: "alter modify column type similar types", 318 SetUpScript: []string{ 319 "create table test(pk bigint primary key, v1 bigint, index (v1))", 320 "insert into test values (0, 3), (1, 2)", 321 }, 322 Assertions: []queries.ScriptTestAssertion{ 323 { 324 Query: "alter table test modify column v1 int", 325 SkipResultsCheck: true, 326 }, 327 { 328 Query: "show create table test", 329 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n" + 330 " `pk` bigint NOT NULL,\n" + 331 " `v1` int,\n" + 332 " PRIMARY KEY (`pk`),\n" + 333 " KEY `v1` (`v1`)\n" + 334 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 335 }, 336 { 337 Query: "select * from test order by pk", 338 Expected: []sql.Row{{0, 3}, {1, 2}}, 339 }, 340 { 341 Query: "select * from test where v1 = 3", 342 Expected: []sql.Row{{0, 3}}, 343 }, 344 }, 345 }, 346 { 347 Name: "alter modify column type different types", 348 SetUpScript: []string{ 349 "create table test(pk bigint primary key, v1 bigint, index (v1))", 350 "insert into test values (0, 3), (1, 2)", 351 }, 352 Assertions: []queries.ScriptTestAssertion{ 353 { 354 Query: "alter table test modify column v1 varchar(20)", 355 SkipResultsCheck: true, 356 }, 357 { 358 Query: "show create table test", 359 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n" + 360 " `pk` bigint NOT NULL,\n" + 361 " `v1` varchar(20),\n" + 362 " PRIMARY KEY (`pk`),\n" + 363 " KEY `v1` (`v1`)\n" + 364 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 365 }, 366 { 367 Query: "select * from test order by pk", 368 Expected: []sql.Row{{0, "3"}, {1, "2"}}, 369 }, 370 { 371 Query: "select * from test where v1 = '3'", 372 Expected: []sql.Row{{0, "3"}}, 373 }, 374 }, 375 }, 376 { 377 Name: "alter modify column type different types reversed", 378 SetUpScript: []string{ 379 "create table test(pk bigint primary key, v1 varchar(20), index (v1))", 380 `insert into test values (0, "3"), (1, "2")`, 381 }, 382 Assertions: []queries.ScriptTestAssertion{ 383 { 384 Query: "alter table test modify column v1 bigint", 385 SkipResultsCheck: true, 386 }, 387 { 388 Query: "show create table test", 389 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n" + 390 " `pk` bigint NOT NULL,\n" + 391 " `v1` bigint,\n" + 392 " PRIMARY KEY (`pk`),\n" + 393 " KEY `v1` (`v1`)\n" + 394 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 395 }, 396 { 397 Query: "select * from test order by pk", 398 Expected: []sql.Row{{0, 3}, {1, 2}}, 399 }, 400 { 401 Query: "select * from test where v1 = 3", 402 Expected: []sql.Row{{0, 3}}, 403 }, 404 }, 405 }, 406 { 407 Name: "alter modify column type primary key", 408 SetUpScript: []string{ 409 "create table test(pk bigint primary key, v1 bigint, index (v1))", 410 "insert into test values (0, 3), (1, 2)", 411 }, 412 Assertions: []queries.ScriptTestAssertion{ 413 { 414 Query: "alter table test modify column pk varchar(20)", 415 SkipResultsCheck: true, 416 }, 417 { 418 Query: "show create table test", 419 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n" + 420 " `pk` varchar(20) NOT NULL,\n" + 421 " `v1` bigint,\n" + 422 " PRIMARY KEY (`pk`),\n" + 423 " KEY `v1` (`v1`)\n" + 424 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 425 }, 426 { 427 Query: "select * from test order by pk", 428 Expected: []sql.Row{{"0", 3}, {"1", 2}}, 429 }, 430 { 431 Query: "select * from test where v1 = 3", 432 Expected: []sql.Row{{"0", 3}}, 433 }, 434 }, 435 }, 436 { 437 Name: "alter modify column type incompatible types with empty table", 438 SetUpScript: []string{ 439 "create table test(pk bigint primary key, v1 bit(20), index (v1))", 440 }, 441 Assertions: []queries.ScriptTestAssertion{ 442 { 443 Query: "alter table test modify column pk datetime(6)", 444 SkipResultsCheck: true, 445 }, 446 { 447 Query: "show create table test", 448 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n" + 449 " `pk` datetime(6) NOT NULL,\n" + 450 " `v1` bit(20),\n" + 451 " PRIMARY KEY (`pk`),\n" + 452 " KEY `v1` (`v1`)\n" + 453 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 454 }, 455 { 456 Query: "select * from test order by pk", 457 Expected: []sql.Row{}, 458 }, 459 }, 460 }, 461 { 462 Name: "alter modify column type incompatible types with non-empty table", 463 SetUpScript: []string{ 464 "create table test(pk bigint primary key, v1 bit(20), index (v1))", 465 "insert into test values (1, 1)", 466 }, 467 Assertions: []queries.ScriptTestAssertion{ 468 { 469 Query: "alter table test modify column pk datetime", 470 ExpectedErr: types.ErrConvertingToTime, 471 }, 472 }, 473 }, 474 { 475 Name: "alter modify column type different types incompatible values", 476 SetUpScript: []string{ 477 "create table test(pk bigint primary key, v1 varchar(20), index (v1))", 478 "insert into test values (0, 3), (1, 'a')", 479 }, 480 Assertions: []queries.ScriptTestAssertion{ 481 { 482 Query: "alter table test modify column v1 bigint", 483 ExpectedErr: sql.ErrInvalidValue, 484 }, 485 }, 486 }, 487 { 488 Name: "alter modify column type foreign key parent", 489 SetUpScript: []string{ 490 "create table test(pk bigint primary key, v1 bigint, index (v1))", 491 "create table test2(pk bigint primary key, v1 bigint, index (v1), foreign key (v1) references test(v1))", 492 }, 493 Assertions: []queries.ScriptTestAssertion{ 494 { 495 Query: "alter table test modify column v1 varchar(20)", 496 ExpectedErr: sql.ErrForeignKeyTypeChange, 497 }, 498 }, 499 }, 500 { 501 Name: "alter modify column type foreign key child", 502 SetUpScript: []string{ 503 "create table test(pk bigint primary key, v1 bigint, index (v1))", 504 "create table test2(pk bigint primary key, v1 bigint, index (v1), foreign key (v1) references test(v1))", 505 }, 506 Assertions: []queries.ScriptTestAssertion{ 507 { 508 Query: "alter table test2 modify column v1 varchar(20)", 509 ExpectedErr: sql.ErrForeignKeyTypeChange, 510 }, 511 }, 512 }, 513 { 514 Name: "alter modify column type, make primary key spatial", 515 SetUpScript: []string{ 516 "create table point_tbl (p int primary key)", 517 }, 518 Assertions: []queries.ScriptTestAssertion{ 519 { 520 Query: "alter table point_tbl modify column p point primary key", 521 ExpectedErr: schema.ErrUsingSpatialKey, 522 }, 523 }, 524 }, 525 } 526 527 var DropColumnScripts = []queries.ScriptTest{ 528 { 529 Name: "alter drop column", 530 SetUpScript: SimpsonsSetup, 531 Assertions: []queries.ScriptTestAssertion{ 532 { 533 Query: "alter table people drop rating", 534 SkipResultsCheck: true, 535 }, 536 { 537 Query: "show create table people", 538 Expected: []sql.Row{{"people", "CREATE TABLE `people` (\n" + 539 " `id` int NOT NULL,\n" + 540 " `first_name` varchar(100) NOT NULL,\n" + 541 " `last_name` varchar(100) NOT NULL,\n" + 542 " `is_married` tinyint,\n" + 543 " `age` int,\n" + 544 " `uuid` varchar(64),\n" + 545 " `num_episodes` int unsigned,\n" + 546 " PRIMARY KEY (`id`)\n" + 547 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 548 }, 549 { 550 Query: "select * from people order by 1", 551 Expected: []sql.Row{ 552 {0, "Homer", "Simpson", 1, 40, nil, nil}, 553 {1, "Marge", "Simpson", 1, 38, "00000000-0000-0000-0000-000000000001", uint(111)}, 554 {2, "Bart", "Simpson", 0, 10, "00000000-0000-0000-0000-000000000002", uint(222)}, 555 {3, "Lisa", "Simpson", 0, 8, "00000000-0000-0000-0000-000000000003", uint(333)}, 556 {4, "Moe", "Szyslak", 0, 48, "00000000-0000-0000-0000-000000000004", uint(444)}, 557 {5, "Barney", "Gumble", 0, 40, "00000000-0000-0000-0000-000000000005", uint(555)}, 558 }, 559 }, 560 }, 561 }, 562 { 563 Name: "alter drop column with optional column keyword", 564 SetUpScript: SimpsonsSetup, 565 Assertions: []queries.ScriptTestAssertion{ 566 { 567 Query: "alter table people drop column rating", 568 SkipResultsCheck: true, 569 }, 570 { 571 Query: "show create table people", 572 Expected: []sql.Row{{"people", "CREATE TABLE `people` (\n" + 573 " `id` int NOT NULL,\n" + 574 " `first_name` varchar(100) NOT NULL,\n" + 575 " `last_name` varchar(100) NOT NULL,\n" + 576 " `is_married` tinyint,\n" + 577 " `age` int,\n" + 578 " `uuid` varchar(64),\n" + 579 " `num_episodes` int unsigned,\n" + 580 " PRIMARY KEY (`id`)\n" + 581 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 582 }, 583 { 584 Query: "select * from people order by 1", 585 Expected: []sql.Row{ 586 {0, "Homer", "Simpson", 1, 40, nil, nil}, 587 {1, "Marge", "Simpson", 1, 38, "00000000-0000-0000-0000-000000000001", uint(111)}, 588 {2, "Bart", "Simpson", 0, 10, "00000000-0000-0000-0000-000000000002", uint(222)}, 589 {3, "Lisa", "Simpson", 0, 8, "00000000-0000-0000-0000-000000000003", uint(333)}, 590 {4, "Moe", "Szyslak", 0, 48, "00000000-0000-0000-0000-000000000004", uint(444)}, 591 {5, "Barney", "Gumble", 0, 40, "00000000-0000-0000-0000-000000000005", uint(555)}, 592 }, 593 }, 594 }, 595 }, 596 { 597 Name: "drop primary key column", 598 SetUpScript: SimpsonsSetup, 599 Assertions: []queries.ScriptTestAssertion{ 600 { 601 Query: "alter table people drop column id", 602 SkipResultsCheck: true, 603 }, 604 { 605 Query: "show create table people", 606 Expected: []sql.Row{{"people", "CREATE TABLE `people` (\n" + 607 " `first_name` varchar(100) NOT NULL,\n" + 608 " `last_name` varchar(100) NOT NULL,\n" + 609 " `is_married` tinyint,\n" + 610 " `age` int,\n" + 611 " `rating` float,\n" + 612 " `uuid` varchar(64),\n" + 613 " `num_episodes` int unsigned\n" + 614 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 615 }, 616 { 617 Query: "select * from people order by first_name", 618 Expected: []sql.Row{ 619 {"Barney", "Gumble", 0, 40, 4.0, "00000000-0000-0000-0000-000000000005", uint(555)}, 620 {"Bart", "Simpson", 0, 10, 9.0, "00000000-0000-0000-0000-000000000002", uint(222)}, 621 {"Homer", "Simpson", 1, 40, 8.5, nil, nil}, 622 {"Lisa", "Simpson", 0, 8, 10.0, "00000000-0000-0000-0000-000000000003", uint(333)}, 623 {"Marge", "Simpson", 1, 38, 8.0, "00000000-0000-0000-0000-000000000001", uint(111)}, 624 {"Moe", "Szyslak", 0, 48, 6.5, "00000000-0000-0000-0000-000000000004", uint(444)}, 625 }, 626 }, 627 }, 628 }, 629 } 630 631 var BrokenDDLScripts = []queries.ScriptTest{ 632 { 633 Name: "drop first of two primary key columns", 634 SetUpScript: []string{ 635 "create table test (p1 int, p2 int, c1 int, c2 int, index (c1))", 636 "insert into test values (0, 1, 2, 3), (4, 5, 6, 7)", 637 }, 638 Assertions: []queries.ScriptTestAssertion{ 639 { 640 Query: "alter table test drop column p1", 641 SkipResultsCheck: true, 642 }, 643 { 644 Query: "show create table test", 645 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n" + 646 " `p2` int,\n" + 647 " `c1` int,\n" + 648 " `c2` int,\n" + 649 " KEY `c1` (`c1`)\n" + 650 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 651 }, 652 { 653 Query: "select * from test order by pk", 654 Expected: []sql.Row{{0, 3}, {1, 2}}, 655 }, 656 { 657 Query: "select * from test where v1 = 3", 658 Expected: []sql.Row{{0, 3}}, 659 }, 660 }, 661 }, 662 { 663 Name: "alter string column to truncate data", 664 SetUpScript: []string{ 665 "create table t1 (a int primary key, b varchar(3))", 666 "insert into t1 values (1, 'hi'), (2, 'bye')", 667 }, 668 Assertions: []queries.ScriptTestAssertion{ 669 { 670 Query: "alter table t1 modify b varchar(2)", 671 ExpectedErr: sql.ErrInvalidValue, // not sure of the type of error, but it should give one 672 }, 673 }, 674 }, 675 { 676 Name: "alter datetime column with invalid values", 677 SetUpScript: []string{ 678 "CREATE TABLE t3(pk BIGINT PRIMARY KEY, v1 DATETIME, INDEX(v1))", 679 "INSERT INTO t3 VALUES (0,'1999-11-02 17:39:38'),(1,'3021-01-08 02:59:27');", 680 }, 681 Assertions: []queries.ScriptTestAssertion{ 682 { 683 Query: "alter table t3 modify v1 timestamp", 684 ExpectedErr: sql.ErrInvalidValue, // not sure of the type of error, but it should give one 685 }, 686 }, 687 }, 688 { 689 // This actually "works", incorrectly 690 // We need to disambiguate the column names when they collide with generated ones 691 // https://github.com/dolthub/dolt/issues/3395 692 Name: "table with commit column should maintain its data in diff", 693 SetUpScript: []string{ 694 "CREATE TABLE t (pk int PRIMARY KEY, commit text);", 695 "CALL DOLT_ADD('.');", 696 "set @Commit1 = dolt_commit('-am', 'creating table t');", 697 "INSERT INTO t VALUES (1, 'hi');", 698 "set @Commit2 = dolt_commit('-am', 'insert data');", 699 }, 700 Assertions: []queries.ScriptTestAssertion{ 701 { 702 Query: "SELECT to_pk, to_commit, from_pk, from_commit, diff_type from dolt_diff(@Commit1, @Commit2, 't');", 703 Expected: []sql.Row{{1, "hi", nil, nil, "added"}}, 704 }, 705 }, 706 }, 707 } 708 709 var AddIndexScripts = []queries.ScriptTest{ 710 { 711 Name: "add unique constraint on keyless table", 712 SetUpScript: []string{ 713 "CREATE TABLE test (uk int);", 714 "insert into test values (0), (0)", 715 }, 716 Assertions: []queries.ScriptTestAssertion{ 717 { 718 Query: "create unique index m on test (uk);", 719 ExpectedErr: sql.ErrUniqueKeyViolation, 720 }, 721 }, 722 }, 723 } 724 725 var AddDropPrimaryKeysScripts = []queries.ScriptTest{ 726 { 727 Name: "drop primary key blocked when foreign key present", 728 SetUpScript: []string{ 729 "create table parent (a int primary key )", 730 "create table child (b int primary key, c int, key (c))", 731 "alter table child add constraint fk1 foreign key (c) references parent (a)", 732 }, 733 Assertions: []queries.ScriptTestAssertion{ 734 { 735 Query: "alter table parent drop primary key", 736 ExpectedErr: sql.ErrCantDropIndex, 737 }, 738 }, 739 }, 740 { 741 Name: "drop primary key succeeds when foreign key present on other column", 742 SetUpScript: []string{ 743 "create table parent (a int primary key, d int, key (d))", 744 "create table child (b int primary key, c int, key (c))", 745 "alter table child add constraint fk1 foreign key (c) references parent (d)", 746 }, 747 Assertions: []queries.ScriptTestAssertion{ 748 { 749 Query: "alter table parent drop primary key", 750 Expected: []sql.Row{{types.OkResult{RowsAffected: 0x0, InsertID: 0x0}}}, 751 }, 752 }, 753 }, 754 { 755 Name: "drop primary key succeeds when foreign key present on other table", 756 SetUpScript: []string{ 757 "create table unrelated (a int primary key, d int)", 758 "create table parent (a int primary key)", 759 "create table child (b int primary key, c int, key (c))", 760 "alter table child add constraint fk1 foreign key (c) references parent (a)", 761 }, 762 Assertions: []queries.ScriptTestAssertion{ 763 { 764 Query: "alter table unrelated drop primary key", 765 Expected: []sql.Row{{types.OkResult{RowsAffected: 0x0, InsertID: 0x0}}}, 766 }, 767 }, 768 }, 769 }