<commit_before><commit_msg>chore(data): add cascading delete where appropriate<commit_after>function getKey(table, foreignKey) {
  return `${table}_${foreignKey}_fkey`;
}

function addCascadingDelete(knex, childTable, foreignKey, primaryKey, parentTable) {
  return knex.schema.alterTable(childTable, (table) => {
    const key = getKey(childTable, foreignKey);
    table.dropForeign(foreignKey, key);
    table.foreign(foreignKey, key).references(primaryKey).inTable(parentTable).onDelete('CASCADE');
  });
}

function removeCascadingDelete(knex, childTable, foreignKey, primaryKey, parentTable) {
  return knex.schema.alterTable(childTable, (table) => {
    const key = getKey(childTable, foreignKey);
    table.dropForeign(foreignKey, key);
    table.foreign(foreignKey, key).references(primaryKey).inTable(parentTable);
  });
}

const INFOS = [
  ['users_have_notification_areas', 'user_id', 'id', 'users'],
  ['users_have_notification_areas', 'area_id', 'id', 'notification_areas'],
  ['routes', 'user_id', 'id', 'users'],
  ['report_infos', 'user_id', 'id', 'users'],
  ['public_reports', 'report_id', 'report_id', 'report_infos'],
  ['pickup_reports', 'report_id', 'report_id', 'report_infos'],
  ['pickup_reports', 'route_id', 'route_id', 'routes'],
];

/**
 * @param { import("knex").Knex } knex
 * @returns { Promise<void> }
 */
exports.up = function (knex) {
  return Promise.all(INFOS.map((info) => addCascadingDelete(knex, ...info)));
};

/**
 * @param { import("knex").Knex } knex
 * @returns { Promise<void> }
 */
exports.down = function (knex) {
  return Promise.all(INFOS.map((info) => removeCascadingDelete(knex, ...info)));
};
{'fraction_non_alphanumeric': 0.182370820668693, 'fraction_numerical': 0.0, 'mean_word_length': 6.064377682403434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6714477', 'n_tokens_mistral': 571, 'n_tokens_neox': 559, 'n_words': 98}