Skip to content

Commit

Permalink
feat: add table checklist_items
Browse files Browse the repository at this point in the history
Related #163
  • Loading branch information
UlisesGascon committed Dec 24, 2024
1 parent 8f29536 commit 0235afc
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/database/migrations/1735042950475_add_checklist_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const priorityGroupOptions = [
'P0', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'P10', 'P11', 'P12', 'P13', 'P14',
'R0', 'R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11', 'R12', 'R13', 'R14'
]

exports.up = async (knex) => {
await knex.schema.createTable('checklist_items', (table) => {
table.increments('id').primary() // Primary key
table.integer('checklist_id').unsigned().notNullable() // Foreign key to compliance_checklists
table.foreign('checklist_id').references('id').inTable('compliance_checklists').onDelete('CASCADE')

table.integer('compliance_check_id').unsigned().notNullable() // Foreign key to compliance_checks
table.foreign('compliance_check_id').references('id').inTable('compliance_checks').onDelete('CASCADE')

// Overrides for the association if needed
table.enum('priority_group', priorityGroupOptions).nullable()
table.string('section_number').nullable()
table.string('section_name').nullable()

// Timestamps
table.timestamp('created_at').defaultTo(knex.fn.now()).notNullable()
table.timestamp('updated_at').defaultTo(knex.fn.now()).notNullable()
})

// Add trigger to automatically update the 'updated_at' column
await knex.raw(`
CREATE TRIGGER set_updated_at_checklist_items
BEFORE UPDATE ON checklist_items
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
`)
}

exports.down = async (knex) => {
// Drop trigger
await knex.raw('DROP TRIGGER IF EXISTS set_updated_at_checklist_items ON checklist_items;')
// Drop table
await knex.schema.dropTableIfExists('checklist_items')
}

0 comments on commit 0235afc

Please sign in to comment.