Skip to content

Commit

Permalink
feat(database:pg, server): store incoming events to database. (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar authored Sep 18, 2024
1 parent f44d366 commit db08520
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 2,564 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
build
.vscode
2 changes: 1 addition & 1 deletion cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func HandleRun(args []string) {
ExitOnError(errors.New("at least 1 arguments expected\nuse help command for more information"))
}

cfg, err := config.LoadfromFile(args[2])
cfg, err := config.LoadFromFile(args[2])
if err != nil {
ExitOnError(err)
}
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Config struct {
DatabaseConf database.Config `yaml:"database"`
}

// LoadfromFile loads config from file, databse and env.
func LoadfromFile(path string) (*Config, error) {
// Load fromFile loads config from file, databse and env.
func LoadFromFile(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, Error{
Expand All @@ -34,7 +34,8 @@ func LoadfromFile(path string) (*Config, error) {
}
}

config.DatabaseConf.DSN = os.Getenv("IMMO_DB_DSN")
// TODO ::: (kehiy) fix read dsn from dsn.
config.DatabaseConf.DSN = "postgresql://dev_user:dev_password@localhost:5432/dev_db?sslmode=disable&search_path=public"

if err = config.basicCheck(); err != nil {
return nil, Error{
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestLoadfromFile(t *testing.T) {
cfg, err := config.LoadfromFile("./config.yml")
cfg, err := config.LoadFromFile("./config.yml")
require.NoError(t, err, "error must be nil.")

assert.Equal(t, uint16(7777), cfg.ServerConf.Port)
Expand Down
72 changes: 35 additions & 37 deletions database/migrations/000001_init.up.sql
Original file line number Diff line number Diff line change
@@ -1,73 +1,71 @@
BEGIN;

CREATE TABLE public.follow_list (
follower CHAR(32), -- Optional, so no NOT NULL
following CHAR(32), -- Optional, so no NOT NULL
follower CHAR(64), -- Optional, so no NOT NULL
following CHAR(64), -- 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)
PRIMARY KEY (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
id CHAR(64) NOT NULL,
text_notesid CHAR(64), -- Optional, so no NOT NULL
users_metadatapub_key CHAR(64), -- Optional, so no NOT NULL
e_tags TEXT[], -- Assuming e is an array of text
p_tags TEXT[], -- Assuming p is an array of text
a_tags TEXT[], -- Assuming a is an array of text
k_tags TEXT[], -- Assuming k is an array of text
r_tags TEXT[], -- Assuming r is an array of text
content VARCHAR(8),
event TEXT NOT NULL,
event_created_at TIMESTAMP NOT NULL,
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,
id CHAR(64) NOT NULL,
e_tags TEXT[], -- Assuming e is an array of text
p_tags TEXT[], -- Assuming p is an array of text
event TEXT NOT NULL UNIQUE,
event_created_at TIMESTAMP NOT NULL,
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
users_metadatapub_key CHAR(64), -- Optional, so no NOT NULL
PRIMARY KEY (id)
);

CREATE TABLE public.users_metadata (
pub_key CHAR(32) NOT NULL,
pub_key CHAR(64) 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,
content VARCHAR(65535),
follow_list_event TEXT,
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);
-- Index for deleted entities in follow_list
CREATE INDEX follow_list_deleted_at_idx ON public.follow_list (deleted_at);

-- Index for deleted entities in reactions
CREATE INDEX reactions_deleted_at_idx ON public.reactions (deleted_at);

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);
-- Index for deleted entities in text_notes
CREATE INDEX text_notes_deleted_at_idx ON public.text_notes (deleted_at);

ALTER TABLE public.reactions
ADD CONSTRAINT FKreactions_text_notes
FOREIGN KEY (text_notesid) REFERENCES public.text_notes (id);
-- Index for deleted entities in users_metadata
CREATE INDEX users_metadata_deleted_at_idx ON public.users_metadata (deleted_at);

ALTER TABLE public.text_notes
ADD CONSTRAINT FKtext_notes_users_metadata
FOREIGN KEY (users_metadatapub_key) REFERENCES public.users_metadata (pub_key);
-- Composite index on follow_list by follower and following
CREATE INDEX idx_follow_list_follower_following ON public.follow_list (follower, following);

COMMIT;
COMMIT;
Loading

0 comments on commit db08520

Please sign in to comment.