| 203 | #endif /* DB_FATAL */ |
| 204 | |
| 205 | struct db *db_setup(const tal_t *ctx, struct plugin *p, |
| 206 | const char *db_dsn, bool *created) |
| 207 | { |
| 208 | bool migrated; |
| 209 | struct db *db; |
| 210 | |
| 211 | /* Set global for db_fatal */ |
| 212 | plugin = p; |
| 213 | db = db_open(ctx, db_dsn); |
| 214 | db->report_changes_fn = NULL; |
| 215 | |
| 216 | db_begin_transaction(db); |
| 217 | migrated = db_migrate(p, db, created); |
| 218 | db->data_version = db_data_version_get(db); |
| 219 | db_commit_transaction(db); |
| 220 | |
| 221 | /* This needs to be done outside a transaction, apparently. |
| 222 | * It's a good idea to do this every so often, and on db |
| 223 | * upgrade is a reasonable time. */ |
| 224 | if (migrated && !db->config->vacuum_fn(db)) |
| 225 | db_fatal("Error vacuuming db: %s", db->error); |
| 226 | |
| 227 | return db; |
| 228 | } |