mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor: update
This commit is contained in:
2
apps/dokploy/drizzle/0070_dusty_wind_dancer.sql
Normal file
2
apps/dokploy/drizzle/0070_dusty_wind_dancer.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
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;
|
||||
27
apps/dokploy/drizzle/0071_migrate-data-projects.sql
Normal file
27
apps/dokploy/drizzle/0071_migrate-data-projects.sql
Normal file
@@ -0,0 +1,27 @@
|
||||
-- Custom SQL migration file, put your code below! --
|
||||
-- Primero, actualizamos los proyectos con la organización del usuario
|
||||
UPDATE "project" p
|
||||
SET "organizationId" = (
|
||||
SELECT m."organization_id"
|
||||
FROM "member" m
|
||||
WHERE m."user_id" = p."userId"
|
||||
AND m."role" = 'owner'
|
||||
LIMIT 1
|
||||
)
|
||||
WHERE p."organizationId" IS NULL;
|
||||
|
||||
-- Verificamos que todos los proyectos tengan una organización
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM "project"
|
||||
WHERE "organizationId" IS NULL
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Hay proyectos 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;
|
||||
5299
apps/dokploy/drizzle/meta/0070_snapshot.json
Normal file
5299
apps/dokploy/drizzle/meta/0070_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
5299
apps/dokploy/drizzle/meta/0071_snapshot.json
Normal file
5299
apps/dokploy/drizzle/meta/0071_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -491,6 +491,20 @@
|
||||
"when": 1739664410814,
|
||||
"tag": "0069_broad_ken_ellis",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 70,
|
||||
"version": "7",
|
||||
"when": 1739671371444,
|
||||
"tag": "0070_dusty_wind_dancer",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 71,
|
||||
"version": "7",
|
||||
"when": 1739671387634,
|
||||
"tag": "0071_migrate-data-projects",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -124,3 +124,27 @@ await db
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
await db
|
||||
.transaction(async (db) => {
|
||||
const projects = await db.query.projects.findMany({
|
||||
with: {
|
||||
user: {
|
||||
with: {
|
||||
organizations: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
for (const project of projects) {
|
||||
const user = await db.update(schema.projects).set({
|
||||
organizationId: project.user.organizations[0]?.id || "",
|
||||
});
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
console.log("Migration finished");
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user