Files
starr-philproperties/apps/api/database/migrations/20270101000067-create-completion-requirements.js
T
2026-08-29 16:02:47 +08:00

42 lines
1.8 KiB
JavaScript

'use strict';
// Raw SQL — Sequelize's createTable() wraps ENUM creation in a `DO $$...$$`
// block for idempotency, which CockroachDB rejects ("CREATE TYPE usage
// inside a function definition is not supported"). STRING + CHECK instead,
// same pattern used throughout this schema for CRDB compatibility.
module.exports = {
async up(queryInterface) {
await queryInterface.sequelize.query(`
CREATE TABLE completion_requirements (
requirement_id UUID PRIMARY KEY,
entity_type TEXT NOT NULL,
entity_id BIGINT NOT NULL,
type TEXT NOT NULL,
min_percent INTEGER NULL,
button_label TEXT NULL,
is_required BOOLEAN NOT NULL DEFAULT true,
"order" INTEGER NOT NULL DEFAULT 0,
"createdBy" BIGINT NULL,
"updatedBy" BIGINT NULL,
"deletedBy" BIGINT NULL,
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deletedAt" TIMESTAMPTZ NULL,
CONSTRAINT check_cr_entity_type CHECK (entity_type IN ('course', 'unit', 'lesson')),
CONSTRAINT check_cr_type CHECK (type IN ('read_all_content', 'pass_quiz', 'watch_percent', 'manual_complete', 'watch_video', 'listen_audio'))
);
`);
await queryInterface.sequelize.query(
`CREATE INDEX idx_cr_entity_type_entity_id ON completion_requirements (entity_type, entity_id);`
);
await queryInterface.sequelize.query(
`CREATE INDEX idx_cr_type ON completion_requirements (type);`
);
},
async down(queryInterface) {
await queryInterface.sequelize.query(`DROP TABLE IF EXISTS completion_requirements;`);
},
};