From bef7d45f1c9eef8ab481607717c4df678b7234ca Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 24 Feb 2019 15:40:37 +1030 Subject: [PATCH] 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 --- wallet/db.c | 17 ++--------------- wallet/test/run-db.c | 9 +++++++++ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index ac10fb4a6..314a6e2a8 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -1,5 +1,6 @@ #include "db.h" +#include #include #include #include @@ -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"); diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 605c0348c..fb80ee307 100644 --- a/wallet/test/run-db.c +++ b/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);