| 520 | } |
| 521 | |
| 522 | static bool complete_table_manip(struct db *db, |
| 523 | const char *tablename, |
| 524 | const char **coldefs, |
| 525 | const char **oldcolnames) |
| 526 | { |
| 527 | char *create_cmd, *insert_cmd, *drop_cmd; |
| 528 | |
| 529 | /* Create table */ |
| 530 | create_cmd = tal_fmt(tmpctx, "CREATE TABLE %s (", tablename); |
| 531 | for (size_t i = 0; i < tal_count(coldefs); i++) { |
| 532 | if (i != 0) |
| 533 | tal_append_fmt(&create_cmd, ", "); |
| 534 | tal_append_fmt(&create_cmd, "%s", coldefs[i]); |
| 535 | } |
| 536 | tal_append_fmt(&create_cmd, ";"); |
| 537 | |
| 538 | db_exec_prepared_v2(take(db_prepare_untranslated(db, create_cmd))); |
| 539 | |
| 540 | /* Populate table from old one */ |
| 541 | insert_cmd = tal_fmt(tmpctx, "INSERT INTO %s SELECT ", tablename); |
| 542 | for (size_t i = 0; i < tal_count(oldcolnames); i++) { |
| 543 | if (i != 0) |
| 544 | tal_append_fmt(&insert_cmd, ", "); |
| 545 | tal_append_fmt(&insert_cmd, "%s", oldcolnames[i]); |
| 546 | } |
| 547 | tal_append_fmt(&insert_cmd, " FROM temp_%s;", tablename); |
| 548 | |
| 549 | db_exec_prepared_v2(take(db_prepare_untranslated(db, insert_cmd))); |
| 550 | |
| 551 | /* Cleanup temp table */ |
| 552 | drop_cmd = tal_fmt(tmpctx, "DROP TABLE temp_%s;", tablename); |
| 553 | db_exec_prepared_v2(take(db_prepare_untranslated(db, drop_cmd))); |
| 554 | db_commit_transaction(db); |
| 555 | |
| 556 | /* Allow links between them (esp. cascade deletes!) */ |
| 557 | db->in_transaction = "Not really"; |
| 558 | db->transaction_started = true; |
| 559 | db_prepare_for_changes(db); |
| 560 | db_exec_prepared_v2(take(db_prepare_untranslated(db, |
| 561 | "PRAGMA foreign_keys = ON;"))); |
| 562 | db_report_changes(db, NULL, 0); |
| 563 | db->in_transaction = NULL; |
| 564 | db->transaction_started = false; |
| 565 | |
| 566 | /* migrations are performed inside transactions, so start one. */ |
| 567 | db_begin_transaction(db); |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | /* FIXME: sqlite3 version 3.25.0 (2018-09-15) supports ALTER TABLE RENAME */ |
| 572 | static bool db_sqlite3_rename_column(struct db *db, |
no test coverage detected