-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add migration for keyset_id as foreign key in SQLite database
- Loading branch information
1 parent
5a7362c
commit 556c384
Showing
4 changed files
with
133 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
crates/cdk-sqlite/src/mint/migrations/20250306154853_keyset_id_as_forign_key.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-- Add foreign key constraints for keyset_id in SQLite | ||
-- SQLite requires recreating tables to add foreign keys | ||
|
||
-- First, ensure we have the right schema information | ||
PRAGMA foreign_keys = OFF; | ||
|
||
-- Create indexes for the foreign keys if they don't exist | ||
CREATE INDEX IF NOT EXISTS proof_keyset_id_index ON proof(keyset_id); | ||
CREATE INDEX IF NOT EXISTS blind_signature_keyset_id_index ON blind_signature(keyset_id); | ||
|
||
-- Create new proof table with foreign key constraint | ||
CREATE TABLE proof_new ( | ||
y BLOB PRIMARY KEY, | ||
amount INTEGER NOT NULL, | ||
keyset_id TEXT NOT NULL REFERENCES keyset(id), | ||
secret TEXT NOT NULL, | ||
c BLOB NOT NULL, | ||
witness TEXT, | ||
state TEXT CHECK (state IN ('SPENT', 'PENDING', 'UNSPENT')) NOT NULL, | ||
quote_id TEXT | ||
); | ||
|
||
-- Copy data from old proof table to new one | ||
INSERT INTO proof_new SELECT * FROM proof; | ||
|
||
-- Create new blind_signature table with foreign key constraint | ||
CREATE TABLE blind_signature_new ( | ||
y BLOB PRIMARY KEY, | ||
amount INTEGER NOT NULL, | ||
keyset_id TEXT NOT NULL REFERENCES keyset(id), | ||
c BLOB NOT NULL, | ||
dleq_e TEXT, | ||
dleq_s TEXT, | ||
quote_id TEXT | ||
); | ||
|
||
-- Copy data from old blind_signature table to new one | ||
INSERT INTO blind_signature_new SELECT * FROM blind_signature; | ||
|
||
-- Drop old tables | ||
DROP TABLE proof; | ||
DROP TABLE blind_signature; | ||
|
||
-- Rename new tables to original names | ||
ALTER TABLE proof_new RENAME TO proof; | ||
ALTER TABLE blind_signature_new RENAME TO blind_signature; | ||
|
||
-- Recreate all indexes | ||
CREATE INDEX IF NOT EXISTS proof_keyset_id_index ON proof(keyset_id); | ||
CREATE INDEX IF NOT EXISTS state_index ON proof(state); | ||
CREATE INDEX IF NOT EXISTS secret_index ON proof(secret); | ||
CREATE INDEX IF NOT EXISTS blind_signature_keyset_id_index ON blind_signature(keyset_id); | ||
|
||
-- Re-enable foreign keys | ||
PRAGMA foreign_keys = ON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters