Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions db/drop.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { exit } from 'node:process';
import { db } from '@/database.providers';
import { articles, favoriteArticles } from '@articles/articles.model';
import { articles, comments, favoriteArticles } from '@articles/articles.model';
import dbConfig from '@db/config';
import { articleTags, tags } from '@tags/tags.model';
import { userFollows, users } from '@users/users.model';
import { getTableName, sql } from 'drizzle-orm';

const tables = [userFollows, favoriteArticles, articles, users];
const tables = [
articleTags,
tags,
userFollows,
favoriteArticles,
comments,
articles,
users,
];
console.log('Dropping all tables from the database');

try {
Expand Down
34 changes: 34 additions & 0 deletions db/migrations/0008_tough_wolfsbane.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CREATE TABLE "comments" (
"id" serial PRIMARY KEY NOT NULL,
"body" text NOT NULL,
"article_id" integer NOT NULL,
"author_id" integer NOT NULL,
"created_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
"updated_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL
);

CREATE TABLE "article_tags" (
"article_id" integer NOT NULL,
"tag_name" text NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "article_tags_article_id_tag_name_pk" PRIMARY KEY("article_id","tag_name")
);

CREATE TABLE "tags" (
"name" text PRIMARY KEY NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);

ALTER TABLE "comments" ADD CONSTRAINT "comments_article_id_articles_id_fk" FOREIGN KEY ("article_id") REFERENCES "public"."articles"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "comments" ADD CONSTRAINT "comments_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "article_tags" ADD CONSTRAINT "article_tags_article_id_articles_id_fk" FOREIGN KEY ("article_id") REFERENCES "public"."articles"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "article_tags" ADD CONSTRAINT "article_tags_tag_name_tags_name_fk" FOREIGN KEY ("tag_name") REFERENCES "public"."tags"("name") ON DELETE cascade ON UPDATE no action;


-- Seed tags and article_tags before dropping tag_list column
INSERT INTO tags (name) SELECT DISTINCT unnest(tag_list) FROM articles;
INSERT INTO article_tags (article_id, tag_name) SELECT id, unnest(tag_list) FROM articles;

ALTER TABLE "articles" DROP COLUMN "tag_list";
Loading
Loading