Skip to content

Commit

Permalink
Handle illegal/corrupt relational rows better
Browse files Browse the repository at this point in the history
Fixes #6013
  • Loading branch information
rijkvanzanten committed Jun 3, 2021
1 parent 1b49dd0 commit e985143
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function up(knex: Knex): Promise<void> {

const foreignKeys = await inspector.foreignKeys();
const relations = await knex
.select<RelationMeta[]>('many_collection', 'many_field', 'one_collection')
.select<RelationMeta[]>('id', 'many_collection', 'many_field', 'one_collection')
.from('directus_relations');

const constraintsToAdd = relations.filter((relation) => {
Expand All @@ -18,11 +18,34 @@ export async function up(knex: Knex): Promise<void> {
return exists === false;
});

const corruptedRelations: number[] = [];

for (const constraint of constraintsToAdd) {
if (!constraint.one_collection) continue;

if (
(await inspector.hasTable(constraint.many_collection)) === false ||
(await inspector.hasTable(constraint.one_collection)) === false
) {
logger.warn(
`Ignoring ${constraint.many_collection}.${constraint.many_field}<->${constraint.one_collection}. Tables don't exist.`
);

corruptedRelations.push(constraint.id);
continue;
}

const currentPrimaryKeyField = await inspector.primary(constraint.many_collection);
const relatedPrimaryKeyField = await inspector.primary(constraint.one_collection);

if (constraint.many_field === currentPrimaryKeyField) {
logger.warn(
`Illegal relationship ${constraint.many_collection}.${constraint.many_field}<->${constraint.one_collection} encountered. Many field equals collections primary key.`
);
corruptedRelations.push(constraint.id);
continue;
}

if (!currentPrimaryKeyField || !relatedPrimaryKeyField) continue;

const rowsWithIllegalFKValues = await knex
Expand Down Expand Up @@ -87,11 +110,19 @@ export async function up(knex: Knex): Promise<void> {
});
} catch (err) {
logger.warn(
`Couldn't add foreign key constraint for ${constraint.many_collection}.${constraint.many_field}->${constraint.one_collection}`
`Couldn't add foreign key constraint for ${constraint.many_collection}.${constraint.many_field}<->${constraint.one_collection}`
);
logger.warn(err);
}
}

if (corruptedRelations.length > 0) {
logger.warn(
`Encountered one or more corrupted relationships. Please check the following rows in "directus_relations": ${corruptedRelations.join(
', '
)}`
);
}
}

export async function down(knex: Knex): Promise<void> {
Expand All @@ -108,7 +139,7 @@ export async function down(knex: Knex): Promise<void> {
});
} catch (err) {
logger.warn(
`Couldn't drop foreign key constraint for ${relation.many_collection}.${relation.many_field}->${relation.one_collection}`
`Couldn't drop foreign key constraint for ${relation.many_collection}.${relation.many_field}<->${relation.one_collection}`
);
logger.warn(err);
}
Expand Down

0 comments on commit e985143

Please sign in to comment.