From cec57332601920afe407aa23302b475cac11f61a Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 4 Aug 2022 21:27:55 +0800 Subject: [PATCH] fix: DB_TABLE_PREFIX doesn't get applied (#710) * fix: env DB_TABLE_PREFIX doesn't get applied * fix: remove before define * fix: test error * fix: table prefix * test: ci * fix: test error * test: ci * test: ci * fix: test error * fix: test error * chore: update node ci --- .github/workflows/node-ci.yml | 2 +- packages/app/server/src/config/database.ts | 1 + .../database/src/__tests__/update-associations.test.ts | 2 +- packages/core/database/src/database.ts | 8 +++++++- packages/core/database/src/mock-database.ts | 3 --- .../multi-app-manager/src/__tests__/multiple-apps.test.ts | 4 ++-- .../plugins/multi-app-manager/src/models/application.ts | 5 ++++- 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.github/workflows/node-ci.yml b/.github/workflows/node-ci.yml index 6b1bca738..fabc86ea2 100644 --- a/.github/workflows/node-ci.yml +++ b/.github/workflows/node-ci.yml @@ -40,7 +40,7 @@ jobs: node-version: ${{ matrix.node_version }} cache: 'yarn' - run: yarn install - - run: yarn build + # - run: yarn build - name: Test with postgres run: yarn test env: diff --git a/packages/app/server/src/config/database.ts b/packages/app/server/src/config/database.ts index 750bcfe67..fa6df44cd 100644 --- a/packages/app/server/src/config/database.ts +++ b/packages/app/server/src/config/database.ts @@ -10,4 +10,5 @@ export default { host: process.env.DB_HOST, port: process.env.DB_PORT as any, timezone: process.env.DB_TIMEZONE, + tablePrefix: process.env.DB_TABLE_PREFIX, } as IDatabaseOptions; diff --git a/packages/core/database/src/__tests__/update-associations.test.ts b/packages/core/database/src/__tests__/update-associations.test.ts index 70d75c128..40d380539 100644 --- a/packages/core/database/src/__tests__/update-associations.test.ts +++ b/packages/core/database/src/__tests__/update-associations.test.ts @@ -390,7 +390,7 @@ describe('update associations', () => { afterEach(async () => { await db.close(); }); - test.only('set through value', async () => { + test('set through value', async () => { const p1 = await Post.repository.create({ values: { title: 'hello', diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 6d90957b1..b72900a74 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -135,7 +135,7 @@ export class Database extends EventEmitter implements AsyncEmitter { }, force: false, }, - ...options, + ...lodash.clone(options), }; if (options.storage && options.storage !== ':memory:') { @@ -197,6 +197,12 @@ export class Database extends EventEmitter implements AsyncEmitter { sequelize: this.sequelize, }), }); + + this.sequelize.beforeDefine((model, opts) => { + if (this.options.tablePrefix) { + opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`; + } + }); } addMigration(item) { diff --git a/packages/core/database/src/mock-database.ts b/packages/core/database/src/mock-database.ts index 9581581f4..58a45be0d 100644 --- a/packages/core/database/src/mock-database.ts +++ b/packages/core/database/src/mock-database.ts @@ -10,9 +10,6 @@ export class MockDatabase extends Database { dialect: 'sqlite', ...options, }); - this.sequelize.beforeDefine((model, opts) => { - opts.tableName = `${this.getTablePrefix()}${opts.tableName || opts.modelName || opts.name.plural}`; - }); } } diff --git a/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts b/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts index 7ca7baccd..f4b312ee7 100644 --- a/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts +++ b/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts @@ -1,7 +1,7 @@ -import { mockServer, MockServer } from '@nocobase/test'; import { Database } from '@nocobase/database'; -import { PluginMultiAppManager } from '../server'; +import { mockServer, MockServer } from '@nocobase/test'; import { ApplicationModel } from '..'; +import { PluginMultiAppManager } from '../server'; describe('multiple apps create', () => { let app: MockServer; diff --git a/packages/plugins/multi-app-manager/src/models/application.ts b/packages/plugins/multi-app-manager/src/models/application.ts index 5b20af3c9..4a7d5ee58 100644 --- a/packages/plugins/multi-app-manager/src/models/application.ts +++ b/packages/plugins/multi-app-manager/src/models/application.ts @@ -99,7 +99,10 @@ export class ApplicationModel extends Model { } return { - database: rawDatabaseOptions, + database: { + ...rawDatabaseOptions, + tablePrefix: '', + }, }; } }