mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 05:29:26 +08:00
fix(plugin-workflow-manual): fix migration (#6484)
This commit is contained in:
parent
ed35d0995e
commit
26d858ae18
@ -281,4 +281,72 @@ describe('20250225175712-change-table-name.test', () => {
|
||||
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
skipSqlite(`1.6.0 -> 1.6.2, wrong migration executed (with id)`, async () => {
|
||||
const app = await createMockServer();
|
||||
await app.version.update('1.5.0');
|
||||
app.db.collection({
|
||||
name: 'workflowManualTasks',
|
||||
migrationRules: ['schema-only'],
|
||||
shared: true,
|
||||
autoGenId: false,
|
||||
fields: [
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'id',
|
||||
},
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'userId',
|
||||
primaryKey: true,
|
||||
},
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'jobId',
|
||||
primaryKey: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await app.db.sync();
|
||||
app.db.removeCollection('workflowManualTasks');
|
||||
const migration = new Migration({ db: app.db, app } as any);
|
||||
await migration.up();
|
||||
app.db.collection({
|
||||
...workflowManualTasks,
|
||||
});
|
||||
await app.db.sync();
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
skipSqlite(`1.6.0 -> 1.6.2, wrong migration executed`, async () => {
|
||||
const app = await createMockServer();
|
||||
await app.version.update('1.5.0');
|
||||
app.db.collection({
|
||||
name: 'workflowManualTasks',
|
||||
migrationRules: ['schema-only'],
|
||||
shared: true,
|
||||
autoGenId: false,
|
||||
fields: [
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'userId',
|
||||
primaryKey: true,
|
||||
},
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'jobId',
|
||||
primaryKey: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
await app.db.sync();
|
||||
app.db.removeCollection('workflowManualTasks');
|
||||
const migration = new Migration({ db: app.db, app } as any);
|
||||
await migration.up();
|
||||
app.db.collection({
|
||||
...workflowManualTasks,
|
||||
});
|
||||
await app.db.sync();
|
||||
await app.destroy();
|
||||
});
|
||||
});
|
||||
|
@ -32,6 +32,53 @@ export default class extends Migration {
|
||||
|
||||
const exists = await queryInterface.tableExists(oldTableName);
|
||||
if (!exists) {
|
||||
const newColumns = await queryInterface.describeTable(newTableName);
|
||||
const newPrimaryKeys = Object.values(newColumns).filter((c) => c.primaryKey);
|
||||
if (newPrimaryKeys.length > 1) {
|
||||
// @ts-ignore
|
||||
const constraints: any = await queryInterface.showConstraint(newTableName);
|
||||
await db.sequelize.transaction(
|
||||
{
|
||||
isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE,
|
||||
},
|
||||
async (transaction) => {
|
||||
if (this.db.isPostgresCompatibleDialect()) {
|
||||
const primaryKeys = constraints.filter((item) => item.constraintType === 'PRIMARY KEY');
|
||||
if (primaryKeys.length) {
|
||||
for (const primaryKey of primaryKeys) {
|
||||
await queryInterface.removeConstraint(newTableName, primaryKey.constraintName, { transaction });
|
||||
}
|
||||
}
|
||||
} else if (this.db.isMySQLCompatibleDialect()) {
|
||||
const idExists = await workflowManualTasksCollection.getField('id').existsInDb();
|
||||
if (!idExists) {
|
||||
await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} ADD COLUMN id BIGINT;`, {
|
||||
transaction,
|
||||
});
|
||||
await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} DROP PRIMARY KEY`, {
|
||||
transaction,
|
||||
});
|
||||
await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} ADD PRIMARY KEY (id)`, {
|
||||
transaction,
|
||||
});
|
||||
await db.sequelize.query(
|
||||
`ALTER TABLE ${newTableNameWithQuotes} MODIFY COLUMN id BIGINT AUTO_INCREMENT`,
|
||||
{
|
||||
transaction,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} DROP PRIMARY KEY`, {
|
||||
transaction,
|
||||
});
|
||||
await db.sequelize.query(`ALTER TABLE ${newTableNameWithQuotes} ADD PRIMARY KEY (id)`, {
|
||||
transaction,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const oldColumns = await queryInterface.describeTable(oldTableName);
|
||||
|
Loading…
x
Reference in New Issue
Block a user