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:
ian
2022-03-25 22:35:42 +08:00
committed by GitHub
parent eafc4a6c69
commit 998bdae567
2 changed files with 56 additions and 2 deletions

View File

@@ -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 });
}
}
}

View 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
}