stacker.news/prisma/migrations/20241024175439_vault/migration.sql

60 lines
2.0 KiB
MySQL
Raw Normal View History

2024-10-23 00:53:56 +00:00
-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.
ALTER TYPE "WalletType" ADD VALUE 'BLINK';
ALTER TYPE "WalletType" ADD VALUE 'LNC';
ALTER TYPE "WalletType" ADD VALUE 'WEBLN';
-- AlterTable
2024-10-24 20:30:56 +00:00
ALTER TABLE "users" ADD COLUMN "vaultKeyHash" TEXT NOT NULL DEFAULT '',
ADD COLUMN "walletsUpdatedAt" TIMESTAMP(3);
2024-10-23 00:53:56 +00:00
-- CreateTable
CREATE TABLE "VaultEntry" (
"id" SERIAL NOT NULL,
2024-10-24 20:30:56 +00:00
"key" TEXT NOT NULL,
"iv" TEXT NOT NULL,
2024-10-23 00:53:56 +00:00
"value" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"walletId" INTEGER,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "VaultEntry_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
2024-10-24 20:30:56 +00:00
CREATE INDEX "VaultEntry_walletId_idx" ON "VaultEntry"("walletId");
2024-10-23 00:53:56 +00:00
-- CreateIndex
2024-10-24 20:30:56 +00:00
CREATE UNIQUE INDEX "VaultEntry_userId_key_key" ON "VaultEntry"("userId", "key");
2024-10-23 00:53:56 +00:00
-- CreateIndex
2024-10-24 20:30:56 +00:00
CREATE INDEX "Wallet_priority_idx" ON "Wallet"("priority");
2024-10-23 00:53:56 +00:00
-- AddForeignKey
ALTER TABLE "VaultEntry" ADD CONSTRAINT "VaultEntry_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "VaultEntry" ADD CONSTRAINT "VaultEntry_walletId_fkey" FOREIGN KEY ("walletId") REFERENCES "Wallet"("id") ON DELETE CASCADE ON UPDATE CASCADE;
2024-10-24 20:30:56 +00:00
CREATE FUNCTION wallet_updated_at_trigger() RETURNS TRIGGER AS $$
BEGIN
UPDATE "users" SET "walletsUpdatedAt" = NOW() WHERE "id" = NEW."userId";
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER wallet_updated_at_trigger
AFTER INSERT OR UPDATE ON "Wallet"
FOR EACH ROW EXECUTE PROCEDURE wallet_updated_at_trigger();
CREATE TRIGGER vault_entry_updated_at_trigger
AFTER INSERT OR UPDATE ON "VaultEntry"
FOR EACH ROW EXECUTE PROCEDURE wallet_updated_at_trigger();