173 lines
6.3 KiB
Go
173 lines
6.3 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"fmt"
|
|
"git.dragse.it/anthrove/otter-space-sdk/internal/postgres"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
_ "github.com/lib/pq"
|
|
migrate "github.com/rubenv/sql-migrate"
|
|
log "github.com/sirupsen/logrus"
|
|
gormPostgres "gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
logger2 "gorm.io/gorm/logger"
|
|
log2 "log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var embedMigrations embed.FS
|
|
|
|
type postgresqlConnection struct {
|
|
db *gorm.DB
|
|
debug bool
|
|
}
|
|
|
|
func NewPostgresqlConnection(debugOutput bool) OtterSpace {
|
|
return &postgresqlConnection{
|
|
db: nil,
|
|
debug: debugOutput,
|
|
}
|
|
}
|
|
|
|
func (p *postgresqlConnection) Connect(_ 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)
|
|
var err error
|
|
|
|
err = p.migrateDatabase(dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Infof("OtterSpace: migration compleate")
|
|
|
|
dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{
|
|
SlowThreshold: 200 * time.Millisecond,
|
|
LogLevel: logger2.Warn,
|
|
IgnoreRecordNotFoundError: true,
|
|
Colorful: true,
|
|
})
|
|
|
|
db, err := gorm.Open(gormPostgres.Open(dsn), &gorm.Config{
|
|
Logger: dbLogger,
|
|
})
|
|
p.db = db
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Infof("OtterSpace: postgres connection established")
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *postgresqlConnection) AddUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error {
|
|
return postgres.CreateUserNodeWithSourceRelation(ctx, p.db, anthroveUserID, sourceDomain, userID, userID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *models.Source) error {
|
|
return postgres.CreateSourceNode(ctx, p.db, anthroveSource)
|
|
}
|
|
|
|
func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *models.Post) error {
|
|
return postgres.CreateAnthrovePostNode(ctx, p.db, anthrovePost)
|
|
}
|
|
|
|
func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error {
|
|
return postgres.CreateTagNodeWitRelation(ctx, p.db, anthrovePostID, anthroveTag)
|
|
}
|
|
|
|
func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error {
|
|
return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, sourceDomain, anthrovePostRelationship)
|
|
}
|
|
|
|
func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error {
|
|
return postgres.EstablishUserToPostLink(ctx, p.db, anthroveUser.ID, anthrovePost.ID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) {
|
|
return postgres.CheckUserToPostLink(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID))
|
|
}
|
|
|
|
func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) {
|
|
return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePost.ID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, error) {
|
|
return postgres.GetPostBySourceURL(ctx, p.db, sourceUrl)
|
|
}
|
|
|
|
func (p *postgresqlConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.Post, error) {
|
|
return postgres.GetPostBySourceID(ctx, p.db, sourcePostID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
|
|
return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
|
|
return postgres.GetUserSourceLink(ctx, p.db, anthroveUserID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) {
|
|
return postgres.GetSpecifiedUserSourceLink(ctx, p.db, anthroveUserID, sourceDisplayName)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.User, error) {
|
|
return postgres.GetAnthroveUser(ctx, p.db, anthroveUserID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {
|
|
return postgres.GetAllAnthroveUserIDs(ctx, p.db)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
|
|
return postgres.GetUserFavoriteNodeWithPagination(ctx, p.db, anthroveUserID, skip, limit)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
|
|
return postgres.GetUserTagNodeWitRelationToFavedPosts(ctx, p.db, anthroveUserID)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) {
|
|
return postgres.GetTags(ctx, p.db)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) {
|
|
return postgres.GetAllSourceNodes(ctx, p.db)
|
|
}
|
|
|
|
func (p *postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) {
|
|
return postgres.GetSourceNodesByURL(ctx, p.db, sourceUrl)
|
|
}
|
|
|
|
func (p *postgresqlConnection) migrateDatabase(connectionString string) error {
|
|
dialect := "postgres"
|
|
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
|
|
|
|
db, err := sql.Open(dialect, connectionString)
|
|
if err != nil {
|
|
return fmt.Errorf("postgres migration: %v", err)
|
|
}
|
|
|
|
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
|
|
if err != nil {
|
|
return fmt.Errorf("postgres migration: %v", err)
|
|
}
|
|
|
|
if p.debug {
|
|
if n != 0 {
|
|
log.Infof("postgres migration: applied %d migrations!", n)
|
|
|
|
} else {
|
|
log.Info("postgres migration: nothing to migrate")
|
|
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|