Skip to content

Commit 3a813fa

Browse files
authored
Merge pull request #873 from FlowiseAI/feature/Migration
feature/Migration
2 parents fcae6f0 + 2574b02 commit 3a813fa

27 files changed

+407
-35
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"lint": "eslint \"**/*.{js,jsx,ts,tsx,json,md}\"",
2323
"lint-fix": "yarn lint --fix",
2424
"quick": "pretty-quick --staged",
25-
"postinstall": "husky install"
25+
"postinstall": "husky install",
26+
"migration:create": "yarn typeorm migration:create"
2627
},
2728
"lint-staged": {
2829
"*.{js,jsx,ts,tsx,json,md}": "eslint --fix"

packages/server/src/DataSource.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import 'reflect-metadata'
22
import path from 'path'
33
import { DataSource } from 'typeorm'
4-
import { ChatFlow } from './entity/ChatFlow'
5-
import { ChatMessage } from './entity/ChatMessage'
6-
import { Credential } from './entity/Credential'
7-
import { Tool } from './entity/Tool'
84
import { getUserHome } from './utils'
5+
import { entities } from './database/entities'
6+
import { sqliteMigrations } from './database/migrations/sqlite'
7+
import { mysqlMigrations } from './database/migrations/mysql'
8+
import { postgresMigrations } from './database/migrations/postgres'
99

1010
let appDataSource: DataSource
1111

1212
export const init = async (): Promise<void> => {
1313
let homePath
14-
const synchronize = process.env.OVERRIDE_DATABASE === 'false' ? false : true
1514
switch (process.env.DATABASE_TYPE) {
1615
case 'sqlite':
1716
homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise')
1817
appDataSource = new DataSource({
1918
type: 'sqlite',
2019
database: path.resolve(homePath, 'database.sqlite'),
21-
synchronize,
22-
entities: [ChatFlow, ChatMessage, Tool, Credential],
23-
migrations: []
20+
synchronize: false,
21+
migrationsRun: false,
22+
entities: Object.values(entities),
23+
migrations: sqliteMigrations
2424
})
2525
break
2626
case 'mysql':
@@ -32,9 +32,10 @@ export const init = async (): Promise<void> => {
3232
password: process.env.DATABASE_PASSWORD,
3333
database: process.env.DATABASE_NAME,
3434
charset: 'utf8mb4',
35-
synchronize,
36-
entities: [ChatFlow, ChatMessage, Tool, Credential],
37-
migrations: []
35+
synchronize: false,
36+
migrationsRun: false,
37+
entities: Object.values(entities),
38+
migrations: mysqlMigrations
3839
})
3940
break
4041
case 'postgres':
@@ -45,19 +46,21 @@ export const init = async (): Promise<void> => {
4546
username: process.env.DATABASE_USER,
4647
password: process.env.DATABASE_PASSWORD,
4748
database: process.env.DATABASE_NAME,
48-
synchronize,
49-
entities: [ChatFlow, ChatMessage, Tool, Credential],
50-
migrations: []
49+
synchronize: false,
50+
migrationsRun: false,
51+
entities: Object.values(entities),
52+
migrations: postgresMigrations
5153
})
5254
break
5355
default:
5456
homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise')
5557
appDataSource = new DataSource({
5658
type: 'sqlite',
5759
database: path.resolve(homePath, 'database.sqlite'),
58-
synchronize,
59-
entities: [ChatFlow, ChatMessage, Tool, Credential],
60-
migrations: []
60+
synchronize: false,
61+
migrationsRun: false,
62+
entities: Object.values(entities),
63+
migrations: sqliteMigrations
6164
})
6265
break
6366
}

packages/server/src/entity/ChatFlow.ts packages/server/src/database/entities/ChatFlow.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
import { Entity, Column, CreateDateColumn, UpdateDateColumn, PrimaryGeneratedColumn } from 'typeorm'
3-
import { IChatFlow } from '../Interface'
3+
import { IChatFlow } from '../../Interface'
44

55
@Entity()
66
export class ChatFlow implements IChatFlow {
@@ -22,7 +22,7 @@ export class ChatFlow implements IChatFlow {
2222
@Column({ nullable: true })
2323
apikeyid?: string
2424

25-
@Column({ nullable: true })
25+
@Column({ nullable: true, type: 'text' })
2626
chatbotConfig?: string
2727

2828
@CreateDateColumn()

packages/server/src/entity/ChatMessage.ts packages/server/src/database/entities/ChatMessage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
import { Entity, Column, CreateDateColumn, PrimaryGeneratedColumn, Index } from 'typeorm'
3-
import { IChatMessage, MessageType } from '../Interface'
3+
import { IChatMessage, MessageType } from '../../Interface'
44

55
@Entity()
66
export class ChatMessage implements IChatMessage {
@@ -17,7 +17,7 @@ export class ChatMessage implements IChatMessage {
1717
@Column({ type: 'text' })
1818
content: string
1919

20-
@Column({ nullable: true })
20+
@Column({ nullable: true, type: 'text' })
2121
sourceDocuments?: string
2222

2323
@CreateDateColumn()

packages/server/src/entity/Credential.ts packages/server/src/database/entities/Credential.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
import { Entity, Column, PrimaryGeneratedColumn, Index, CreateDateColumn, UpdateDateColumn } from 'typeorm'
3-
import { ICredential } from '../Interface'
3+
import { ICredential } from '../../Interface'
44

55
@Entity()
66
export class Credential implements ICredential {
@@ -13,7 +13,7 @@ export class Credential implements ICredential {
1313
@Column()
1414
credentialName: string
1515

16-
@Column()
16+
@Column({ type: 'text' })
1717
encryptedData: string
1818

1919
@CreateDateColumn()

packages/server/src/entity/Tool.ts packages/server/src/database/entities/Tool.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
import { Entity, Column, CreateDateColumn, UpdateDateColumn, PrimaryGeneratedColumn } from 'typeorm'
3-
import { ITool } from '../Interface'
3+
import { ITool } from '../../Interface'
44

55
@Entity()
66
export class Tool implements ITool {
@@ -19,10 +19,10 @@ export class Tool implements ITool {
1919
@Column({ nullable: true })
2020
iconSrc?: string
2121

22-
@Column({ nullable: true })
22+
@Column({ nullable: true, type: 'text' })
2323
schema?: string
2424

25-
@Column({ nullable: true })
25+
@Column({ nullable: true, type: 'text' })
2626
func?: string
2727

2828
@CreateDateColumn()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ChatFlow } from './ChatFlow'
2+
import { ChatMessage } from './ChatMessage'
3+
import { Credential } from './Credential'
4+
import { Tool } from './Tool'
5+
6+
export const entities = {
7+
ChatFlow,
8+
ChatMessage,
9+
Credential,
10+
Tool
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class Init1693840429259 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(
6+
`CREATE TABLE IF NOT EXISTS \`chat_flow\` (
7+
\`id\` varchar(36) NOT NULL,
8+
\`name\` varchar(255) NOT NULL,
9+
\`flowData\` text NOT NULL,
10+
\`deployed\` tinyint DEFAULT NULL,
11+
\`isPublic\` tinyint DEFAULT NULL,
12+
\`apikeyid\` varchar(255) DEFAULT NULL,
13+
\`chatbotConfig\` varchar(255) DEFAULT NULL,
14+
\`createdDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
15+
\`updatedDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
16+
PRIMARY KEY (\`id\`)
17+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;`
18+
)
19+
await queryRunner.query(
20+
`CREATE TABLE IF NOT EXISTS \`chat_message\` (
21+
\`id\` varchar(36) NOT NULL,
22+
\`role\` varchar(255) NOT NULL,
23+
\`chatflowid\` varchar(255) NOT NULL,
24+
\`content\` text NOT NULL,
25+
\`sourceDocuments\` varchar(255) DEFAULT NULL,
26+
\`createdDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
27+
PRIMARY KEY (\`id\`),
28+
KEY \`IDX_e574527322272fd838f4f0f3d3\` (\`chatflowid\`)
29+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;`
30+
)
31+
await queryRunner.query(
32+
`CREATE TABLE IF NOT EXISTS \`credential\` (
33+
\`id\` varchar(36) NOT NULL,
34+
\`name\` varchar(255) NOT NULL,
35+
\`credentialName\` varchar(255) NOT NULL,
36+
\`encryptedData\` varchar(255) NOT NULL,
37+
\`createdDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
38+
\`updatedDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
39+
PRIMARY KEY (\`id\`)
40+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;`
41+
)
42+
await queryRunner.query(
43+
`CREATE TABLE IF NOT EXISTS \`tool\` (
44+
\`id\` varchar(36) NOT NULL,
45+
\`name\` varchar(255) NOT NULL,
46+
\`description\` text NOT NULL,
47+
\`color\` varchar(255) NOT NULL,
48+
\`iconSrc\` varchar(255) DEFAULT NULL,
49+
\`schema\` varchar(255) DEFAULT NULL,
50+
\`func\` varchar(255) DEFAULT NULL,
51+
\`createdDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
52+
\`updatedDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
53+
PRIMARY KEY (\`id\`)
54+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;`
55+
)
56+
}
57+
58+
public async down(queryRunner: QueryRunner): Promise<void> {
59+
await queryRunner.query(`DROP TABLE chat_flow`)
60+
await queryRunner.query(`DROP TABLE chat_message`)
61+
await queryRunner.query(`DROP TABLE credential`)
62+
await queryRunner.query(`DROP TABLE tool`)
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class ModifyChatFlow1693997791471 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`ALTER TABLE \`chat_flow\` MODIFY \`chatbotConfig\` TEXT;`)
6+
}
7+
8+
public async down(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`ALTER TABLE \`chat_flow\` MODIFY \`chatbotConfig\` VARCHAR;`)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class ModifyChatMessage1693999022236 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`ALTER TABLE \`chat_message\` MODIFY \`sourceDocuments\` TEXT;`)
6+
}
7+
8+
public async down(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`ALTER TABLE \`chat_message\` MODIFY \`sourceDocuments\` VARCHAR;`)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class ModifyCredential1693999261583 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`ALTER TABLE \`credential\` MODIFY \`encryptedData\` TEXT NOT NULL;`)
6+
}
7+
8+
public async down(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`ALTER TABLE \`credential\` MODIFY \`encryptedData\` VARCHAR NOT NULL;`)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class ModifyTool1694001465232 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`ALTER TABLE \`tool\` MODIFY \`schema\` TEXT, MODIFY \`func\` TEXT;`)
6+
}
7+
8+
public async down(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`ALTER TABLE \`tool\` MODIFY \`schema\` VARCHAR, MODIFY \`func\` VARCHAR;`)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Init1693840429259 } from './1693840429259-Init'
2+
import { ModifyChatFlow1693997791471 } from './1693997791471-ModifyChatFlow'
3+
import { ModifyChatMessage1693999022236 } from './1693999022236-ModifyChatMessage'
4+
import { ModifyCredential1693999261583 } from './1693999261583-ModifyCredential'
5+
import { ModifyTool1694001465232 } from './1694001465232-ModifyTool'
6+
7+
export const mysqlMigrations = [
8+
Init1693840429259,
9+
ModifyChatFlow1693997791471,
10+
ModifyChatMessage1693999022236,
11+
ModifyCredential1693999261583,
12+
ModifyTool1694001465232
13+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class Init1693891895163 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(
6+
`CREATE TABLE IF NOT EXISTS chat_flow (
7+
id uuid NOT NULL DEFAULT uuid_generate_v4(),
8+
"name" varchar NOT NULL,
9+
"flowData" text NOT NULL,
10+
deployed bool NULL,
11+
"isPublic" bool NULL,
12+
apikeyid varchar NULL,
13+
"chatbotConfig" varchar NULL,
14+
"createdDate" timestamp NOT NULL DEFAULT now(),
15+
"updatedDate" timestamp NOT NULL DEFAULT now(),
16+
CONSTRAINT "PK_3c7cea7d047ac4b91764574cdbf" PRIMARY KEY (id)
17+
);`
18+
)
19+
await queryRunner.query(
20+
`CREATE TABLE IF NOT EXISTS chat_message (
21+
id uuid NOT NULL DEFAULT uuid_generate_v4(),
22+
"role" varchar NOT NULL,
23+
chatflowid varchar NOT NULL,
24+
"content" text NOT NULL,
25+
"sourceDocuments" varchar NULL,
26+
"createdDate" timestamp NOT NULL DEFAULT now(),
27+
CONSTRAINT "PK_3cc0d85193aade457d3077dd06b" PRIMARY KEY (id)
28+
);`
29+
)
30+
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_e574527322272fd838f4f0f3d3" ON chat_message USING btree (chatflowid);`)
31+
await queryRunner.query(
32+
`CREATE TABLE IF NOT EXISTS credential (
33+
id uuid NOT NULL DEFAULT uuid_generate_v4(),
34+
"name" varchar NOT NULL,
35+
"credentialName" varchar NOT NULL,
36+
"encryptedData" varchar NOT NULL,
37+
"createdDate" timestamp NOT NULL DEFAULT now(),
38+
"updatedDate" timestamp NOT NULL DEFAULT now(),
39+
CONSTRAINT "PK_3a5169bcd3d5463cefeec78be82" PRIMARY KEY (id)
40+
);`
41+
)
42+
await queryRunner.query(
43+
`CREATE TABLE IF NOT EXISTS tool (
44+
id uuid NOT NULL DEFAULT uuid_generate_v4(),
45+
"name" varchar NOT NULL,
46+
description text NOT NULL,
47+
color varchar NOT NULL,
48+
"iconSrc" varchar NULL,
49+
"schema" varchar NULL,
50+
func varchar NULL,
51+
"createdDate" timestamp NOT NULL DEFAULT now(),
52+
"updatedDate" timestamp NOT NULL DEFAULT now(),
53+
CONSTRAINT "PK_3bf5b1016a384916073184f99b7" PRIMARY KEY (id)
54+
);`
55+
)
56+
}
57+
58+
public async down(queryRunner: QueryRunner): Promise<void> {
59+
await queryRunner.query(`DROP TABLE chat_flow`)
60+
await queryRunner.query(`DROP TABLE chat_message`)
61+
await queryRunner.query(`DROP TABLE credential`)
62+
await queryRunner.query(`DROP TABLE tool`)
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class ModifyChatFlow1693995626941 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`ALTER TABLE "chat_flow" ALTER COLUMN "chatbotConfig" TYPE TEXT;`)
6+
}
7+
8+
public async down(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`ALTER TABLE "chat_flow" ALTER COLUMN "chatbotConfig" TYPE VARCHAR;`)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class ModifyChatMessage1693996694528 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "sourceDocuments" TYPE TEXT;`)
6+
}
7+
8+
public async down(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "sourceDocuments" TYPE VARCHAR;`)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm'
2+
3+
export class ModifyCredential1693997070000 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`ALTER TABLE "credential" ALTER COLUMN "encryptedData" TYPE TEXT;`)
6+
}
7+
8+
public async down(queryRunner: QueryRunner): Promise<void> {
9+
await queryRunner.query(`ALTER TABLE "credential" ALTER COLUMN "encryptedData" TYPE VARCHAR;`)
10+
}
11+
}

0 commit comments

Comments
 (0)