This commit is contained in:
GitProtogen
2026-03-17 18:54:04 +01:00
parent bbd3d390c2
commit 912136e2bc
6 changed files with 104 additions and 52 deletions
+47
View File
@@ -0,0 +1,47 @@
package main
import (
"context"
//"golang.org/x/crypto/bcrypt"
"github.com/jackc/pgx/v5"
)
var dbConn *pgx.Conn
func InitDatabase(ctx context.Context) {
conn, err := pgx.Connect(ctx, "postgres://master:secret@localhost:5432")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(20) UNIQUE NOT NULL,
pass_hash VARCHAR(60) NOT NULL,
color VARCHAR(3) DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS chat_groups (
id SERIAL PRIMARY KEY,
name VARCHAR(48) NOT NULL,
creator_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
owner_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
enable_user_colors BOOLEAN NOT NULL DEFAULT true,
group_color VARCHAR(3),
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS chat_group_members (
group_id INTEGER NOT NULL REFERENCES chat_groups(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
joined_at TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (group_id, user_id)
);
`)
if err != nil {
panic(err)
}
dbConn = conn
}