Skip to content

Commit

Permalink
Merge pull request #9 from codermuss/migrate/blog_to_post
Browse files Browse the repository at this point in the history
Migrate/blog to post
  • Loading branch information
codermuss authored Jul 28, 2024
2 parents 4df1100 + 4db3f29 commit 5574df0
Show file tree
Hide file tree
Showing 34 changed files with 867 additions and 867 deletions.
4 changes: 2 additions & 2 deletions api/blog.go → api/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func (server *Server) GetBlogs(ctx *gin.Context) {

}

arg := db.GetBlogsParams{
arg := db.GetPostsParams{
Limit: int32(size),
Offset: int32((page - 1) * size),
}

blogs, err := server.store.GetBlogs(ctx, arg)
blogs, err := server.store.GetPosts(ctx, arg)
if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusInternalServerError,
Expand Down
28 changes: 14 additions & 14 deletions db/migration/000001_initial.down.sql
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
-- Drop foreign key constraints first
ALTER TABLE "sessions" DROP CONSTRAINT "sessions_user_id_fkey";
ALTER TABLE "blog_likes" DROP CONSTRAINT "blog_likes_user_id_fkey";
ALTER TABLE "blog_likes" DROP CONSTRAINT "blog_likes_blog_id_fkey";
ALTER TABLE "post_likes" DROP CONSTRAINT "post_likes_user_id_fkey";
ALTER TABLE "post_likes" DROP CONSTRAINT "post_likes_post_id_fkey";
ALTER TABLE "comments" DROP CONSTRAINT "comments_user_id_fkey";
ALTER TABLE "comments" DROP CONSTRAINT "comments_blog_id_fkey";
ALTER TABLE "comments" DROP CONSTRAINT "comments_post_id_fkey";
ALTER TABLE "user_followers" DROP CONSTRAINT "user_followers_follower_id_fkey";
ALTER TABLE "user_followers" DROP CONSTRAINT "user_followers_user_id_fkey";
ALTER TABLE "user_posts" DROP CONSTRAINT "user_posts_blog_id_fkey";
ALTER TABLE "user_posts" DROP CONSTRAINT "user_posts_post_id_fkey";
ALTER TABLE "user_posts" DROP CONSTRAINT "user_posts_user_id_fkey";
ALTER TABLE "profiles" DROP CONSTRAINT "profiles_user_id_fkey";
ALTER TABLE "featured_stories" DROP CONSTRAINT "featured_stories_blog_id_fkey";
ALTER TABLE "blog_categories" DROP CONSTRAINT "blog_categories_category_id_fkey";
ALTER TABLE "blog_categories" DROP CONSTRAINT "blog_categories_blog_id_fkey";
ALTER TABLE "blog_tags" DROP CONSTRAINT "blog_tags_tag_id_fkey";
ALTER TABLE "blog_tags" DROP CONSTRAINT "blog_tags_blog_id_fkey";
ALTER TABLE "blogs" DROP CONSTRAINT "blogs_user_id_fkey";
ALTER TABLE "featured_stories" DROP CONSTRAINT "featured_stories_post_id_fkey";
ALTER TABLE "post_categories" DROP CONSTRAINT "post_categories_category_id_fkey";
ALTER TABLE "post_categories" DROP CONSTRAINT "post_categories_post_id_fkey";
ALTER TABLE "post_tags" DROP CONSTRAINT "post_tags_tag_id_fkey";
ALTER TABLE "post_tags" DROP CONSTRAINT "post_tags_post_id_fkey";
ALTER TABLE "posts" DROP CONSTRAINT "posts_user_id_fkey";

-- Drop tables in reverse order of creation
DROP TABLE IF EXISTS "sessions";
DROP TABLE IF EXISTS "blog_likes";
DROP TABLE IF EXISTS "post_likes";
DROP TABLE IF EXISTS "comments";
DROP TABLE IF EXISTS "user_followers";
DROP TABLE IF EXISTS "user_posts";
DROP TABLE IF EXISTS "profiles";
DROP TABLE IF EXISTS "featured_stories";
DROP TABLE IF EXISTS "blog_categories";
DROP TABLE IF EXISTS "blog_tags";
DROP TABLE IF EXISTS "post_categories";
DROP TABLE IF EXISTS "post_tags";
DROP TABLE IF EXISTS "tags";
DROP TABLE IF EXISTS "blogs";
DROP TABLE IF EXISTS "posts";
DROP TABLE IF EXISTS "categories";
DROP TABLE IF EXISTS "users";
DROP TABLE IF EXISTS "onboarding";
50 changes: 25 additions & 25 deletions db/migration/000001_initial.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CREATE TABLE "categories" (
"name" varchar(50) UNIQUE NOT NULL
);

CREATE TABLE "blogs" (
CREATE TABLE "posts" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"user_id" int,
"title" varchar(255) NOT NULL,
Expand All @@ -40,36 +40,36 @@ CREATE TABLE "tags" (
"name" varchar(50) UNIQUE NOT NULL
);

CREATE TABLE "blog_tags" (
"blog_id" int NOT NULL,
CREATE TABLE "post_tags" (
"post_id" int NOT NULL,
"tag_id" int NOT NULL,
PRIMARY KEY ("blog_id", "tag_id")
PRIMARY KEY ("post_id", "tag_id")
);

CREATE TABLE "blog_categories" (
"blog_id" int NOT NULL,
CREATE TABLE "post_categories" (
"post_id" int NOT NULL,
"category_id" int NOT NULL,
PRIMARY KEY ("blog_id", "category_id")
PRIMARY KEY ("post_id", "category_id")
);

CREATE TABLE "featured_stories" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"blog_id" int NOT NULL,
"post_id" int NOT NULL,
"featured_date" date DEFAULT (current_date)
);

CREATE TABLE "profiles" (
"user_id" int PRIMARY KEY,
"bio" text,
"blog_count" int DEFAULT 0,
"post_count" int DEFAULT 0,
"like_count" int DEFAULT 0,
"follower_count" int DEFAULT 0
);

CREATE TABLE "user_posts" (
"user_id" int NOT NULL,
"blog_id" int NOT NULL,
PRIMARY KEY ("user_id", "blog_id")
"post_id" int NOT NULL,
PRIMARY KEY ("user_id", "post_id")
);

CREATE TABLE "user_followers" (
Expand All @@ -80,16 +80,16 @@ CREATE TABLE "user_followers" (

CREATE TABLE "comments" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"blog_id" int NOT NULL,
"post_id" int NOT NULL,
"user_id" int NOT NULL,
"content" text NOT NULL,
"created_at" timestamptz DEFAULT (now())
);

CREATE TABLE "blog_likes" (
"blog_id" int NOT NULL,
CREATE TABLE "post_likes" (
"post_id" int NOT NULL,
"user_id" int NOT NULL,
PRIMARY KEY ("blog_id", "user_id")
PRIMARY KEY ("post_id", "user_id")
);

CREATE TABLE "sessions" (
Expand All @@ -103,34 +103,34 @@ CREATE TABLE "sessions" (
"created_at" timestamptz NOT NULL DEFAULT (now())
);

ALTER TABLE "blogs" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "posts" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "blog_tags" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id");
ALTER TABLE "post_tags" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");

ALTER TABLE "blog_tags" ADD FOREIGN KEY ("tag_id") REFERENCES "tags" ("id");
ALTER TABLE "post_tags" ADD FOREIGN KEY ("tag_id") REFERENCES "tags" ("id");

ALTER TABLE "blog_categories" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id");
ALTER TABLE "post_categories" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");

ALTER TABLE "blog_categories" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id");
ALTER TABLE "post_categories" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id");

ALTER TABLE "featured_stories" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id");
ALTER TABLE "featured_stories" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");

ALTER TABLE "profiles" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "user_posts" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "user_posts" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id");
ALTER TABLE "user_posts" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");

ALTER TABLE "user_followers" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "user_followers" ADD FOREIGN KEY ("follower_id") REFERENCES "users" ("id");

ALTER TABLE "comments" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");

ALTER TABLE "comments" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "blog_likes" ADD FOREIGN KEY ("blog_id") REFERENCES "blogs" ("id");
ALTER TABLE "post_likes" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");

ALTER TABLE "blog_likes" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "post_likes" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "sessions" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
Loading

0 comments on commit 5574df0

Please sign in to comment.