Skip to content

Commit

Permalink
refactor: project migration
Browse files Browse the repository at this point in the history
  • Loading branch information
abuaboud committed Dec 24, 2024
1 parent 94cd06c commit d127e2a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 93 deletions.
46 changes: 21 additions & 25 deletions packages/react-ui/src/app/routes/project-release/apply-plan.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMutation } from '@tanstack/react-query';
import { useState } from 'react';
import { ProjectReleaseType, DiffReleaseRequest } from '@activepieces/shared';
import { DiffReleaseRequest } from '@activepieces/shared';
import { INTERNAL_ERROR_TOAST, useToast } from '@/components/ui/use-toast';
import { projectReleaseApi } from '@/features/project-version/lib/project-release-api';
import { CreateReleaseDialog } from './create-release-dialog';
Expand All @@ -10,15 +10,17 @@ import { ReactNode } from 'react';
type ApplyButtonProps = ButtonProps & {
request: DiffReleaseRequest;
children: ReactNode;
onSuccess: () => void;
defaultName?: string;
};

export const useApplyPlan = ({ onSuccess }: { onSuccess: () => void }) => {
export const ApplyButton = ({ request, children, onSuccess, defaultName, ...props }: ApplyButtonProps) => {
const { toast } = useToast();
const [dialogOpen, setDialogOpen] = useState(false);
const [syncPlan, setSyncPlan] = useState<any>(null);
const [loadingRequestId, setLoadingRequestId] = useState<string | null>(null);

const { mutate: loadSyncPlan } = useMutation({
const { mutate: loadSyncPlan, isPending: isLoadingApplyPlan } = useMutation({
mutationFn: (request: DiffReleaseRequest) => projectReleaseApi.diff(request),
onSuccess: (plan) => {
setSyncPlan(plan);
Expand All @@ -31,22 +33,11 @@ export const useApplyPlan = ({ onSuccess }: { onSuccess: () => void }) => {
},
});

const ApplyPlanDialog = () => (
dialogOpen && (
<CreateReleaseDialog
open={dialogOpen}
setOpen={setDialogOpen}
refetch={onSuccess}
plan={syncPlan}
/>
)
);

const ApplyButton = ({ request, children, ...props }: ApplyButtonProps) => {
const requestId = JSON.stringify(request);
const isLoading = loadingRequestId === requestId;
const requestId = JSON.stringify(request);
const isLoading = loadingRequestId === requestId;

return (
return (
<>
<Button
{...props}
loading={isLoading}
Expand All @@ -57,11 +48,16 @@ export const useApplyPlan = ({ onSuccess }: { onSuccess: () => void }) => {
>
{children}
</Button>
);
};

return {
ApplyPlanDialog,
ApplyButton,
};
};
{dialogOpen && (
<CreateReleaseDialog
open={dialogOpen}
setOpen={setDialogOpen}
refetch={onSuccess}
plan={syncPlan}
defaultName={defaultName}
/>
)}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type GitReleaseDialogProps = {
setOpen: (open: boolean) => void;
refetch: () => void;
plan: ProjectSyncPlan | undefined;
defaultName?: string;
};

const formSchema = z.object({
Expand All @@ -48,6 +49,7 @@ const CreateReleaseDialog = ({
setOpen,
refetch,
plan,
defaultName = '',
}: GitReleaseDialogProps) => {
const [isApplyingChanges, setIsApplyingChanges] = useState(false);
const { platform } = platformHooks.useCurrentPlatform();
Expand All @@ -59,7 +61,7 @@ const CreateReleaseDialog = ({
const form = useForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
name: '',
name: defaultName,
description: '',
},
});
Expand Down
18 changes: 11 additions & 7 deletions packages/react-ui/src/app/routes/project-release/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ import {
ProjectReleaseType,
} from '@activepieces/shared';
import { DownloadButton } from './download-button';
import { useApplyPlan } from './apply-plan';
import { useState } from 'react';
import { ApplyButton } from './apply-plan';

const ProjectReleasesPage = () => {

const { data, isLoading, refetch } = useQuery({
queryKey: ['project-releases'],
queryFn: () => projectReleaseApi.list(),
});

const { ApplyButton, ApplyPlanDialog } = useApplyPlan({
onSuccess: refetch,
});
const [selectedRelease, setSelectedRelease] = useState<ProjectRelease | null>(null);
const [isLoadingApplyPlan, setIsLoadingApplyPlan] = useState(false);

const columns: ColumnDef<RowDataWithActions<ProjectRelease>>[] = [
{
Expand Down Expand Up @@ -99,11 +98,14 @@ const ProjectReleasesPage = () => {
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem className="cursor-pointer">
<DropdownMenuItem className="cursor-pointer" asChild>
<ApplyButton
loading={isLoadingApplyPlan}
variant="ghost"
onSuccess={refetch}
className="w-full justify-start"
request={{ type: ProjectReleaseType.GIT }}
onClick={() => setSelectedRelease(null)}
>
<div className="flex flex-row gap-2 items-center">
<Plus className="h-4 w-4" />
Expand All @@ -127,12 +129,15 @@ const ProjectReleasesPage = () => {
<Tooltip>
<TooltipTrigger asChild>
<ApplyButton
onSuccess={refetch}
variant="ghost"
className="size-8 p-0"
request={{
type: ProjectReleaseType.ROLLBACK,
projectReleaseId: row.id,
}}
defaultName={`Rollback ${row.name}`}
onClick={() => setSelectedRelease(row)}
>
<Undo2 className="size-4" />
</ApplyButton>
Expand All @@ -144,7 +149,6 @@ const ProjectReleasesPage = () => {
},
]}
/>
<ApplyPlanDialog />
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { ApEdition } from '@activepieces/shared'
import { MigrationInterface, QueryRunner } from 'typeorm'
import { isNotOneOfTheseEditions } from '../../database-common'

export class CreateProjectReleaseTable1734418823028 implements MigrationInterface {
name = 'CreateProjectReleaseTable1734418823028'

public async up(queryRunner: QueryRunner): Promise<void> {
if (isNotOneOfTheseEditions([ApEdition.CLOUD, ApEdition.ENTERPRISE])) {
return
}

await queryRunner.query(`
CREATE TABLE "project_release" (
"id" character varying(21) NOT NULL,
Expand Down Expand Up @@ -42,9 +38,6 @@ export class CreateProjectReleaseTable1734418823028 implements MigrationInterfac
}

public async down(queryRunner: QueryRunner): Promise<void> {
if (isNotOneOfTheseEditions([ApEdition.CLOUD, ApEdition.ENTERPRISE])) {
return
}
await queryRunner.query(`
ALTER TABLE "project_release" DROP CONSTRAINT "fk_project_release_file_id"
`)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export class AddMappingForProject1734886495968 implements MigrationInterface {
name = 'AddMappingForProject1734886495968'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "platform"
RENAME COLUMN "gitSyncEnabled" TO "environmentEnabled"
`)
await queryRunner.query(`
ALTER TABLE "project"
ADD "releasesEnabled" boolean NOT NULL DEFAULT false
`)
await queryRunner.query(`
ALTER TABLE "project"
ADD "mapping" jsonb
Expand All @@ -26,6 +34,13 @@ export class AddMappingForProject1734886495968 implements MigrationInterface {
public async down(queryRunner: QueryRunner): Promise<void> {
const hasGitRepoTable = await queryRunner.hasTable('git_repo')

await queryRunner.query(`
ALTER TABLE "platform"
RENAME COLUMN "environmentEnabled" TO "gitSyncEnabled"
`)
await queryRunner.query(`
ALTER TABLE "project" DROP COLUMN "releasesEnabled"
`)
await queryRunner.query(`
ALTER TABLE "project" DROP COLUMN "mapping"
`)
Expand Down
6 changes: 1 addition & 5 deletions packages/server/api/src/app/database/postgres-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ import { AddAuditLogIndicies1731711188507 } from './migration/postgres/173171118
import { AddIndiciesToRunAndTriggerData1732324567513 } from './migration/postgres/1732324567513-AddIndiciesToRunAndTriggerData'
import { AddProjectRelationInUserInvitation1732790412900 } from './migration/postgres/1732790673766-AddProjectRelationInUserInvitation'
import { CreateProjectReleaseTable1734418823028 } from './migration/postgres/1734418823028-CreateProjectReleaseTable'
import { AddReleasesEnablingBoolean1734429537542 } from './migration/postgres/1734429537542-AddReleasesEnablingBoolean'
import { RenameGitSyncToEnvironment1734431436773 } from './migration/postgres/1734431436773-RenameGitSyncToEnvironment'
import { RemoveWorkerType1734439097357 } from './migration/postgres/1734439097357-RemoveWorkerType'
import { AddCopilotSettings1734479886363 } from './migration/postgres/1734479886363-AddCopilotSettings'
import { AddMappingForProject1734886495968 } from './migration/postgres/1734886495968-AddMappingForProject'
Expand Down Expand Up @@ -283,9 +281,6 @@ const getMigrations = (): (new () => MigrationInterface)[] => {
AddGlobalConnectionsAndRbacForPlatform1731532843905,
AddIndiciesToRunAndTriggerData1732324567513,
AddProjectRelationInUserInvitation1732790412900,
CreateProjectReleaseTable1734418823028,
AddReleasesEnablingBoolean1734429537542,
RenameGitSyncToEnvironment1734431436773,
RemoveWorkerType1734439097357,
AddCopilotSettings1734479886363,
AddMappingForProject1734886495968,
Expand Down Expand Up @@ -364,6 +359,7 @@ const getMigrations = (): (new () => MigrationInterface)[] => {
MigrateAuditEventSchema1723489038729,
AddAiTokensForProjectPlan1726446092010,
AddAuditLogIndicies1731711188507,
CreateProjectReleaseTable1734418823028

)
break
Expand Down

0 comments on commit d127e2a

Please sign in to comment.