feat: pg schema support (#1439)

* chore: pg schema test

* test: collection schema

* fix: test

* feat: create schema if not exist in sync method

* fix: test
This commit is contained in:
ChengLei Shao 2023-02-08 22:08:25 +08:00 committed by GitHub
parent 3cd79006be
commit 1b63403811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 6 deletions

View File

@ -0,0 +1,63 @@
import { mockDatabase } from '../index';
import { Database } from '../../database';
describe('postgres schema', () => {
let db: Database;
beforeEach(async () => {
db = mockDatabase({
schema: 'test_schema',
});
if (!db.inDialect('postgres')) return;
});
afterEach(async () => {
if (db.inDialect('postgres')) {
await db.sequelize.query(`DROP SCHEMA IF EXISTS ${db.options.schema} CASCADE;`);
}
await db.close();
});
it('should drop all tables in schemas', async () => {
if (!db.inDialect('postgres')) return;
const collection = db.collection({
name: 'test',
});
await db.sync();
await db.clean({ drop: true });
const tableInfo = await db.sequelize.query(
`SELECT * FROM information_schema.tables where table_schema = '${db.options.schema}'`,
);
expect(tableInfo[0].length).toEqual(0);
});
it('should support database schema option', async () => {
if (!db.inDialect('postgres')) return;
await db.clean({ drop: true });
const tableInfo = await db.sequelize.query(
`SELECT * FROM information_schema.tables where table_schema = '${db.options.schema}'`,
);
expect(tableInfo[0].length).toEqual(0);
const collection = db.collection({
name: 'test',
});
await db.sync();
const newTableInfo = await db.sequelize.query(
`SELECT * FROM information_schema.tables where table_schema = '${db.options.schema}'`,
);
expect(newTableInfo[0].find((item) => item['table_name'] == collection.model.tableName)).toBeTruthy();
});
});

View File

@ -7,7 +7,7 @@ import {
QueryInterfaceDropTableOptions,
SyncOptions,
Transactionable,
Utils
Utils,
} from 'sequelize';
import { Database } from './database';
import { Field, FieldOptions } from './fields';

View File

@ -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';
export interface MergeOptions extends merge.Options {}
@ -484,6 +484,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 0', null);
}
if (this.options.schema && this.inDialect('postgres')) {
await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${this.options.schema}"`, null);
}
const result = await this.sequelize.sync(options);
if (isMySQL) {
@ -494,10 +498,29 @@ export class Database extends EventEmitter implements AsyncEmitter {
}
async clean(options: CleanOptions) {
const { drop, ...others } = options;
if (drop) {
await this.sequelize.getQueryInterface().dropAllTables(others);
const { drop, ...others } = options || {};
if (drop !== true) {
return;
}
if (this.options.schema) {
const tableNames = (await this.sequelize.getQueryInterface().showAllTables()).map((table) => {
return `"${this.options.schema}"."${table}"`;
});
const skip = options.skip || [];
// @ts-ignore
for (const tableName of tableNames) {
if (skip.includes(tableName)) {
continue;
}
await this.sequelize.query(`DROP TABLE IF EXISTS ${tableName} CASCADE`);
}
return;
}
await this.sequelize.getQueryInterface().dropAllTables(others);
}
async collectionExistsInDb(name, options?: Transactionable) {