fix: collection schema updated but model _schema not change (#1500)

* fix: collection schema updated but model _schema not change

* test: schema changed
This commit is contained in:
ChengLei Shao 2023-02-26 08:04:29 +08:00 committed by GitHub
parent c8aba5f139
commit dfbdbc741d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 12 deletions

View File

@ -97,4 +97,24 @@ describe('postgres schema', () => {
expect(newTableInfo[0].find((item) => item['table_name'] == collection.model.tableName)).toBeTruthy();
});
it('should update schema options', async () => {
if (!db.inDialect('postgres')) return;
await db.clean({ drop: true });
const collection = db.collection({
name: 'test',
});
await db.sync();
collection.updateOptions({
...collection.options,
schema: 'test',
});
// @ts-ignore
expect(collection.model._schema).toEqual('test');
});
});

View File

@ -351,8 +351,8 @@ export class Collection<
updateOptions(options: CollectionOptions, mergeOptions?: any) {
let newOptions = lodash.cloneDeep(options);
newOptions = merge(this.options, newOptions, mergeOptions);
this.context.database.emit('beforeUpdateCollection', this, newOptions);
this.context.database.emit('beforeUpdateCollection', this, newOptions);
this.options = newOptions;
this.setFields(options.fields, false);

View File

@ -312,6 +312,12 @@ export class Database extends EventEmitter implements AsyncEmitter {
}
});
this.on('afterUpdateCollection', (collection, options) => {
if (collection.options.schema) {
collection.model._schema = collection.options.schema;
}
});
this.on('beforeDefineCollection', (options) => {
if (options.underscored) {
if (lodash.get(options, 'sortable.scopeKey')) {

View File

@ -483,7 +483,9 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
if (this.db.inDialect('mysql')) {
const result = await this.db.sequelize.query(`SHOW VARIABLES LIKE 'lower_case_table_names'`, { plain: true });
if (result?.Value === '1' && !this.db.options.underscored) {
console.log(`Your database lower_case_table_names=1, please add ${chalk.yellow('DB_UNDERSCORED=true')} to the .env file`);
console.log(
`Your database lower_case_table_names=1, please add ${chalk.yellow('DB_UNDERSCORED=true')} to the .env file`,
);
if (options?.exit) {
process.exit();
}

View File

@ -45,7 +45,11 @@ export class PluginManagerRepository extends Repository {
}
async load() {
const items = await this.find();
// sort plugins by id
const items = await this.find({
sort: 'id',
});
for (const item of items) {
await this.pm.addStatic(item.get('name'), {
...item.get('options'),

View File

@ -52,6 +52,12 @@ export class CollectionRepository extends Repository {
async db2cm(collectionName: string) {
const collection = this.database.getCollection(collectionName);
// skip if collection already exists
if (await this.findOne({ filter: { name: collectionName } })) {
return;
}
const options = collection.options;
const fields = [];
for (const [name, field] of collection.fields) {

View File

@ -18,14 +18,15 @@ export class ApplicationModel extends Model {
await app.load();
if (!(await app.isInstalled())) {
await app.db.sync({
force: false,
alter: {
drop: false,
},
});
await app.db.sync();
await app.install();
// emit an event on mainApp
// current if you add listener on subApp through `subApp.on('afterInstall')` , it will be clear after subApp installed
await mainApp.emitAsync('afterSubAppInstalled', {
mainApp,
subApp: app,
});
}
await app.start();

View File

@ -83,7 +83,7 @@ export class PresetNocoBase extends Plugin {
this.app.on('beforeUpgrade', async (options) => {
const result = await this.app.version.satisfies('<0.8.0-alpha.1');
if (result) {
console.log(`Initialize all built-in plugins`);
console.log(`Initialize all built-in plugins beforeUpgrade`);
await this.addBuiltInPlugins({ method: 'upgrade' });
}
const builtInPlugins = this.getBuiltInPlugins();
@ -105,8 +105,9 @@ export class PresetNocoBase extends Plugin {
await this.app.reload({ method: 'upgrade' });
await this.app.db.sync();
});
this.app.on('beforeInstall', async (options) => {
console.log(`Initialize all built-in plugins`);
console.log(`Initialize all built-in plugins beforeInstall`);
await this.addBuiltInPlugins({ method: 'install' });
});
}