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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,7 +13,7 @@ import {
beforeCreateForChildrenCollection,
beforeCreateForReverseField,
beforeDestroyForeignKey,
beforeInitOptions,
beforeInitOptions
} from './hooks';
import { CollectionModel, FieldModel } from './models';
@ -130,7 +130,16 @@ export class CollectionManagerPlugin extends Plugin {
}
const exists = await this.app.db.collectionExistsInDb('collections');
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;
}
}
}
});