-- RedefineTables PRAGMA defer_foreign_keys=ON; PRAGMA foreign_keys=OFF; CREATE TABLE "new_TbankPortfolioValuePoint" ( "userId" TEXT NOT NULL, "accountId" TEXT NOT NULL, "attemptId" TEXT NOT NULL, "value" TEXT NOT NULL, "capturedAt" DATETIME NOT NULL, "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("userId", "accountId", "attemptId"), CONSTRAINT "TbankPortfolioValuePoint_userId_accountId_fkey" FOREIGN KEY ("userId", "accountId") REFERENCES "TbankPortfolioValueHistory" ("userId", "accountId") ON DELETE CASCADE ON UPDATE RESTRICT ); -- RUB-only: keep the RUB row per key; non-RUB rows are dropped. INSERT INTO "new_TbankPortfolioValuePoint" ("accountId", "attemptId", "capturedAt", "createdAt", "userId", "value") SELECT "accountId", "attemptId", "capturedAt", "createdAt", "userId", "value" FROM "TbankPortfolioValuePoint" WHERE "currency" = 'RUB'; DROP TABLE "TbankPortfolioValuePoint"; ALTER TABLE "new_TbankPortfolioValuePoint" RENAME TO "TbankPortfolioValuePoint"; CREATE INDEX "TbankPortfolioValuePoint_userId_accountId_capturedAt_idx" ON "TbankPortfolioValuePoint"("userId", "accountId", "capturedAt"); -- RUB-only: persisted JSON payloads written in the old multi-currency shape -- are stale. A missing snapshot/forecast is the app's normal "not synced yet" -- state: reads report availability 'missing' and the hourly walk rebuilds them -- on the next pass. Delete the stale rows instead of failing reads with 500. DELETE FROM "TbankPortfolio"; DELETE FROM "TbankForecast"; PRAGMA foreign_keys=ON; PRAGMA defer_foreign_keys=OFF;