mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-27 13:39:44 -08:00
7879239e03
* ➕ Add `@oclif/core` * 📦 Update `package-lock.json` * 📘 Export `Logger` for use as type * ✨ Create `BaseCommand` * 🐛 Prevent DB re-init * ♻️ Refactor `reset` command * 🧪 Fix `reset` test * 👕 Add lint exception Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { Not } from 'typeorm';
|
|
import { Db } from '../../src';
|
|
import { BaseCommand } from '../BaseCommand';
|
|
|
|
export class Reset extends BaseCommand {
|
|
static description = '\nResets the database to the default user state';
|
|
|
|
async run(): Promise<void> {
|
|
const owner = await this.getInstanceOwner();
|
|
|
|
const ownerWorkflowRole = await Db.collections.Role.findOneOrFail({
|
|
name: 'owner',
|
|
scope: 'workflow',
|
|
});
|
|
|
|
const ownerCredentialRole = await Db.collections.Role.findOneOrFail({
|
|
name: 'owner',
|
|
scope: 'credential',
|
|
});
|
|
|
|
await Db.collections.SharedWorkflow.update(
|
|
{ user: { id: Not(owner.id) }, role: ownerWorkflowRole },
|
|
{ user: owner },
|
|
);
|
|
|
|
await Db.collections.SharedCredentials.update(
|
|
{ user: { id: Not(owner.id) }, role: ownerCredentialRole },
|
|
{ user: owner },
|
|
);
|
|
|
|
await Db.collections.User.delete({ id: Not(owner.id) });
|
|
await Db.collections.User.save(Object.assign(owner, this.defaultUserProps));
|
|
|
|
await Db.collections.Settings.update(
|
|
{ key: 'userManagement.isInstanceOwnerSetUp' },
|
|
{ value: 'false' },
|
|
);
|
|
await Db.collections.Settings.update(
|
|
{ key: 'userManagement.skipInstanceOwnerSetup' },
|
|
{ value: 'false' },
|
|
);
|
|
|
|
this.logger.info('Successfully reset the database to default user state.');
|
|
}
|
|
|
|
async catch(error: Error): Promise<void> {
|
|
this.logger.error('Error resetting database. See log messages for details.');
|
|
this.logger.error(error.message);
|
|
this.exit(1);
|
|
}
|
|
}
|