* db_get_version - Determine the current DB schema version * * Will attempt to determine the current schema version of the * database @db by querying the `version` table. If the table does not * exist it'll return schema version -1, so that migration 0 is * applied, which should create the `version` table. */
| 14 | * applied, which should create the `version` table. |
| 15 | */ |
| 16 | int db_get_version(struct db *db) |
| 17 | { |
| 18 | int res = -1; |
| 19 | struct db_stmt *stmt = db_prepare_v2(db, SQL("SELECT version FROM version LIMIT 1")); |
| 20 | |
| 21 | /* |
| 22 | * Tentatively execute a query, but allow failures. Some databases |
| 23 | * like postgres will terminate the DB transaction if there is an |
| 24 | * error during the execution of a query, e.g., trying to access a |
| 25 | * table that doesn't exist yet, so we need to terminate and restart |
| 26 | * the DB transaction. |
| 27 | */ |
| 28 | if (!db_query_prepared(stmt)) { |
| 29 | db_commit_transaction(stmt->db); |
| 30 | db_begin_transaction(stmt->db); |
| 31 | tal_free(stmt); |
| 32 | return res; |
| 33 | } |
| 34 | |
| 35 | if (db_step(stmt)) |
| 36 | res = db_col_int(stmt, "version"); |
| 37 | |
| 38 | tal_free(stmt); |
| 39 | return res; |
| 40 | } |
| 41 | |
| 42 | u32 db_data_version_get(struct db *db) |
| 43 | { |