aspans_patrol/apps/api/src/database/migrations/1761200000000-Checklists.ts

67 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { MigrationInterface, QueryRunner } from 'typeorm';
/**
* Чеклисты.
*
* checklist_items — справочник пунктов компании («Стёкла не выбиты»). Один пункт — одна запись,
* сколько бы чеклистов его ни включали. У другой компании такой же текст — своя запись.
* checklists + checklist_entries — именованные списки пунктов с порядком.
* checkpoint_checklists — какие чеклисты висят на контрольной точке маршрута.
* checkpoint_visits.checklist — ответы охранника: [{ checklistId, itemId, text, checked }].
*/
export class Checklists1761200000000 implements MigrationInterface {
name = 'Checklists1761200000000';
async up(q: QueryRunner): Promise<void> {
await q.query(`
CREATE TABLE checklist_items (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id uuid NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
text text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
)`);
// Один и тот же текст в компании — один пункт: без учёта регистра, пробелов по краям и «ё»/«е».
await q.query(`CREATE UNIQUE INDEX checklist_items_org_text_key ON checklist_items (organization_id, replace(lower(btrim(text)), 'ё', 'е'))`);
await q.query(`
CREATE TABLE checklists (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
organization_id uuid NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
name text NOT NULL,
description text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
archived_at timestamptz
)`);
await q.query(`CREATE INDEX checklists_org_idx ON checklists (organization_id) WHERE archived_at IS NULL`);
await q.query(`
CREATE TABLE checklist_entries (
checklist_id uuid NOT NULL REFERENCES checklists(id) ON DELETE CASCADE,
item_id uuid NOT NULL REFERENCES checklist_items(id) ON DELETE RESTRICT,
position int NOT NULL,
PRIMARY KEY (checklist_id, item_id)
)`);
await q.query(`CREATE INDEX checklist_entries_item_idx ON checklist_entries (item_id)`);
await q.query(`
CREATE TABLE checkpoint_checklists (
checkpoint_id uuid NOT NULL REFERENCES checkpoints(id) ON DELETE CASCADE,
checklist_id uuid NOT NULL REFERENCES checklists(id) ON DELETE CASCADE,
position int NOT NULL,
PRIMARY KEY (checkpoint_id, checklist_id)
)`);
await q.query(`CREATE INDEX checkpoint_checklists_checklist_idx ON checkpoint_checklists (checklist_id)`);
await q.query(`ALTER TABLE checkpoint_visits ADD COLUMN checklist jsonb NOT NULL DEFAULT '[]'`);
}
async down(q: QueryRunner): Promise<void> {
await q.query(`ALTER TABLE checkpoint_visits DROP COLUMN checklist`);
await q.query(`DROP TABLE checkpoint_checklists`);
await q.query(`DROP TABLE checklist_entries`);
await q.query(`DROP TABLE checklists`);
await q.query(`DROP TABLE checklist_items`);
}
}