Skip to content

Commit

Permalink
feat(database:pg): init database and basic models. (dezh-tech#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar authored Sep 14, 2024
1 parent 5217002 commit a535212
Show file tree
Hide file tree
Showing 25 changed files with 9,373 additions and 11 deletions.
6 changes: 5 additions & 1 deletion cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func HandleRun(args []string) {
ExitOnError(err)
}

r := relay.NewRelay(*cfg)
r, err := relay.New(*cfg)
if err != nil {
ExitOnError(err)
}

if err := r.Start(); err != nil {
ExitOnError(err)
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Config struct {
ServerConf server.Config `yaml:"server"`
DSN string
// TODO ::: db connection pool config
}

// LoadfromFile loads config from file, databse and env.
Expand Down
25 changes: 25 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package database

import (
"database/sql"

_ "github.com/lib/pq" // no-lint
"github.com/volatiletech/sqlboiler/v4/boil"
)

type Database struct {
db *sql.DB
}

func New(dsn string) (*Database, error) {
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}

boil.SetDB(db)
// TODO ::: config the connection pool
return &Database{
db: db,
}, nil
}
15 changes: 15 additions & 0 deletions database/migrations/000001_init.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Drop foreign key constraints first
ALTER TABLE public.reactions DROP CONSTRAINT IF EXISTS FKreactions_users_metadata;
ALTER TABLE public.reactions DROP CONSTRAINT IF EXISTS FKreactions_text_notes;
ALTER TABLE public.follow_list DROP CONSTRAINT IF EXISTS FKfollow_list_following;
ALTER TABLE public.follow_list DROP CONSTRAINT IF EXISTS FKfollow_list_follower;
ALTER TABLE public.text_notes DROP CONSTRAINT IF EXISTS FKtext_notes_users_metadata;

-- Drop indexes
DROP INDEX IF EXISTS public.reactions_text_notesid;

-- Drop tables
DROP TABLE IF EXISTS public.follow_list CASCADE;
DROP TABLE IF EXISTS public.reactions CASCADE;
DROP TABLE IF EXISTS public.text_notes CASCADE;
DROP TABLE IF EXISTS public.users_metadata CASCADE;
73 changes: 73 additions & 0 deletions database/migrations/000001_init.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
BEGIN;

CREATE TABLE public.follow_list (
follower CHAR(32), -- Optional, so no NOT NULL
following CHAR(32), -- Optional, so no NOT NULL
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- You may need a trigger for automatic updates
deleted_at TIMESTAMP,
PRIMARY KEY (follower, following),
CONSTRAINT follower_following UNIQUE (follower, following)
);

CREATE TABLE public.reactions (
id UUID NOT NULL,
text_notesid UUID, -- Optional, so no NOT NULL
users_metadatapub_key CHAR(32), -- Optional, so no NOT NULL
e TEXT[], -- Assuming e is an array of text
p TEXT[], -- Assuming p is an array of text
a TEXT[], -- Assuming a is an array of text
event JSONB NOT NULL,
k TEXT[], -- Assuming k is an array of text
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP,
PRIMARY KEY (id)
);

CREATE TABLE public.text_notes (
id UUID NOT NULL,
e TEXT[], -- Assuming e is an array of text
p TEXT[], -- Assuming p is an array of text
content VARCHAR(65535),
event JSONB NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP,
users_metadatapub_key CHAR(32), -- Optional, so no NOT NULL
PRIMARY KEY (id)
);

CREATE TABLE public.users_metadata (
pub_key CHAR(32) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP,
content VARCHAR(255),
follow_list_event JSONB,
PRIMARY KEY (pub_key)
);

-- Index for better query performance
CREATE INDEX reactions_text_notesid ON public.reactions (text_notesid);

-- Foreign key constraints with optional references
ALTER TABLE public.reactions
ADD CONSTRAINT FKreactions_users_metadata
FOREIGN KEY (users_metadatapub_key) REFERENCES public.users_metadata (pub_key);

ALTER TABLE public.follow_list
ADD CONSTRAINT FKfollow_list_following
FOREIGN KEY (following) REFERENCES public.users_metadata (pub_key),
ADD CONSTRAINT FKfollow_list_follower
FOREIGN KEY (follower) REFERENCES public.users_metadata (pub_key);

ALTER TABLE public.reactions
ADD CONSTRAINT FKreactions_text_notes
FOREIGN KEY (text_notesid) REFERENCES public.text_notes (id);

ALTER TABLE public.text_notes
ADD CONSTRAINT FKtext_notes_users_metadata
FOREIGN KEY (users_metadatapub_key) REFERENCES public.users_metadata (pub_key);

COMMIT;
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'

services:
postgres:
image: postgres:15
environment:
POSTGRES_USER: dev_user
POSTGRES_PASSWORD: dev_password
POSTGRES_DB: dev_db
ports:
- "5432:5432"
Empty file.
Binary file modified documents/erd/immortal-erd.vpp
Binary file not shown.
Binary file added documents/erd/immortal-erd.vpp.bak_001d
Binary file not shown.
Binary file added documents/erd/immortal-erd.vpp.bak_002d
Binary file not shown.
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ go 1.22.5

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4
github.com/gorilla/websocket v1.5.3
github.com/friendsofgo/errors v0.9.2
github.com/lib/pq v1.10.9
github.com/mailru/easyjson v0.7.7
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.3
github.com/volatiletech/null/v8 v8.1.2
github.com/volatiletech/sqlboiler/v4 v4.16.2
github.com/volatiletech/strmangle v0.0.6
github.com/gorilla/websocket v1.5.3
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -16,8 +21,14 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ericlagergren/decimal v0.0.0-20190420051523-6335edbaa640 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
github.com/volatiletech/randomize v0.0.1 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
)
Loading

0 comments on commit a535212

Please sign in to comment.