| 169 | } |
| 170 | |
| 171 | static bool test_manip_columns(void) |
| 172 | { |
| 173 | struct db_stmt *stmt; |
| 174 | struct db *db = create_test_db(); |
| 175 | const char *field1 = "field1"; |
| 176 | |
| 177 | db_begin_transaction(db); |
| 178 | /* tablea refers to tableb */ |
| 179 | stmt = db_prepare_v2(db, SQL("CREATE TABLE tablea (" |
| 180 | " id BIGSERIAL" |
| 181 | ", field1 INTEGER" |
| 182 | ", PRIMARY KEY (id))")); |
| 183 | CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed"); |
| 184 | CHECK_MSG(!db_err, "Simple correct SQL command"); |
| 185 | tal_free(stmt); |
| 186 | |
| 187 | stmt = db_prepare_v2(db, SQL("INSERT INTO tablea (id, field1) VALUES (0, 1);")); |
| 188 | CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed"); |
| 189 | CHECK_MSG(!db_err, "Simple correct SQL command"); |
| 190 | tal_free(stmt); |
| 191 | |
| 192 | stmt = db_prepare_v2(db, SQL("CREATE TABLE tableb (" |
| 193 | " id REFERENCES tablea(id) ON DELETE CASCADE" |
| 194 | ", field1 INTEGER" |
| 195 | ", field2 INTEGER);")); |
| 196 | CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed"); |
| 197 | CHECK_MSG(!db_err, "Simple correct SQL command"); |
| 198 | tal_free(stmt); |
| 199 | |
| 200 | stmt = db_prepare_v2(db, SQL("INSERT INTO tableb (id, field1, field2) VALUES (0, 1, 2);")); |
| 201 | CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed"); |
| 202 | CHECK_MSG(!db_err, "Simple correct SQL command"); |
| 203 | tal_free(stmt); |
| 204 | |
| 205 | /* Needs vars table, since this changes db. */ |
| 206 | stmt = db_prepare_v2(db, SQL("CREATE TABLE vars (name VARCHAR(32), intval);")); |
| 207 | CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed"); |
| 208 | CHECK_MSG(!db_err, "Simple correct SQL command"); |
| 209 | tal_free(stmt); |
| 210 | stmt = db_prepare_v2(db, SQL("INSERT INTO vars VALUES ('data_version', 0);")); |
| 211 | CHECK_MSG(db_exec_prepared_v2(stmt), "db_exec_prepared must succeed"); |
| 212 | CHECK_MSG(!db_err, "Simple correct SQL command"); |
| 213 | tal_free(stmt); |
| 214 | |
| 215 | /* Rename tablea.field1 -> table1.field1a. */ |
| 216 | CHECK(db->config->rename_column(db, "tablea", "field1", "field1a")); |
| 217 | /* Remove tableb.field1. */ |
| 218 | CHECK(db->config->delete_columns(db, "tableb", &field1, 1)); |
| 219 | |
| 220 | stmt = db_prepare_v2(db, SQL("SELECT id, field1a FROM tablea;")); |
| 221 | CHECK_MSG(db_query_prepared(stmt), "db_query_prepared must succeed"); |
| 222 | CHECK_MSG(!db_err, "Simple correct SQL command"); |
| 223 | CHECK(db_step(stmt)); |
| 224 | CHECK(db_col_u64(stmt, "id") == 0); |
| 225 | CHECK(db_col_u64(stmt, "field1a") == 1); |
| 226 | CHECK(!db_step(stmt)); |
| 227 | tal_free(stmt); |
| 228 |
no test coverage detected