-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add migration to create model for folders feature (#13060)
- Loading branch information
1 parent
aedea7a
commit 03f4ed8
Showing
9 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
Column, | ||
Entity, | ||
JoinColumn, | ||
JoinTable, | ||
ManyToMany, | ||
ManyToOne, | ||
OneToMany, | ||
} from '@n8n/typeorm'; | ||
|
||
import { WithTimestampsAndStringId } from './abstract-entity'; | ||
import { Project } from './project'; | ||
import { TagEntity } from './tag-entity'; | ||
import { type WorkflowEntity } from './workflow-entity'; | ||
|
||
@Entity() | ||
export class Folder extends WithTimestampsAndStringId { | ||
@Column() | ||
name: string; | ||
|
||
@ManyToOne(() => Folder, { nullable: true }) | ||
@JoinColumn({ name: 'parentFolderId' }) | ||
parentFolder: Folder | null; | ||
|
||
@ManyToOne(() => Project) | ||
@JoinColumn({ name: 'projectId' }) | ||
project: Project; | ||
|
||
@OneToMany('WorkflowEntity', 'parentFolder') | ||
workflows: WorkflowEntity[]; | ||
|
||
@ManyToMany(() => TagEntity) | ||
@JoinTable({ | ||
name: 'folder_tag', | ||
joinColumn: { | ||
name: 'folderId', | ||
referencedColumnName: 'id', | ||
}, | ||
inverseJoinColumn: { | ||
name: 'tagId', | ||
referencedColumnName: 'id', | ||
}, | ||
}) | ||
tags: TagEntity[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/cli/src/databases/migrations/common/1738709609940-CreateFolderTable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { MigrationContext, ReversibleMigration } from '@/databases/types'; | ||
|
||
export class CreateFolderTable1738709609940 implements ReversibleMigration { | ||
async up({ runQuery, escape, schemaBuilder: { createTable, column } }: MigrationContext) { | ||
const workflowTable = escape.tableName('workflow_entity'); | ||
const workflowFolderId = escape.columnName('parentFolderId'); | ||
const folderTable = escape.tableName('folder'); | ||
const folderId = escape.columnName('id'); | ||
|
||
await createTable('folder') | ||
.withColumns( | ||
column('id').varchar(36).primary.notNull, | ||
column('name').varchar(128).notNull, | ||
column('parentFolderId').varchar(36).default(null), | ||
column('projectId').varchar(36).notNull, | ||
) | ||
.withForeignKey('projectId', { | ||
tableName: 'project', | ||
columnName: 'id', | ||
onDelete: 'CASCADE', | ||
}) | ||
.withForeignKey('parentFolderId', { | ||
tableName: 'folder', | ||
columnName: 'id', | ||
onDelete: 'CASCADE', | ||
}) | ||
.withIndexOn(['projectId', 'id'], true).withTimestamps; | ||
|
||
await createTable('folder_tag') | ||
.withColumns( | ||
column('folderId').varchar(36).primary.notNull, | ||
column('tagId').varchar(36).primary.notNull, | ||
) | ||
.withForeignKey('folderId', { | ||
tableName: 'folder', | ||
columnName: 'id', | ||
onDelete: 'CASCADE', | ||
}) | ||
.withForeignKey('tagId', { | ||
tableName: 'tag_entity', | ||
columnName: 'id', | ||
onDelete: 'CASCADE', | ||
}); | ||
|
||
await runQuery( | ||
`ALTER TABLE ${workflowTable} ADD COLUMN ${workflowFolderId} VARCHAR(36) DEFAULT NULL REFERENCES ${folderTable}(${folderId}) ON DELETE SET NULL`, | ||
); | ||
} | ||
|
||
async down({ runQuery, escape, schemaBuilder: { dropTable } }: MigrationContext) { | ||
const workflowTable = escape.tableName('workflow_entity'); | ||
const workflowFolderId = escape.columnName('parentFolderId'); | ||
|
||
await runQuery(`ALTER TABLE ${workflowTable} DROP COLUMN ${workflowFolderId}`); | ||
|
||
await dropTable('folder_tag'); | ||
|
||
await dropTable('folder'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/cli/src/databases/migrations/sqlite/1738709609940-CreateFolderTable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { CreateFolderTable1738709609940 as BaseMigration } from '../common/1738709609940-CreateFolderTable'; | ||
|
||
export class CreateFolderTable1738709609940 extends BaseMigration { | ||
transaction = false as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/cli/src/databases/repositories/folder.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Service } from '@n8n/di'; | ||
import { DataSource, Repository } from '@n8n/typeorm'; | ||
|
||
import { Folder } from '../entities/folder'; | ||
|
||
@Service() | ||
export class FolderRepository extends Repository<Folder> { | ||
constructor(dataSource: DataSource) { | ||
super(Folder, dataSource.manager); | ||
} | ||
} |