fix: update sequence and foreignKey (#1116)
* fix: update sequence and foreignKey * chore: rename migration
This commit is contained in:
parent
fc7da4a04c
commit
c861a6884a
@ -17,23 +17,6 @@ describe('collection', () => {
|
||||
afterEach(async () => {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
test.skip('indexes', async () => {
|
||||
await db.clean({ drop: true });
|
||||
const collection = db.collection({
|
||||
name: 'test',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
index: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
collection.removeField('name');
|
||||
await db.sync();
|
||||
});
|
||||
|
||||
test('removeFromDb', async () => {
|
||||
await db.clean({ drop: true });
|
||||
const collection = db.collection({
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Database, MigrationContext } from '@nocobase/database';
|
||||
import Migrator from '../../migrations/20221117111110-update-id-to-bigint';
|
||||
import Migrator from '../../migrations/20221121111110-update-id-to-bigint';
|
||||
|
||||
const excludeSqlite = () => (process.env.DB_DIALECT != 'sqlite' ? describe : describe.skip);
|
||||
|
||||
|
@ -7,9 +7,19 @@ export default class UpdateIdToBigIntMigrator extends Migration {
|
||||
|
||||
await db.getCollection('fields').repository.update({
|
||||
filter: {
|
||||
$or: [
|
||||
{
|
||||
name: 'id',
|
||||
type: 'integer',
|
||||
},
|
||||
{
|
||||
options: {
|
||||
isForeignKey: true,
|
||||
},
|
||||
type: 'integer',
|
||||
},
|
||||
],
|
||||
},
|
||||
values: {
|
||||
type: 'bigInt',
|
||||
},
|
||||
@ -62,6 +72,29 @@ export default class UpdateIdToBigIntMigrator extends Migration {
|
||||
throw err;
|
||||
}
|
||||
|
||||
const collection = db.modelCollection.get(model);
|
||||
const fieldRecord = await db.getCollection('fields').repository.findOne({
|
||||
filter: {
|
||||
collectionName: collection.name,
|
||||
name: fieldName,
|
||||
type: 'integer',
|
||||
},
|
||||
});
|
||||
|
||||
if (fieldRecord) {
|
||||
fieldRecord.set('type', 'bigInt');
|
||||
await fieldRecord.save();
|
||||
}
|
||||
|
||||
if (db.inDialect('postgres')) {
|
||||
const sequenceQuery = `SELECT pg_get_serial_sequence('"${model.tableName}"', '${fieldName}');`;
|
||||
const [result] = await this.sequelize.query(sequenceQuery, {});
|
||||
const sequenceName = result[0]['pg_get_serial_sequence'];
|
||||
|
||||
if (sequenceName) {
|
||||
await this.sequelize.query(`ALTER SEQUENCE ${sequenceName} AS BIGINT;`, {});
|
||||
}
|
||||
}
|
||||
this.app.log.info(`updated ${tableName}.${fieldName} to BIGINT`, tableName, fieldName);
|
||||
}
|
||||
};
|
||||
@ -90,6 +123,7 @@ export default class UpdateIdToBigIntMigrator extends Migration {
|
||||
}
|
||||
|
||||
const associations = model.associations;
|
||||
|
||||
for (const associationName of Object.keys(associations)) {
|
||||
const association = associations[associationName];
|
||||
|
Loading…
Reference in New Issue
Block a user