feat: implements article table
This commit is contained in:
@@ -12,7 +12,7 @@ CREATE TABLE users (
|
|||||||
role user_role NOT NULL DEFAULT 'user',
|
role user_role NOT NULL DEFAULT 'user',
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
updated_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`);
|
--end-sql`);
|
||||||
}
|
}
|
||||||
|
|||||||
28
migrations/1775010269415-adds-articles-table.ts
Normal file
28
migrations/1775010269415-adds-articles-table.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
|
export class AddsArticlesTable1775010269415 implements MigrationInterface {
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
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<void> {
|
||||||
|
await queryRunner.query(`--sql
|
||||||
|
DROP TABLE articles;
|
||||||
|
--end-sql`);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
migrations/1775011588385-adds-base-triggers.ts
Normal file
33
migrations/1775011588385-adds-base-triggers.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
|
export class AddsBaseTriggers1775011588385 implements MigrationInterface {
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
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<void> {
|
||||||
|
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`);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user