feat(db): finalizing migration statements

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-04 13:06:38 +02:00
parent b2b354c60f
commit 751f387b86
12 changed files with 118 additions and 5 deletions

View File

@ -0,0 +1,9 @@
-- +migrate Up
CREATE TYPE Rating AS ENUM (
'safe',
'questionable',
'explicit'
);
-- +migrate Down
DROP TYPE Rating;

View File

@ -0,0 +1,13 @@
-- +migrate Up
CREATE TYPE TagType AS ENUM (
'general',
'species',
'character',
'artist',
'lore',
'meta',
'invalid'
);
-- +migrate Down
DROP TYPE TagType;

View File

@ -0,0 +1,10 @@
-- +migrate Up
CREATE TABLE "Post"
(
id VARCHAR(25) PRIMARY KEY,
rating Rating,
created_at TIMESTAMP
);
-- +migrate Down
DROP TYPE Post;

View File

@ -0,0 +1,11 @@
-- +migrate Up
CREATE TABLE "Source"
(
id VARCHAR(25) PRIMARY KEY,
display_name TEXT,
domain TEXT NOT NULL UNIQUE
);
-- +migrate Down
DROP TABLE "Source";

View File

@ -0,0 +1,9 @@
-- +migrate Up
CREATE TABLE "Tag"
(
name TEXT PRIMARY KEY,
type TagType
);
-- +migrate Down
DROP TABLE "Tag";

View File

@ -0,0 +1,9 @@
-- +migrate Up
CREATE TABLE "User"
(
id TEXT PRIMARY KEY,
created_at TIMESTAMP
);
-- +migrate Down
DROP TABLE "User";

View File

@ -0,0 +1,12 @@
-- +migrate Up
CREATE TABLE "PostReference"
(
post_id TEXT REFERENCES "Post" (id),
source_id TEXT REFERENCES "Source" (id),
url TEXT NOT NULL UNIQUE,
source_post_id TEXT,
PRIMARY KEY (post_id, source_id)
);
-- +migrate Down
DROP TABLE "PostReference";

View File

@ -0,0 +1,8 @@
-- +migrate Up
CREATE TABLE "TagAlias"
(
name TEXT PRIMARY KEY,
tag_id TEXT REFERENCES "Tag" (name)
);
-- +migrate Down
DROP TABLE "TagAlias";

View File

@ -0,0 +1,9 @@
-- +migrate Up
CREATE TABLE "TagGroup"
(
name TEXT PRIMARY KEY,
tag_id TEXT REFERENCES "Tag" (name)
);
-- +migrate Down
DROP TABLE "TagGroup";

View File

@ -0,0 +1,11 @@
-- +migrate Up
CREATE TABLE "UserFavorites"
(
user_id TEXT REFERENCES "User" (id),
post_id TEXT REFERENCES "Post" (id),
created_at TIMESTAMP,
PRIMARY KEY (user_id, post_id)
);
-- +migrate Down
DROP TABLE "UserFavorites";

View File

@ -0,0 +1,13 @@
-- +migrate Up
CREATE TABLE "UserSource"
(
user_id TEXT REFERENCES "User" (id),
source_id TEXT REFERENCES "Source" (id),
account_username TEXT,
account_id TEXT,
PRIMARY KEY (user_id, source_id)
);
-- +migrate Down
DROP TABLE "UserSource";

View File

@ -25,16 +25,15 @@ func NewPostgresqlConnection() OtterSpace {
} }
func (p postgresqlConnection) Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error { func (p postgresqlConnection) Connect(ctx context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", endpoint, username, password, database, port, ssl, timezone) dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", endpoint, username, password, database, port, ssl, timezone)
db, err := gorm.Open(gormPostgres.Open(dsn), &gorm.Config{}) var err error
p.db = db
err = p.migrateDatabase(dsn)
if err != nil { if err != nil {
return err return err
} }
err = p.migrateDatabase(dsn) p.db, err = gorm.Open(gormPostgres.Open(dsn), &gorm.Config{})
if err != nil { if err != nil {
return err return err
} }