Skip to content

Instantly share code, notes, and snippets.

@igo
Last active February 12, 2022 13:25
Show Gist options
  • Save igo/db74ddd57edbe6d557eaa5ac8e7b0363 to your computer and use it in GitHub Desktop.
Save igo/db74ddd57edbe6d557eaa5ac8e7b0363 to your computer and use it in GitHub Desktop.
Prisma next-auth SQL migration v3 to v4
-- DropForeignKey
ALTER TABLE "Account" DROP CONSTRAINT "Account_userId_fkey";
-- DropForeignKey
ALTER TABLE "Session" DROP CONSTRAINT "Session_userId_fkey";
-- DropIndex
DROP INDEX "Account_providerId_providerAccountId_key";
-- DropIndex
DROP INDEX "Session_accessToken_key";
-- AlterTable
ALTER TABLE "Account"
ADD COLUMN "expires_at" INTEGER,
ADD COLUMN "id_token" TEXT,
ADD COLUMN "oauth_token" TEXT,
ADD COLUMN "oauth_token_secret" TEXT,
ADD COLUMN "scope" TEXT,
ADD COLUMN "session_state" TEXT,
ADD COLUMN "token_type" TEXT;
ALTER TABLE "Account" RENAME COLUMN "accessToken" TO "access_token";
ALTER TABLE "Account" RENAME COLUMN "refreshToken" TO "refresh_token";
ALTER TABLE "Account" RENAME COLUMN "providerType" TO "type";
ALTER TABLE "Account" RENAME COLUMN "providerId" TO "provider";
UPDATE "Account" SET "expires_at" = CAST(EXTRACT (epoch FROM "accessTokenExpires") AS integer);
ALTER TABLE "Account" DROP COLUMN "accessTokenExpires";
-- AlterTable
ALTER TABLE "Session" DROP COLUMN "accessToken";
-- DropTable
DROP TABLE "VerificationRequest";
-- CreateTable
CREATE TABLE "VerificationToken" (
"identifier" TEXT NOT NULL,
"token" TEXT NOT NULL,
"expires" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token");
-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");
-- CreateIndex
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");
-- AddForeignKey
ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
/*
Warnings:
- You are about to drop the column `accessToken` on the `Account` table. All the data in the column will be lost.
- You are about to drop the column `accessTokenExpires` on the `Account` table. All the data in the column will be lost.
- You are about to drop the column `providerId` on the `Account` table. All the data in the column will be lost.
- You are about to drop the column `providerType` on the `Account` table. All the data in the column will be lost.
- You are about to drop the column `refreshToken` on the `Account` table. All the data in the column will be lost.
- You are about to drop the column `accessToken` on the `Session` table. All the data in the column will be lost.
- You are about to drop the `VerificationRequest` table. If the table is not empty, all the data it contains will be lost.
- A unique constraint covering the columns `[provider,providerAccountId]` on the table `Account` will be added. If there are existing duplicate values, this will fail.
- Added the required column `provider` to the `Account` table without a default value. This is not possible if the table is not empty.
- Added the required column `type` to the `Account` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "Account" DROP CONSTRAINT "Account_userId_fkey";
-- DropForeignKey
ALTER TABLE "Session" DROP CONSTRAINT "Session_userId_fkey";
-- DropIndex
DROP INDEX "Account_providerId_providerAccountId_key";
-- DropIndex
DROP INDEX "Session_accessToken_key";
-- AlterTable
ALTER TABLE "Account" DROP COLUMN "accessToken",
DROP COLUMN "accessTokenExpires",
DROP COLUMN "providerId",
DROP COLUMN "providerType",
DROP COLUMN "refreshToken",
ADD COLUMN "access_token" TEXT,
ADD COLUMN "expires_at" INTEGER,
ADD COLUMN "id_token" TEXT,
ADD COLUMN "oauth_token" TEXT,
ADD COLUMN "oauth_token_secret" TEXT,
ADD COLUMN "provider" TEXT NOT NULL,
ADD COLUMN "refresh_token" TEXT,
ADD COLUMN "scope" TEXT,
ADD COLUMN "session_state" TEXT,
ADD COLUMN "token_type" TEXT,
ADD COLUMN "type" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "Session" DROP COLUMN "accessToken";
-- DropTable
DROP TABLE "VerificationRequest";
-- CreateTable
CREATE TABLE "VerificationToken" (
"identifier" TEXT NOT NULL,
"token" TEXT NOT NULL,
"expires" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token");
-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");
-- CreateIndex
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");
-- AddForeignKey
ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment