Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database:pg): init database and basic models. #33

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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