mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Fix typecast migration array (#12372)
* Fix incorrect saving of field special in migration * Add hotfix * Run typecasting again just in case... * Rename migration Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -34,7 +34,9 @@ export async function up(knex: Knex): Promise<void> {
|
||||
});
|
||||
|
||||
if (updateRequired) {
|
||||
await knex('directus_fields').update({ special: parsedSpecial }).where({ id });
|
||||
await knex('directus_fields')
|
||||
.update({ special: parsedSpecial.join(',') })
|
||||
.where({ id });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +73,9 @@ export async function down(knex: Knex): Promise<void> {
|
||||
});
|
||||
|
||||
if (updateRequired) {
|
||||
await knex('directus_fields').update({ special: parsedSpecial }).where({ id });
|
||||
await knex('directus_fields')
|
||||
.update({ special: parsedSpecial.join(',') })
|
||||
.where({ id });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
50
api/src/database/migrations/20220325A-fix-typecast-flags.ts
Normal file
50
api/src/database/migrations/20220325A-fix-typecast-flags.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { toArray } from '@directus/shared/utils';
|
||||
import { Knex } from 'knex';
|
||||
import { isArray } from 'lodash';
|
||||
|
||||
export async function up(knex: Knex): Promise<void> {
|
||||
const fields = await knex
|
||||
.select<{ id: number; special: string }[]>('id', 'special')
|
||||
.from('directus_fields')
|
||||
.whereNotNull('special')
|
||||
.orWhere('special', '<>', '');
|
||||
|
||||
for (const { id, special } of fields) {
|
||||
let parsedSpecial;
|
||||
|
||||
try {
|
||||
if (special.includes('{')) {
|
||||
// Fix invalid data in Postgres
|
||||
parsedSpecial = toArray(special.replace(/{/g, '').replace(/}/g, '').replace(/"/g, ''));
|
||||
} else {
|
||||
parsedSpecial = toArray(special);
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (parsedSpecial && isArray(parsedSpecial)) {
|
||||
// Perform the update again in case it was not performed prior
|
||||
parsedSpecial = parsedSpecial.map((special) => {
|
||||
switch (special) {
|
||||
case 'boolean':
|
||||
case 'csv':
|
||||
case 'json':
|
||||
return 'cast-' + special;
|
||||
default:
|
||||
return special;
|
||||
}
|
||||
});
|
||||
|
||||
const parsedSpecialString = parsedSpecial.join(',');
|
||||
|
||||
if (parsedSpecialString !== special) {
|
||||
await knex('directus_fields').update({ special: parsedSpecialString }).where({ id });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function down(_knex: Knex): Promise<void> {
|
||||
// Do nothing
|
||||
}
|
||||
Reference in New Issue
Block a user