feat: initial commit

This commit is contained in:
2025-03-31 16:17:35 +01:00
commit 6719f8a61a
91 changed files with 7119 additions and 0 deletions
@@ -0,0 +1 @@
DROP TABLE IF EXISTS users;
@@ -0,0 +1,14 @@
CREATE EXTENSION IF NOT EXISTS citext;
CREATE TABLE IF NOT EXISTS users (
id bigserial PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
username VARCHAR(255) UNIQUE NOT NULL,
email citext UNIQUE NOT NULL,
password bytea NOT NULL,
created_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
updated_at timestamp(0) with time zone NOT NULL DEFAULT NOW()
);
@@ -0,0 +1 @@
DROP TABLE IF EXISTS posts;
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS posts (
id bigserial PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
user_id bigint NOT NULL,
created_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
updated_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
@@ -0,0 +1,2 @@
ALTER TABLE posts
DROP COLUMN tags;
@@ -0,0 +1,2 @@
ALTER TABLE posts
ADD COLUMN tags VARCHAR(100)[] NOT NULL DEFAULT '{}';
@@ -0,0 +1 @@
DROP TABLE IF EXISTS Comments;
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS Comments (
id bigserial PRIMARY KEY,
post_id bigserial NOT NULL,
user_id bigserial NOT NULL,
content TEXT NOT NULL,
created_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
updated_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
FOREIGN KEY (post_id) REFERENCES Posts(id),
FOREIGN KEY (user_id) REFERENCES Users(id)
);
@@ -0,0 +1 @@
ALTER TABLE posts DROP COLUMN version;
@@ -0,0 +1 @@
ALTER TABLE posts ADD COLUMN version INT NOT NULL DEFAULT 0;
@@ -0,0 +1 @@
DROP TABLE IF EXISTS followers;
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS followers (
user_id bigint NOT NULL,
follower_id bigint NOT NULL,
created_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
PRIMARY KEY (user_id, follower_id),
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (follower_id) REFERENCES users (id)
);
@@ -0,0 +1,8 @@
DROP INDEX IF EXISTS idx_comments_content;
DROP INDEX IF EXISTS idx_comments_post_id;
DROP INDEX IF EXISTS idx_posts_title;
DROP INDEX IF EXISTS idx_posts_tags;
DROP INDEX IF EXISTS idx_posts_user_id;
DROP INDEX IF EXISTS idx_users_username;
@@ -0,0 +1,19 @@
-- Indexes are used to speed up the query process. They are used to quickly locate the rows in a table that match the search condition.
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX idx_comments_content ON comments USING gin (content gin_trgm_ops);
CREATE INDEX IF NOT EXISTS idx_posts_title ON posts USING gin (title gin_trgm_ops);
CREATE INDEX IF NOT EXISTS idx_posts_tags ON posts USING gin (tags);
CREATE INDEX IF NOT EXISTS idx_users_username ON users (username);
CREATE INDEX IF NOT EXISTS idx_posts_user_id ON posts (user_id);
CREATE INDEX IF NOT EXISTS idx_comments_post_id ON comments (post_id);
@@ -0,0 +1 @@
DROP TABLE IF EXISTS invitations;
@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS invitations (
token bytea PRIMARY KEY,
user_id bigint NOT NULL
);
@@ -0,0 +1,4 @@
ALTER TABLE
users
DROP COLUMN
is_active;
@@ -0,0 +1,4 @@
ALTER TABLE
users
ADD COLUMN
is_active boolean NOT NULL DEFAULT false;
@@ -0,0 +1,2 @@
ALTER TABLE
invitations DROP COLUMN expiry;
@@ -0,0 +1,4 @@
ALTER TABLE
invitations
ADD COLUMN
expiry TIMESTAMP(0) WITH TIME ZONE NOT NULL;
@@ -0,0 +1 @@
DROP TABLE IF EXISTS roles;
@@ -0,0 +1,30 @@
CREATE TABLE IF NOT EXISTS roles (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
level INT NOT NULL DEFAULT 1,
description TEXT
);
INSERT INTO
roles (name, level, description)
VALUES (
'user',
1,
'User'
);
INSERT INTO
roles (name, level, description)
VALUES (
'moderator',
2,
'Moderator'
);
INSERT INTO
roles (name, level, description)
VALUES (
'admin',
3,
'Administrator'
);
@@ -0,0 +1,2 @@
ALTER TABLE
IF EXISTS users DROP COLUMN role_id;
@@ -0,0 +1,27 @@
ALTER TABLE
IF EXISTS users
ADD
COLUMN role_id INT REFERENCES roles(id) DEFAULT 1;
UPDATE
users
SET
role_id = (
SELECT
id
FROM
roles
WHERE
name = 'user'
)
WHERE
role_id IS NULL;
ALTER TABLE
users
ALTER COLUMN
role_id DROP DEFAULT;
ALTER TABLE users
ALTER COLUMN role_id
SET NOT NULL;
+21
View File
@@ -0,0 +1,21 @@
package main
import (
"log"
"github.com/FernandoVideira/LK_API_Temp/internal/db"
"github.com/FernandoVideira/LK_API_Temp/internal/env"
"github.com/FernandoVideira/LK_API_Temp/internal/store"
)
func main() {
addr := env.GetString("DB_ADDR", "postgres://postgres:postgres@localhost:5432/lk_tmpl?sslmode=disable")
conn, err := db.New(addr, 3, 3, env.GetDuration("DB_MAX_IDLE_TIME", "5m"))
if err != nil {
log.Fatalf("could not connect to db: %v", err)
}
defer conn.Close()
store := store.NewStorage(conn)
db.Seed(store, conn)
}