status-go/appdatabase/migrations/sql/1688636552_keycards_table_columns_update.up.sql
Sale Djenic 7063ad11aa chore: synchronization improvements applied to keycards
This is the second step of improvements over keypairs/keycards/accounts.
- `SyncKeycardAction` protobuf removed
- `SyncKeypair` protobuf is used for syncing keycards state as well as for all
keycards related changes
- `last_update_clock` column removed from `keypairs` table cause as well as
for accounts, any keycard related change is actually a change made on a related
keypair, thus a keypair's clock keeps the clock of the last change
- `position` column added to `keypairs` table, needed to display keycards in
the same order accross devices
2023-07-11 15:14:49 +02:00

35 lines
1.0 KiB
SQL

ALTER TABLE keycards RENAME TO keycards_old;
ALTER TABLE keycards_accounts RENAME TO keycards_accounts_old;
CREATE TABLE IF NOT EXISTS keycards (
keycard_uid VARCHAR NOT NULL PRIMARY KEY,
keycard_name VARCHAR NOT NULL,
keycard_locked BOOLEAN DEFAULT FALSE,
key_uid VARCHAR NOT NULL,
position INT NOT NULL DEFAULT 0,
FOREIGN KEY(key_uid) REFERENCES keypairs(key_uid)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS keycards_accounts (
keycard_uid VARCHAR NOT NULL,
account_address VARCHAR NOT NULL,
PRIMARY KEY (keycard_uid, account_address),
FOREIGN KEY(keycard_uid) REFERENCES keycards(keycard_uid)
ON UPDATE CASCADE
ON DELETE CASCADE
);
INSERT INTO keycards
SELECT keycard_uid, keycard_name, keycard_locked, key_uid, last_update_clock
FROM keycards_old
ORDER BY last_update_clock;
INSERT INTO keycards_accounts
SELECT keycard_uid, account_address
FROM keycards_accounts_old;
UPDATE keycards SET position = rowid - 1;
DROP TABLE keycards_accounts_old;
DROP TABLE keycards_old;