From 324653dd2a8e64db0e59f3a0554968b606835073 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 31 Mar 2026 23:55:53 -0300 Subject: [PATCH] feat: implements article table --- migrations/1774988438124-adds-user-table.ts | 2 +- .../1775010269415-adds-articles-table.ts | 28 ++++++++++++++++ .../1775011588385-adds-base-triggers.ts | 33 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 migrations/1775010269415-adds-articles-table.ts create mode 100644 migrations/1775011588385-adds-base-triggers.ts diff --git a/migrations/1774988438124-adds-user-table.ts b/migrations/1774988438124-adds-user-table.ts index 3708661..b7bb4c6 100644 --- a/migrations/1774988438124-adds-user-table.ts +++ b/migrations/1774988438124-adds-user-table.ts @@ -12,7 +12,7 @@ CREATE TABLE users ( role user_role NOT NULL DEFAULT 'user', created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), - external_id UUID NOT NULL DEFAULT gen_random_uuid() + external_id UUID NOT NULL DEFAULT gen_random_uuid() UNIQUE ); --end-sql`); } diff --git a/migrations/1775010269415-adds-articles-table.ts b/migrations/1775010269415-adds-articles-table.ts new file mode 100644 index 0000000..ba4fcd5 --- /dev/null +++ b/migrations/1775010269415-adds-articles-table.ts @@ -0,0 +1,28 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddsArticlesTable1775010269415 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`--sql +CREATE TABLE articles ( + id BIGSERIAL PRIMARY KEY, + title VARCHAR(255) NOT NULL, + slug VARCHAR(255) NOT NULL UNIQUE, + description TEXT NOT NULL, + cover_image_url TEXT NOT NULL, + content TEXT NOT NULL, + author_id BIGINT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + external_id UUID NOT NULL DEFAULT gen_random_uuid() UNIQUE, + + FOREIGN KEY (author_id) REFERENCES users (id) ON DELETE CASCADE +); +--end-sql`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`--sql +DROP TABLE articles; +--end-sql`); + } +} diff --git a/migrations/1775011588385-adds-base-triggers.ts b/migrations/1775011588385-adds-base-triggers.ts new file mode 100644 index 0000000..8370791 --- /dev/null +++ b/migrations/1775011588385-adds-base-triggers.ts @@ -0,0 +1,33 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddsBaseTriggers1775011588385 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query(` +CREATE OR REPLACE FUNCTION set_updated_at() +RETURNS TRIGGER AS $$ +BEGIN + NEW.updated_at = now(); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER set_articles_updated_at +BEFORE UPDATE ON articles +FOR EACH ROW +EXECUTE FUNCTION set_updated_at(); + +CREATE TRIGGER set_users_updated_at +BEFORE UPDATE on users +FOR EACH ROW +EXECUTE FUNCTION set_updated_at(); + --end-sql`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`--sql +DROP TRIGGER set_articles_updated_at ON articles; +DROP TRIGGER set_users_updated_at ON users; +DROP FUNCTION set_updated_at(); + --end-sql`); + } +}