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-24 10:59:51 +00:00
|
|
|
log2 "log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-06-03 20:18:02 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/internal/postgres"
|
2024-06-03 19:31:44 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
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-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-24 10:59:51 +00:00
|
|
|
func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error {
|
|
|
|
var localSSL string
|
|
|
|
if config.SSL {
|
|
|
|
localSSL = "require"
|
|
|
|
} else {
|
|
|
|
localSSL = "disable"
|
|
|
|
}
|
|
|
|
|
|
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.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-23 20:35:46 +00:00
|
|
|
func (p *postgresqlConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
|
|
|
|
return postgres.CreateUserWithRelationToSource(ctx, p.db, anthroveUserID, sourceID, accountId, accountUsername)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func (p *postgresqlConnection) CreateSource(ctx context.Context, anthroveSource *models.Source) error {
|
|
|
|
return postgres.CreateSource(ctx, p.db, anthroveSource)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func (p *postgresqlConnection) CreatePost(ctx context.Context, anthrovePost *models.Post) error {
|
2024-06-22 23:18:23 +00:00
|
|
|
return postgres.CreatePost(ctx, p.db, anthrovePost)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func (p *postgresqlConnection) CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error {
|
|
|
|
return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error {
|
|
|
|
return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:39:54 +00:00
|
|
|
func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
|
2024-06-23 20:35:46 +00:00
|
|
|
return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func (p *postgresqlConnection) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
|
|
|
|
return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 20:30:03 +00:00
|
|
|
func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) {
|
2024-06-23 20:39:54 +00:00
|
|
|
return postgres.GetPostByAnthroveID(ctx, p.db, models.AnthrovePostID(anthrovePost.ID))
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func (p *postgresqlConnection) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) {
|
|
|
|
return postgres.GetPostByURL(ctx, p.db, sourceUrl)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) {
|
|
|
|
return postgres.GetPostBySourceID(ctx, p.db, sourceID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func (p *postgresqlConnection) GetUserFavoritesCount(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-22 20:27:38 +00:00
|
|
|
func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
|
2024-06-22 21:23:23 +00:00
|
|
|
return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) {
|
|
|
|
return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID)
|
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-23 19:23:38 +00:00
|
|
|
func (p *postgresqlConnection) GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
|
|
|
|
return postgres.GetUserFavoriteWithPagination(ctx, p.db, anthroveUserID, skip, limit)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func (p *postgresqlConnection) GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
|
|
|
|
return postgres.GetUserTagWitRelationToFavedPosts(ctx, p.db, anthroveUserID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.Tag, error) {
|
2024-06-22 20:27:38 +00:00
|
|
|
return postgres.GetTags(ctx, p.db)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 20:27:38 +00:00
|
|
|
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) {
|
2024-06-23 19:23:38 +00:00
|
|
|
return postgres.GetAllSource(ctx, p.db)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) {
|
|
|
|
return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
|
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
|
|
|
|
}
|