forked from dezh-tech/immortal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database:pg): init database and basic models. (dezh-tech#33)
- Loading branch information
1 parent
5217002
commit a535212
Showing
25 changed files
with
9,373 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.