package database import ( "context" "embed" "fmt" log2 "log" "os" "time" "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" ) //go:embed migrations/*.sql var embedMigrations embed.FS type postgresqlConnection struct { db *gorm.DB debug bool } func NewPostgresqlConnection() OtterSpace { return &postgresqlConnection{ db: nil, } } func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error { var localSSL string if config.SSL { localSSL = "require" } else { localSSL = "disable" } p.debug = config.Debug 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) var err error 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: database connection established") err = p.migrateDatabase(db) if err != nil { return err } log.Infof("OtterSpace: migration compleate") return nil } 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) } func (p *postgresqlConnection) CreateSource(ctx context.Context, anthroveSource *models.Source) error { return postgres.CreateSource(ctx, p.db, anthroveSource) } func (p *postgresqlConnection) CreatePost(ctx context.Context, anthrovePost *models.Post) error { return postgres.CreatePost(ctx, p.db, anthrovePost) } func (p *postgresqlConnection) CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag) } func (p *postgresqlConnection) CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain) error { return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain) } func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID) } func (p *postgresqlConnection) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID) } func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) { return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePostID) } func (p *postgresqlConnection) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) { return postgres.GetPostByURL(ctx, p.db, sourceUrl) } func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) { return postgres.GetPostBySourceID(ctx, p.db, sourceID) } func (p *postgresqlConnection) GetUserFavoritesCount(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.GetUserSourceLinks(ctx, p.db, anthroveUserID) } 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) } func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) { return postgres.GetAllAnthroveUserIDs(ctx, p.db) } 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) } func (p *postgresqlConnection) GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { return postgres.GetUserTagWitRelationToFavedPosts(ctx, p.db, anthroveUserID) } func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.Tag, error) { return postgres.GetTags(ctx, p.db) } func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) { return postgres.GetAllSource(ctx, p.db) } func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) { return postgres.GetSourceByDomain(ctx, p.db, sourceDomain) } func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error { dialect := "postgres" migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} db, err := dbPool.DB() 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 }