fix: db sync failed (#1037)

* fix: db sync failed

* fix: remove db sync
This commit is contained in:
chenos 2022-11-04 15:38:08 +08:00 committed by GitHub
parent 3e06e575f1
commit 3aa7c0787a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 12 deletions

View File

@ -46,9 +46,9 @@ module.exports = (cli) => {
}); });
} }
await runAppCommand('install', ['--silent']); await runAppCommand('install', ['--silent']);
if (opts.dbSync) { // if (opts.dbSync) {
await runAppCommand('db:sync'); // await runAppCommand('db:sync');
} // }
if (server || !client) { if (server || !client) {
console.log('starting server', serverPort); console.log('starting server', serverPort);
const argv = [ const argv = [
@ -61,6 +61,9 @@ module.exports = (cli) => {
...process.argv.slice(3), ...process.argv.slice(3),
`--port=${serverPort}`, `--port=${serverPort}`,
]; ];
if (opts.dbSync) {
argv.push('--db-sync');
}
run('ts-node-dev', argv, { run('ts-node-dev', argv, {
env: { env: {
APP_PORT: serverPort, APP_PORT: serverPort,

View File

@ -62,12 +62,7 @@ export const collectionSchema: ISchema = {
params: { params: {
pageSize: 50, pageSize: 50,
filter: { filter: {
inherit: false, 'hidden.$isFalsy': true,
options: {
// filter auto create through collections
autoCreate: { $not: true },
isThrough: { $not: true },
},
}, },
sort: ['sort'], sort: ['sort'],
appends: [], appends: [],

View File

@ -83,6 +83,7 @@ interface ListenOptions {
interface StartOptions { interface StartOptions {
cliArgs?: any[]; cliArgs?: any[];
dbSync?: boolean;
listen?: ListenOptions; listen?: ListenOptions;
} }
@ -352,6 +353,11 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
await this.db.reconnect(); await this.db.reconnect();
} }
if (options.dbSync) {
console.log('db sync...');
await this.db.sync();
}
await this.emitAsync('beforeStart', this, options); await this.emitAsync('beforeStart', this, options);
if (options?.listen?.port) { if (options?.listen?.port) {

View File

@ -4,7 +4,6 @@ export default (app: Application) => {
app.command('db:sync').action(async (...cliArgs) => { app.command('db:sync').action(async (...cliArgs) => {
const [opts] = cliArgs; const [opts] = cliArgs;
console.log('db sync...'); console.log('db sync...');
await app.emitAsync('cli.beforeDbSync', ...cliArgs);
const force = false; const force = false;
await app.db.sync({ await app.db.sync({
force, force,

View File

@ -6,12 +6,14 @@ export default (app: Application) => {
.option('-s, --silent') .option('-s, --silent')
.option('-p, --port [post]') .option('-p, --port [post]')
.option('-h, --host [host]') .option('-h, --host [host]')
.option('--db-sync')
.action(async (...cliArgs) => { .action(async (...cliArgs) => {
const [opts] = cliArgs; const [opts] = cliArgs;
const port = opts.port || process.env.APP_PORT || 13000; const port = opts.port || process.env.APP_PORT || 13000;
const host = opts.host || process.env.APP_HOST || '0.0.0.0'; const host = opts.host || process.env.APP_HOST || '0.0.0.0';
await app.start({ await app.start({
dbSync: opts?.dbSync,
cliArgs, cliArgs,
listen: { listen: {
port, port,

View File

@ -31,6 +31,11 @@ export default {
name: 'inherit', name: 'inherit',
defaultValue: false, defaultValue: false,
}, },
{
type: 'boolean',
name: 'hidden',
defaultValue: false,
},
{ {
type: 'json', type: 'json',
name: 'options', name: 'options',

View File

@ -140,6 +140,7 @@ export function afterCreateForForeignKeyField(db: Database) {
title: through, title: through,
timestamps: false, timestamps: false,
autoGenId: false, autoGenId: false,
hidden: true,
autoCreate: true, autoCreate: true,
isThrough: true, isThrough: true,
}, },

View File

@ -13,7 +13,7 @@ import {
beforeCreateForChildrenCollection, beforeCreateForChildrenCollection,
beforeCreateForReverseField, beforeCreateForReverseField,
beforeDestroyForeignKey, beforeDestroyForeignKey,
beforeInitOptions, beforeInitOptions
} from './hooks'; } from './hooks';
import { CollectionModel, FieldModel } from './models'; import { CollectionModel, FieldModel } from './models';
@ -130,7 +130,16 @@ export class CollectionManagerPlugin extends Plugin {
} }
const exists = await this.app.db.collectionExistsInDb('collections'); const exists = await this.app.db.collectionExistsInDb('collections');
if (exists) { if (exists) {
await this.app.db.getRepository<CollectionRepository>('collections').load(); try {
await this.app.db.getRepository<CollectionRepository>('collections').load();
} catch (error) {
await this.app.db.sync();
try {
await this.app.db.getRepository<CollectionRepository>('collections').load();
} catch (error) {
throw error;
}
}
} }
}); });