From 927845f1f015eb2f8752b56651190d612e3ee502 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 27 Jan 2021 11:41:03 -0400 Subject: [PATCH] fix: allow having multiple databases each one with their own migration definition --- .gitignore | 3 +- Makefile | 12 +++---- nim_status/lib/accounts.nim | 4 +-- nim_status/lib/database.nim | 10 ++++-- nim_status/lib/migration.nim | 32 ++++++++++--------- .../accounts/0001_accounts.down.sql | 1 + .../migrations/accounts/0001_accounts.up.sql | 7 ++++ .../1605007189_identity_images.down.sql | 1 + .../1605007189_identity_images.up.sql | 10 ++++++ ...181_drop_photo_path_from_accounts.down.sql | 23 +++++++++++++ ...24181_drop_photo_path_from_accounts.up.sql | 23 +++++++++++++ .../{ => app}/000001_init.down.db.sql | 0 .../{ => app}/000001_init.up.db.sql | 0 .../000002_add_last_ens_clock_value.down.sql | 0 .../000002_add_last_ens_clock_value.up.sql | 0 .../migrations/{ => app}/0001_app.down.sql | 0 .../lib/migrations/{ => app}/0001_app.up.sql | 0 .../migrations/{ => app}/0002_tokens.down.sql | 0 .../migrations/{ => app}/0002_tokens.up.sql | 0 .../{ => app}/0003_settings.down.sql | 0 .../migrations/{ => app}/0003_settings.up.sql | 0 .../{ => app}/0004_pending_stickers.down.sql | 0 .../{ => app}/0004_pending_stickers.up.sql | 0 .../{ => app}/0005_waku_mode.down.sql | 0 .../{ => app}/0005_waku_mode.up.sql | 0 .../{ => app}/0006_appearance.up.sql | 0 .../{ => app}/0007_enable_waku_default.up.sql | 0 .../0008_add_push_notifications.up.sql | 0 ...enable_sending_push_notifications.down.sql | 0 ...9_enable_sending_push_notifications.up.sql | 0 .../0010_add_block_mentions.down.sql | 0 .../{ => app}/0010_add_block_mentions.up.sql | 0 ...allow_webview_permission_requests.down.sql | 0 ...1_allow_webview_permission_requests.up.sql | 0 .../0012_pending_transactions.down.sql | 0 .../0012_pending_transactions.up.sql | 0 .../{ => app}/0013_favourites.down.sql | 0 .../{ => app}/0013_favourites.up.sql | 0 .../0014_add_use_mailservers.down.sql | 0 .../{ => app}/0014_add_use_mailservers.up.sql | 0 .../{ => app}/0015_link_previews.down.sql | 0 .../{ => app}/0015_link_previews.up.sql | 0 ...6_local_notifications_preferences.down.sql | 0 ...016_local_notifications_preferences.up.sql | 0 .../{ => app}/0017_bookmarks.down.sql | 0 .../{ => app}/0017_bookmarks.up.sql | 0 .../{ => app}/1586358095_add_replace.down.sql | 0 .../{ => app}/1586358095_add_replace.up.sql | 0 .../1588665364_add_image_data.down.sql | 0 .../1588665364_add_image_data.up.sql | 0 .../1589365189_add_pow_target.down.sql | 0 .../1589365189_add_pow_target.up.sql | 0 .../1591277220_add_index_messages.down.sql | 0 .../1591277220_add_index_messages.up.sql | 0 ..._mute_chat_and_raw_message_fields.down.sql | 0 ...dd_mute_chat_and_raw_message_fields.up.sql | 0 .../1595862781_add_audio_data.down.sql | 0 .../1595862781_add_audio_data.up.sql | 0 ...5249_create_emoji_reactions_table.down.sql | 0 ...865249_create_emoji_reactions_table.up.sql | 0 ...eate_group_chat_invitations_table.down.sql | 0 ...create_group_chat_invitations_table.up.sql | 0 ...655_add_invitation_admin_chat_field.up.sql | 0 .../{ => app}/1597757544_add_nickname.up.sql | 0 .../1598955122_add_mentions.down.sql | 0 .../{ => app}/1598955122_add_mentions.up.sql | 0 ...9641390_add_emoji_reactions_index.down.sql | 0 ...599641390_add_emoji_reactions_index.up.sql | 0 ...d_seen_index_remove_long_messages.down.sql | 0 ...add_seen_index_remove_long_messages.up.sql | 0 .../1603198582_add_profile_chat_field.up.sql | 0 .../{ => app}/1603816533_add_links.down.sql | 0 .../{ => app}/1603816533_add_links.up.sql | 0 ...hat_identity_last_published_table.down.sql | 0 ..._chat_identity_last_published_table.up.sql | 0 .../1605075346_add_communities.up.sql | 0 nim_status/lib/migrations/sql_generate.nim | 19 +++++------ nim_status/lib/migrations/types.nim | 6 ++++ test/nim/accounts.nim | 3 +- test/nim/callrpc.nim | 8 +++-- test/nim/chats.nim | 5 +-- test/nim/contacts.nim | 5 +-- test/nim/mailservers.nim | 3 +- test/nim/messages.nim | 5 +-- test/nim/migrations.nim | 25 ++++++++------- test/nim/pendingtxs.nim | 5 +-- test/nim/permissions.nim | 5 +-- test/nim/settings.nim | 3 +- test/nim/tokens.nim | 4 ++- 89 files changed, 158 insertions(+), 64 deletions(-) create mode 100644 nim_status/lib/migrations/accounts/0001_accounts.down.sql create mode 100644 nim_status/lib/migrations/accounts/0001_accounts.up.sql create mode 100644 nim_status/lib/migrations/accounts/1605007189_identity_images.down.sql create mode 100644 nim_status/lib/migrations/accounts/1605007189_identity_images.up.sql create mode 100644 nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.down.sql create mode 100644 nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.up.sql rename nim_status/lib/migrations/{ => app}/000001_init.down.db.sql (100%) rename nim_status/lib/migrations/{ => app}/000001_init.up.db.sql (100%) rename nim_status/lib/migrations/{ => app}/000002_add_last_ens_clock_value.down.sql (100%) rename nim_status/lib/migrations/{ => app}/000002_add_last_ens_clock_value.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0001_app.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0001_app.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0002_tokens.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0002_tokens.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0003_settings.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0003_settings.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0004_pending_stickers.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0004_pending_stickers.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0005_waku_mode.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0005_waku_mode.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0006_appearance.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0007_enable_waku_default.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0008_add_push_notifications.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0009_enable_sending_push_notifications.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0009_enable_sending_push_notifications.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0010_add_block_mentions.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0010_add_block_mentions.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0011_allow_webview_permission_requests.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0011_allow_webview_permission_requests.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0012_pending_transactions.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0012_pending_transactions.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0013_favourites.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0013_favourites.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0014_add_use_mailservers.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0014_add_use_mailservers.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0015_link_previews.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0015_link_previews.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0016_local_notifications_preferences.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0016_local_notifications_preferences.up.sql (100%) rename nim_status/lib/migrations/{ => app}/0017_bookmarks.down.sql (100%) rename nim_status/lib/migrations/{ => app}/0017_bookmarks.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1586358095_add_replace.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1586358095_add_replace.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1588665364_add_image_data.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1588665364_add_image_data.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1589365189_add_pow_target.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1589365189_add_pow_target.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1591277220_add_index_messages.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1591277220_add_index_messages.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1593087212_add_mute_chat_and_raw_message_fields.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1593087212_add_mute_chat_and_raw_message_fields.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1595862781_add_audio_data.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1595862781_add_audio_data.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1595865249_create_emoji_reactions_table.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1595865249_create_emoji_reactions_table.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1596805115_create_group_chat_invitations_table.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1596805115_create_group_chat_invitations_table.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1597322655_add_invitation_admin_chat_field.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1597757544_add_nickname.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1598955122_add_mentions.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1598955122_add_mentions.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1599641390_add_emoji_reactions_index.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1599641390_add_emoji_reactions_index.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1599720851_add_seen_index_remove_long_messages.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1599720851_add_seen_index_remove_long_messages.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1603198582_add_profile_chat_field.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1603816533_add_links.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1603816533_add_links.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1603888149_create_chat_identity_last_published_table.down.sql (100%) rename nim_status/lib/migrations/{ => app}/1603888149_create_chat_identity_last_published_table.up.sql (100%) rename nim_status/lib/migrations/{ => app}/1605075346_add_communities.up.sql (100%) create mode 100644 nim_status/lib/migrations/types.nim diff --git a/.gitignore b/.gitignore index 824df06..a3fceff 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ /vendor/.nimble TODO sql_generate -sql_scripts.nim \ No newline at end of file +sql_scripts_accounts.nim +sql_scripts_app.nim diff --git a/Makefile b/Makefile index 9e8de31..60926fc 100644 --- a/Makefile +++ b/Makefile @@ -212,16 +212,16 @@ ifneq ($(NIMSTATUS_CFLAGS),) NIM_PARAMS += --passC:"$(NIMSTATUS_CFLAGS)" endif -MIGRATIONS ?= nim_status/lib/migrations/sql_scripts.nim +MIGRATIONS ?= nim_status/lib/migrations/sql_scripts_app.nim $(MIGRATIONS): | deps - $(ENV_SCRIPT) nim c -r $(NIM_PARAMS) \ - --verbosity:0 \ - nim_status/lib/migrations/sql_generate.nim > \ - nim_status/lib/migrations/sql_scripts.nim 2> /dev/null + $(ENV_SCRIPT) nim c $(NIM_PARAMS) --verbosity:0 nim_status/lib/migrations/sql_generate.nim + nim_status/lib/migrations/sql_generate nim_status/lib/migrations/accounts > nim_status/lib/migrations/sql_scripts_accounts.nim + nim_status/lib/migrations/sql_generate nim_status/lib/migrations/app > nim_status/lib/migrations/sql_scripts_app.nim clean-migration-file: - rm -f nim_status/lib/migrations/sql_scripts.nim + rm -f nim_status/lib/migrations/sql_scripts_accounts.nim + rm -f nim_status/lib/migrations/sql_scripts_app.nim migrations: clean-migration-file $(MIGRATIONS) diff --git a/nim_status/lib/accounts.nim b/nim_status/lib/accounts.nim index 1b94ec3..9418917 100644 --- a/nim_status/lib/accounts.nim +++ b/nim_status/lib/accounts.nim @@ -4,7 +4,7 @@ import # vendor libs web3, chronos, web3/conversions as web3_conversions, web3/ethtypes, sqlcipher, json_serialization, json_serialization/[reader, writer, lexer], stew/byteutils - +import migrations/sql_scripts_app import conversions, settings/types, settings, database, conversions, callrpc var db_conn*: DbConn @@ -18,7 +18,7 @@ proc login*(accountData, password: string) = # TODO: determine where the web3 conn will live let path = getCurrentDir() / accountData & ".db" - db_conn = initializeDB(path, password) + db_conn = initializeDB(path, password, newMigrationDefinition()) # TODO: these settings should have been set when calling saveAccountAndLogin let settingsStr = """{ diff --git a/nim_status/lib/database.nim b/nim_status/lib/database.nim index 54b319a..ea58f7e 100644 --- a/nim_status/lib/database.nim +++ b/nim_status/lib/database.nim @@ -2,8 +2,14 @@ import sqlcipher import migration import results -proc initializeDB*(path, password: string):DbConn = +proc initializeDB*(path:string, definition: MigrationDefinition):DbConn = + result = openDatabase(path) + if not result.migrate(definition).isOk: + raise newException(SqliteError, "Failure executing migrations") + + +proc initializeDB*(path, password: string, definition: MigrationDefinition):DbConn = result = openDatabase(path) result.key(password) - if not result.migrate().isOk: + if not result.migrate(definition).isOk: raise newException(SqliteError, "Failure executing migrations") diff --git a/nim_status/lib/migration.nim b/nim_status/lib/migration.nim index ac5efb3..1ce915b 100644 --- a/nim_status/lib/migration.nim +++ b/nim_status/lib/migration.nim @@ -1,9 +1,11 @@ import sqlcipher, results import sequtils, tables, algorithm, strformat import stew/byteutils -import migrations/sql_scripts import nimcrypto import chronicles +import migrations/types + +export MigrationDefinition type Migration* {.dbTableName("migrations").} = object name* {.dbColumnName("name").}: string @@ -36,9 +38,9 @@ proc getAllMigrationsExecuted*(db: DbConn): seq[Migration] = const query = fmt"SELECT {migration.name.columnName}, {migration.hash.columnName} FROM {migration.tableName} ORDER BY rowid ASC;" db.all(Migration, query) -proc checkMigrations*(db: DbConn): bool = +proc checkMigrations*(db: DbConn, definition: MigrationDefinition): bool = let allMigrationsExecuted = db.getAllMigrationsExecuted() - let migrations = toSeq(migrationUp.keys) + let migrations = toSeq(definition.migrationUp.keys) debug "Verifying migration data" @@ -53,37 +55,37 @@ proc checkMigrations*(db: DbConn): bool = warn "Migration order mismatch", migration=migration.name return false - if keccak_256.digest(migrationUp[migration.name]).data.toHex() != migration.hash: + if keccak_256.digest(definition.migrationUp[migration.name]).data.toHex() != migration.hash: warn "Migration hash mismatch", migration=migration.name return false return true -proc isUpToDate*(db: DbConn):bool = +proc isUpToDate*(db: DbConn, definition: MigrationDefinition):bool = let lastMigrationExecuted = db.getLastMigrationExecuted() if lastMigrationExecuted.isOk: # Check what's the latest migration let currentMigration = lastMigrationExecuted.get() var index = 0 - for name in migrationUp.keys: - if name == currentMigration.name and index == migrationUp.len - 1: + for name in definition.migrationUp.keys: + if name == currentMigration.name and index == definition.migrationUp.len - 1: return true index += 1 result = false -proc migrate*(db: DbConn): MigrationResult = +proc migrate*(db: DbConn, definition: MigrationDefinition): MigrationResult = db.createMigrationTableIfNotExists() - if not db.checkMigrations(): return MigrationResult.err "db/migration mismatch" + if not db.checkMigrations(definition): return MigrationResult.err "db/migration mismatch" var migration: Migration let lastMigrationExecuted = db.getLastMigrationExecuted() if not lastMigrationExecuted.isOk: try: db.transaction: - for name, query in migrationUp.pairs: + for name, query in definition.migrationUp.pairs: debug "Executing migration", name db.execScript(string.fromBytes(query)) db.exec(fmt"INSERT INTO {migration.tableName}({migration.name.columnName}, {migration.hash.columnName}) VALUES(?, ?)", name, keccak_256.digest(query).data.toHex()) @@ -91,13 +93,13 @@ proc migrate*(db: DbConn): MigrationResult = warn "Could not execute migration" return MigrationResult.err "Could not execute migration" else: - if db.isUpToDate(): return lastMigrationExecuted + if db.isUpToDate(definition): return lastMigrationExecuted let allMigrationsExecuted = db.getAllMigrationsExecuted() var index = -1 try: db.transaction: - for name, query in migrationUp.pairs: + for name, query in definition.migrationUp.pairs: index += 1 if index <= (allMigrationsExecuted.len - 1): continue debug "Executing migration", name @@ -110,15 +112,15 @@ proc migrate*(db: DbConn): MigrationResult = return db.getLastMigrationExecuted() -proc tearDown*(db: DbConn):bool = +proc tearDown*(db: DbConn, definition: MigrationDefinition):bool = var migration: Migration var allMigrationsExecuted = db.getAllMigrationsExecuted().reversed() try: db.transaction: for m in allMigrationsExecuted: debug "Rolling back migration", name=m.name - if migrationDown.hasKey(m.name): - let script = string.fromBytes(migrationDown[m.name]) + if definition.migrationDown.hasKey(m.name): + let script = string.fromBytes(definition.migrationDown[m.name]) if script != "": db.execScript(script) db.exec(fmt"DELETE FROM {migration.tableName} WHERE {migration.name.columnName} = ?", m.name) except SqliteError: diff --git a/nim_status/lib/migrations/accounts/0001_accounts.down.sql b/nim_status/lib/migrations/accounts/0001_accounts.down.sql new file mode 100644 index 0000000..5eb8e05 --- /dev/null +++ b/nim_status/lib/migrations/accounts/0001_accounts.down.sql @@ -0,0 +1 @@ +DROP TABLE accounts; diff --git a/nim_status/lib/migrations/accounts/0001_accounts.up.sql b/nim_status/lib/migrations/accounts/0001_accounts.up.sql new file mode 100644 index 0000000..f156b8d --- /dev/null +++ b/nim_status/lib/migrations/accounts/0001_accounts.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS accounts ( +keyUid VARCHAR PRIMARY KEY, +name TEXT NOT NULL, +loginTimestamp BIG INT, +photoPath TEXT, +keycardPairing TEXT +) WITHOUT ROWID; diff --git a/nim_status/lib/migrations/accounts/1605007189_identity_images.down.sql b/nim_status/lib/migrations/accounts/1605007189_identity_images.down.sql new file mode 100644 index 0000000..f3f8889 --- /dev/null +++ b/nim_status/lib/migrations/accounts/1605007189_identity_images.down.sql @@ -0,0 +1 @@ +DROP TABLE identity_images; diff --git a/nim_status/lib/migrations/accounts/1605007189_identity_images.up.sql b/nim_status/lib/migrations/accounts/1605007189_identity_images.up.sql new file mode 100644 index 0000000..0be8c64 --- /dev/null +++ b/nim_status/lib/migrations/accounts/1605007189_identity_images.up.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS identity_images( + key_uid VARCHAR, + name VARCHAR, + image_payload BLOB NOT NULL, + width int, + height int, + file_size int, + resize_target int, + PRIMARY KEY (key_uid, name) ON CONFLICT REPLACE +) WITHOUT ROWID; diff --git a/nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.down.sql b/nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.down.sql new file mode 100644 index 0000000..1f99ff8 --- /dev/null +++ b/nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.down.sql @@ -0,0 +1,23 @@ +/* Copy the accounts table into a temp table, EXCLUDE the `identicon` column and INCLUDE the `photoPath` column */ +CREATE TEMPORARY TABLE accounts_backup( + keyUid VARCHAR PRIMARY KEY, + name TEXT NOT NULL, + loginTimestamp BIG INT, + photoPath TEXT, + keycardPairing TEXT +) WITHOUT ROWID; +INSERT INTO accounts_backup SELECT keyUid, name, loginTimestamp, identicon, keycardPairing FROM accounts; + +/* Drop the old accounts table and recreate with all columns EXCLUDING `identicon` and INCLUDING `photoPath` */ +DROP TABLE accounts; +CREATE TABLE IF NOT EXISTS accounts ( + keyUid VARCHAR PRIMARY KEY, + name TEXT NOT NULL, + loginTimestamp BIG INT, + photoPath TEXT, + keycardPairing TEXT +) WITHOUT ROWID; +INSERT INTO accounts SELECT keyUid, name, loginTimestamp, photoPath, keycardPairing FROM accounts_backup; + +/* Tidy up, drop the temp table */ +DROP TABLE accounts_backup; \ No newline at end of file diff --git a/nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.up.sql b/nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.up.sql new file mode 100644 index 0000000..3b4f28f --- /dev/null +++ b/nim_status/lib/migrations/accounts/1606224181_drop_photo_path_from_accounts.up.sql @@ -0,0 +1,23 @@ +/* Copy the accounts table into a temp table, EXCLUDE the `photoPath` and INCLUDE `identicon` column */ +CREATE TEMPORARY TABLE accounts_backup( + keyUid VARCHAR PRIMARY KEY, + name TEXT NOT NULL, + loginTimestamp BIG INT, + identicon TEXT, + keycardPairing TEXT +) WITHOUT ROWID; +INSERT INTO accounts_backup SELECT keyUid, name, loginTimestamp, photoPath, keycardPairing FROM accounts; + +/* Drop the old accounts table and recreate with all columns EXCLUDING `photoPath` and INCLUDING `identicon`*/ +DROP TABLE accounts; +CREATE TABLE accounts( + keyUid VARCHAR PRIMARY KEY, + name TEXT NOT NULL, + loginTimestamp BIG INT, + identicon TEXT, + keycardPairing TEXT + ) WITHOUT ROWID; +INSERT INTO accounts SELECT keyUid, name, loginTimestamp, identicon, keycardPairing FROM accounts_backup; + +/* Tidy up, drop the temp table */ +DROP TABLE accounts_backup; \ No newline at end of file diff --git a/nim_status/lib/migrations/000001_init.down.db.sql b/nim_status/lib/migrations/app/000001_init.down.db.sql similarity index 100% rename from nim_status/lib/migrations/000001_init.down.db.sql rename to nim_status/lib/migrations/app/000001_init.down.db.sql diff --git a/nim_status/lib/migrations/000001_init.up.db.sql b/nim_status/lib/migrations/app/000001_init.up.db.sql similarity index 100% rename from nim_status/lib/migrations/000001_init.up.db.sql rename to nim_status/lib/migrations/app/000001_init.up.db.sql diff --git a/nim_status/lib/migrations/000002_add_last_ens_clock_value.down.sql b/nim_status/lib/migrations/app/000002_add_last_ens_clock_value.down.sql similarity index 100% rename from nim_status/lib/migrations/000002_add_last_ens_clock_value.down.sql rename to nim_status/lib/migrations/app/000002_add_last_ens_clock_value.down.sql diff --git a/nim_status/lib/migrations/000002_add_last_ens_clock_value.up.sql b/nim_status/lib/migrations/app/000002_add_last_ens_clock_value.up.sql similarity index 100% rename from nim_status/lib/migrations/000002_add_last_ens_clock_value.up.sql rename to nim_status/lib/migrations/app/000002_add_last_ens_clock_value.up.sql diff --git a/nim_status/lib/migrations/0001_app.down.sql b/nim_status/lib/migrations/app/0001_app.down.sql similarity index 100% rename from nim_status/lib/migrations/0001_app.down.sql rename to nim_status/lib/migrations/app/0001_app.down.sql diff --git a/nim_status/lib/migrations/0001_app.up.sql b/nim_status/lib/migrations/app/0001_app.up.sql similarity index 100% rename from nim_status/lib/migrations/0001_app.up.sql rename to nim_status/lib/migrations/app/0001_app.up.sql diff --git a/nim_status/lib/migrations/0002_tokens.down.sql b/nim_status/lib/migrations/app/0002_tokens.down.sql similarity index 100% rename from nim_status/lib/migrations/0002_tokens.down.sql rename to nim_status/lib/migrations/app/0002_tokens.down.sql diff --git a/nim_status/lib/migrations/0002_tokens.up.sql b/nim_status/lib/migrations/app/0002_tokens.up.sql similarity index 100% rename from nim_status/lib/migrations/0002_tokens.up.sql rename to nim_status/lib/migrations/app/0002_tokens.up.sql diff --git a/nim_status/lib/migrations/0003_settings.down.sql b/nim_status/lib/migrations/app/0003_settings.down.sql similarity index 100% rename from nim_status/lib/migrations/0003_settings.down.sql rename to nim_status/lib/migrations/app/0003_settings.down.sql diff --git a/nim_status/lib/migrations/0003_settings.up.sql b/nim_status/lib/migrations/app/0003_settings.up.sql similarity index 100% rename from nim_status/lib/migrations/0003_settings.up.sql rename to nim_status/lib/migrations/app/0003_settings.up.sql diff --git a/nim_status/lib/migrations/0004_pending_stickers.down.sql b/nim_status/lib/migrations/app/0004_pending_stickers.down.sql similarity index 100% rename from nim_status/lib/migrations/0004_pending_stickers.down.sql rename to nim_status/lib/migrations/app/0004_pending_stickers.down.sql diff --git a/nim_status/lib/migrations/0004_pending_stickers.up.sql b/nim_status/lib/migrations/app/0004_pending_stickers.up.sql similarity index 100% rename from nim_status/lib/migrations/0004_pending_stickers.up.sql rename to nim_status/lib/migrations/app/0004_pending_stickers.up.sql diff --git a/nim_status/lib/migrations/0005_waku_mode.down.sql b/nim_status/lib/migrations/app/0005_waku_mode.down.sql similarity index 100% rename from nim_status/lib/migrations/0005_waku_mode.down.sql rename to nim_status/lib/migrations/app/0005_waku_mode.down.sql diff --git a/nim_status/lib/migrations/0005_waku_mode.up.sql b/nim_status/lib/migrations/app/0005_waku_mode.up.sql similarity index 100% rename from nim_status/lib/migrations/0005_waku_mode.up.sql rename to nim_status/lib/migrations/app/0005_waku_mode.up.sql diff --git a/nim_status/lib/migrations/0006_appearance.up.sql b/nim_status/lib/migrations/app/0006_appearance.up.sql similarity index 100% rename from nim_status/lib/migrations/0006_appearance.up.sql rename to nim_status/lib/migrations/app/0006_appearance.up.sql diff --git a/nim_status/lib/migrations/0007_enable_waku_default.up.sql b/nim_status/lib/migrations/app/0007_enable_waku_default.up.sql similarity index 100% rename from nim_status/lib/migrations/0007_enable_waku_default.up.sql rename to nim_status/lib/migrations/app/0007_enable_waku_default.up.sql diff --git a/nim_status/lib/migrations/0008_add_push_notifications.up.sql b/nim_status/lib/migrations/app/0008_add_push_notifications.up.sql similarity index 100% rename from nim_status/lib/migrations/0008_add_push_notifications.up.sql rename to nim_status/lib/migrations/app/0008_add_push_notifications.up.sql diff --git a/nim_status/lib/migrations/0009_enable_sending_push_notifications.down.sql b/nim_status/lib/migrations/app/0009_enable_sending_push_notifications.down.sql similarity index 100% rename from nim_status/lib/migrations/0009_enable_sending_push_notifications.down.sql rename to nim_status/lib/migrations/app/0009_enable_sending_push_notifications.down.sql diff --git a/nim_status/lib/migrations/0009_enable_sending_push_notifications.up.sql b/nim_status/lib/migrations/app/0009_enable_sending_push_notifications.up.sql similarity index 100% rename from nim_status/lib/migrations/0009_enable_sending_push_notifications.up.sql rename to nim_status/lib/migrations/app/0009_enable_sending_push_notifications.up.sql diff --git a/nim_status/lib/migrations/0010_add_block_mentions.down.sql b/nim_status/lib/migrations/app/0010_add_block_mentions.down.sql similarity index 100% rename from nim_status/lib/migrations/0010_add_block_mentions.down.sql rename to nim_status/lib/migrations/app/0010_add_block_mentions.down.sql diff --git a/nim_status/lib/migrations/0010_add_block_mentions.up.sql b/nim_status/lib/migrations/app/0010_add_block_mentions.up.sql similarity index 100% rename from nim_status/lib/migrations/0010_add_block_mentions.up.sql rename to nim_status/lib/migrations/app/0010_add_block_mentions.up.sql diff --git a/nim_status/lib/migrations/0011_allow_webview_permission_requests.down.sql b/nim_status/lib/migrations/app/0011_allow_webview_permission_requests.down.sql similarity index 100% rename from nim_status/lib/migrations/0011_allow_webview_permission_requests.down.sql rename to nim_status/lib/migrations/app/0011_allow_webview_permission_requests.down.sql diff --git a/nim_status/lib/migrations/0011_allow_webview_permission_requests.up.sql b/nim_status/lib/migrations/app/0011_allow_webview_permission_requests.up.sql similarity index 100% rename from nim_status/lib/migrations/0011_allow_webview_permission_requests.up.sql rename to nim_status/lib/migrations/app/0011_allow_webview_permission_requests.up.sql diff --git a/nim_status/lib/migrations/0012_pending_transactions.down.sql b/nim_status/lib/migrations/app/0012_pending_transactions.down.sql similarity index 100% rename from nim_status/lib/migrations/0012_pending_transactions.down.sql rename to nim_status/lib/migrations/app/0012_pending_transactions.down.sql diff --git a/nim_status/lib/migrations/0012_pending_transactions.up.sql b/nim_status/lib/migrations/app/0012_pending_transactions.up.sql similarity index 100% rename from nim_status/lib/migrations/0012_pending_transactions.up.sql rename to nim_status/lib/migrations/app/0012_pending_transactions.up.sql diff --git a/nim_status/lib/migrations/0013_favourites.down.sql b/nim_status/lib/migrations/app/0013_favourites.down.sql similarity index 100% rename from nim_status/lib/migrations/0013_favourites.down.sql rename to nim_status/lib/migrations/app/0013_favourites.down.sql diff --git a/nim_status/lib/migrations/0013_favourites.up.sql b/nim_status/lib/migrations/app/0013_favourites.up.sql similarity index 100% rename from nim_status/lib/migrations/0013_favourites.up.sql rename to nim_status/lib/migrations/app/0013_favourites.up.sql diff --git a/nim_status/lib/migrations/0014_add_use_mailservers.down.sql b/nim_status/lib/migrations/app/0014_add_use_mailservers.down.sql similarity index 100% rename from nim_status/lib/migrations/0014_add_use_mailservers.down.sql rename to nim_status/lib/migrations/app/0014_add_use_mailservers.down.sql diff --git a/nim_status/lib/migrations/0014_add_use_mailservers.up.sql b/nim_status/lib/migrations/app/0014_add_use_mailservers.up.sql similarity index 100% rename from nim_status/lib/migrations/0014_add_use_mailservers.up.sql rename to nim_status/lib/migrations/app/0014_add_use_mailservers.up.sql diff --git a/nim_status/lib/migrations/0015_link_previews.down.sql b/nim_status/lib/migrations/app/0015_link_previews.down.sql similarity index 100% rename from nim_status/lib/migrations/0015_link_previews.down.sql rename to nim_status/lib/migrations/app/0015_link_previews.down.sql diff --git a/nim_status/lib/migrations/0015_link_previews.up.sql b/nim_status/lib/migrations/app/0015_link_previews.up.sql similarity index 100% rename from nim_status/lib/migrations/0015_link_previews.up.sql rename to nim_status/lib/migrations/app/0015_link_previews.up.sql diff --git a/nim_status/lib/migrations/0016_local_notifications_preferences.down.sql b/nim_status/lib/migrations/app/0016_local_notifications_preferences.down.sql similarity index 100% rename from nim_status/lib/migrations/0016_local_notifications_preferences.down.sql rename to nim_status/lib/migrations/app/0016_local_notifications_preferences.down.sql diff --git a/nim_status/lib/migrations/0016_local_notifications_preferences.up.sql b/nim_status/lib/migrations/app/0016_local_notifications_preferences.up.sql similarity index 100% rename from nim_status/lib/migrations/0016_local_notifications_preferences.up.sql rename to nim_status/lib/migrations/app/0016_local_notifications_preferences.up.sql diff --git a/nim_status/lib/migrations/0017_bookmarks.down.sql b/nim_status/lib/migrations/app/0017_bookmarks.down.sql similarity index 100% rename from nim_status/lib/migrations/0017_bookmarks.down.sql rename to nim_status/lib/migrations/app/0017_bookmarks.down.sql diff --git a/nim_status/lib/migrations/0017_bookmarks.up.sql b/nim_status/lib/migrations/app/0017_bookmarks.up.sql similarity index 100% rename from nim_status/lib/migrations/0017_bookmarks.up.sql rename to nim_status/lib/migrations/app/0017_bookmarks.up.sql diff --git a/nim_status/lib/migrations/1586358095_add_replace.down.sql b/nim_status/lib/migrations/app/1586358095_add_replace.down.sql similarity index 100% rename from nim_status/lib/migrations/1586358095_add_replace.down.sql rename to nim_status/lib/migrations/app/1586358095_add_replace.down.sql diff --git a/nim_status/lib/migrations/1586358095_add_replace.up.sql b/nim_status/lib/migrations/app/1586358095_add_replace.up.sql similarity index 100% rename from nim_status/lib/migrations/1586358095_add_replace.up.sql rename to nim_status/lib/migrations/app/1586358095_add_replace.up.sql diff --git a/nim_status/lib/migrations/1588665364_add_image_data.down.sql b/nim_status/lib/migrations/app/1588665364_add_image_data.down.sql similarity index 100% rename from nim_status/lib/migrations/1588665364_add_image_data.down.sql rename to nim_status/lib/migrations/app/1588665364_add_image_data.down.sql diff --git a/nim_status/lib/migrations/1588665364_add_image_data.up.sql b/nim_status/lib/migrations/app/1588665364_add_image_data.up.sql similarity index 100% rename from nim_status/lib/migrations/1588665364_add_image_data.up.sql rename to nim_status/lib/migrations/app/1588665364_add_image_data.up.sql diff --git a/nim_status/lib/migrations/1589365189_add_pow_target.down.sql b/nim_status/lib/migrations/app/1589365189_add_pow_target.down.sql similarity index 100% rename from nim_status/lib/migrations/1589365189_add_pow_target.down.sql rename to nim_status/lib/migrations/app/1589365189_add_pow_target.down.sql diff --git a/nim_status/lib/migrations/1589365189_add_pow_target.up.sql b/nim_status/lib/migrations/app/1589365189_add_pow_target.up.sql similarity index 100% rename from nim_status/lib/migrations/1589365189_add_pow_target.up.sql rename to nim_status/lib/migrations/app/1589365189_add_pow_target.up.sql diff --git a/nim_status/lib/migrations/1591277220_add_index_messages.down.sql b/nim_status/lib/migrations/app/1591277220_add_index_messages.down.sql similarity index 100% rename from nim_status/lib/migrations/1591277220_add_index_messages.down.sql rename to nim_status/lib/migrations/app/1591277220_add_index_messages.down.sql diff --git a/nim_status/lib/migrations/1591277220_add_index_messages.up.sql b/nim_status/lib/migrations/app/1591277220_add_index_messages.up.sql similarity index 100% rename from nim_status/lib/migrations/1591277220_add_index_messages.up.sql rename to nim_status/lib/migrations/app/1591277220_add_index_messages.up.sql diff --git a/nim_status/lib/migrations/1593087212_add_mute_chat_and_raw_message_fields.down.sql b/nim_status/lib/migrations/app/1593087212_add_mute_chat_and_raw_message_fields.down.sql similarity index 100% rename from nim_status/lib/migrations/1593087212_add_mute_chat_and_raw_message_fields.down.sql rename to nim_status/lib/migrations/app/1593087212_add_mute_chat_and_raw_message_fields.down.sql diff --git a/nim_status/lib/migrations/1593087212_add_mute_chat_and_raw_message_fields.up.sql b/nim_status/lib/migrations/app/1593087212_add_mute_chat_and_raw_message_fields.up.sql similarity index 100% rename from nim_status/lib/migrations/1593087212_add_mute_chat_and_raw_message_fields.up.sql rename to nim_status/lib/migrations/app/1593087212_add_mute_chat_and_raw_message_fields.up.sql diff --git a/nim_status/lib/migrations/1595862781_add_audio_data.down.sql b/nim_status/lib/migrations/app/1595862781_add_audio_data.down.sql similarity index 100% rename from nim_status/lib/migrations/1595862781_add_audio_data.down.sql rename to nim_status/lib/migrations/app/1595862781_add_audio_data.down.sql diff --git a/nim_status/lib/migrations/1595862781_add_audio_data.up.sql b/nim_status/lib/migrations/app/1595862781_add_audio_data.up.sql similarity index 100% rename from nim_status/lib/migrations/1595862781_add_audio_data.up.sql rename to nim_status/lib/migrations/app/1595862781_add_audio_data.up.sql diff --git a/nim_status/lib/migrations/1595865249_create_emoji_reactions_table.down.sql b/nim_status/lib/migrations/app/1595865249_create_emoji_reactions_table.down.sql similarity index 100% rename from nim_status/lib/migrations/1595865249_create_emoji_reactions_table.down.sql rename to nim_status/lib/migrations/app/1595865249_create_emoji_reactions_table.down.sql diff --git a/nim_status/lib/migrations/1595865249_create_emoji_reactions_table.up.sql b/nim_status/lib/migrations/app/1595865249_create_emoji_reactions_table.up.sql similarity index 100% rename from nim_status/lib/migrations/1595865249_create_emoji_reactions_table.up.sql rename to nim_status/lib/migrations/app/1595865249_create_emoji_reactions_table.up.sql diff --git a/nim_status/lib/migrations/1596805115_create_group_chat_invitations_table.down.sql b/nim_status/lib/migrations/app/1596805115_create_group_chat_invitations_table.down.sql similarity index 100% rename from nim_status/lib/migrations/1596805115_create_group_chat_invitations_table.down.sql rename to nim_status/lib/migrations/app/1596805115_create_group_chat_invitations_table.down.sql diff --git a/nim_status/lib/migrations/1596805115_create_group_chat_invitations_table.up.sql b/nim_status/lib/migrations/app/1596805115_create_group_chat_invitations_table.up.sql similarity index 100% rename from nim_status/lib/migrations/1596805115_create_group_chat_invitations_table.up.sql rename to nim_status/lib/migrations/app/1596805115_create_group_chat_invitations_table.up.sql diff --git a/nim_status/lib/migrations/1597322655_add_invitation_admin_chat_field.up.sql b/nim_status/lib/migrations/app/1597322655_add_invitation_admin_chat_field.up.sql similarity index 100% rename from nim_status/lib/migrations/1597322655_add_invitation_admin_chat_field.up.sql rename to nim_status/lib/migrations/app/1597322655_add_invitation_admin_chat_field.up.sql diff --git a/nim_status/lib/migrations/1597757544_add_nickname.up.sql b/nim_status/lib/migrations/app/1597757544_add_nickname.up.sql similarity index 100% rename from nim_status/lib/migrations/1597757544_add_nickname.up.sql rename to nim_status/lib/migrations/app/1597757544_add_nickname.up.sql diff --git a/nim_status/lib/migrations/1598955122_add_mentions.down.sql b/nim_status/lib/migrations/app/1598955122_add_mentions.down.sql similarity index 100% rename from nim_status/lib/migrations/1598955122_add_mentions.down.sql rename to nim_status/lib/migrations/app/1598955122_add_mentions.down.sql diff --git a/nim_status/lib/migrations/1598955122_add_mentions.up.sql b/nim_status/lib/migrations/app/1598955122_add_mentions.up.sql similarity index 100% rename from nim_status/lib/migrations/1598955122_add_mentions.up.sql rename to nim_status/lib/migrations/app/1598955122_add_mentions.up.sql diff --git a/nim_status/lib/migrations/1599641390_add_emoji_reactions_index.down.sql b/nim_status/lib/migrations/app/1599641390_add_emoji_reactions_index.down.sql similarity index 100% rename from nim_status/lib/migrations/1599641390_add_emoji_reactions_index.down.sql rename to nim_status/lib/migrations/app/1599641390_add_emoji_reactions_index.down.sql diff --git a/nim_status/lib/migrations/1599641390_add_emoji_reactions_index.up.sql b/nim_status/lib/migrations/app/1599641390_add_emoji_reactions_index.up.sql similarity index 100% rename from nim_status/lib/migrations/1599641390_add_emoji_reactions_index.up.sql rename to nim_status/lib/migrations/app/1599641390_add_emoji_reactions_index.up.sql diff --git a/nim_status/lib/migrations/1599720851_add_seen_index_remove_long_messages.down.sql b/nim_status/lib/migrations/app/1599720851_add_seen_index_remove_long_messages.down.sql similarity index 100% rename from nim_status/lib/migrations/1599720851_add_seen_index_remove_long_messages.down.sql rename to nim_status/lib/migrations/app/1599720851_add_seen_index_remove_long_messages.down.sql diff --git a/nim_status/lib/migrations/1599720851_add_seen_index_remove_long_messages.up.sql b/nim_status/lib/migrations/app/1599720851_add_seen_index_remove_long_messages.up.sql similarity index 100% rename from nim_status/lib/migrations/1599720851_add_seen_index_remove_long_messages.up.sql rename to nim_status/lib/migrations/app/1599720851_add_seen_index_remove_long_messages.up.sql diff --git a/nim_status/lib/migrations/1603198582_add_profile_chat_field.up.sql b/nim_status/lib/migrations/app/1603198582_add_profile_chat_field.up.sql similarity index 100% rename from nim_status/lib/migrations/1603198582_add_profile_chat_field.up.sql rename to nim_status/lib/migrations/app/1603198582_add_profile_chat_field.up.sql diff --git a/nim_status/lib/migrations/1603816533_add_links.down.sql b/nim_status/lib/migrations/app/1603816533_add_links.down.sql similarity index 100% rename from nim_status/lib/migrations/1603816533_add_links.down.sql rename to nim_status/lib/migrations/app/1603816533_add_links.down.sql diff --git a/nim_status/lib/migrations/1603816533_add_links.up.sql b/nim_status/lib/migrations/app/1603816533_add_links.up.sql similarity index 100% rename from nim_status/lib/migrations/1603816533_add_links.up.sql rename to nim_status/lib/migrations/app/1603816533_add_links.up.sql diff --git a/nim_status/lib/migrations/1603888149_create_chat_identity_last_published_table.down.sql b/nim_status/lib/migrations/app/1603888149_create_chat_identity_last_published_table.down.sql similarity index 100% rename from nim_status/lib/migrations/1603888149_create_chat_identity_last_published_table.down.sql rename to nim_status/lib/migrations/app/1603888149_create_chat_identity_last_published_table.down.sql diff --git a/nim_status/lib/migrations/1603888149_create_chat_identity_last_published_table.up.sql b/nim_status/lib/migrations/app/1603888149_create_chat_identity_last_published_table.up.sql similarity index 100% rename from nim_status/lib/migrations/1603888149_create_chat_identity_last_published_table.up.sql rename to nim_status/lib/migrations/app/1603888149_create_chat_identity_last_published_table.up.sql diff --git a/nim_status/lib/migrations/1605075346_add_communities.up.sql b/nim_status/lib/migrations/app/1605075346_add_communities.up.sql similarity index 100% rename from nim_status/lib/migrations/1605075346_add_communities.up.sql rename to nim_status/lib/migrations/app/1605075346_add_communities.up.sql diff --git a/nim_status/lib/migrations/sql_generate.nim b/nim_status/lib/migrations/sql_generate.nim index ed1d820..4f07b0a 100644 --- a/nim_status/lib/migrations/sql_generate.nim +++ b/nim_status/lib/migrations/sql_generate.nim @@ -4,8 +4,8 @@ import stew/byteutils var upScripts = initOrderedTable[string, string]() var downScripts = initOrderedTable[string, string]() -for kind, path in walkDir(currentSourcePath.parentDir): - let (dir, name, ext) = splitFile(path) +for kind, path in walkDir(paramStr(1)): + let (_, name, ext) = splitFile(path) if ext != ".sql": continue let parts = name.split(".") @@ -33,12 +33,13 @@ upScripts.sort(system.cmp) downScripts.sort(system.cmp) echo "# THIS FILE IS AUTOGENERATED - DO NOT MODIFY MANUALY - USE make migrations" -echo "import tables\n" -echo "var migrationUp*:OrderedTable[string, seq[byte]] = {" +echo "import tables" +echo "import types\n" +echo "proc newMigrationDefinition*(): MigrationDefinition =" +echo " result = MigrationDefinition()" +echo " result.migrationUp = initOrderedTable[string, seq[byte]]()" +echo " result.migrationDown = initOrderedTable[string, seq[byte]]()" for name, query in upScripts.pairs(): - echo " \"" & name & "\": " & query.toBytes().print & "," -echo "}.toOrderedTable\n" -echo "var migrationDown*:OrderedTable[string, seq[byte]] = {" + echo " result.migrationUp[\"" & name & "\"] = " & query.toBytes().print for name, query in downScripts.pairs(): - echo " \"" & name & "\": " & query.toBytes().print & "," -echo "}.toOrderedTable\n" + echo " result.migrationDown[\"" & name & "\"] = " & query.toBytes().print diff --git a/nim_status/lib/migrations/types.nim b/nim_status/lib/migrations/types.nim new file mode 100644 index 0000000..5d94e4b --- /dev/null +++ b/nim_status/lib/migrations/types.nim @@ -0,0 +1,6 @@ +import tables + +type + MigrationDefinition* = ref object of RootObj + migrationUp*:OrderedTable[string, seq[byte]] + migrationDown*:OrderedTable[string, seq[byte]] diff --git a/test/nim/accounts.nim b/test/nim/accounts.nim index 108849a..fdcf6ce 100644 --- a/test/nim/accounts.nim +++ b/test/nim/accounts.nim @@ -6,12 +6,13 @@ import # vendor libs import # nim-status libs ../../nim_status/lib/[accounts, database, conversions] + ../../nim_status/lib/migrations/sql_accounts_app import times let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, newMigrationDefinition()) var account:Account = Account( name: "Test", diff --git a/test/nim/callrpc.nim b/test/nim/callrpc.nim index a3dec46..adf7996 100644 --- a/test/nim/callrpc.nim +++ b/test/nim/callrpc.nim @@ -4,11 +4,13 @@ import options import ../../nim_status/lib/settings import ../../nim_status/lib/database import ../../nim_status/lib/callrpc +import ../../nim_status/lib/migrations/sql_scripts_app + import web3/conversions let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) let settingsStr = """{ "address": "0x1122334455667788990011223344556677889900", @@ -26,7 +28,7 @@ let settingsStr = """{ }""" let settingsObj = JSON.decode(settingsStr, Settings, allowUnknownFields = true) -#[ + let web3Obj = newWeb3(settingsObj) let rGasPrice = callRPC(web3Obj, eth_gasPrice, %[]) @@ -42,6 +44,6 @@ let rSendTransaction = callRPC(web3Obj, "eth_sendTransaction", %* [%*{"from": "0 assert rSendTransaction.error == true assert rSendTransaction.result["code"].getInt == -32601 assert rSendTransaction.result["message"].getStr == "the method eth_sendTransaction does not exist/is not available" -]# + db.close() removeFile(path) diff --git a/test/nim/chats.nim b/test/nim/chats.nim index 9cd0615..7401afa 100644 --- a/test/nim/chats.nim +++ b/test/nim/chats.nim @@ -5,11 +5,12 @@ import # vendor libs sqlcipher, json_serialization, web3/conversions as web3_conversions import # nim-status libs - ../../nim_status/lib/[chats, database, conversions, contacts, messages] + ../../nim_status/lib/[chats, database, conversions, contacts, messages], + ../../nim_status/lib/migrations/sql_scripts_app let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) var chat = Chat( id: "ContactId", diff --git a/test/nim/contacts.nim b/test/nim/contacts.nim index 59d533e..b4115bb 100644 --- a/test/nim/contacts.nim +++ b/test/nim/contacts.nim @@ -6,11 +6,12 @@ import # vendor libs web3/ethtypes import # nim-status libs - ../../nim_status/lib/[contacts, database, conversions] + ../../nim_status/lib/[contacts, database, conversions], + ../../nim_status/lib/migrations/sql_scripts_app let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) let contact1 = Contact( id: "Contact1", diff --git a/test/nim/mailservers.nim b/test/nim/mailservers.nim index ac78640..a488c6a 100644 --- a/test/nim/mailservers.nim +++ b/test/nim/mailservers.nim @@ -3,10 +3,11 @@ import os, json, json_serialization import options import ../../nim_status/lib/mailservers import ../../nim_status/lib/database +import ../../nim_status/lib/migrations/sql_scripts_app let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/my.db" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) let mailserver1 = Mailserver( id: "mailserver-1", diff --git a/test/nim/messages.nim b/test/nim/messages.nim index f3685e2..42ffcea 100644 --- a/test/nim/messages.nim +++ b/test/nim/messages.nim @@ -5,11 +5,12 @@ import # vendor libs sqlcipher, json_serialization, web3/conversions as web3_conversions import # nim-status libs - ../../nim_status/lib/[messages, database, conversions, chats] + ../../nim_status/lib/[messages, database, conversions, chats], + ../../nim_status/lib/migrations/sql_scripts_app let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) var msg = Message( id: "msg1", diff --git a/test/nim/migrations.nim b/test/nim/migrations.nim index 4a5e985..fa6a42a 100644 --- a/test/nim/migrations.nim +++ b/test/nim/migrations.nim @@ -1,7 +1,7 @@ import os, tables import sqlcipher, results import ../../nim_status/lib/migration -import ../../nim_status/lib/migrations/sql_scripts +import ../../nim_status/lib/migrations/sql_scripts_accounts as migration_accounts import stew/byteutils let passwd = "qwerty" @@ -10,26 +10,27 @@ var dbConn = openDatabase(path) dbConn.key(passwd) +var migrationDefinition = migration_accounts.newMigrationDefinition() assert dbConn.getLastMigrationExecuted().error() == "No migrations were executed" -assert dbConn.migrate().isOk -assert dbConn.isUpToDate() +assert dbConn.migrate(migrationDefinition).isOk +assert dbConn.isUpToDate(migrationDefinition) # Creating dinamically new migrations just to check if isUpToDate and migrate work as expected -migrationUp["002_abc"] = "CREATE TABLE anotherTable (address VARCHAR NOT NULL PRIMARY KEY) WITHOUT ROWID;".toBytes -migrationDown["002_abc"] = "DROP TABLE anotherTable;".toBytes +migrationDefinition.migrationUp["002_abc"] = "CREATE TABLE anotherTable (address VARCHAR NOT NULL PRIMARY KEY) WITHOUT ROWID;".toBytes +migrationDefinition.migrationDown["002_abc"] = "DROP TABLE anotherTable;".toBytes -assert not dbConn.isUpToDate() -assert dbConn.migrate().isOk +assert not dbConn.isUpToDate(migrationDefinition) +assert dbConn.migrate(migrationDefinition).isOk -assert dbConn.isUpToDate() +assert dbConn.isUpToDate(migrationDefinition) -assert dbConn.migrate().isOk +assert dbConn.migrate(migrationDefinition).isOk -assert dbConn.tearDown() -assert dbConn.tearDown() +assert dbConn.tearDown(migrationDefinition) +assert dbConn.tearDown(migrationDefinition) -assert not dbConn.isUpToDate() +assert not dbConn.isUpToDate(migrationDefinition) assert dbConn.getLastMigrationExecuted().error() == "No migrations were executed" diff --git a/test/nim/pendingtxs.nim b/test/nim/pendingtxs.nim index 5ba6f76..353994e 100644 --- a/test/nim/pendingtxs.nim +++ b/test/nim/pendingtxs.nim @@ -5,11 +5,12 @@ import # vendor libs sqlcipher, json_serialization, web3/conversions as web3_conversions import # nim-status libs - ../../nim_status/lib/[pendingtxs, database, conversions] + ../../nim_status/lib/[pendingtxs, database, conversions], + ../../nim_status/lib/migrations/sql_scripts_app let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) let tx = PendingTx( networkId: 1, diff --git a/test/nim/permissions.nim b/test/nim/permissions.nim index c6928a7..3c8ed43 100644 --- a/test/nim/permissions.nim +++ b/test/nim/permissions.nim @@ -5,11 +5,12 @@ import # vendor libs json_serialization import # nim-status libs - ../../nim_status/lib/[database, permissions] + ../../nim_status/lib/[database, permissions], + ../../nim_status/lib/migrations/sql_scripts_app let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) let dappPerm1: DappPermissions = DappPermissions( name: "Dapp1", diff --git a/test/nim/settings.nim b/test/nim/settings.nim index 554782a..9fd2b4f 100644 --- a/test/nim/settings.nim +++ b/test/nim/settings.nim @@ -7,10 +7,11 @@ import # vendor libs import # nim-status libs ../../nim_status/lib/[settings, database, conversions] +import ../../nim_status/lib/migrations/sql_scripts_app let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) let settingsStr = """{ "address": "0x1122334455667788990011223344556677889900", diff --git a/test/nim/tokens.nim b/test/nim/tokens.nim index ba5c794..f5e311d 100644 --- a/test/nim/tokens.nim +++ b/test/nim/tokens.nim @@ -4,10 +4,12 @@ import options import ../../nim_status/lib/tokens import ../../nim_status/lib/database import web3/conversions +import ../../nim_status/lib/migrations/sql_scripts_app + let passwd = "qwerty" let path = currentSourcePath.parentDir() & "/build/myDatabase" -let db = initializeDB(path, passwd) +let db = initializeDB(path, passwd, newMigrationDefinition()) let tokensStr = """{ "address": "0x1122334455667788990011223344556677889900",