From 1ae93d71f670cfa3873bc72bfef56fd352d05c0c Mon Sep 17 00:00:00 2001 From: greysoh Date: Sun, 21 Apr 2024 16:09:20 -0400 Subject: [PATCH] chore: Migrates development to use PostgreSQL instead. --- .gitignore | 3 +- dev.env | 2 +- init.sh | 33 ++++++++++++ .../20240417003924_init/migration.sql | 43 --------------- .../20240417153312_fix_add_pw/migration.sql | 21 -------- .../migration.sql | 21 -------- .../migration.sql | 20 ------- .../migration.sql | 21 -------- .../20240421200334_init/migration.sql | 53 +++++++++++++++++++ prisma/migrations/migration_lock.toml | 2 +- prisma/schema.prisma | 6 ++- shell.nix | 4 +- 12 files changed, 96 insertions(+), 133 deletions(-) create mode 100755 init.sh delete mode 100644 prisma/migrations/20240417003924_init/migration.sql delete mode 100644 prisma/migrations/20240417153312_fix_add_pw/migration.sql delete mode 100644 prisma/migrations/20240417173957_fix_name_no_longer_optional/migration.sql delete mode 100644 prisma/migrations/20240417221012_fix_changes_permission_name_and_uniqueness/migration.sql delete mode 100644 prisma/migrations/20240419124642_fix_fixes_permission_ids/migration.sql create mode 100644 prisma/migrations/20240421200334_init/migration.sql diff --git a/.gitignore b/.gitignore index 1949637..12b3a99 100644 --- a/.gitignore +++ b/.gitignore @@ -132,5 +132,4 @@ dist .yarn/install-state.gz .pnp.* -*.db -*.db-journal \ No newline at end of file +.tmp \ No newline at end of file diff --git a/dev.env b/dev.env index 305e40b..750a474 100644 --- a/dev.env +++ b/dev.env @@ -4,4 +4,4 @@ # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. # See the documentation for all the connection string options: https://pris.ly/d/connection-strings -DATABASE_URL="file:./db/dev.db" \ No newline at end of file +DATABASE_URL="postgresql://nextnet:nextnet@localhost:5432/nextnet?schema=nextnet" \ No newline at end of file diff --git a/init.sh b/init.sh new file mode 100755 index 0000000..83de5b3 --- /dev/null +++ b/init.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +if [ ! -d ".tmp" ]; then + echo "Hello and welcome to the NextNet project! Please wait while I initialize things for you..." + cp dev.env .env + mkdir .tmp +fi + +lsof -i:5432 | grep postgres +IS_PG_RUNNING=$? + +if [ ! -f ".tmp/ispginit" ]; then + if [[ "$IS_PG_RUNNING" == 0 ]]; then + kill -9 $(lsof -t -i:5432) + fi + + echo " - Database not initialized! Initializing database..." + mkdir .tmp/pglock + + initdb -D .tmp/db + pg_ctl -D .tmp/db -l .tmp/logfile -o "--unix_socket_directories='$PWD/.tmp/pglock/'" start + createdb -h localhost -p 5432 nextnet + + psql -h localhost -p 5432 nextnet -c "CREATE ROLE nextnet WITH LOGIN SUPERUSER PASSWORD 'nextnet';" + + npm install --save-dev + npx prisma migrate dev + + touch .tmp/ispginit +elif [[ "$IS_PG_RUNNING" == 1 ]]; then + pg_ctl -D .tmp/db -l .tmp/logfile -o "--unix_socket_directories='$PWD/.tmp/pglock/'" start +fi + +source .env # Make sure we actually load correctly \ No newline at end of file diff --git a/prisma/migrations/20240417003924_init/migration.sql b/prisma/migrations/20240417003924_init/migration.sql deleted file mode 100644 index 0bb7a1e..0000000 --- a/prisma/migrations/20240417003924_init/migration.sql +++ /dev/null @@ -1,43 +0,0 @@ --- CreateTable -CREATE TABLE "DesinationProvider" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "name" TEXT NOT NULL, - "description" TEXT, - "backend" TEXT NOT NULL, - "connectionDetails" TEXT NOT NULL -); - --- CreateTable -CREATE TABLE "ForwardRule" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "name" TEXT NOT NULL, - "description" TEXT, - "sourceIP" TEXT NOT NULL, - "sourcePort" INTEGER NOT NULL, - "destIP" TEXT NOT NULL, - "destPort" INTEGER NOT NULL, - "destProviderID" INTEGER NOT NULL, - "enabled" BOOLEAN NOT NULL -); - --- CreateTable -CREATE TABLE "Permission" ( - "permissionID" TEXT NOT NULL, - "has" BOOLEAN NOT NULL, - "userID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - CONSTRAINT "Permission_userID_fkey" FOREIGN KEY ("userID") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE -); - --- CreateTable -CREATE TABLE "User" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "email" TEXT NOT NULL, - "name" TEXT, - "rootToken" TEXT -); - --- CreateIndex -CREATE UNIQUE INDEX "Permission_permissionID_key" ON "Permission"("permissionID"); - --- CreateIndex -CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); diff --git a/prisma/migrations/20240417153312_fix_add_pw/migration.sql b/prisma/migrations/20240417153312_fix_add_pw/migration.sql deleted file mode 100644 index 8035bde..0000000 --- a/prisma/migrations/20240417153312_fix_add_pw/migration.sql +++ /dev/null @@ -1,21 +0,0 @@ -/* - Warnings: - - - Added the required column `password` to the `User` table without a default value. This is not possible if the table is not empty. - -*/ --- RedefineTables -PRAGMA foreign_keys=OFF; -CREATE TABLE "new_User" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "email" TEXT NOT NULL, - "name" TEXT, - "password" TEXT NOT NULL, - "rootToken" TEXT -); -INSERT INTO "new_User" ("email", "id", "name", "rootToken") SELECT "email", "id", "name", "rootToken" FROM "User"; -DROP TABLE "User"; -ALTER TABLE "new_User" RENAME TO "User"; -CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); -PRAGMA foreign_key_check; -PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/20240417173957_fix_name_no_longer_optional/migration.sql b/prisma/migrations/20240417173957_fix_name_no_longer_optional/migration.sql deleted file mode 100644 index 7d4372f..0000000 --- a/prisma/migrations/20240417173957_fix_name_no_longer_optional/migration.sql +++ /dev/null @@ -1,21 +0,0 @@ -/* - Warnings: - - - Made the column `name` on table `User` required. This step will fail if there are existing NULL values in that column. - -*/ --- RedefineTables -PRAGMA foreign_keys=OFF; -CREATE TABLE "new_User" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "email" TEXT NOT NULL, - "name" TEXT NOT NULL, - "password" TEXT NOT NULL, - "rootToken" TEXT -); -INSERT INTO "new_User" ("email", "id", "name", "password", "rootToken") SELECT "email", "id", "name", "password", "rootToken" FROM "User"; -DROP TABLE "User"; -ALTER TABLE "new_User" RENAME TO "User"; -CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); -PRAGMA foreign_key_check; -PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/20240417221012_fix_changes_permission_name_and_uniqueness/migration.sql b/prisma/migrations/20240417221012_fix_changes_permission_name_and_uniqueness/migration.sql deleted file mode 100644 index 3ab22a3..0000000 --- a/prisma/migrations/20240417221012_fix_changes_permission_name_and_uniqueness/migration.sql +++ /dev/null @@ -1,20 +0,0 @@ -/* - Warnings: - - - You are about to drop the column `permissionID` on the `Permission` table. All the data in the column will be lost. - - Added the required column `permission` to the `Permission` table without a default value. This is not possible if the table is not empty. - -*/ --- RedefineTables -PRAGMA foreign_keys=OFF; -CREATE TABLE "new_Permission" ( - "permission" TEXT NOT NULL, - "has" BOOLEAN NOT NULL, - "userID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - CONSTRAINT "Permission_userID_fkey" FOREIGN KEY ("userID") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE -); -INSERT INTO "new_Permission" ("has", "userID") SELECT "has", "userID" FROM "Permission"; -DROP TABLE "Permission"; -ALTER TABLE "new_Permission" RENAME TO "Permission"; -PRAGMA foreign_key_check; -PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/20240419124642_fix_fixes_permission_ids/migration.sql b/prisma/migrations/20240419124642_fix_fixes_permission_ids/migration.sql deleted file mode 100644 index 78ee943..0000000 --- a/prisma/migrations/20240419124642_fix_fixes_permission_ids/migration.sql +++ /dev/null @@ -1,21 +0,0 @@ -/* - Warnings: - - - The primary key for the `Permission` table will be changed. If it partially fails, the table could be left without primary key constraint. - - Added the required column `id` to the `Permission` table without a default value. This is not possible if the table is not empty. - -*/ --- RedefineTables -PRAGMA foreign_keys=OFF; -CREATE TABLE "new_Permission" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "permission" TEXT NOT NULL, - "has" BOOLEAN NOT NULL, - "userID" INTEGER NOT NULL, - CONSTRAINT "Permission_userID_fkey" FOREIGN KEY ("userID") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE -); -INSERT INTO "new_Permission" ("has", "permission", "userID") SELECT "has", "permission", "userID" FROM "Permission"; -DROP TABLE "Permission"; -ALTER TABLE "new_Permission" RENAME TO "Permission"; -PRAGMA foreign_key_check; -PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/20240421200334_init/migration.sql b/prisma/migrations/20240421200334_init/migration.sql new file mode 100644 index 0000000..17e7104 --- /dev/null +++ b/prisma/migrations/20240421200334_init/migration.sql @@ -0,0 +1,53 @@ +-- CreateTable +CREATE TABLE "DesinationProvider" ( + "id" SERIAL NOT NULL, + "name" TEXT NOT NULL, + "description" TEXT, + "backend" TEXT NOT NULL, + "connectionDetails" TEXT NOT NULL, + + CONSTRAINT "DesinationProvider_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "ForwardRule" ( + "id" SERIAL NOT NULL, + "name" TEXT NOT NULL, + "description" TEXT, + "sourceIP" TEXT NOT NULL, + "sourcePort" INTEGER NOT NULL, + "destIP" TEXT NOT NULL, + "destPort" INTEGER NOT NULL, + "destProviderID" INTEGER NOT NULL, + "enabled" BOOLEAN NOT NULL, + + CONSTRAINT "ForwardRule_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Permission" ( + "id" SERIAL NOT NULL, + "permission" TEXT NOT NULL, + "has" BOOLEAN NOT NULL, + "userID" INTEGER NOT NULL, + + CONSTRAINT "Permission_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "User" ( + "id" SERIAL NOT NULL, + "email" TEXT NOT NULL, + "name" TEXT NOT NULL, + "password" TEXT NOT NULL, + "rootToken" TEXT, + "isRootServiceAccount" BOOLEAN, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- AddForeignKey +ALTER TABLE "Permission" ADD CONSTRAINT "Permission_userID_fkey" FOREIGN KEY ("userID") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index e5e5c47..fbffa92 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "sqlite" \ No newline at end of file +provider = "postgresql" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d098c1e..2d8c39e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -6,7 +6,7 @@ generator client { } datasource db { - provider = "sqlite" + provider = "postgresql" url = env("DATABASE_URL") } @@ -54,6 +54,8 @@ model User { password String // Will be hashed using bcrypt - rootToken String? + rootToken String? + isRootServiceAccount Boolean? + permissions Permission[] } diff --git a/shell.nix b/shell.nix index 72697b9..6082b24 100644 --- a/shell.nix +++ b/shell.nix @@ -2,10 +2,12 @@ let pkgs = import (fetchTarball ("channel:nixpkgs-unstable")) { }; in pkgs.mkShell { - buildInputs = [ pkgs.nodejs pkgs.sqlite pkgs.openssl ]; + buildInputs = [ pkgs.nodejs pkgs.openssl pkgs.postgresql pkgs.lsof ]; shellHook = '' export PRISMA_QUERY_ENGINE_BINARY=${pkgs.prisma-engines}/bin/query-engine export PRISMA_QUERY_ENGINE_LIBRARY=${pkgs.prisma-engines}/lib/libquery_engine.node export PRISMA_SCHEMA_ENGINE_BINARY=${pkgs.prisma-engines}/bin/schema-engine + + bash init.sh ''; } \ No newline at end of file