From 4da48ff235e981bb6759a6d432c11a57b936bc8c Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Mon, 1 Apr 2024 22:01:53 -0500 Subject: [PATCH] Update database migrations to postgres and added docker-compose.yml for local postgres In this commit, the SQL schema in migration.sql was refactored. Adjustments also include the modification of foreign key references and constraints for enhanced data consistency and integrity. Furthermore, other minor changes include updating the `.gitignore`, `package.json`, and `.env.example` files, as well as adding a `docker-compose.yml` file for easier project setup. --- .env.example | 5 + .gitignore | 6 + CONTRIBUTING.md | 14 +- docker-compose.yml | 13 ++ package.json | 3 + .../20230819171953_init/migration.sql | 157 ++++++++++-------- prisma/migrations/migration_lock.toml | 3 - 7 files changed, 128 insertions(+), 73 deletions(-) create mode 100644 docker-compose.yml delete mode 100644 prisma/migrations/migration_lock.toml diff --git a/.env.example b/.env.example index 5c2629e..9a58b83 100644 --- a/.env.example +++ b/.env.example @@ -4,11 +4,16 @@ NODE_ENV=development # Authentication (NextAuth.js) NEXTAUTH_URL=http://localhost:3000 +# https://generate-secret.vercel.app/32 NEXTAUTH_SECRET= +# https://console.cloud.google.com/apis/credentials +# Callback URL: http://localhost:3000/api/auth/callback/google GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET= +# https://github.com/settings/applications +# Callback URL: http://localhost:3000/api/auth/callback/github GITHUB_CLIENT_ID= GITHUB_CLIENT_SECRET= diff --git a/.gitignore b/.gitignore index 81c8173..95f7c58 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,9 @@ next-env.d.ts # extra /private_docs .vercel + +# IDE +.idea/ + +# Docker Compose Volumes +/data/ \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d42b6fb..467ee41 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -32,7 +32,19 @@ cp .env.example .env.local pnpm postinstall ``` -6. Start the development server. +6. Start local Postgres server with Docker. + +```bash +docker-compose up -d +``` + +7. Run the migrations. + +```bash +pnpm prisma:migrate +``` + +8. Start the development server. ```bash pnpm dev diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..06c858f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3.8' +services: + db: + image: postgres:13 + restart: always + environment: + POSTGRES_USER: test_user + POSTGRES_PASSWORD: test_password + POSTGRES_DB: test_db + ports: + - 5432:5432 + volumes: + - ./data:/var/lib/postgresql/data diff --git a/package.json b/package.json index 41255ae..3a1f27f 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,9 @@ "lint": "next lint", "preview": "next build && next start", "postinstall": "prisma generate", + "prisma:generate": "prisma generate", + "prisma:migrate": "prisma migrate dev", + "prisma:studio": "prisma studio", "test": "jest --watch", "test:ci": "jest --ci", "format": "pnpm prettier . --write", diff --git a/prisma/migrations/20230819171953_init/migration.sql b/prisma/migrations/20230819171953_init/migration.sql index 9c7862d..8fe8aad 100644 --- a/prisma/migrations/20230819171953_init/migration.sql +++ b/prisma/migrations/20230819171953_init/migration.sql @@ -1,90 +1,109 @@ -- CreateTable -CREATE TABLE `accounts` ( - `id` VARCHAR(191) NOT NULL, - `user_id` VARCHAR(191) NOT NULL, - `type` VARCHAR(191) NOT NULL, - `provider` VARCHAR(191) NOT NULL, - `provider_account_id` VARCHAR(191) NOT NULL, - `refresh_token` TEXT NULL, - `access_token` TEXT NULL, - `expires_at` INTEGER NULL, - `token_type` VARCHAR(191) NULL, - `scope` VARCHAR(191) NULL, - `id_token` TEXT NULL, - `session_state` VARCHAR(191) NULL, - `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `updated_at` DATETIME(3) NOT NULL, - - UNIQUE INDEX `accounts_provider_provider_account_id_key`(`provider`, `provider_account_id`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +CREATE TABLE "accounts" +( + "id" TEXT NOT NULL, + "user_id" TEXT NOT NULL, + "type" TEXT NOT NULL, + "provider" TEXT NOT NULL, + "provider_account_id" TEXT NOT NULL, + "refresh_token" TEXT, + "access_token" TEXT, + "expires_at" INTEGER, + "token_type" TEXT, + "scope" TEXT, + "id_token" TEXT, + "session_state" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "accounts_pkey" PRIMARY KEY ("id") +); -- CreateTable -CREATE TABLE `sessions` ( - `id` VARCHAR(191) NOT NULL, - `session_token` VARCHAR(191) NOT NULL, - `user_id` VARCHAR(191) NOT NULL, - `expires` DATETIME(3) NOT NULL, +CREATE TABLE "sessions" +( + "id" TEXT NOT NULL, + "session_token" TEXT NOT NULL, + "user_id" TEXT NOT NULL, + "expires" TIMESTAMP(3) NOT NULL, - UNIQUE INDEX `sessions_session_token_key`(`session_token`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + CONSTRAINT "sessions_pkey" PRIMARY KEY ("id") +); -- CreateTable -CREATE TABLE `users` ( - `id` VARCHAR(191) NOT NULL, - `name` VARCHAR(191) NULL, - `email` VARCHAR(191) NULL, - `email_verified` DATETIME(3) NULL, - `image` VARCHAR(191) NULL, - `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `updated_at` DATETIME(3) NOT NULL, - - UNIQUE INDEX `users_email_key`(`email`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +CREATE TABLE "users" +( + "id" TEXT NOT NULL, + "name" TEXT, + "email" TEXT, + "email_verified" TIMESTAMP(3), + "image" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, --- CreateTable -CREATE TABLE `verification_tokens` ( - `identifier` VARCHAR(191) NOT NULL, - `token` VARCHAR(191) NOT NULL, - `expires` DATETIME(3) NOT NULL, + CONSTRAINT "users_pkey" PRIMARY KEY ("id") +); - UNIQUE INDEX `verification_tokens_token_key`(`token`), - UNIQUE INDEX `verification_tokens_identifier_token_key`(`identifier`, `token`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +-- CreateTable +CREATE TABLE "verification_tokens" +( + "identifier" TEXT NOT NULL, + "token" TEXT NOT NULL, + "expires" TIMESTAMP(3) NOT NULL +); -- CreateTable -CREATE TABLE `activities` ( - `id` VARCHAR(191) NOT NULL, - `user_id` VARCHAR(191) NOT NULL, - `name` VARCHAR(191) NOT NULL, - `description` VARCHAR(191) NULL, - `color_code` VARCHAR(191) NOT NULL, - `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `updated_at` DATETIME(3) NOT NULL, - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +CREATE TABLE "activities" +( + "id" TEXT NOT NULL, + "user_id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "description" TEXT, + "color_code" TEXT NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "activities_pkey" PRIMARY KEY ("id") +); -- CreateTable -CREATE TABLE `activity_log` ( - `id` VARCHAR(191) NOT NULL, - `activity_id` VARCHAR(191) NOT NULL, - `date` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `count` INTEGER NOT NULL DEFAULT 1, +CREATE TABLE "activity_log" +( + "id" TEXT NOT NULL, + "activity_id" TEXT NOT NULL, + "date" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "count" INTEGER NOT NULL DEFAULT 1, + + CONSTRAINT "activity_log_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "accounts_provider_provider_account_id_key" ON "accounts" ("provider", "provider_account_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "sessions_session_token_key" ON "sessions" ("session_token"); + +-- CreateIndex +CREATE UNIQUE INDEX "users_email_key" ON "users" ("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "verification_tokens_token_key" ON "verification_tokens" ("token"); - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +-- CreateIndex +CREATE UNIQUE INDEX "verification_tokens_identifier_token_key" ON "verification_tokens" ("identifier", "token"); -- AddForeignKey -ALTER TABLE `accounts` ADD CONSTRAINT `accounts_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "accounts" + ADD CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `sessions` ADD CONSTRAINT `sessions_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "sessions" + ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `activities` ADD CONSTRAINT `activities_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE "activities" + ADD CONSTRAINT "activities_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey -ALTER TABLE `activity_log` ADD CONSTRAINT `activity_log_activity_id_fkey` FOREIGN KEY (`activity_id`) REFERENCES `activities`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; +ALTER TABLE "activity_log" + ADD CONSTRAINT "activity_log_activity_id_fkey" FOREIGN KEY ("activity_id") REFERENCES "activities" ("id") ON DELETE RESTRICT ON UPDATE CASCADE; \ No newline at end of file diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml deleted file mode 100644 index e5a788a..0000000 --- a/prisma/migrations/migration_lock.toml +++ /dev/null @@ -1,3 +0,0 @@ -# Please do not edit this file manually -# It should be added in your version-control system (i.e. Git) -provider = "mysql" \ No newline at end of file