refactor: update organization

This commit is contained in:
Mauricio Siu
2025-02-15 20:25:58 -06:00
parent 87b12ff6e9
commit 53ce5e57fa
16 changed files with 5776 additions and 82 deletions

View File

@@ -1,2 +0,0 @@
ALTER TABLE "project" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "project" ADD CONSTRAINT "project_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;

View File

@@ -0,0 +1,16 @@
ALTER TABLE "project" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "destination" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "certificate" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "registry" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "notification" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "ssh-key" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "git_provider" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "server" ADD COLUMN "organizationId" text;--> statement-breakpoint
ALTER TABLE "project" ADD CONSTRAINT "project_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "destination" ADD CONSTRAINT "destination_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "certificate" ADD CONSTRAINT "certificate_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "registry" ADD CONSTRAINT "registry_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notification" ADD CONSTRAINT "notification_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ssh-key" ADD CONSTRAINT "ssh-key_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "git_provider" ADD CONSTRAINT "git_provider_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "server" ADD CONSTRAINT "server_organizationId_organization_id_fk" FOREIGN KEY ("organizationId") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;

View File

@@ -1,5 +1,6 @@
-- Custom SQL migration file, put your code below! --
-- Primero, actualizamos los proyectos con la organización del usuario
-- Custom SQL migration file
-- Actualizar projects
UPDATE "project" p
SET "organizationId" = (
SELECT m."organization_id"
@@ -10,18 +11,132 @@ SET "organizationId" = (
)
WHERE p."organizationId" IS NULL;
-- Verificamos que todos los proyectos tengan una organización
-- Actualizar servers
UPDATE "server" s
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = s."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE s."organizationId" IS NULL;
-- Actualizar ssh-keys
UPDATE "ssh-key" k
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = k."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE k."organizationId" IS NULL;
-- Actualizar destinations
UPDATE "destination" d
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = d."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE d."organizationId" IS NULL;
-- Actualizar registry
UPDATE "registry" r
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = r."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE r."organizationId" IS NULL;
-- Actualizar notifications
UPDATE "notification" n
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = n."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE n."organizationId" IS NULL;
-- Actualizar certificates
UPDATE "certificate" c
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = c."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE c."organizationId" IS NULL;
-- Actualizar git_provider
UPDATE "git_provider" g
SET "organizationId" = (
SELECT m."organization_id"
FROM "member" m
WHERE m."user_id" = g."userId"
AND m."role" = 'owner'
LIMIT 1
)
WHERE g."organizationId" IS NULL;
-- Verificar que todos los recursos tengan una organización
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM "project"
WHERE "organizationId" IS NULL
SELECT 1 FROM "project" WHERE "organizationId" IS NULL
UNION ALL
SELECT 1 FROM "server" WHERE "organizationId" IS NULL
UNION ALL
SELECT 1 FROM "ssh-key" WHERE "organizationId" IS NULL
UNION ALL
SELECT 1 FROM "destination" WHERE "organizationId" IS NULL
UNION ALL
SELECT 1 FROM "registry" WHERE "organizationId" IS NULL
UNION ALL
SELECT 1 FROM "notification" WHERE "organizationId" IS NULL
UNION ALL
SELECT 1 FROM "certificate" WHERE "organizationId" IS NULL
UNION ALL
SELECT 1 FROM "git_provider" WHERE "organizationId" IS NULL
) THEN
RAISE EXCEPTION 'Hay proyectos sin organización asignada';
RAISE EXCEPTION 'Hay recursos sin organización asignada';
END IF;
END $$;
-- Hacemos organization_id NOT NULL después de la migración
ALTER TABLE "project"
ALTER COLUMN "organizationId" SET NOT NULL;
-- Hacer organization_id NOT NULL en todas las tablas
ALTER TABLE "project" ALTER COLUMN "organizationId" SET NOT NULL;
ALTER TABLE "server" ALTER COLUMN "organizationId" SET NOT NULL;
ALTER TABLE "ssh-key" ALTER COLUMN "organizationId" SET NOT NULL;
ALTER TABLE "destination" ALTER COLUMN "organizationId" SET NOT NULL;
ALTER TABLE "registry" ALTER COLUMN "organizationId" SET NOT NULL;
ALTER TABLE "notification" ALTER COLUMN "organizationId" SET NOT NULL;
ALTER TABLE "certificate" ALTER COLUMN "organizationId" SET NOT NULL;
ALTER TABLE "git_provider" ALTER COLUMN "organizationId" SET NOT NULL;
-- Crear índices para mejorar el rendimiento de búsquedas por organización
CREATE INDEX IF NOT EXISTS "idx_project_organization" ON "project" ("organizationId");
CREATE INDEX IF NOT EXISTS "idx_server_organization" ON "server" ("organizationId");
CREATE INDEX IF NOT EXISTS "idx_sshkey_organization" ON "ssh-key" ("organizationId");
CREATE INDEX IF NOT EXISTS "idx_destination_organization" ON "destination" ("organizationId");
CREATE INDEX IF NOT EXISTS "idx_registry_organization" ON "registry" ("organizationId");
CREATE INDEX IF NOT EXISTS "idx_notification_organization" ON "notification" ("organizationId");
CREATE INDEX IF NOT EXISTS "idx_certificate_organization" ON "certificate" ("organizationId");
CREATE INDEX IF NOT EXISTS "idx_git_provider_organization" ON "git_provider" ("organizationId");

View File

@@ -0,0 +1,32 @@
ALTER TABLE "project" DROP CONSTRAINT "project_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "destination" DROP CONSTRAINT "destination_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "certificate" DROP CONSTRAINT "certificate_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "registry" DROP CONSTRAINT "registry_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "notification" DROP CONSTRAINT "notification_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "ssh-key" DROP CONSTRAINT "ssh-key_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "git_provider" DROP CONSTRAINT "git_provider_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "server" DROP CONSTRAINT "server_userId_user_temp_id_fk";
--> statement-breakpoint
ALTER TABLE "project" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "destination" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "certificate" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "registry" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "notification" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "ssh-key" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "git_provider" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "server" ALTER COLUMN "organizationId" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "project" DROP COLUMN "userId";--> statement-breakpoint
ALTER TABLE "destination" DROP COLUMN "userId";--> statement-breakpoint
ALTER TABLE "certificate" DROP COLUMN "userId";--> statement-breakpoint
ALTER TABLE "registry" DROP COLUMN "userId";--> statement-breakpoint
ALTER TABLE "notification" DROP COLUMN "userId";--> statement-breakpoint
ALTER TABLE "ssh-key" DROP COLUMN "userId";--> statement-breakpoint
ALTER TABLE "git_provider" DROP COLUMN "userId";--> statement-breakpoint
ALTER TABLE "server" DROP COLUMN "userId";

View File

@@ -1,5 +1,5 @@
{
"id": "cd06dbbf-61cb-4aba-b096-49a1850ff32b",
"id": "8e8626de-fb34-4c11-b9d0-b7a958993fb7",
"prevId": "e0842a94-530d-4a3c-a6b1-16cf37618b8b",
"version": "7",
"dialect": "postgresql",
@@ -2352,6 +2352,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -2368,6 +2374,19 @@
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"destination_organizationId_organization_id_fk": {
"name": "destination_organizationId_organization_id_fk",
"tableFrom": "destination",
"tableTo": "organization",
"columnsFrom": [
"organizationId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
@@ -2761,6 +2780,12 @@
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
},
"serverId": {
"name": "serverId",
"type": "text",
@@ -2783,6 +2808,19 @@
"onDelete": "cascade",
"onUpdate": "no action"
},
"certificate_organizationId_organization_id_fk": {
"name": "certificate_organizationId_organization_id_fk",
"tableFrom": "certificate",
"tableTo": "organization",
"columnsFrom": [
"organizationId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"certificate_serverId_server_serverId_fk": {
"name": "certificate_serverId_server_serverId_fk",
"tableFrom": "certificate",
@@ -3677,6 +3715,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -3693,6 +3737,19 @@
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"registry_organizationId_organization_id_fk": {
"name": "registry_organizationId_organization_id_fk",
"tableFrom": "registry",
"tableTo": "organization",
"columnsFrom": [
"organizationId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
@@ -3937,6 +3994,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -4018,6 +4081,19 @@
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"notification_organizationId_organization_id_fk": {
"name": "notification_organizationId_organization_id_fk",
"tableFrom": "notification",
"tableTo": "organization",
"columnsFrom": [
"organizationId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
@@ -4140,6 +4216,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -4156,6 +4238,19 @@
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"ssh-key_organizationId_organization_id_fk": {
"name": "ssh-key_organizationId_organization_id_fk",
"tableFrom": "ssh-key",
"tableTo": "organization",
"columnsFrom": [
"organizationId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
@@ -4199,6 +4294,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -4215,6 +4316,19 @@
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"git_provider_organizationId_organization_id_fk": {
"name": "git_provider_organizationId_organization_id_fk",
"tableFrom": "git_provider",
"tableTo": "organization",
"columnsFrom": [
"organizationId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
@@ -4515,6 +4629,12 @@
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
},
"serverStatus": {
"name": "serverStatus",
"type": "serverStatus",
@@ -4559,6 +4679,19 @@
"onDelete": "cascade",
"onUpdate": "no action"
},
"server_organizationId_organization_id_fk": {
"name": "server_organizationId_organization_id_fk",
"tableFrom": "server",
"tableTo": "organization",
"columnsFrom": [
"organizationId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"server_sshKeyId_ssh-key_sshKeyId_fk": {
"name": "server_sshKeyId_ssh-key_sshKeyId_fk",
"tableFrom": "server",

View File

@@ -1,6 +1,6 @@
{
"id": "30a4b462-2f38-42ae-8291-f6a2ce6d0bb4",
"prevId": "cd06dbbf-61cb-4aba-b096-49a1850ff32b",
"id": "11cb27c0-0f5a-4ec9-98be-5b6f7c5e7799",
"prevId": "8e8626de-fb34-4c11-b9d0-b7a958993fb7",
"version": "7",
"dialect": "postgresql",
"tables": {
@@ -2352,6 +2352,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -2368,6 +2374,19 @@
],
"onUpdate": "no action",
"onDelete": "cascade"
},
"destination_organizationId_organization_id_fk": {
"name": "destination_organizationId_organization_id_fk",
"tableFrom": "destination",
"columnsFrom": [
"organizationId"
],
"tableTo": "organization",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "cascade"
}
},
"compositePrimaryKeys": {},
@@ -2761,6 +2780,12 @@
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
},
"serverId": {
"name": "serverId",
"type": "text",
@@ -2783,6 +2808,19 @@
"onUpdate": "no action",
"onDelete": "cascade"
},
"certificate_organizationId_organization_id_fk": {
"name": "certificate_organizationId_organization_id_fk",
"tableFrom": "certificate",
"columnsFrom": [
"organizationId"
],
"tableTo": "organization",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "cascade"
},
"certificate_serverId_server_serverId_fk": {
"name": "certificate_serverId_server_serverId_fk",
"tableFrom": "certificate",
@@ -3677,6 +3715,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -3693,6 +3737,19 @@
],
"onUpdate": "no action",
"onDelete": "cascade"
},
"registry_organizationId_organization_id_fk": {
"name": "registry_organizationId_organization_id_fk",
"tableFrom": "registry",
"columnsFrom": [
"organizationId"
],
"tableTo": "organization",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "cascade"
}
},
"compositePrimaryKeys": {},
@@ -3937,6 +3994,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -4018,6 +4081,19 @@
],
"onUpdate": "no action",
"onDelete": "cascade"
},
"notification_organizationId_organization_id_fk": {
"name": "notification_organizationId_organization_id_fk",
"tableFrom": "notification",
"columnsFrom": [
"organizationId"
],
"tableTo": "organization",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "cascade"
}
},
"compositePrimaryKeys": {},
@@ -4140,6 +4216,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -4156,6 +4238,19 @@
],
"onUpdate": "no action",
"onDelete": "cascade"
},
"ssh-key_organizationId_organization_id_fk": {
"name": "ssh-key_organizationId_organization_id_fk",
"tableFrom": "ssh-key",
"columnsFrom": [
"organizationId"
],
"tableTo": "organization",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "cascade"
}
},
"compositePrimaryKeys": {},
@@ -4199,6 +4294,12 @@
"type": "text",
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
@@ -4215,6 +4316,19 @@
],
"onUpdate": "no action",
"onDelete": "cascade"
},
"git_provider_organizationId_organization_id_fk": {
"name": "git_provider_organizationId_organization_id_fk",
"tableFrom": "git_provider",
"columnsFrom": [
"organizationId"
],
"tableTo": "organization",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "cascade"
}
},
"compositePrimaryKeys": {},
@@ -4515,6 +4629,12 @@
"primaryKey": false,
"notNull": true
},
"organizationId": {
"name": "organizationId",
"type": "text",
"primaryKey": false,
"notNull": false
},
"serverStatus": {
"name": "serverStatus",
"type": "serverStatus",
@@ -4559,6 +4679,19 @@
"onUpdate": "no action",
"onDelete": "cascade"
},
"server_organizationId_organization_id_fk": {
"name": "server_organizationId_organization_id_fk",
"tableFrom": "server",
"columnsFrom": [
"organizationId"
],
"tableTo": "organization",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "cascade"
},
"server_sshKeyId_ssh-key_sshKeyId_fk": {
"name": "server_sshKeyId_ssh-key_sshKeyId_fk",
"tableFrom": "server",

File diff suppressed because it is too large Load Diff

View File

@@ -495,16 +495,23 @@
{
"idx": 70,
"version": "7",
"when": 1739671371444,
"tag": "0070_dusty_wind_dancer",
"when": 1739671869809,
"tag": "0070_nervous_vivisector",
"breakpoints": true
},
{
"idx": 71,
"version": "7",
"when": 1739671387634,
"when": 1739671878698,
"tag": "0071_migrate-data-projects",
"breakpoints": true
},
{
"idx": 72,
"version": "7",
"when": 1739672367223,
"tag": "0072_lazy_pixie",
"breakpoints": true
}
]
}