2024-06-03 20:18:02 +00:00
|
|
|
package database
|
2024-06-03 19:31:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-04 07:33:35 +00:00
|
|
|
"database/sql"
|
2024-06-04 11:28:14 +00:00
|
|
|
"embed"
|
2024-06-03 19:31:44 +00:00
|
|
|
"fmt"
|
2024-06-03 20:18:02 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/internal/postgres"
|
2024-06-04 13:16:37 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/internal/utils"
|
2024-06-03 19:31:44 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
2024-06-03 21:55:30 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
|
2024-06-04 07:33:35 +00:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
migrate "github.com/rubenv/sql-migrate"
|
2024-06-04 11:28:14 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-06-03 20:18:02 +00:00
|
|
|
gormPostgres "gorm.io/driver/postgres"
|
2024-06-03 19:31:44 +00:00
|
|
|
"gorm.io/gorm"
|
2024-06-21 21:35:41 +00:00
|
|
|
logger2 "gorm.io/gorm/logger"
|
2024-06-21 21:48:18 +00:00
|
|
|
log2 "log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2024-06-03 19:31:44 +00:00
|
|
|
)
|
|
|
|
|
2024-06-04 11:28:14 +00:00
|
|
|
//go:embed migrations/*.sql
|
|
|
|
var embedMigrations embed.FS
|
|
|
|
|
2024-06-03 19:31:44 +00:00
|
|
|
type postgresqlConnection struct {
|
2024-06-14 10:06:58 +00:00
|
|
|
db *gorm.DB
|
|
|
|
debug bool
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 10:06:58 +00:00
|
|
|
func NewPostgresqlConnection(debugOutput bool) OtterSpace {
|
2024-06-03 19:31:44 +00:00
|
|
|
return &postgresqlConnection{
|
2024-06-14 10:06:58 +00:00
|
|
|
db: nil,
|
|
|
|
debug: debugOutput,
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) Connect(_ context.Context, endpoint string, username string, password string, database string, port int, ssl string, timezone string) error {
|
2024-06-03 19:31:44 +00:00
|
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", endpoint, username, password, database, port, ssl, timezone)
|
2024-06-04 11:06:38 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
err = p.migrateDatabase(dsn)
|
2024-06-03 19:31:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-04 07:33:35 +00:00
|
|
|
|
2024-06-04 11:44:15 +00:00
|
|
|
log.Infof("OtterSpace: migration compleate")
|
|
|
|
|
2024-06-21 21:48:18 +00:00
|
|
|
dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{
|
|
|
|
SlowThreshold: 200 * time.Millisecond,
|
|
|
|
LogLevel: logger2.Warn,
|
|
|
|
IgnoreRecordNotFoundError: true,
|
|
|
|
Colorful: true,
|
|
|
|
})
|
|
|
|
|
2024-06-21 21:35:41 +00:00
|
|
|
db, err := gorm.Open(gormPostgres.Open(dsn), &gorm.Config{
|
2024-06-21 21:48:18 +00:00
|
|
|
Logger: dbLogger,
|
2024-06-21 21:35:41 +00:00
|
|
|
})
|
2024-06-04 13:16:37 +00:00
|
|
|
p.db = db
|
2024-06-04 07:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-04 11:44:15 +00:00
|
|
|
log.Infof("OtterSpace: postgres connection established")
|
|
|
|
|
2024-06-03 19:31:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error {
|
2024-06-21 12:16:03 +00:00
|
|
|
return postgres.CreateUserNodeWithSourceRelation(ctx, p.db, anthroveUserID, sourceDomain, userID, userID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *graphModels.AnthroveSource) error {
|
|
|
|
return postgres.CreateSourceNode(ctx, p.db, source)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *graphModels.AnthrovePost) error {
|
2024-06-14 11:29:53 +00:00
|
|
|
return postgres.CreateAnthrovePostNode(ctx, p.db, anthrovePost.PostID, anthrovePost.Rating)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error {
|
2024-06-04 21:14:02 +00:00
|
|
|
return postgres.CreateTagNodeWitRelation(ctx, p.db, anthrovePostID, utils.GraphConvertTag(anthroveTag))
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error {
|
2024-06-21 13:15:20 +00:00
|
|
|
return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error {
|
2024-06-14 11:22:52 +00:00
|
|
|
return postgres.EstablishUserToPostLink(ctx, p.db, anthroveUser.UserID, anthrovePost.PostID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) {
|
2024-06-14 11:25:54 +00:00
|
|
|
return postgres.CheckUserToPostLink(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID))
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) {
|
2024-06-14 11:57:03 +00:00
|
|
|
exists, err := postgres.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, p.db, anthrovePost.PostID)
|
|
|
|
return anthrovePost, exists, err
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:57:03 +00:00
|
|
|
// CheckPostNodeExistsBySourceURL NOT WORKING! TODO!
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) {
|
2024-06-21 20:54:47 +00:00
|
|
|
post, exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceURL(ctx, p.db, sourceUrl)
|
2024-06-21 20:58:52 +00:00
|
|
|
return post, exists, err
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 11:57:03 +00:00
|
|
|
// CheckPostNodeExistsBySourceID NOT WORKING! TODO!
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*graphModels.AnthrovePost, bool, error) {
|
2024-06-14 11:57:03 +00:00
|
|
|
var post graphModels.AnthrovePost
|
|
|
|
exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceID(ctx, p.db, sourcePostID)
|
|
|
|
return &post, exists, err
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
|
2024-06-14 12:50:29 +00:00
|
|
|
return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) {
|
2024-06-14 13:03:34 +00:00
|
|
|
return postgres.GetUserSourceLink(ctx, p.db, anthroveUserID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) {
|
2024-06-14 13:04:45 +00:00
|
|
|
return postgres.GetSpecifiedUserSourceLink(ctx, p.db, anthroveUserID, sourceDisplayName)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) {
|
2024-06-14 13:07:23 +00:00
|
|
|
return postgres.GetAnthroveUser(ctx, p.db, anthroveUserID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {
|
2024-06-14 13:09:11 +00:00
|
|
|
return postgres.GetAllAnthroveUserIDs(ctx, p.db)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) {
|
2024-06-14 13:11:13 +00:00
|
|
|
return postgres.GetUserFavoriteNodeWithPagination(ctx, p.db, anthroveUserID, skip, limit)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) {
|
2024-06-14 13:16:31 +00:00
|
|
|
return postgres.GetUserTagNodeWitRelationToFavedPosts(ctx, p.db, anthroveUserID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]graphModels.TagsWithFrequency, error) {
|
2024-06-14 11:05:07 +00:00
|
|
|
tags, err := postgres.GetTags(ctx, p.db)
|
|
|
|
|
|
|
|
return utils.ConvertToTagsWithFrequency(tags), err
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]graphModels.AnthroveSource, error) {
|
|
|
|
var anthroveSources []graphModels.AnthroveSource
|
|
|
|
source, err := postgres.GetAllSourceNodes(ctx, p.db)
|
|
|
|
|
|
|
|
for _, v := range source {
|
|
|
|
anthroveSource := utils.PostgresConvertToAnthroveSource(&v)
|
|
|
|
anthroveSources = append(anthroveSources, *anthroveSource)
|
|
|
|
}
|
|
|
|
return nil, err
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*graphModels.AnthroveSource, error) {
|
|
|
|
source, err := postgres.GetSourceNodesByURL(ctx, p.db, sourceUrl)
|
|
|
|
return utils.PostgresConvertToAnthroveSource(source), err
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
2024-06-04 07:33:35 +00:00
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
func (p *postgresqlConnection) migrateDatabase(connectionString string) error {
|
2024-06-04 07:33:35 +00:00
|
|
|
dialect := "postgres"
|
2024-06-04 11:28:14 +00:00
|
|
|
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
|
2024-06-04 07:33:35 +00:00
|
|
|
|
|
|
|
db, err := sql.Open(dialect, connectionString)
|
|
|
|
if err != nil {
|
2024-06-04 11:28:14 +00:00
|
|
|
return fmt.Errorf("postgres migration: %v", err)
|
2024-06-04 07:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
|
|
|
|
if err != nil {
|
2024-06-04 11:28:14 +00:00
|
|
|
return fmt.Errorf("postgres migration: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-06-14 10:06:58 +00:00
|
|
|
if p.debug {
|
|
|
|
if n != 0 {
|
|
|
|
log.Infof("postgres migration: applied %d migrations!", n)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
log.Info("postgres migration: nothing to migrate")
|
2024-06-04 11:28:14 +00:00
|
|
|
|
2024-06-14 10:06:58 +00:00
|
|
|
}
|
2024-06-04 07:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|