MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_manip_columns

Function test_manip_columns

wallet/test/run-db.c:171–259  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169}
170
171static 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

Callers 1

mainFunction · 0.85

Calls 7

db_exec_prepared_v2Function · 0.85
tal_freeFunction · 0.85
db_query_preparedFunction · 0.85
db_stepFunction · 0.85
db_col_u64Function · 0.85
create_test_dbFunction · 0.70
db_commit_transactionFunction · 0.50

Tested by

no test coverage detected