Skip to content

Commit

Permalink
Merge branch 'main' into refactor/webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
abuaboud committed Mar 4, 2024
2 parents 5c86deb + 817dc3e commit c5939ac
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 65 deletions.
Expand Up @@ -16,6 +16,9 @@ export class MigrateWebhook1709581196563 implements MigrationInterface {
const [flowVersion] = await queryRunner.query('SELECT * FROM flow_version WHERE id = $1', [id])
const step = parseJson(flowVersion.trigger)
const isString = typeof flowVersion.trigger === 'string'
if (count % 1000 === 0) {
logger.info('MigrateWebhook1709581196563, flows migrated ' + count)
}
if (step.type === 'WEBHOOK') {
step.type = 'PIECE_TRIGGER'
step.settings = {
Expand Down
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { logger } from 'server-shared'

export class AddPlatformForeignKeyToProjectPostgres1709566642531 implements MigrationInterface {
name = 'AddPlatformForeignKeyToProjectPostgres1709566642531'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "project"
ADD CONSTRAINT "fk_project_platform_id" FOREIGN KEY ("platformId") REFERENCES "platform"("id")
ON DELETE RESTRICT ON UPDATE RESTRICT
`)

logger.info({ name: this.name }, 'up')
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "project" DROP CONSTRAINT "fk_project_platform_id"
`)

logger.info({ name: this.name }, 'down')
}

}
Expand Up @@ -12,8 +12,7 @@ export class MigrateWebhookTemplate1709581196564 implements MigrationInterface {

for (const { id } of flowVersionsIds) {
const [flowVersion] = await queryRunner.query('SELECT * FROM flow_template WHERE id = $1', [id])
const step = parseJson(flowVersion.template.trigger)
const isString = typeof flowVersion.template.trigger === 'string'
const step = flowVersion.template.trigger
if (step.type === 'WEBHOOK') {
step.type = 'PIECE_TRIGGER'
step.settings = {
Expand All @@ -26,10 +25,13 @@ export class MigrateWebhookTemplate1709581196564 implements MigrationInterface {
'packageType': 'REGISTRY',
}
count++
const result = isString ? JSON.stringify(step) : step
const endResult = {
...flowVersion.template,
trigger: step,
}
await queryRunner.query(
'UPDATE flow_template SET template = $1 WHERE id = $2',
[result, flowVersion.id],
[endResult, flowVersion.id],
)
}
}
Expand All @@ -45,19 +47,21 @@ export class MigrateWebhookTemplate1709581196564 implements MigrationInterface {
for (const { id } of flowVersionsIds) {
const [flowVersion] = await queryRunner.query('SELECT * FROM flow_template WHERE id = $1', [id])

const step = parseJson(flowVersion.template.trigger)
const isString = typeof flowVersion.template.trigger === 'string'
const step = flowVersion.template.trigger
if (step.type === 'PIECE_TRIGGER') {
if (step.settings.pieceName === '@activepieces/piece-webhook') {
step.type = 'WEBHOOK'
step.settings = {
'inputUiInfo': step.settings.inputUiInfo,
}
count++
const result = isString ? JSON.stringify(step) : step
const endResult = {
...flowVersion.template,
trigger: step,
}
await queryRunner.query(
'UPDATE flow_template SET template = $1 WHERE id = $2',
[result, flowVersion.id],
[endResult, flowVersion.id],
)
}
}
Expand All @@ -67,13 +71,3 @@ export class MigrateWebhookTemplate1709581196564 implements MigrationInterface {
)
}
}


const parseJson = (json: string) => {
try {
return JSON.parse(json)
}
catch (e) {
return json
}
}
@@ -0,0 +1,124 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { logger } from 'server-shared'

export class AddPlatformForeignKeyToProjectSqlite1709566629593 implements MigrationInterface {
name = 'AddPlatformForeignKeyToProjectSqlite1709566629593'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP INDEX "idx_project_owner_id"
`)
await queryRunner.query(`
DROP INDEX "idx_project_platform_id_external_id"
`)
await queryRunner.query(`
CREATE TABLE "temporary_project" (
"id" varchar(21) PRIMARY KEY NOT NULL,
"created" datetime NOT NULL DEFAULT (datetime('now')),
"updated" datetime NOT NULL DEFAULT (datetime('now')),
"ownerId" varchar(21) NOT NULL,
"displayName" varchar NOT NULL,
"notifyStatus" varchar NOT NULL,
"platformId" varchar(21) NOT NULL,
"externalId" varchar,
CONSTRAINT "fk_project_owner_id" FOREIGN KEY ("ownerId") REFERENCES "user" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT "fk_project_platform_id" FOREIGN KEY ("platformId") REFERENCES "platform" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT
)
`)
await queryRunner.query(`
INSERT INTO "temporary_project"(
"id",
"created",
"updated",
"ownerId",
"displayName",
"notifyStatus",
"platformId",
"externalId"
)
SELECT "id",
"created",
"updated",
"ownerId",
"displayName",
"notifyStatus",
"platformId",
"externalId"
FROM "project"
`)
await queryRunner.query(`
DROP TABLE "project"
`)
await queryRunner.query(`
ALTER TABLE "temporary_project"
RENAME TO "project"
`)
await queryRunner.query(`
CREATE INDEX "idx_project_owner_id" ON "project" ("ownerId")
`)
await queryRunner.query(`
CREATE UNIQUE INDEX "idx_project_platform_id_external_id" ON "project" ("platformId", "externalId")
`)

logger.info({ name: this.name }, 'up')
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP INDEX "idx_project_platform_id_external_id"
`)
await queryRunner.query(`
DROP INDEX "idx_project_owner_id"
`)
await queryRunner.query(`
ALTER TABLE "project"
RENAME TO "temporary_project"
`)
await queryRunner.query(`
CREATE TABLE "project" (
"id" varchar(21) PRIMARY KEY NOT NULL,
"created" datetime NOT NULL DEFAULT (datetime('now')),
"updated" datetime NOT NULL DEFAULT (datetime('now')),
"ownerId" varchar(21) NOT NULL,
"displayName" varchar NOT NULL,
"notifyStatus" varchar NOT NULL,
"platformId" varchar(21),
"externalId" varchar,
CONSTRAINT "fk_project_owner_id" FOREIGN KEY ("ownerId") REFERENCES "user" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
)
`)
await queryRunner.query(`
INSERT INTO "project"(
"id",
"created",
"updated",
"ownerId",
"displayName",
"notifyStatus",
"platformId",
"externalId"
)
SELECT "id",
"created",
"updated",
"ownerId",
"displayName",
"notifyStatus",
"platformId",
"externalId"
FROM "temporary_project"
`)
await queryRunner.query(`
DROP TABLE "temporary_project"
`)
await queryRunner.query(`
CREATE UNIQUE INDEX "idx_project_platform_id_external_id" ON "project" ("platformId", "externalId")
`)
await queryRunner.query(`
CREATE INDEX "idx_project_owner_id" ON "project" ("ownerId")
`)

logger.info({ name: this.name }, 'down')
}

}
8 changes: 6 additions & 2 deletions packages/server/api/src/app/database/postgres-connection.ts
Expand Up @@ -113,6 +113,7 @@ import { DropUnusedPlatformIndex1709500873378 } from './migration/postgres/17095
import { SetNotNullOnPlatform1709505632771 } from './migration/postgres/1709505632771-SetNotNullOnPlatform'
import { MigrateWebhook1709581196563 } from './migration/common/1709581196563-migrate-webhook'
import { MigrateWebhookTemplate1709581196564 } from './migration/postgres/1709581196564-migrate-webhook-templates'
import { AddPlatformForeignKeyToProjectPostgres1709566642531 } from './migration/postgres/1709566642531-add-platform-foreign-key-to-project-postgres'

const getSslConfig = (): boolean | TlsOptions => {
const useSsl = system.get(SystemProp.POSTGRES_USE_SSL)
Expand Down Expand Up @@ -240,6 +241,7 @@ const getMigrations = (): (new () => MigrationInterface)[] => {
AddSlugToGitRepo1709151540095,
DropUnusedPlatformIndex1709500873378,
MigrateWebhookTemplate1709581196564,
AddPlatformForeignKeyToProjectPostgres1709566642531,
)
break
case ApEdition.ENTERPRISE:
Expand Down Expand Up @@ -287,12 +289,14 @@ const getMigrations = (): (new () => MigrationInterface)[] => {
AddSlugToGitRepo1709151540095,
DropUnusedPlatformIndex1709500873378,
MigrateWebhookTemplate1709581196564,
AddPlatformForeignKeyToProjectPostgres1709566642531,
)
break
case ApEdition.COMMUNITY:
commonMigration.push(
AddPlatformToPostgres1709052740378,
SetNotNullOnPlatform1709505632771)
AddPlatformToPostgres1709052740378,
SetNotNullOnPlatform1709505632771,
)
break
}

Expand Down
3 changes: 3 additions & 0 deletions packages/server/api/src/app/database/sqlite-connection.ts
Expand Up @@ -34,6 +34,8 @@ import { AddCategoriesToPieceMetadata1707229986819 } from './migration/sqlite/17
import { AddUniqueStoreConstraint1708527446535 } from './migration/sqlite/1708527446535-AddUniqueStoreConstraint'
import { CreateDefaultPlaformSqlite1709051625110 } from './migration/sqlite/1709051625110-CreateDefaultPlaformSqlite'
import { MigrateWebhook1709581196563 } from './migration/common/1709581196563-migrate-webhook'
import { AddPlatformForeignKeyToProjectSqlite1709566629593 } from './migration/sqlite/1709566629593-add-platform-foreign-key-to-project-sqlite'


const getSqliteDatabaseFilePath = (): string => {
const apConfigDirectoryPath = system.getOrThrow(SystemProp.CONFIG_PATH)
Expand Down Expand Up @@ -85,6 +87,7 @@ const getMigrations = (): (new () => MigrationInterface)[] => {
AddUniqueStoreConstraint1708527446535,
CreateDefaultPlaformSqlite1709051625110,
MigrateWebhook1709581196563,
AddPlatformForeignKeyToProjectSqlite1709566629593,
]
const edition = getEdition()
if (edition !== ApEdition.COMMUNITY) {
Expand Down
27 changes: 19 additions & 8 deletions packages/server/api/src/app/project/project-entity.ts
Expand Up @@ -52,6 +52,25 @@ export const ProjectEntity = new EntitySchema<ProjectSchema>({
},
],
relations: {
owner: {
type: 'many-to-one',
target: 'user',
joinColumn: {
name: 'ownerId',
foreignKeyConstraintName: 'fk_project_owner_id',
},
},
platform: {
type: 'many-to-one',
target: 'platform',
cascade: true,
onDelete: 'RESTRICT',
onUpdate: 'RESTRICT',
joinColumn: {
name: 'platformId',
foreignKeyConstraintName: 'fk_project_platform_id',
},
},
folders: {
type: 'one-to-many',
target: 'folder',
Expand All @@ -62,14 +81,6 @@ export const ProjectEntity = new EntitySchema<ProjectSchema>({
target: 'app_connection',
inverseSide: 'project',
},
owner: {
type: 'many-to-one',
target: 'user',
joinColumn: {
name: 'ownerId',
foreignKeyConstraintName: 'fk_project_owner_id',
},
},
events: {
type: 'one-to-many',
target: 'trigger_event',
Expand Down
14 changes: 10 additions & 4 deletions packages/server/api/test/integration/ce/flows/flow.test.ts
Expand Up @@ -250,7 +250,7 @@ describe('Flow API', () => {
// arrange
const mockUser = createMockUser()
await databaseConnection.getRepository('user').save([mockUser])

const mockPlatform = createMockPlatform({ ownerId: mockUser.id })
await databaseConnection.getRepository('platform').save(mockPlatform)

Expand Down Expand Up @@ -327,7 +327,7 @@ describe('Flow API', () => {
platformId: mockPlatform.id,
})
await databaseConnection.getRepository('project').save([mockProject])

const mockEnabledFlow = createMockFlow({
projectId: mockProject.id,
status: FlowStatus.ENABLED,
Expand Down Expand Up @@ -381,7 +381,10 @@ describe('Flow API', () => {
const mockUser = createMockUser()
await databaseConnection.getRepository('user').save([mockUser])

const mockProject = createMockProject({ ownerId: mockUser.id })
const mockPlatform = createMockPlatform({ ownerId: mockUser.id })
await databaseConnection.getRepository('platform').save(mockPlatform)

const mockProject = createMockProject({ platformId: mockPlatform.id, ownerId: mockUser.id })
await databaseConnection.getRepository('project').save([mockProject])

const mockFlow = createMockFlow({ projectId: mockProject.id })
Expand Down Expand Up @@ -423,7 +426,10 @@ describe('Flow API', () => {
const mockUser = createMockUser()
await databaseConnection.getRepository('user').save([mockUser])

const mockProject = createMockProject({ ownerId: mockUser.id })
const mockPlatform = createMockPlatform({ ownerId: mockUser.id })
await databaseConnection.getRepository('platform').save(mockPlatform)

const mockProject = createMockProject({ platformId: mockPlatform.id, ownerId: mockUser.id })
await databaseConnection.getRepository('project').save([mockProject])

const mockFlow = createMockFlow({ projectId: mockProject.id })
Expand Down

0 comments on commit c5939ac

Please sign in to comment.