Browse Source

db: remove manual db_migration_count function.

More efficient to measure the ARRAY_SIZE(), which is a runtime
constant.  We move it into the unit test.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
issue-2504
Rusty Russell 6 years ago
parent
commit
bef7d45f1c
  1. 17
      wallet/db.c
  2. 9
      wallet/test/run-db.c

17
wallet/db.c

@ -1,5 +1,6 @@
#include "db.h"
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/json_escaped.h>
#include <common/version.h>
@ -600,20 +601,6 @@ static int db_get_version(struct db *db)
}
}
/**
* db_migration_count - Count how many migrations are available
*
* Returns the maximum migration index, i.e., the version number of an
* up-to-date database schema.
*/
static int db_migration_count(void)
{
int count = 0;
while (dbmigrations[count] != NULL)
count++;
return count - 1;
}
/**
* db_migrate - Apply all remaining migrations from the current version
*/
@ -625,7 +612,7 @@ static void db_migrate(struct db *db, struct log *log)
db_begin_transaction(db);
orig = current = db_get_version(db);
available = db_migration_count();
available = ARRAY_SIZE(dbmigrations) - 2;
if (current == -1)
log_info(log, "Creating database");

9
wallet/test/run-db.c

@ -51,6 +51,14 @@ static struct db *create_test_db(void)
return db;
}
static int db_migration_count(void)
{
int count = 0;
while (dbmigrations[count] != NULL)
count++;
return count - 1;
}
static bool test_empty_db_migrate(void)
{
struct db *db = create_test_db();
@ -61,6 +69,7 @@ static bool test_empty_db_migrate(void)
db_migrate(db, NULL);
db_begin_transaction(db);
CHECK(db_get_version(db) == db_migration_count());
CHECK(db_get_version(db) == ARRAY_SIZE(dbmigrations) - 2);
db_commit_transaction(db);
tal_free(db);

Loading…
Cancel
Save