exports.up = async function(knex) { // user reports about entities, primarily to flag broken scrapers await knex.schema.createTable('reports', (table) => { table.increments('id'); table.integer('entity_id') .notNullable() .references('id') .inTable('entities') .onDelete('cascade'); table.text('reasons'); // comma-separated reason keys, see reportReasons in traxxx-web table.text('comment'); table.integer('user_id') .references('id') .inTable('users') .onDelete('set null'); table.boolean('is_read') .notNullable() .defaultTo(false); table.boolean('is_completed') .notNullable() .defaultTo(false); table.datetime('created_at') .notNullable() .defaultTo(knex.fn.now()); }); // a user can't report the same entity again while their previous report is still open await knex.raw(` CREATE UNIQUE INDEX reports_open_entity_user_unique ON reports (entity_id, user_id) WHERE is_completed = false `); }; exports.down = async function(knex) { await knex.schema.dropTable('reports'); };