chore(collection-manager): migration - 20221104151410-update-collections-hidden (#1039)

This commit is contained in:
lyf-coder 2022-11-04 17:17:35 +08:00 committed by GitHub
parent b425ac0413
commit dd20951323
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,47 @@
import { Database, MigrationContext, mockDatabase } from '@nocobase/database';
import UpdateCollectionsHiddenMigration from '../../migrations/20221104151410-update-collections-hidden';
import { createApp } from '../index';
import { MockServer } from '@nocobase/test';
describe('migration 20221104151410-update-collections-hidden test', () => {
let app: MockServer;
let db: Database;
beforeEach(async () => {
app = await createApp();
db = app.db;
});
afterEach(async () => {
await app.destroy();
});
it('20221104151410-update-collections-hidden up method test', async () => {
await db.getCollection('collections').model.create({
name: 'test',
options: {
autoCreate: true,
isThrough: true,
},
hidden: false,
});
const result = await db.getRepository('collections').findOne({
filter: {
name: 'test',
},
});
expect(result.get('hidden')).toBeFalsy();
const migration = new UpdateCollectionsHiddenMigration({ db } as MigrationContext);
migration.context.app = app;
await migration.up();
const upResult = await db.getRepository('collections').findOne({
filter: {
name: 'test',
},
});
expect(upResult.get('hidden')).toBeTruthy();
});
});

View File

@ -0,0 +1,31 @@
import { Migration } from '@nocobase/server';
export default class UpdateCollectionsHiddenMigration extends Migration {
async up() {
const result = await this.app.version.satisfies('<=0.8.0-alpha.9');
if (!result) {
return;
}
const db = this.app.db;
const transaction = await db.sequelize.transaction();
try {
await db.getRepository('collections').update({
filter: {
options: {
autoCreate: true,
isThrough: true,
},
},
values: {
hidden: true,
},
transaction,
});
await transaction.commit();
} catch (error) {
console.log(error);
await transaction.rollback();
}
}
}