tachybase_todo/packages/plugins/@nocobase/plugin-users/src/server/migrations/20230802170800-add-username-constraint.ts
2024-01-13 09:47:35 +08:00

33 lines
932 B
TypeScript

import { DataTypes } from '@nocobase/database';
import { Migration } from '@nocobase/server';
export default class AddUserNameMigration extends Migration {
on = 'beforeLoad';
appVersion = '<0.13.0-alpha.1';
async up() {
const collection = this.db.collection({
name: 'users',
fields: [
{
type: 'string',
name: 'username',
},
],
});
const tableNameWithSchema = collection.getTableNameWithSchema();
const field = collection.getField('username');
const exists = await field.existsInDb();
if (!exists) {
await this.db.sequelize.getQueryInterface().addColumn(tableNameWithSchema, field.columnName(), {
type: DataTypes.STRING,
});
}
await this.db.sequelize.getQueryInterface().addConstraint(tableNameWithSchema, {
type: 'unique',
fields: [field.columnName()],
});
this.db.removeCollection('users');
}
}