diff --git a/apps/dokploy/drizzle/0066_calm_vengeance.sql b/apps/dokploy/drizzle/0066_calm_vengeance.sql deleted file mode 100644 index 3901740b..00000000 --- a/apps/dokploy/drizzle/0066_calm_vengeance.sql +++ /dev/null @@ -1,367 +0,0 @@ --- Primero ejecutar todas las modificaciones estructurales -ALTER TABLE "user" RENAME COLUMN "userId" TO "id"; ---> statement-breakpoint -ALTER TABLE "project" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "destination" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "certificate" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "registry" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "notification" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "ssh-key" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "git_provider" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "server" RENAME COLUMN "adminId" TO "userId"; ---> statement-breakpoint -ALTER TABLE "user" DROP CONSTRAINT "user_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "user" DROP CONSTRAINT "user_authId_auth_id_fk"; ---> statement-breakpoint -ALTER TABLE "admin" DROP CONSTRAINT "admin_authId_auth_id_fk"; ---> statement-breakpoint -ALTER TABLE "project" DROP CONSTRAINT "project_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "destination" DROP CONSTRAINT "destination_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "certificate" DROP CONSTRAINT "certificate_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "session" DROP CONSTRAINT "session_user_id_auth_id_fk"; ---> statement-breakpoint -ALTER TABLE "registry" DROP CONSTRAINT "registry_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "notification" DROP CONSTRAINT "notification_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "ssh-key" DROP CONSTRAINT "ssh-key_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "git_provider" DROP CONSTRAINT "git_provider_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "server" DROP CONSTRAINT "server_adminId_admin_adminId_fk"; ---> statement-breakpoint -ALTER TABLE "user" ALTER COLUMN "expirationDate" SET DATA TYPE text; ---> statement-breakpoint -ALTER TABLE "session" ALTER COLUMN "expires_at" SET DATA TYPE timestamp; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "name" text DEFAULT '' NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "email" text NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "email_verified" boolean NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "image" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "role" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "banned" boolean; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "ban_reason" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "ban_expires" timestamp; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "updated_at" timestamp NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "serverIp" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "certificateType" "certificateType" DEFAULT 'none' NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "host" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "letsEncryptEmail" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "sshPrivateKey" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "enableDockerCleanup" boolean DEFAULT false NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "enableLogRotation" boolean DEFAULT false NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "enablePaidFeatures" boolean DEFAULT false NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "metricsConfig" jsonb DEFAULT '{"server":{"type":"Dokploy","refreshRate":60,"port":4500,"token":"","retentionDays":2,"cronJob":"","urlCallback":"","thresholds":{"cpu":0,"memory":0}},"containers":{"refreshRate":60,"services":{"include":[],"exclude":[]}}}'::jsonb NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "cleanupCacheApplications" boolean DEFAULT false NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "cleanupCacheOnPreviews" boolean DEFAULT false NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "cleanupCacheOnCompose" boolean DEFAULT false NOT NULL; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "stripeCustomerId" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "stripeSubscriptionId" text; ---> statement-breakpoint -ALTER TABLE "user" ADD COLUMN "serversQuantity" integer DEFAULT 0 NOT NULL; ---> statement-breakpoint -ALTER TABLE "session" ADD COLUMN "token" text NOT NULL; ---> statement-breakpoint -ALTER TABLE "session" ADD COLUMN "created_at" timestamp NOT NULL; ---> statement-breakpoint -ALTER TABLE "session" ADD COLUMN "updated_at" timestamp NOT NULL; ---> statement-breakpoint -ALTER TABLE "session" ADD COLUMN "ip_address" text; ---> statement-breakpoint -ALTER TABLE "session" ADD COLUMN "user_agent" text; ---> statement-breakpoint -ALTER TABLE "session" ADD COLUMN "impersonated_by" text; ---> statement-breakpoint -ALTER TABLE "session" ADD COLUMN "active_organization_id" text; ---> statement-breakpoint -CREATE TABLE "account" ( - "id" text PRIMARY KEY NOT NULL, - "account_id" text NOT NULL, - "provider_id" text NOT NULL, - "user_id" text NOT NULL, - "access_token" text, - "refresh_token" text, - "id_token" text, - "access_token_expires_at" timestamp, - "refresh_token_expires_at" timestamp, - "scope" text, - "password" text, - "is2FAEnabled" boolean DEFAULT false NOT NULL, - "created_at" timestamp NOT NULL, - "updated_at" timestamp NOT NULL, - "resetPasswordToken" text, - "resetPasswordExpiresAt" text, - "confirmationToken" text, - "confirmationExpiresAt" text -); ---> statement-breakpoint -CREATE TABLE "invitation" ( - "id" text PRIMARY KEY NOT NULL, - "organization_id" text NOT NULL, - "email" text NOT NULL, - "role" text, - "status" text NOT NULL, - "expires_at" timestamp NOT NULL, - "inviter_id" text NOT NULL -); ---> statement-breakpoint -CREATE TABLE "member" ( - "id" text PRIMARY KEY NOT NULL, - "organization_id" text NOT NULL, - "user_id" text NOT NULL, - "role" text NOT NULL, - "created_at" timestamp NOT NULL -); ---> statement-breakpoint -CREATE TABLE "organization" ( - "id" text PRIMARY KEY NOT NULL, - "name" text NOT NULL, - "slug" text, - "logo" text, - "created_at" timestamp NOT NULL, - "metadata" text, - "owner_id" text NOT NULL, - CONSTRAINT "organization_slug_unique" UNIQUE("slug") -); ---> statement-breakpoint -CREATE TABLE "verification" ( - "id" text PRIMARY KEY NOT NULL, - "identifier" text NOT NULL, - "value" text NOT NULL, - "expires_at" timestamp NOT NULL, - "created_at" timestamp, - "updated_at" timestamp -); ---> statement-breakpoint -ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "invitation" ADD CONSTRAINT "invitation_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "invitation" ADD CONSTRAINT "invitation_inviter_id_user_id_fk" FOREIGN KEY ("inviter_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "member" ADD CONSTRAINT "member_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "member" ADD CONSTRAINT "member_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "organization" ADD CONSTRAINT "organization_owner_id_user_id_fk" FOREIGN KEY ("owner_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "project" ADD CONSTRAINT "project_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "destination" ADD CONSTRAINT "destination_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "certificate" ADD CONSTRAINT "certificate_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "registry" ADD CONSTRAINT "registry_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "notification" ADD CONSTRAINT "notification_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "ssh-key" ADD CONSTRAINT "ssh-key_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "git_provider" ADD CONSTRAINT "git_provider_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "server" ADD CONSTRAINT "server_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; ---> statement-breakpoint -ALTER TABLE "user" DROP COLUMN "adminId"; ---> statement-breakpoint -ALTER TABLE "user" DROP COLUMN "authId"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "adminId"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "serverIp"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "certificateType"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "host"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "letsEncryptEmail"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "sshPrivateKey"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "enableDockerCleanup"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "enableLogRotation"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "authId"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "createdAt"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "stripeCustomerId"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "stripeSubscriptionId"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "serversQuantity"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "enablePaidFeatures"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "metricsConfig"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "cleanupCacheApplications"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "cleanupCacheOnPreviews"; ---> statement-breakpoint -ALTER TABLE "admin" DROP COLUMN "cleanupCacheOnCompose"; ---> statement-breakpoint -ALTER TABLE "user" ADD CONSTRAINT "user_email_unique" UNIQUE("email"); ---> statement-breakpoint - --- Primero quitar NOT NULL temporalmente -ALTER TABLE "session" ALTER COLUMN "token" DROP NOT NULL; ---> statement-breakpoint - --- Actualizar tokens existentes -UPDATE "session" SET - token = gen_random_uuid(), - created_at = COALESCE(created_at, NOW() - interval '1 day'), - updated_at = NOW() -WHERE token IS NULL; - --- Restablecer restricciones -ALTER TABLE "session" ALTER COLUMN "token" SET NOT NULL; -ALTER TABLE "session" ADD CONSTRAINT "session_token_unique" UNIQUE (token); - --- Luego realizar la migración de datos --- Migración de datos para Admins -WITH admin_users AS ( - INSERT INTO "user" ( - id, created_at, token, email, email_verified, role, updated_at, - certificate_type, server_ip, host, lets_encrypt_email, ssh_private_key, - enable_docker_cleanup, enable_log_rotation, enable_paid_features, - metrics_config, cleanup_cache_applications, cleanup_cache_on_previews, - cleanup_cache_on_compose, stripe_customer_id, stripe_subscription_id, - servers_quantity - ) - SELECT - gen_random_uuid(), - a.created_at, - a.token, - a.email, - true, - 'admin', - a.created_at, - ad.certificate_type, - ad.server_ip, - ad.host, - ad.lets_encrypt_email, - ad.ssh_private_key, - ad.enable_docker_cleanup, - ad.enable_log_rotation, - ad.enable_paid_features, - ad.metrics_config, - ad.cleanup_cache_applications, - ad.cleanup_cache_on_previews, - ad.cleanup_cache_on_compose, - ad.stripe_customer_id, - ad.stripe_subscription_id, - ad.servers_quantity - FROM auth a - JOIN admins ad ON a.id = ad.auth_id - RETURNING id AS user_id, created_at, email -) -INSERT INTO account (id, account_id, provider_id, user_id, password, created_at, updated_at) -SELECT - gen_random_uuid(), - a.id, - 'credentials', - au.user_id, - a.password, - au.created_at, - au.created_at -FROM auth a -JOIN admin_users au ON a.email = au.email; - --- Crear organizaciones para admins -WITH admin_orgs AS ( - INSERT INTO organization (id, name, slug, created_at, owner_id) - SELECT - gen_random_uuid(), - 'My Organization', - 'org/' || au.user_id, - au.created_at, - au.user_id - FROM admin_users au - RETURNING id AS org_id, owner_id -) --- Migrar usuarios regulares y asociar a organizaciones -INSERT INTO "user" ( - id, created_at, token, email, email_verified, role, updated_at, - can_create_projects, can_access_ssh_keys -) -SELECT - gen_random_uuid(), - a.created_at, - a.token, - a.email, - true, - 'user', - a.created_at, - u.can_create_projects, - u.can_access_ssh_keys -FROM auth a -JOIN users u ON a.id = u.auth_id -WHERE a.role = 'user'; - --- Crear accounts para usuarios -INSERT INTO account (id, account_id, provider_id, user_id, password, created_at, updated_at) -SELECT - gen_random_uuid(), - a.id, - 'credentials', - u.id, - a.password, - a.created_at, - a.created_at -FROM auth a -JOIN "user" u ON a.email = u.email; - --- Asociar usuarios a organizaciones de sus admins -INSERT INTO member (id, organization_id, user_id, role, created_at) -SELECT - gen_random_uuid(), - ao.org_id, - u.id, - 'user', - u.created_at -FROM "user" u -JOIN users old_u ON u.email = old_u.email -JOIN auth a ON old_u.auth_id = a.id -JOIN admin_orgs ao ON ao.owner_id = a.id; - --- Eliminar tablas obsoletas -DROP TABLE IF EXISTS admins; -DROP TABLE IF EXISTS users; -DROP TABLE IF EXISTS auth; \ No newline at end of file diff --git a/apps/dokploy/drizzle/0066_soft_kronos.sql b/apps/dokploy/drizzle/0066_soft_kronos.sql new file mode 100644 index 00000000..e75a6b24 --- /dev/null +++ b/apps/dokploy/drizzle/0066_soft_kronos.sql @@ -0,0 +1,183 @@ +-- Create new tables +CREATE TABLE IF NOT EXISTS "account" ( + "id" text PRIMARY KEY NOT NULL, + "accountId" text NOT NULL, + "providerId" text NOT NULL, + "userId" text NOT NULL REFERENCES "user"("userId"), + "accessToken" text, + "refreshToken" text, + "idToken" text, + "accessTokenExpiresAt" timestamp, + "refreshTokenExpiresAt" timestamp, + "scope" text, + "password" text, + "is2FAEnabled" boolean DEFAULT false NOT NULL, + "createdAt" timestamp NOT NULL, + "updatedAt" timestamp NOT NULL, + "resetPasswordToken" text, + "resetPasswordExpiresAt" text, + "confirmationToken" text, + "confirmationExpiresAt" text +); + +CREATE TABLE IF NOT EXISTS "organization" ( + "id" text PRIMARY KEY NOT NULL, + "name" text NOT NULL, + "slug" text, + "logo" text, + "createdAt" timestamp NOT NULL, + "metadata" text, + "ownerId" text NOT NULL REFERENCES "user"("userId"), + CONSTRAINT "organization_slug_unique" UNIQUE("slug") +); + +CREATE TABLE IF NOT EXISTS "member" ( + "id" text PRIMARY KEY NOT NULL, + "organizationId" text NOT NULL REFERENCES "organization"("id"), + "userId" text NOT NULL REFERENCES "user"("userId"), + "role" text NOT NULL, + "createdAt" timestamp NOT NULL +); + +CREATE TABLE IF NOT EXISTS "invitation" ( + "id" text PRIMARY KEY NOT NULL, + "organizationId" text NOT NULL, + "email" text NOT NULL, + "role" text, + "status" text NOT NULL, + "expiresAt" timestamp NOT NULL, + "inviterId" text NOT NULL +); + +CREATE TABLE IF NOT EXISTS "verification" ( + "id" text PRIMARY KEY NOT NULL, + "identifier" text NOT NULL, + "value" text NOT NULL, + "expiresAt" timestamp NOT NULL, + "createdAt" timestamp, + "updatedAt" timestamp +); + +-- Alter existing user table to add new columns +ALTER TABLE "user" +ADD COLUMN IF NOT EXISTS "email" text, +ADD COLUMN IF NOT EXISTS "emailVerified" boolean DEFAULT false, +ADD COLUMN IF NOT EXISTS "role" text, +ADD COLUMN IF NOT EXISTS "certificateType" text DEFAULT 'none', +ADD COLUMN IF NOT EXISTS "serverIp" text, +ADD COLUMN IF NOT EXISTS "host" text, +ADD COLUMN IF NOT EXISTS "letsEncryptEmail" text, +ADD COLUMN IF NOT EXISTS "sshPrivateKey" text, +ADD COLUMN IF NOT EXISTS "enableDockerCleanup" boolean DEFAULT false, +ADD COLUMN IF NOT EXISTS "enableLogRotation" boolean DEFAULT false, +ADD COLUMN IF NOT EXISTS "enablePaidFeatures" boolean DEFAULT false, +ADD COLUMN IF NOT EXISTS "metricsConfig" jsonb DEFAULT '{}', +ADD COLUMN IF NOT EXISTS "cleanupCacheApplications" boolean DEFAULT false, +ADD COLUMN IF NOT EXISTS "cleanupCacheOnPreviews" boolean DEFAULT false, +ADD COLUMN IF NOT EXISTS "cleanupCacheOnCompose" boolean DEFAULT false, +ADD COLUMN IF NOT EXISTS "stripeCustomerId" text, +ADD COLUMN IF NOT EXISTS "stripeSubscriptionId" text, +ADD COLUMN IF NOT EXISTS "serversQuantity" integer DEFAULT 0; + +-- Migrate admin users +WITH admin_users AS ( + UPDATE "user" u + SET + "email" = a."email", + "emailVerified" = true, + "role" = 'admin', + "token" = a."token", + "certificateType" = adm."certificateType", + "serverIp" = adm."serverIp", + "host" = adm."host", + "letsEncryptEmail" = adm."letsEncryptEmail", + "sshPrivateKey" = adm."sshPrivateKey", + "enableDockerCleanup" = adm."enableDockerCleanup", + "enableLogRotation" = adm."enableLogRotation", + "enablePaidFeatures" = adm."enablePaidFeatures", + "metricsConfig" = adm."metricsConfig", + "cleanupCacheApplications" = adm."cleanupCacheApplications", + "cleanupCacheOnPreviews" = adm."cleanupCacheOnPreviews", + "cleanupCacheOnCompose" = adm."cleanupCacheOnCompose", + "stripeCustomerId" = adm."stripeCustomerId", + "stripeSubscriptionId" = adm."stripeSubscriptionId", + "serversQuantity" = adm."serversQuantity" + FROM "auth" a + INNER JOIN "admin" adm ON a."id" = adm."adminId" + WHERE a."id" = u."userId" + RETURNING u."userId", a."email" +) +INSERT INTO "account" ("id", "accountId", "providerId", "password", "userId", "createdAt", "updatedAt") +SELECT + gen_random_uuid(), + a."id", + 'credentials', + a."password", + au."userId", + NOW(), + NOW() +FROM "auth" a +INNER JOIN admin_users au ON a."email" = au."email"; + +-- Create organizations for admin users +WITH admin_orgs AS ( + INSERT INTO "organization" ("id", "name", "slug", "createdAt", "ownerId") + SELECT + gen_random_uuid(), + 'My Organization', + concat('org/', u."userId"), + NOW(), + u."userId" + FROM "user" u + WHERE u."role" = 'admin' + RETURNING * +) +-- Migrate regular users +UPDATE "user" u +SET + "email" = a."email", + "emailVerified" = true, + "role" = 'user', + "token" = a."token", + "canCreateProjects" = usr."canCreateProjects", + "canAccessToSSHKeys" = usr."canAccessToSSHKeys" +FROM "auth" a +INNER JOIN "user" usr ON a."id" = usr."userId" +WHERE a."id" = u."userId" +AND NOT EXISTS ( + SELECT 1 FROM "admin" adm WHERE a."id" = adm."adminId" +); + +-- Create accounts for regular users +INSERT INTO "account" ("id", "accountId", "providerId", "password", "userId", "createdAt", "updatedAt") +SELECT + gen_random_uuid(), + a."id", + 'credentials', + a."password", + u."userId", + NOW(), + NOW() +FROM "auth" a +INNER JOIN "user" u ON a."email" = u."email" +WHERE u."role" = 'user'; + +-- Create member relationships +INSERT INTO "member" ("id", "organizationId", "role", "userId", "createdAt") +SELECT + gen_random_uuid(), + o."id", + 'user', + u."userId", + NOW() +FROM "user" usr +INNER JOIN "user" u ON usr."userId" = u."userId" +INNER JOIN "admin" adm ON usr."adminId" = adm."adminId" +INNER JOIN "user" admin_u ON adm."adminId" = admin_u."userId" +INNER JOIN "organization" o ON o."ownerId" = admin_u."userId" +WHERE u."role" = 'user'; + +-- Drop old tables (after all data is migrated) +DROP TABLE IF EXISTS "sessionTable" CASCADE; +DROP TABLE IF EXISTS "admin" CASCADE; +DROP TABLE IF EXISTS "auth" CASCADE; \ No newline at end of file diff --git a/apps/dokploy/drizzle/meta/0066_snapshot.json b/apps/dokploy/drizzle/meta/0066_snapshot.json index 83cdfe7e..3ffb0dbf 100644 --- a/apps/dokploy/drizzle/meta/0066_snapshot.json +++ b/apps/dokploy/drizzle/meta/0066_snapshot.json @@ -1,5 +1,5 @@ { - "id": "5963096c-1a97-4e26-9f3f-f8613a359596", + "id": "d76ab830-b647-4e53-b6cc-0cf515968758", "prevId": "1240ec96-1751-4de3-b64f-cef9cb716786", "version": "7", "dialect": "postgresql", @@ -735,8 +735,8 @@ "name": "user", "schema": "", "columns": { - "id": { - "name": "id", + "userId": { + "name": "userId", "type": "text", "primaryKey": true, "notNull": true @@ -1172,15 +1172,15 @@ }, "indexes": {}, "foreignKeys": { - "project_userId_user_id_fk": { - "name": "project_userId_user_id_fk", + "project_userId_user_userId_fk": { + "name": "project_userId_user_userId_fk", "tableFrom": "project", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -2042,15 +2042,15 @@ }, "indexes": {}, "foreignKeys": { - "destination_userId_user_id_fk": { - "name": "destination_userId_user_id_fk", + "destination_userId_user_userId_fk": { + "name": "destination_userId_user_userId_fk", "tableFrom": "destination", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -2456,15 +2456,15 @@ }, "indexes": {}, "foreignKeys": { - "certificate_userId_user_id_fk": { - "name": "certificate_userId_user_id_fk", + "certificate_userId_user_userId_fk": { + "name": "certificate_userId_user_userId_fk", "tableFrom": "certificate", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -2513,12 +2513,6 @@ "primaryKey": false, "notNull": true }, - "token": { - "name": "token", - "type": "text", - "primaryKey": false, - "notNull": true - }, "created_at": { "name": "created_at", "type": "timestamp", @@ -2564,30 +2558,22 @@ }, "indexes": {}, "foreignKeys": { - "session_user_id_user_id_fk": { - "name": "session_user_id_user_id_fk", + "session_user_id_user_userId_fk": { + "name": "session_user_id_user_userId_fk", "tableFrom": "session", "tableTo": "user", "columnsFrom": [ "user_id" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "no action", "onUpdate": "no action" } }, "compositePrimaryKeys": {}, - "uniqueConstraints": { - "session_token_unique": { - "name": "session_token_unique", - "nullsNotDistinct": false, - "columns": [ - "token" - ] - } - }, + "uniqueConstraints": {}, "policies": {}, "checkConstraints": {}, "isRLSEnabled": false @@ -3322,15 +3308,15 @@ }, "indexes": {}, "foreignKeys": { - "registry_userId_user_id_fk": { - "name": "registry_userId_user_id_fk", + "registry_userId_user_userId_fk": { + "name": "registry_userId_user_userId_fk", "tableFrom": "registry", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -3647,15 +3633,15 @@ "onDelete": "cascade", "onUpdate": "no action" }, - "notification_userId_user_id_fk": { - "name": "notification_userId_user_id_fk", + "notification_userId_user_userId_fk": { + "name": "notification_userId_user_userId_fk", "tableFrom": "notification", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -3785,15 +3771,15 @@ }, "indexes": {}, "foreignKeys": { - "ssh-key_userId_user_id_fk": { - "name": "ssh-key_userId_user_id_fk", + "ssh-key_userId_user_userId_fk": { + "name": "ssh-key_userId_user_userId_fk", "tableFrom": "ssh-key", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -3844,15 +3830,15 @@ }, "indexes": {}, "foreignKeys": { - "git_provider_userId_user_id_fk": { - "name": "git_provider_userId_user_id_fk", + "git_provider_userId_user_userId_fk": { + "name": "git_provider_userId_user_userId_fk", "tableFrom": "git_provider", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -4187,15 +4173,15 @@ }, "indexes": {}, "foreignKeys": { - "server_userId_user_id_fk": { - "name": "server_userId_user_id_fk", + "server_userId_user_userId_fk": { + "name": "server_userId_user_userId_fk", "tableFrom": "server", "tableTo": "user", "columnsFrom": [ "userId" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "cascade", "onUpdate": "no action" @@ -4464,15 +4450,15 @@ }, "indexes": {}, "foreignKeys": { - "account_user_id_user_id_fk": { - "name": "account_user_id_user_id_fk", + "account_user_id_user_userId_fk": { + "name": "account_user_id_user_userId_fk", "tableFrom": "account", "tableTo": "user", "columnsFrom": [ "user_id" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "no action", "onUpdate": "no action" @@ -4546,15 +4532,15 @@ "onDelete": "no action", "onUpdate": "no action" }, - "invitation_inviter_id_user_id_fk": { - "name": "invitation_inviter_id_user_id_fk", + "invitation_inviter_id_user_userId_fk": { + "name": "invitation_inviter_id_user_userId_fk", "tableFrom": "invitation", "tableTo": "user", "columnsFrom": [ "inviter_id" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "no action", "onUpdate": "no action" @@ -4616,15 +4602,15 @@ "onDelete": "no action", "onUpdate": "no action" }, - "member_user_id_user_id_fk": { - "name": "member_user_id_user_id_fk", + "member_user_id_user_userId_fk": { + "name": "member_user_id_user_userId_fk", "tableFrom": "member", "tableTo": "user", "columnsFrom": [ "user_id" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "no action", "onUpdate": "no action" @@ -4685,15 +4671,15 @@ }, "indexes": {}, "foreignKeys": { - "organization_owner_id_user_id_fk": { - "name": "organization_owner_id_user_id_fk", + "organization_owner_id_user_userId_fk": { + "name": "organization_owner_id_user_userId_fk", "tableFrom": "organization", "tableTo": "user", "columnsFrom": [ "owner_id" ], "columnsTo": [ - "id" + "userId" ], "onDelete": "no action", "onUpdate": "no action" diff --git a/apps/dokploy/drizzle/meta/_journal.json b/apps/dokploy/drizzle/meta/_journal.json index 8c06b637..d3de7564 100644 --- a/apps/dokploy/drizzle/meta/_journal.json +++ b/apps/dokploy/drizzle/meta/_journal.json @@ -467,8 +467,8 @@ { "idx": 66, "version": "7", - "when": 1739168611366, - "tag": "0066_calm_vengeance", + "when": 1739173929725, + "tag": "0066_soft_kronos", "breakpoints": true } ] diff --git a/packages/server/auth-schema.ts b/packages/server/auth-schema.ts index 8b09e336..3e19bebe 100644 --- a/packages/server/auth-schema.ts +++ b/packages/server/auth-schema.ts @@ -26,7 +26,7 @@ export const session = pgTable("session", { userAgent: text("user_agent"), userId: text("user_id") .notNull() - .references(() => user.id), + .references(() => user.userId), activeOrganizationId: text("active_organization_id"), }); @@ -36,7 +36,7 @@ export const account = pgTable("account", { providerId: text("provider_id").notNull(), userId: text("user_id") .notNull() - .references(() => user.id), + .references(() => user.userId), accessToken: text("access_token"), refreshToken: text("refresh_token"), idToken: text("id_token"), @@ -66,7 +66,7 @@ export const organization = pgTable("organization", { metadata: text("metadata"), ownerId: text("owner_id") .notNull() - .references(() => user.id), + .references(() => user.userId), }); export const member = pgTable("member", { @@ -76,7 +76,7 @@ export const member = pgTable("member", { .references(() => organization.id), userId: text("user_id") .notNull() - .references(() => user.id), + .references(() => user.userId), role: text("role").notNull(), createdAt: timestamp("created_at").notNull(), }); @@ -92,5 +92,5 @@ export const invitation = pgTable("invitation", { expiresAt: timestamp("expires_at").notNull(), inviterId: text("inviter_id") .notNull() - .references(() => user.id), + .references(() => user.userId), }); diff --git a/packages/server/src/db/schema/account.ts b/packages/server/src/db/schema/account.ts index ce963a0d..24350c55 100644 --- a/packages/server/src/db/schema/account.ts +++ b/packages/server/src/db/schema/account.ts @@ -7,7 +7,7 @@ export const account = pgTable("account", { providerId: text("provider_id").notNull(), userId: text("user_id") .notNull() - .references(() => user.id), + .references(() => user.userId), accessToken: text("access_token"), refreshToken: text("refresh_token"), idToken: text("id_token"), @@ -42,7 +42,7 @@ export const organization = pgTable("organization", { metadata: text("metadata"), ownerId: text("owner_id") .notNull() - .references(() => user.id), + .references(() => user.userId), }); export const member = pgTable("member", { @@ -52,7 +52,7 @@ export const member = pgTable("member", { .references(() => organization.id), userId: text("user_id") .notNull() - .references(() => user.id), + .references(() => user.userId), role: text("role").notNull(), createdAt: timestamp("created_at").notNull(), }); @@ -68,5 +68,5 @@ export const invitation = pgTable("invitation", { expiresAt: timestamp("expires_at").notNull(), inviterId: text("inviter_id") .notNull() - .references(() => user.id), + .references(() => user.userId), }); diff --git a/packages/server/src/db/schema/certificate.ts b/packages/server/src/db/schema/certificate.ts index 3a7ec596..c1a57a5a 100644 --- a/packages/server/src/db/schema/certificate.ts +++ b/packages/server/src/db/schema/certificate.ts @@ -20,7 +20,7 @@ export const certificates = pgTable("certificate", { .$defaultFn(() => generateAppName("certificate")) .unique(), autoRenew: boolean("autoRenew"), - userId: text("userId").references(() => user.id, { + userId: text("userId").references(() => user.userId, { onDelete: "cascade", }), serverId: text("serverId").references(() => server.serverId, { diff --git a/packages/server/src/db/schema/destination.ts b/packages/server/src/db/schema/destination.ts index 37faf0fa..1fe48a34 100644 --- a/packages/server/src/db/schema/destination.ts +++ b/packages/server/src/db/schema/destination.ts @@ -22,7 +22,7 @@ export const destinations = pgTable("destination", { endpoint: text("endpoint").notNull(), userId: text("userId") .notNull() - .references(() => user.id, { onDelete: "cascade" }), + .references(() => user.userId, { onDelete: "cascade" }), }); export const destinationsRelations = relations( diff --git a/packages/server/src/db/schema/git-provider.ts b/packages/server/src/db/schema/git-provider.ts index f044b0a0..3a3bade8 100644 --- a/packages/server/src/db/schema/git-provider.ts +++ b/packages/server/src/db/schema/git-provider.ts @@ -25,7 +25,7 @@ export const gitProvider = pgTable("git_provider", { createdAt: text("createdAt") .notNull() .$defaultFn(() => new Date().toISOString()), - userId: text("userId").references(() => user.id, { + userId: text("userId").references(() => user.userId, { onDelete: "cascade", }), }); diff --git a/packages/server/src/db/schema/notification.ts b/packages/server/src/db/schema/notification.ts index 5174c17c..1771a92a 100644 --- a/packages/server/src/db/schema/notification.ts +++ b/packages/server/src/db/schema/notification.ts @@ -45,7 +45,7 @@ export const notifications = pgTable("notification", { gotifyId: text("gotifyId").references(() => gotify.gotifyId, { onDelete: "cascade", }), - userId: text("userId").references(() => user.id, { + userId: text("userId").references(() => user.userId, { onDelete: "cascade", }), }); diff --git a/packages/server/src/db/schema/project.ts b/packages/server/src/db/schema/project.ts index 2602a8ec..d83936a3 100644 --- a/packages/server/src/db/schema/project.ts +++ b/packages/server/src/db/schema/project.ts @@ -26,7 +26,7 @@ export const projects = pgTable("project", { .$defaultFn(() => new Date().toISOString()), userId: text("userId") .notNull() - .references(() => user.id, { onDelete: "cascade" }), + .references(() => user.userId, { onDelete: "cascade" }), env: text("env").notNull().default(""), }); diff --git a/packages/server/src/db/schema/registry.ts b/packages/server/src/db/schema/registry.ts index 166168c5..936f65fc 100644 --- a/packages/server/src/db/schema/registry.ts +++ b/packages/server/src/db/schema/registry.ts @@ -30,7 +30,7 @@ export const registry = pgTable("registry", { registryType: registryType("selfHosted").notNull().default("cloud"), userId: text("userId") .notNull() - .references(() => user.id, { onDelete: "cascade" }), + .references(() => user.userId, { onDelete: "cascade" }), }); export const registryRelations = relations(registry, ({ one, many }) => ({ diff --git a/packages/server/src/db/schema/server.ts b/packages/server/src/db/schema/server.ts index e3a14f95..4e238e23 100644 --- a/packages/server/src/db/schema/server.ts +++ b/packages/server/src/db/schema/server.ts @@ -46,7 +46,7 @@ export const server = pgTable("server", { .$defaultFn(() => new Date().toISOString()), userId: text("userId") .notNull() - .references(() => user.id, { onDelete: "cascade" }), + .references(() => user.userId, { onDelete: "cascade" }), serverStatus: serverStatus("serverStatus").notNull().default("active"), command: text("command").notNull().default(""), sshKeyId: text("sshKeyId").references(() => sshKeys.sshKeyId, { diff --git a/packages/server/src/db/schema/session.ts b/packages/server/src/db/schema/session.ts index 66a410b3..d5932764 100644 --- a/packages/server/src/db/schema/session.ts +++ b/packages/server/src/db/schema/session.ts @@ -1,3 +1,4 @@ +import { sql } from "drizzle-orm"; import { pgTable, text, timestamp } from "drizzle-orm/pg-core"; import { user } from "./user"; @@ -5,14 +6,14 @@ import { user } from "./user"; export const session = pgTable("session", { id: text("id").primaryKey(), expiresAt: timestamp("expires_at").notNull(), - token: text("token").notNull().unique(), + // token: text("token").notNull().unique().default(sql`gen_random_uuid()`), createdAt: timestamp("created_at").notNull(), updatedAt: timestamp("updated_at").notNull(), ipAddress: text("ip_address"), userAgent: text("user_agent"), userId: text("user_id") .notNull() - .references(() => user.id), + .references(() => user.userId), impersonatedBy: text("impersonated_by"), activeOrganizationId: text("active_organization_id"), }); diff --git a/packages/server/src/db/schema/ssh-key.ts b/packages/server/src/db/schema/ssh-key.ts index 6c5ba0a7..9bb69fee 100644 --- a/packages/server/src/db/schema/ssh-key.ts +++ b/packages/server/src/db/schema/ssh-key.ts @@ -22,7 +22,7 @@ export const sshKeys = pgTable("ssh-key", { .notNull() .$defaultFn(() => new Date().toISOString()), lastUsedAt: text("lastUsedAt"), - userId: text("userId").references(() => user.id, { + userId: text("userId").references(() => user.userId, { onDelete: "cascade", }), }); diff --git a/packages/server/src/db/schema/user.ts b/packages/server/src/db/schema/user.ts index 886cedbb..00e16679 100644 --- a/packages/server/src/db/schema/user.ts +++ b/packages/server/src/db/schema/user.ts @@ -22,7 +22,7 @@ import { certificateType } from "./shared"; // OLD TABLE export const user = pgTable("user", { - id: text("id") + userId: text("userId") .notNull() .primaryKey() .$defaultFn(() => nanoid()),