Skip to content

Commit cacb663

Browse files
committed
Migrate existing vault entries to new schema
1 parent 37eb0e9 commit cacb663

File tree

2 files changed

+233
-11
lines changed

2 files changed

+233
-11
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
Warnings:
3+
4+
- A unique constraint covering the columns `[apiKeySendId]` on the table `WalletBlink` will be added. If there are existing duplicate values, this will fail.
5+
- A unique constraint covering the columns `[currencySendId]` on the table `WalletBlink` will be added. If there are existing duplicate values, this will fail.
6+
- A unique constraint covering the columns `[adminKeyId]` on the table `WalletLNbits` will be added. If there are existing duplicate values, this will fail.
7+
- A unique constraint covering the columns `[nwcUrlSendId]` on the table `WalletNWC` will be added. If there are existing duplicate values, this will fail.
8+
- A unique constraint covering the columns `[primaryPasswordId]` on the table `WalletPhoenixd` will be added. If there are existing duplicate values, this will fail.
9+
10+
*/
11+
-- AlterTable
12+
ALTER TABLE "WalletBlink" ADD COLUMN "apiKeySendId" INTEGER,
13+
ADD COLUMN "currencySendId" INTEGER;
14+
15+
-- AlterTable
16+
ALTER TABLE "WalletLNbits" ADD COLUMN "adminKeyId" INTEGER;
17+
18+
-- AlterTable
19+
ALTER TABLE "WalletNWC" ADD COLUMN "nwcUrlSendId" INTEGER;
20+
21+
-- AlterTable
22+
ALTER TABLE "WalletPhoenixd" ADD COLUMN "primaryPasswordId" INTEGER;
23+
24+
-- CreateTable
25+
CREATE TABLE "Vault" (
26+
"id" SERIAL NOT NULL,
27+
"iv" TEXT NOT NULL,
28+
"value" TEXT NOT NULL,
29+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
30+
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
31+
32+
CONSTRAINT "Vault_pkey" PRIMARY KEY ("id")
33+
);
34+
35+
-- CreateTable
36+
CREATE TABLE "WalletLNC" (
37+
"id" SERIAL NOT NULL,
38+
"walletId" INTEGER NOT NULL,
39+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
40+
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
41+
"pairingPhraseId" INTEGER,
42+
"localKeyId" INTEGER,
43+
"remoteKeyId" INTEGER,
44+
"serverHostId" INTEGER,
45+
46+
CONSTRAINT "WalletLNC_pkey" PRIMARY KEY ("id")
47+
);
48+
49+
-- CreateIndex
50+
CREATE UNIQUE INDEX "WalletBlink_apiKeySendId_key" ON "WalletBlink"("apiKeySendId");
51+
52+
-- CreateIndex
53+
CREATE UNIQUE INDEX "WalletBlink_currencySendId_key" ON "WalletBlink"("currencySendId");
54+
55+
-- CreateIndex
56+
CREATE UNIQUE INDEX "WalletLNbits_adminKeyId_key" ON "WalletLNbits"("adminKeyId");
57+
58+
-- CreateIndex
59+
CREATE UNIQUE INDEX "WalletNWC_nwcUrlSendId_key" ON "WalletNWC"("nwcUrlSendId");
60+
61+
-- CreateIndex
62+
CREATE UNIQUE INDEX "WalletPhoenixd_primaryPasswordId_key" ON "WalletPhoenixd"("primaryPasswordId");
63+
64+
-- CreateIndex
65+
CREATE UNIQUE INDEX "WalletLNC_walletId_key" ON "WalletLNC"("walletId");
66+
67+
-- CreateIndex
68+
CREATE UNIQUE INDEX "WalletLNC_pairingPhraseId_key" ON "WalletLNC"("pairingPhraseId");
69+
70+
-- CreateIndex
71+
CREATE UNIQUE INDEX "WalletLNC_localKeyId_key" ON "WalletLNC"("localKeyId");
72+
73+
-- CreateIndex
74+
CREATE UNIQUE INDEX "WalletLNC_remoteKeyId_key" ON "WalletLNC"("remoteKeyId");
75+
76+
-- CreateIndex
77+
CREATE UNIQUE INDEX "WalletLNC_serverHostId_key" ON "WalletLNC"("serverHostId");
78+
79+
-- AddForeignKey
80+
ALTER TABLE "WalletLNbits" ADD CONSTRAINT "WalletLNbits_adminKeyId_fkey" FOREIGN KEY ("adminKeyId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
81+
82+
-- AddForeignKey
83+
ALTER TABLE "WalletNWC" ADD CONSTRAINT "WalletNWC_nwcUrlSendId_fkey" FOREIGN KEY ("nwcUrlSendId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
84+
85+
-- AddForeignKey
86+
ALTER TABLE "WalletBlink" ADD CONSTRAINT "WalletBlink_apiKeySendId_fkey" FOREIGN KEY ("apiKeySendId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
87+
88+
-- AddForeignKey
89+
ALTER TABLE "WalletBlink" ADD CONSTRAINT "WalletBlink_currencySendId_fkey" FOREIGN KEY ("currencySendId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
90+
91+
-- AddForeignKey
92+
ALTER TABLE "WalletPhoenixd" ADD CONSTRAINT "WalletPhoenixd_primaryPasswordId_fkey" FOREIGN KEY ("primaryPasswordId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
93+
94+
-- AddForeignKey
95+
ALTER TABLE "WalletLNC" ADD CONSTRAINT "WalletLNC_walletId_fkey" FOREIGN KEY ("walletId") REFERENCES "Wallet"("id") ON DELETE CASCADE ON UPDATE CASCADE;
96+
97+
-- AddForeignKey
98+
ALTER TABLE "WalletLNC" ADD CONSTRAINT "WalletLNC_pairingPhraseId_fkey" FOREIGN KEY ("pairingPhraseId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
99+
100+
-- AddForeignKey
101+
ALTER TABLE "WalletLNC" ADD CONSTRAINT "WalletLNC_localKeyId_fkey" FOREIGN KEY ("localKeyId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
102+
103+
-- AddForeignKey
104+
ALTER TABLE "WalletLNC" ADD CONSTRAINT "WalletLNC_remoteKeyId_fkey" FOREIGN KEY ("remoteKeyId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
105+
106+
-- AddForeignKey
107+
ALTER TABLE "WalletLNC" ADD CONSTRAINT "WalletLNC_serverHostId_fkey" FOREIGN KEY ("serverHostId") REFERENCES "Vault"("id") ON DELETE SET NULL ON UPDATE CASCADE;
108+
109+
110+
CREATE OR REPLACE FUNCTION migrate_wallet_vault()
111+
RETURNS void AS
112+
$$
113+
DECLARE
114+
vaultEntry "VaultEntry"%ROWTYPE;
115+
BEGIN
116+
FOR vaultEntry IN SELECT * FROM "VaultEntry" LOOP
117+
DECLARE
118+
vaultId INT;
119+
walletType "WalletType";
120+
BEGIN
121+
INSERT INTO "Vault" ("iv", "value")
122+
VALUES (vaultEntry."iv", vaultEntry."value")
123+
RETURNING id INTO vaultId;
124+
125+
SELECT type INTO walletType
126+
FROM "Wallet"
127+
WHERE id = vaultEntry."walletId";
128+
129+
CASE walletType
130+
WHEN 'LNBITS' THEN
131+
UPDATE "WalletLNbits"
132+
SET "adminKeyId" = vaultId
133+
WHERE "walletId" = vaultEntry."walletId";
134+
WHEN 'NWC' THEN
135+
UPDATE "WalletNWC"
136+
SET "nwcUrlSendId" = vaultId
137+
WHERE "walletId" = vaultEntry."walletId";
138+
WHEN 'BLINK' THEN
139+
IF vaultEntry."key" = 'apiKey' THEN
140+
UPDATE "WalletBlink"
141+
SET "apiKeySendId" = vaultId
142+
WHERE "walletId" = vaultEntry."walletId";
143+
ELSE
144+
UPDATE "WalletBlink"
145+
SET "currencySendId" = vaultId
146+
WHERE "walletId" = vaultEntry."walletId";
147+
END IF;
148+
WHEN 'PHOENIXD' THEN
149+
UPDATE "WalletPhoenixd"
150+
SET "primaryPasswordId" = vaultId
151+
WHERE "walletId" = vaultEntry."walletId";
152+
WHEN 'LNC' THEN
153+
IF vaultEntry."key" = 'pairingPhrase' THEN
154+
UPDATE "WalletLNC"
155+
SET "pairingPhraseId" = vaultId
156+
WHERE "walletId" = vaultEntry."walletId";
157+
ELSIF vaultEntry."key" = 'localKey' THEN
158+
UPDATE "WalletLNC"
159+
SET "localKeyId" = vaultId
160+
WHERE "walletId" = vaultEntry."walletId";
161+
ELSIF vaultEntry."key" = 'remoteKey' THEN
162+
UPDATE "WalletLNC"
163+
SET "remoteKeyId" = vaultId
164+
WHERE "walletId" = vaultEntry."walletId";
165+
ELSIF vaultEntry."key" = 'serverHost' THEN
166+
UPDATE "WalletLNC"
167+
SET "serverHostId" = vaultId
168+
WHERE "walletId" = vaultEntry."walletId";
169+
END IF;
170+
END CASE;
171+
END;
172+
END LOOP;
173+
END;
174+
$$ LANGUAGE plpgsql;
175+
176+
SELECT migrate_wallet_vault();
177+
DROP FUNCTION migrate_wallet_vault();

prisma/schema.prisma

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ model Wallet {
240240
walletNWC WalletNWC?
241241
walletPhoenixd WalletPhoenixd?
242242
walletBlink WalletBlink?
243+
walletLNC WalletLNC?
243244
244245
vaultEntries VaultEntry[] @relation("VaultEntries")
245246
withdrawals Withdrawl[]
@@ -250,6 +251,24 @@ model Wallet {
250251
@@index([priority])
251252
}
252253

254+
model Vault {
255+
id Int @id @default(autoincrement())
256+
iv String @db.Text
257+
value String @db.Text
258+
createdAt DateTime @default(now()) @map("created_at")
259+
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
260+
261+
walletLNbits WalletLNbits?
262+
walletNWC WalletNWC?
263+
walletBlinkApiKey WalletBlink? @relation("blinkApiKeySend")
264+
walletBlinkCurrency WalletBlink? @relation("blinkCurrencySend")
265+
walletPhoenixd WalletPhoenixd?
266+
walletLNCPairingPhrase WalletLNC? @relation("lncPairingPhrase")
267+
walletLNCRemoteKey WalletLNC? @relation("lncRemoteKey")
268+
walletLNCServerHost WalletLNC? @relation("lncServerHost")
269+
walletLNCLocalKey WalletLNC? @relation("lncLocalKey")
270+
}
271+
253272
model VaultEntry {
254273
id Int @id @default(autoincrement())
255274
key String @db.Text
@@ -322,25 +341,33 @@ model WalletLNbits {
322341
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
323342
url String
324343
invoiceKey String?
344+
adminKeyId Int? @unique
345+
adminKey Vault? @relation(fields: [adminKeyId], references: [id])
325346
}
326347

327348
model WalletNWC {
328-
id Int @id @default(autoincrement())
329-
walletId Int @unique
330-
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
331-
createdAt DateTime @default(now()) @map("created_at")
332-
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
333-
nwcUrlRecv String?
334-
}
335-
336-
model WalletBlink {
337349
id Int @id @default(autoincrement())
338350
walletId Int @unique
339351
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
340352
createdAt DateTime @default(now()) @map("created_at")
341353
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
342-
apiKeyRecv String?
343-
currencyRecv String?
354+
nwcUrlRecv String?
355+
nwcUrlSendId Int? @unique
356+
nwcUrlSend Vault? @relation(fields: [nwcUrlSendId], references: [id])
357+
}
358+
359+
model WalletBlink {
360+
id Int @id @default(autoincrement())
361+
walletId Int @unique
362+
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
363+
createdAt DateTime @default(now()) @map("created_at")
364+
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
365+
apiKeyRecv String?
366+
currencyRecv String?
367+
apiKeySendId Int? @unique
368+
apiKeySend Vault? @relation("blinkApiKeySend", fields: [apiKeySendId], references: [id])
369+
currencySendId Int? @unique
370+
currencySend Vault? @relation("blinkCurrencySend", fields: [currencySendId], references: [id])
344371
}
345372

346373
model WalletPhoenixd {
@@ -351,6 +378,24 @@ model WalletPhoenixd {
351378
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
352379
url String
353380
secondaryPassword String?
381+
primaryPasswordId Int? @unique
382+
primaryPassword Vault? @relation(fields: [primaryPasswordId], references: [id])
383+
}
384+
385+
model WalletLNC {
386+
id Int @id @default(autoincrement())
387+
walletId Int @unique
388+
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
389+
createdAt DateTime @default(now()) @map("created_at")
390+
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
391+
pairingPhraseId Int? @unique
392+
pairingPhrase Vault? @relation("lncPairingPhrase", fields: [pairingPhraseId], references: [id])
393+
localKeyId Int? @unique
394+
localKey Vault? @relation("lncLocalKey", fields: [localKeyId], references: [id])
395+
remoteKeyId Int? @unique
396+
remoteKey Vault? @relation("lncRemoteKey", fields: [remoteKeyId], references: [id])
397+
serverHostId Int? @unique
398+
serverHost Vault? @relation("lncServerHost", fields: [serverHostId], references: [id])
354399
}
355400

356401
model Mute {

0 commit comments

Comments
 (0)