Use field key instead of ID for group value (#8349)

* Add migration to replace group ID with group name

* Replace group IDs with field names

* Tweak group interfaces

* Ignore schema export
This commit is contained in:
Rijk van Zanten
2021-09-27 15:06:19 -04:00
committed by GitHub
parent a03aee614a
commit f5f68417cf
9 changed files with 78 additions and 13 deletions

View File

@@ -0,0 +1,63 @@
import { Knex } from 'knex';
import { uniq } from 'lodash';
export async function up(knex: Knex): Promise<void> {
const groupsInUse = await knex.select('id', 'group').from('directus_fields').whereNotNull('group');
if (groupsInUse.length === 0) return;
const groupIDs: number[] = uniq(groupsInUse.map(({ group }) => group));
const groupFields = await knex.select('id', 'field').from('directus_fields').whereIn('id', groupIDs);
const groupMap = new Map();
for (const { id, field } of groupFields) {
groupMap.set(id, field);
}
await knex.schema.alterTable('directus_fields', (table) => {
table.dropColumn('group');
});
await knex.schema.alterTable('directus_fields', (table) => {
table.string('group', 64);
});
for (const { id, group } of groupsInUse) {
await knex('directus_fields')
.update({ group: groupMap.get(group) })
.where({ id });
}
}
export async function down(knex: Knex): Promise<void> {
const fieldsThatUseAGroup = await knex
.select('id', 'collection', 'group')
.from('directus_fields')
.whereNotNull('group');
if (fieldsThatUseAGroup.length === 0) return;
const groupMap = new Map();
for (const { collection, group } of fieldsThatUseAGroup) {
const { id } = await knex.select('id').from('directus_fields').where({ collection, field: group }).first();
groupMap.set(group, id);
}
await knex.schema.alterTable('directus_fields', (table) => {
table.dropColumn('group');
});
await knex.schema.alterTable('directus_fields', (table) => {
table.integer('group').references('id').inTable('directus_fields');
});
for (const { id, group } of fieldsThatUseAGroup) {
await knex('directus_fields')
.update({ group: groupMap.get(group) })
.where({ id });
}
}