| 482 | } |
| 483 | |
| 484 | static bool test_manip_columns(void) |
| 485 | { |
| 486 | struct db_stmt *stmt; |
| 487 | struct db *db = create_test_db(); |
| 488 | const char *field1 = "field1"; |
| 489 | |
| 490 | db_begin_transaction(db); |
| 491 | /* tablea refers to tableb */ |
| 492 | stmt = db_prepare_v2(db, SQL("CREATE TABLE tablea (" |
| 493 | " id BIGSERIAL" |
| 494 | ", field1 INTEGER" |
| 495 | ", PRIMARY KEY (id))")); |
| 496 | db_exec_prepared_v2(stmt); |
| 497 | tal_free(stmt); |
| 498 | |
| 499 | stmt = db_prepare_v2(db, SQL("INSERT INTO tablea (id, field1) VALUES (0, 1);")); |
| 500 | db_exec_prepared_v2(stmt); |
| 501 | tal_free(stmt); |
| 502 | |
| 503 | stmt = db_prepare_v2(db, SQL("CREATE TABLE tableb (" |
| 504 | " id REFERENCES tablea(id) ON DELETE CASCADE" |
| 505 | ", field1 INTEGER" |
| 506 | ", field2 INTEGER);")); |
| 507 | db_exec_prepared_v2(stmt); |
| 508 | tal_free(stmt); |
| 509 | |
| 510 | stmt = db_prepare_v2(db, SQL("INSERT INTO tableb (id, field1, field2) VALUES (0, 1, 2);")); |
| 511 | db_exec_prepared_v2(stmt); |
| 512 | tal_free(stmt); |
| 513 | |
| 514 | /* Needs vars table, since this changes db. */ |
| 515 | stmt = db_prepare_v2(db, SQL("CREATE TABLE vars (name VARCHAR(32), intval);")); |
| 516 | db_exec_prepared_v2(stmt); |
| 517 | tal_free(stmt); |
| 518 | stmt = db_prepare_v2(db, SQL("INSERT INTO vars VALUES ('data_version', 0);")); |
| 519 | db_exec_prepared_v2(stmt); |
| 520 | tal_free(stmt); |
| 521 | |
| 522 | /* Rename tablea.field1 -> table1.field1a. */ |
| 523 | CHECK(db->config->rename_column(db, "tablea", "field1", "field1a")); |
| 524 | /* Remove tableb.field1. */ |
| 525 | CHECK(db->config->delete_columns(db, "tableb", &field1, 1)); |
| 526 | |
| 527 | stmt = db_prepare_v2(db, SQL("SELECT id, field1a FROM tablea;")); |
| 528 | CHECK_MSG(db_query_prepared_canfail(stmt), "db_query_prepared must succeed"); |
| 529 | CHECK(db_step(stmt)); |
| 530 | CHECK(db_col_u64(stmt, "id") == 0); |
| 531 | CHECK(db_col_u64(stmt, "field1a") == 1); |
| 532 | CHECK(!db_step(stmt)); |
| 533 | tal_free(stmt); |
| 534 | |
| 535 | stmt = db_prepare_v2(db, SQL("SELECT id, field2 FROM tableb;")); |
| 536 | CHECK_MSG(db_query_prepared_canfail(stmt), "db_query_prepared must succeed"); |
| 537 | CHECK(db_step(stmt)); |
| 538 | CHECK(db_col_u64(stmt, "id") == 0); |
| 539 | CHECK(db_col_u64(stmt, "field2") == 2); |
| 540 | CHECK(!db_step(stmt)); |
| 541 | tal_free(stmt); |
no test coverage detected