feat: prepare database method (#1492)
This commit is contained in:
parent
fa0951171a
commit
856b6efa77
@ -1,5 +1,36 @@
|
||||
import { mockDatabase } from '../index';
|
||||
import { Database } from '../../database';
|
||||
import { randomStr } from '@nocobase/test';
|
||||
|
||||
describe('auth', () => {
|
||||
let db: Database;
|
||||
afterEach(async () => {
|
||||
if (db) {
|
||||
await db.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('should auto create schema on prepare when schema missing', async () => {
|
||||
const schemaName = randomStr();
|
||||
|
||||
db = mockDatabase({
|
||||
schema: schemaName,
|
||||
});
|
||||
|
||||
if (!db.inDialect('postgres')) return;
|
||||
|
||||
const querySchemaExists = async () =>
|
||||
await db.sequelize.query(
|
||||
`SELECT schema_name
|
||||
FROM information_schema.schemata
|
||||
WHERE schema_name = '${schemaName}';`,
|
||||
);
|
||||
|
||||
expect((await querySchemaExists())[0].length).toEqual(0);
|
||||
await db.prepare();
|
||||
expect((await querySchemaExists())[0].length).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('postgres schema', () => {
|
||||
let db: Database;
|
||||
@ -31,7 +62,9 @@ describe('postgres schema', () => {
|
||||
await db.clean({ drop: true });
|
||||
|
||||
const tableInfo = await db.sequelize.query(
|
||||
`SELECT * FROM information_schema.tables where table_schema = '${db.options.schema}'`,
|
||||
`SELECT *
|
||||
FROM information_schema.tables
|
||||
where table_schema = '${db.options.schema}'`,
|
||||
);
|
||||
|
||||
expect(tableInfo[0].length).toEqual(0);
|
||||
@ -43,7 +76,9 @@ describe('postgres schema', () => {
|
||||
await db.clean({ drop: true });
|
||||
|
||||
const tableInfo = await db.sequelize.query(
|
||||
`SELECT * FROM information_schema.tables where table_schema = '${db.options.schema}'`,
|
||||
`SELECT *
|
||||
FROM information_schema.tables
|
||||
where table_schema = '${db.options.schema}'`,
|
||||
);
|
||||
|
||||
expect(tableInfo[0].length).toEqual(0);
|
||||
@ -55,7 +90,9 @@ describe('postgres schema', () => {
|
||||
await db.sync();
|
||||
|
||||
const newTableInfo = await db.sequelize.query(
|
||||
`SELECT * FROM information_schema.tables where table_schema = '${db.options.schema}'`,
|
||||
`SELECT *
|
||||
FROM information_schema.tables
|
||||
where table_schema = '${db.options.schema}'`,
|
||||
);
|
||||
|
||||
expect(newTableInfo[0].find((item) => item['table_name'] == collection.model.tableName)).toBeTruthy();
|
||||
|
@ -15,7 +15,7 @@ import {
|
||||
Sequelize,
|
||||
SyncOptions,
|
||||
Transactionable,
|
||||
Utils
|
||||
Utils,
|
||||
} from 'sequelize';
|
||||
import { SequelizeStorage, Umzug } from 'umzug';
|
||||
import { Collection, CollectionOptions, RepositoryType } from './collection';
|
||||
@ -58,7 +58,7 @@ import {
|
||||
SyncListener,
|
||||
UpdateListener,
|
||||
UpdateWithAssociationsListener,
|
||||
ValidateListener
|
||||
ValidateListener,
|
||||
} from './types';
|
||||
import { patchSequelizeQueryInterface, snakeCase } from './utils';
|
||||
|
||||
@ -653,6 +653,12 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
||||
return await authenticate();
|
||||
}
|
||||
|
||||
async prepare() {
|
||||
if (this.inDialect('postgres') && this.options.schema && this.options.schema != 'public') {
|
||||
await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${this.options.schema}"`, null);
|
||||
}
|
||||
}
|
||||
|
||||
async reconnect() {
|
||||
if (this.isSqliteMemory()) {
|
||||
return;
|
||||
|
@ -377,8 +377,11 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
|
||||
console.log(chalk.red(error.message));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await this.dbVersionCheck({ exit: true });
|
||||
|
||||
await this.db.prepare();
|
||||
|
||||
if (argv?.[2] !== 'upgrade') {
|
||||
await this.load({
|
||||
method: argv?.[2],
|
||||
|
@ -3,3 +3,8 @@ export * from './mockServer';
|
||||
|
||||
const pgOnly: () => jest.Describe = () => (process.env.DB_DIALECT == 'postgres' ? describe : describe.skip);
|
||||
export { pgOnly };
|
||||
|
||||
export function randomStr() {
|
||||
// create random string
|
||||
return Math.random().toString(36).substring(2);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user