mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-18 01:45:17 +00:00
feat(environment): introduce isDefault flag for environments
- Added `isDefault` boolean column to the environment schema, defaulting to false. - Updated environment creation and deletion logic to handle default environments, allowing the production environment to be created and renamed. - Enhanced error handling for environment updates and deletions to prevent modifications to default environments. - Updated UI components to reflect changes in environment selection based on the new default logic.
This commit is contained in:
@@ -102,7 +102,9 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
setName("");
|
setName("");
|
||||||
setDescription("");
|
setDescription("");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error("Failed to create environment");
|
toast.error(
|
||||||
|
`Failed to create environment: ${error instanceof Error ? error.message : error}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -123,7 +125,9 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
setName("");
|
setName("");
|
||||||
setDescription("");
|
setDescription("");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error("Failed to update environment");
|
toast.error(
|
||||||
|
`Failed to update environment: ${error instanceof Error ? error.message : error}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -140,15 +144,18 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
setIsDeleteDialogOpen(false);
|
setIsDeleteDialogOpen(false);
|
||||||
setSelectedEnvironment(null);
|
setSelectedEnvironment(null);
|
||||||
|
|
||||||
// Redirect to production if we deleted the current environment
|
// Redirect to first available environment if we deleted the current environment
|
||||||
if (selectedEnvironment.environmentId === currentEnvironmentId) {
|
if (selectedEnvironment.environmentId === currentEnvironmentId) {
|
||||||
const productionEnv = environments?.find(
|
const firstEnv = environments?.find(
|
||||||
(env) => env.name === "production",
|
(env) => env.environmentId !== selectedEnvironment.environmentId,
|
||||||
);
|
);
|
||||||
if (productionEnv) {
|
if (firstEnv) {
|
||||||
router.push(
|
router.push(
|
||||||
`/dashboard/project/${projectId}/environment/${productionEnv.environmentId}`,
|
`/dashboard/project/${projectId}/environment/${firstEnv.environmentId}`,
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
// No other environments, redirect to project page
|
||||||
|
router.push(`/dashboard/project/${projectId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -239,8 +246,8 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
{environment.name !== "production" && (
|
<div className="flex items-center gap-1 px-2">
|
||||||
<div className="flex items-center gap-1 px-2">
|
{!environment.isDefault && (
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
@@ -252,22 +259,21 @@ export const AdvancedEnvironmentSelector = ({
|
|||||||
>
|
>
|
||||||
<PencilIcon className="h-3 w-3" />
|
<PencilIcon className="h-3 w-3" />
|
||||||
</Button>
|
</Button>
|
||||||
|
)}
|
||||||
{canDeleteEnvironments && (
|
{canDeleteEnvironments && !environment.isDefault && (
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
className="h-6 w-6 p-0 text-red-600 hover:text-red-700"
|
className="h-6 w-6 p-0 text-red-600 hover:text-red-700"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
openDeleteDialog(environment);
|
openDeleteDialog(environment);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<TrashIcon className="h-3 w-3" />
|
<TrashIcon className="h-3 w-3" />
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -89,24 +89,26 @@ export const SearchCommand = () => {
|
|||||||
<CommandGroup heading={"Projects"}>
|
<CommandGroup heading={"Projects"}>
|
||||||
<CommandList>
|
<CommandList>
|
||||||
{data?.map((project) => {
|
{data?.map((project) => {
|
||||||
const productionEnvironment = project.environments.find(
|
// Find default environment, or fall back to first environment
|
||||||
(environment) => environment.name === "production",
|
const defaultEnvironment =
|
||||||
);
|
project.environments.find(
|
||||||
|
(environment) => environment.isDefault,
|
||||||
|
) || project?.environments?.[0];
|
||||||
|
|
||||||
if (!productionEnvironment) return null;
|
if (!defaultEnvironment) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CommandItem
|
<CommandItem
|
||||||
key={project.projectId}
|
key={project.projectId}
|
||||||
onSelect={() => {
|
onSelect={() => {
|
||||||
router.push(
|
router.push(
|
||||||
`/dashboard/project/${project.projectId}/environment/${productionEnvironment!.environmentId}`,
|
`/dashboard/project/${project.projectId}/environment/${defaultEnvironment.environmentId}`,
|
||||||
);
|
);
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<BookIcon className="size-4 text-muted-foreground mr-2" />
|
<BookIcon className="size-4 text-muted-foreground mr-2" />
|
||||||
{project.name} / {productionEnvironment!.name}
|
{project.name} / {defaultEnvironment.name}
|
||||||
</CommandItem>
|
</CommandItem>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
ALTER TABLE "environment" ADD COLUMN "isDefault" boolean DEFAULT false NOT NULL;
|
||||||
|
|
||||||
|
-- Set isDefault to true for existing production environments
|
||||||
|
UPDATE "environment" SET "isDefault" = true WHERE "name" = 'production';
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -925,6 +925,13 @@
|
|||||||
"when": 1765342621312,
|
"when": 1765342621312,
|
||||||
"tag": "0131_volatile_beast",
|
"tag": "0131_volatile_beast",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 132,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1765346573500,
|
||||||
|
"tag": "0132_clean_layla_miller",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -66,10 +66,12 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
if (input.name === "production") {
|
if (input.name === "production") {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "BAD_REQUEST",
|
code: "BAD_REQUEST",
|
||||||
message: "Environment name cannot be production",
|
message:
|
||||||
|
"You cannot create a environment with the name 'production'",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allow users to create environments with any name, including "production"
|
||||||
const environment = await createEnvironment(input);
|
const environment = await createEnvironment(input);
|
||||||
|
|
||||||
if (ctx.user.role === "member") {
|
if (ctx.user.role === "member") {
|
||||||
@@ -243,13 +245,7 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
try {
|
try {
|
||||||
const { environmentId, ...updateData } = input;
|
const { environmentId, ...updateData } = input;
|
||||||
|
|
||||||
if (updateData.name === "production") {
|
// Allow users to rename environments to any name, including "production"
|
||||||
throw new TRPCError({
|
|
||||||
code: "BAD_REQUEST",
|
|
||||||
message: "Environment name cannot be production",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ctx.user.role === "member") {
|
if (ctx.user.role === "member") {
|
||||||
await checkEnvironmentAccess(
|
await checkEnvironmentAccess(
|
||||||
ctx.user.id,
|
ctx.user.id,
|
||||||
@@ -259,6 +255,13 @@ export const environmentRouter = createTRPCRouter({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
const currentEnvironment = await findEnvironmentById(environmentId);
|
const currentEnvironment = await findEnvironmentById(environmentId);
|
||||||
|
|
||||||
|
if (currentEnvironment.isDefault) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message: "You cannot update the default environment",
|
||||||
|
});
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
currentEnvironment.project.organizationId !==
|
currentEnvironment.project.organizationId !==
|
||||||
ctx.session.activeOrganizationId
|
ctx.session.activeOrganizationId
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { relations } from "drizzle-orm";
|
import { relations } from "drizzle-orm";
|
||||||
import { pgTable, text } from "drizzle-orm/pg-core";
|
import { boolean, pgTable, text } from "drizzle-orm/pg-core";
|
||||||
import { createInsertSchema } from "drizzle-zod";
|
import { createInsertSchema } from "drizzle-zod";
|
||||||
import { nanoid } from "nanoid";
|
import { nanoid } from "nanoid";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
@@ -26,6 +26,7 @@ export const environments = pgTable("environment", {
|
|||||||
projectId: text("projectId")
|
projectId: text("projectId")
|
||||||
.notNull()
|
.notNull()
|
||||||
.references(() => projects.projectId, { onDelete: "cascade" }),
|
.references(() => projects.projectId, { onDelete: "cascade" }),
|
||||||
|
isDefault: boolean("isDefault").notNull().default(false),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const environmentRelations = relations(
|
export const environmentRelations = relations(
|
||||||
@@ -69,9 +70,14 @@ export const apiRemoveEnvironment = createSchema
|
|||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
export const apiUpdateEnvironment = createSchema.partial().extend({
|
export const apiUpdateEnvironment = createSchema
|
||||||
environmentId: z.string().min(1),
|
.partial()
|
||||||
});
|
.extend({
|
||||||
|
environmentId: z.string().min(1),
|
||||||
|
})
|
||||||
|
.omit({
|
||||||
|
isDefault: true,
|
||||||
|
});
|
||||||
|
|
||||||
export const apiDuplicateEnvironment = createSchema
|
export const apiDuplicateEnvironment = createSchema
|
||||||
.pick({
|
.pick({
|
||||||
|
|||||||
@@ -103,10 +103,10 @@ export const findEnvironmentsByProjectId = async (projectId: string) => {
|
|||||||
|
|
||||||
export const deleteEnvironment = async (environmentId: string) => {
|
export const deleteEnvironment = async (environmentId: string) => {
|
||||||
const currentEnvironment = await findEnvironmentById(environmentId);
|
const currentEnvironment = await findEnvironmentById(environmentId);
|
||||||
if (currentEnvironment.name === "production") {
|
if (currentEnvironment.isDefault) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "BAD_REQUEST",
|
code: "BAD_REQUEST",
|
||||||
message: "You cannot delete the production environment",
|
message: "You cannot delete the default environment",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const deletedEnvironment = await db
|
const deletedEnvironment = await db
|
||||||
@@ -162,9 +162,23 @@ export const duplicateEnvironment = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const createProductionEnvironment = async (projectId: string) => {
|
export const createProductionEnvironment = async (projectId: string) => {
|
||||||
return createEnvironment({
|
const newEnvironment = await db
|
||||||
name: "production",
|
.insert(environments)
|
||||||
description: "Production environment",
|
.values({
|
||||||
projectId,
|
name: "production",
|
||||||
});
|
description: "Production environment",
|
||||||
|
projectId,
|
||||||
|
isDefault: true,
|
||||||
|
})
|
||||||
|
.returning()
|
||||||
|
.then((value) => value[0]);
|
||||||
|
|
||||||
|
if (!newEnvironment) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message: "Error creating the production environment",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return newEnvironment;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user