From 29b35e931a4b4656aa0d5f70af68709753e3986a Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 15 Oct 2024 19:55:56 +0300 Subject: [PATCH] sqlc --- enshi_back/db/db.go | 32 +++++++++ enshi_back/db/migrations/migration1.sql | 28 ++++++++ enshi_back/db/models.go | 92 +++++++++++++++++++++++++ enshi_back/db/queries/user_queries.sql | 4 ++ enshi_back/db/sqlc.yml | 17 +++++ enshi_back/db/user_queries.sql.go | 40 +++++++++++ enshi_back/main.go | 18 +++++ enshi_back/utils/database.go | 3 + 8 files changed, 234 insertions(+) create mode 100644 enshi_back/db/db.go create mode 100644 enshi_back/db/migrations/migration1.sql create mode 100644 enshi_back/db/models.go create mode 100644 enshi_back/db/queries/user_queries.sql create mode 100644 enshi_back/db/sqlc.yml create mode 100644 enshi_back/db/user_queries.sql.go diff --git a/enshi_back/db/db.go b/enshi_back/db/db.go new file mode 100644 index 0000000..f2cd0e1 --- /dev/null +++ b/enshi_back/db/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 + +package db_repo + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/enshi_back/db/migrations/migration1.sql b/enshi_back/db/migrations/migration1.sql new file mode 100644 index 0000000..ce6fea4 --- /dev/null +++ b/enshi_back/db/migrations/migration1.sql @@ -0,0 +1,28 @@ +-- Add new schema named "public" +CREATE SCHEMA IF NOT EXISTS "public"; +-- Set comment to schema: "public" +COMMENT ON SCHEMA "public" IS 'standard public schema'; +-- Create "categories" table +CREATE TABLE "public"."categories" ("category_id" integer NOT NULL, "category_name" character varying(50) NOT NULL, PRIMARY KEY ("category_id"), CONSTRAINT "categories_category_name_key" UNIQUE ("category_name")); +-- Create "users" table +CREATE TABLE "public"."users" ("user_id" bigint NOT NULL, "username" character varying(50) NOT NULL, "email" character varying(100) NOT NULL, "password" character varying(255) NOT NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("user_id"), CONSTRAINT "users_email_key" UNIQUE ("email"), CONSTRAINT "users_username_key" UNIQUE ("username")); +-- Create "blogs" table +CREATE TABLE "public"."blogs" ("blog_id" bigint NOT NULL, "user_id" bigint NOT NULL, "title" character varying(255) NULL, "description" text NULL, "category_id" integer NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("blog_id"), CONSTRAINT "blogs_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "public"."categories" ("category_id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "blogs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE); +-- Create "posts" table +CREATE TABLE "public"."posts" ("post_id" bigint NOT NULL, "blog_id" bigint NULL, "user_id" bigint NULL, "title" character varying(255) NULL, "content" text NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("post_id"), CONSTRAINT "posts_blog_id_fkey" FOREIGN KEY ("blog_id") REFERENCES "public"."blogs" ("blog_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "posts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE); +-- Create "bookmarks" table +CREATE TABLE "public"."bookmarks" ("user_id" bigint NOT NULL, "post_id" bigint NOT NULL, "bookmarked_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("user_id", "post_id"), CONSTRAINT "bookmarks_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "bookmarks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE); +-- Create "comments" table +CREATE TABLE "public"."comments" ("comment_id" bigint NOT NULL, "post_id" bigint NULL, "user_id" bigint NULL, "content" text NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("comment_id"), CONSTRAINT "comments_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE); +-- Create "favorites" table +CREATE TABLE "public"."favorites" ("user_id" bigint NOT NULL, "blog_id" bigint NOT NULL, "favorited_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("user_id", "blog_id"), CONSTRAINT "favorites_blog_id_fkey" FOREIGN KEY ("blog_id") REFERENCES "public"."blogs" ("blog_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "favorites_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE); +-- Create "likes" table +CREATE TABLE "public"."likes" ("like_id" bigint NOT NULL, "user_id" bigint NULL, "comment_id" bigint NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("like_id"), CONSTRAINT "likes_comment_id_fkey" FOREIGN KEY ("comment_id") REFERENCES "public"."comments" ("comment_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "likes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE); +-- Create "tags" table +CREATE TABLE "public"."tags" ("tag_id" integer NOT NULL, "tag_name" character varying(50) NOT NULL, PRIMARY KEY ("tag_id"), CONSTRAINT "tags_tag_name_key" UNIQUE ("tag_name")); +-- Create "post_tags" table +CREATE TABLE "public"."post_tags" ("post_id" bigint NOT NULL, "tag_id" integer NOT NULL, PRIMARY KEY ("post_id", "tag_id"), CONSTRAINT "post_tags_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "post_tags_tag_id_fkey" FOREIGN KEY ("tag_id") REFERENCES "public"."tags" ("tag_id") ON UPDATE NO ACTION ON DELETE CASCADE); +-- Create "post_votes" table +CREATE TABLE "public"."post_votes" ("post_id" bigint NOT NULL, "user_id" bigint NOT NULL, "vote" boolean NOT NULL, PRIMARY KEY ("post_id", "user_id"), CONSTRAINT "post_votes_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "public"."posts" ("post_id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "post_votes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE NO ACTION); +-- Create "profiles" table +CREATE TABLE "public"."profiles" ("profile_id" bigint NOT NULL, "user_id" bigint NULL, "bio" text NULL, "avatar_url" character varying(255) NULL, "website_url" character varying(100) NULL, PRIMARY KEY ("profile_id"), CONSTRAINT "profiles_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE); diff --git a/enshi_back/db/models.go b/enshi_back/db/models.go new file mode 100644 index 0000000..9081f33 --- /dev/null +++ b/enshi_back/db/models.go @@ -0,0 +1,92 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 + +package db_repo + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type Blog struct { + BlogID int64 `json:"blog_id"` + UserID int64 `json:"user_id"` + Title pgtype.Text `json:"title"` + Description pgtype.Text `json:"description"` + CategoryID pgtype.Int4 `json:"category_id"` + CreatedAt pgtype.Timestamp `json:"created_at"` +} + +type Bookmark struct { + UserID int64 `json:"user_id"` + PostID int64 `json:"post_id"` + BookmarkedAt pgtype.Timestamp `json:"bookmarked_at"` +} + +type Category struct { + CategoryID int32 `json:"category_id"` + CategoryName string `json:"category_name"` +} + +type Comment struct { + CommentID int64 `json:"comment_id"` + PostID pgtype.Int8 `json:"post_id"` + UserID pgtype.Int8 `json:"user_id"` + Content pgtype.Text `json:"content"` + CreatedAt pgtype.Timestamp `json:"created_at"` +} + +type Favorite struct { + UserID int64 `json:"user_id"` + BlogID int64 `json:"blog_id"` + FavoritedAt pgtype.Timestamp `json:"favorited_at"` +} + +type Like struct { + LikeID int64 `json:"like_id"` + UserID pgtype.Int8 `json:"user_id"` + CommentID pgtype.Int8 `json:"comment_id"` + CreatedAt pgtype.Timestamp `json:"created_at"` +} + +type Post struct { + PostID int64 `json:"post_id"` + BlogID pgtype.Int8 `json:"blog_id"` + UserID pgtype.Int8 `json:"user_id"` + Title pgtype.Text `json:"title"` + Content pgtype.Text `json:"content"` + CreatedAt pgtype.Timestamp `json:"created_at"` + UpdatedAt pgtype.Timestamp `json:"updated_at"` +} + +type PostTag struct { + PostID int64 `json:"post_id"` + TagID int32 `json:"tag_id"` +} + +type PostVote struct { + PostID int64 `json:"post_id"` + UserID int64 `json:"user_id"` + Vote bool `json:"vote"` +} + +type Profile struct { + ProfileID int64 `json:"profile_id"` + UserID pgtype.Int8 `json:"user_id"` + Bio pgtype.Text `json:"bio"` + AvatarUrl pgtype.Text `json:"avatar_url"` + WebsiteUrl pgtype.Text `json:"website_url"` +} + +type Tag struct { + TagID int32 `json:"tag_id"` + TagName string `json:"tag_name"` +} + +type User struct { + UserID int64 `json:"user_id"` + Username string `json:"username"` + Email string `json:"email"` + Password string `json:"password"` + CreatedAt pgtype.Timestamp `json:"created_at"` +} diff --git a/enshi_back/db/queries/user_queries.sql b/enshi_back/db/queries/user_queries.sql new file mode 100644 index 0000000..4409c56 --- /dev/null +++ b/enshi_back/db/queries/user_queries.sql @@ -0,0 +1,4 @@ +-- name: GetAllUsers :many +SELECT * FROM users; + + diff --git a/enshi_back/db/sqlc.yml b/enshi_back/db/sqlc.yml new file mode 100644 index 0000000..1725b39 --- /dev/null +++ b/enshi_back/db/sqlc.yml @@ -0,0 +1,17 @@ +version: "2" +sql: + - engine: "postgresql" + schema: "./migrations" + queries: "./queries" + gen: + go: + emit_json_tags: true + package: "db_repo" + out: "." + sql_package: "pgx/v5" + overrides: + - db_type: "uuid" + go_type: + import: 'github.com/google/uuid' + type: 'UUID' + \ No newline at end of file diff --git a/enshi_back/db/user_queries.sql.go b/enshi_back/db/user_queries.sql.go new file mode 100644 index 0000000..c976895 --- /dev/null +++ b/enshi_back/db/user_queries.sql.go @@ -0,0 +1,40 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 +// source: user_queries.sql + +package db_repo + +import ( + "context" +) + +const getAllUsers = `-- name: GetAllUsers :many +SELECT user_id, username, email, password, created_at FROM users +` + +func (q *Queries) GetAllUsers(ctx context.Context) ([]User, error) { + rows, err := q.db.Query(ctx, getAllUsers) + if err != nil { + return nil, err + } + defer rows.Close() + var items []User + for rows.Next() { + var i User + if err := rows.Scan( + &i.UserID, + &i.Username, + &i.Email, + &i.Password, + &i.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/enshi_back/main.go b/enshi_back/main.go index ffdf7a3..524da9f 100644 --- a/enshi_back/main.go +++ b/enshi_back/main.go @@ -1,6 +1,8 @@ package main import ( + "context" + db_repo "enshi/db" "enshi/utils" "fmt" @@ -19,6 +21,7 @@ func main() { return } defer utils.Dbx.Close() + defer utils.Dbx_connection.Close(context.Background()) router := gin.Default() if err := utils.SetupRotes(router); err != nil { @@ -26,5 +29,20 @@ func main() { return } + // Transaction + tx, _ := utils.Dbx_connection.Begin(context.Background()) + defer tx.Rollback(context.Background()) + + repo := db_repo.New(tx) + users, _ := repo.GetAllUsers(context.Background()) + + for _, user := range users { + fmt.Printf("%v\n", user.Username) + } + + if err := tx.Commit(context.Background()); err != nil { + return + } + fmt.Printf("Hey!, %v", "you") } diff --git a/enshi_back/utils/database.go b/enshi_back/utils/database.go index df60d81..9cf96eb 100644 --- a/enshi_back/utils/database.go +++ b/enshi_back/utils/database.go @@ -4,11 +4,13 @@ import ( "context" "fmt" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" ) // Pgx connection to database var Dbx *pgxpool.Pool +var Dbx_connection *pgx.Conn func SetupDatabase() error { @@ -29,6 +31,7 @@ func SetupDatabase() error { // Connecting to database Dbx, err = pgxpool.New(context.Background(), url) + Dbx_connection, err = pgx.Connect(context.Background(), url) if err != nil { fmt.Println("Unable to connect")