feat(database): new fields

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-28 10:43:55 +02:00
parent 9c8a7c1e16
commit 429f68899d
6 changed files with 54 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import (
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error" otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
gonanoid "github.com/matoous/go-nanoid/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -56,15 +57,21 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
return &otterError.EntityValidationFailed{Reason: "accountUsername cannot be empty"} return &otterError.EntityValidationFailed{Reason: "accountUsername cannot be empty"}
} }
validationCode, err := gonanoid.New(25)
if err != nil {
return err
}
result := db.WithContext(ctx).Exec(`WITH userObj AS ( result := db.WithContext(ctx).Exec(`WITH userObj AS (
INSERT INTO "User" (id) INSERT INTO "User" (id)
VALUES ($1) VALUES ($1)
ON CONFLICT (id) DO NOTHING ON CONFLICT (id) DO NOTHING
) )
INSERT INTO "UserSource" (user_id, source_id, account_username, account_id) INSERT INTO "UserSource" (user_id, source_id, account_username, account_id, account_validate, account_validation_key)
SELECT $2, source.id, $3, $4 SELECT $2, source.id, $3, $4, false, $5
FROM "Source" AS source FROM "Source" AS source
WHERE source.id = $5;`, anthroveUserID, anthroveUserID, accountUsername, accountId, sourceID) WHERE source.id = $6;`, anthroveUserID, anthroveUserID, accountUsername, accountId, validationCode, sourceID)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {

View File

@ -2,6 +2,7 @@ package database
import ( import (
"context" "context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
) )
@ -76,5 +77,8 @@ type OtterSpace interface {
CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID)
DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error
UpdateUserWithScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error
UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error
UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error
} }

View File

@ -27,7 +27,7 @@ CREATE TABLE "Post"
CREATE TABLE "Source" CREATE TABLE "Source"
( (
id CHAR(25) PRIMARY KEY, id CHAR(25) PRIMARY KEY,
display_name TEXT NULL, display_name TEXT NULL,
icon TEXT NULL, icon TEXT NULL,
domain TEXT NOT NULL UNIQUE, domain TEXT NOT NULL UNIQUE,
@ -89,11 +89,14 @@ CREATE TABLE "UserFavorites"
CREATE TABLE "UserSource" CREATE TABLE "UserSource"
( (
user_id TEXT REFERENCES "User" (id), user_id TEXT REFERENCES "User" (id),
source_id TEXT REFERENCES "Source" (id), source_id TEXT REFERENCES "Source" (id),
scrape_time_interval TEXT, scrape_time_interval INT,
account_username TEXT, account_username TEXT,
account_id TEXT, account_id TEXT,
last_scrape_time TIMESTAMP,
account_validate BOOL DEFAULT FALSE,
account_validation_key CHAR(25),
PRIMARY KEY (user_id, source_id), PRIMARY KEY (user_id, source_id),
UNIQUE (source_id, account_username, account_id) UNIQUE (source_id, account_username, account_id)
); );
@ -104,16 +107,3 @@ CREATE TABLE "post_tags"
tag_name TEXT REFERENCES "Tag" (name), tag_name TEXT REFERENCES "Tag" (name),
PRIMARY KEY (post_id, tag_name) PRIMARY KEY (post_id, tag_name)
); );
-- +migrate Down
DROP TYPE Rating;
DROP TYPE TagType;
DROP TABLE Post;
DROP TABLE Source;
DROP TABLE Tag;
DROP TABLE User;
DROP TABLE PostReference;
DROP TABLE TagAlias;
DROP TABLE TagGroup;
DROP TABLE UserFavorites;
DROP TABLE UserSource;

View File

@ -193,6 +193,21 @@ func (p *postgresqlConnection) UpdateUserWithScrapeTimeInterval(ctx context.Cont
panic("implement me") panic("implement me")
} }
func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error {
//TODO implement me
panic("implement me")
}
func (p *postgresqlConnection) UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error {
//TODO implement me
panic("implement me")
}
func (p *postgresqlConnection) UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error {
//TODO implement me
panic("implement me")
}
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error { func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {
dialect := "postgres" dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}

View File

@ -1,7 +1,5 @@
package models package models
import "google.golang.org/genproto/googleapis/type/date"
type AnthroveUserID string type AnthroveUserID string
type AnthrovePostID string type AnthrovePostID string
type AnthroveSourceID string type AnthroveSourceID string
@ -10,7 +8,8 @@ type AnthrovePostURL string
type AnthroveTagGroupName string type AnthroveTagGroupName string
type AnthroveTagAliasName string type AnthroveTagAliasName string
type AnthroveTagID string type AnthroveTagID string
type AnthroveScrapeTimeInterval date.Date type AnthroveScrapeTimeInterval int
type AnthroveUserLastScrapeTime int
type Rating string type Rating string
type TagType string type TagType string

View File

@ -1,13 +1,18 @@
package models package models
import "time"
type UserSource struct { type UserSource struct {
User User `gorm:"foreignKey:ID;references:UserID"` User User `gorm:"foreignKey:ID;references:UserID"`
UserID string `gorm:"primaryKey"` UserID string `gorm:"primaryKey"`
Source Source `gorm:"foreignKey:ID;references:SourceID"` Source Source `gorm:"foreignKey:ID;references:SourceID"`
SourceID string `gorm:"primaryKey"` SourceID string `gorm:"primaryKey"`
ScrapeTimeInterval string ScrapeTimeInterval string
AccountUsername string AccountUsername string
AccountID string AccountID string
LastScrapeTime time.Time
AccountValidate bool
AccountValidationKey string
} }
func (UserSource) TableName() string { func (UserSource) TableName() string {