fix: update role migration (#1366)
Co-authored-by: sealday <sealday@gmail.com> Reviewed-on: daoyoucloud/tachybase#1366
This commit is contained in:
parent
b6b94e200e
commit
e8cc2b21fa
@ -1,6 +1,7 @@
|
|||||||
import { Migration } from '@tachybase/server';
|
import { Migration } from '@tachybase/server';
|
||||||
|
|
||||||
export default class AddGuestSpecialRoleMigration extends Migration {
|
export default class AddGuestSpecialRoleMigration extends Migration {
|
||||||
|
on = 'afterLoad';
|
||||||
appVersion = '<0.21.88';
|
appVersion = '<0.21.88';
|
||||||
|
|
||||||
async up() {
|
async up() {
|
||||||
|
@ -1,54 +1,17 @@
|
|||||||
import { DataTypes } from '@tachybase/database';
|
|
||||||
import { Migration } from '@tachybase/server';
|
import { Migration } from '@tachybase/server';
|
||||||
|
|
||||||
export default class AddUsersSpecialRoleMigration extends Migration {
|
export default class AddUsersSpecialRoleMigration extends Migration {
|
||||||
on = 'beforeLoad';
|
on = 'afterLoad';
|
||||||
appVersion = '<0.21.88';
|
appVersion = '<0.21.88';
|
||||||
|
|
||||||
async up() {
|
async up() {
|
||||||
const collection = this.db.collection({
|
|
||||||
name: 'users',
|
|
||||||
fields: [
|
|
||||||
{
|
|
||||||
type: 'string',
|
|
||||||
name: 'specialRole',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
const tableNameWithSchema = collection.getTableNameWithSchema();
|
|
||||||
const field = collection.getField('specialRole');
|
|
||||||
const exists = await field.existsInDb();
|
|
||||||
if (!exists) {
|
|
||||||
await this.db.sequelize.getQueryInterface().addColumn(tableNameWithSchema, field.columnName(), {
|
|
||||||
type: DataTypes.STRING,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await this.db.sequelize.getQueryInterface().addConstraint(tableNameWithSchema, {
|
|
||||||
type: 'unique',
|
|
||||||
fields: [field.columnName()],
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(error);
|
|
||||||
}
|
|
||||||
const repo = this.context.db.getRepository('users');
|
const repo = this.context.db.getRepository('users');
|
||||||
const user = await repo.findOne({
|
await repo.update({
|
||||||
filter: {
|
filter: { $and: [{ roles: { title: { $includes: 'root' } } }] },
|
||||||
email: process.env.INIT_ROOT_EMAIL || 'admin@tachybase.com',
|
values: {
|
||||||
|
specialRole: 'root',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (user) {
|
|
||||||
await repo.update({
|
|
||||||
values: {
|
|
||||||
specialRole: 'root',
|
|
||||||
},
|
|
||||||
filter: {
|
|
||||||
id: user.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
throw new Error('Root user not found');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async down() {}
|
async down() {}
|
||||||
|
Loading…
Reference in New Issue
Block a user