package database import ( "context" "embed" "fmt" log2 "log" "os" "time" "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/postgres" "git.anthrove.art/Anthrove/otter-space-sdk/v2/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" gormLogger "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 var logLevel gormLogger.LogLevel 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 if p.debug { logLevel = gormLogger.Info } else { logLevel = gormLogger.Silent } dbLogger := gormLogger.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), gormLogger.Config{ SlowThreshold: 200 * time.Millisecond, LogLevel: logLevel, 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) CreatePostInBatch(ctx context.Context, anthrovePost []models.Post, batchSize int) error { return postgres.CreatePostInBatch(ctx, p.db, anthrovePost, batchSize) } func (p *postgresqlConnection) CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error { return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag) } func (p *postgresqlConnection) CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error { return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, postURL, config) } 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) CheckIfUserHasPostAsFavorite(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.GetPostBySourceURL(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) GetAllUserSources(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) (*models.UserSource, error) { return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID) } func (p *postgresqlConnection) GetAllUsers(ctx context.Context) ([]models.User, error) { return postgres.GetAllUsers(ctx, p.db) } func (p *postgresqlConnection) GetAllUserFavoritesWithPagination(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) GetAllTagsFromUser(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) } // NEW FUNCTIONS func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error { return postgres.UpdateUserSourceScrapeTimeInterval(ctx, p.db, anthroveUserID, sourceID, scrapeTime) } func (p *postgresqlConnection) UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error { return postgres.UpdateUserSourceLastScrapeTime(ctx, p.db, anthroveUserID, sourceID, lastScrapeTime) } func (p *postgresqlConnection) UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error { return postgres.UpdateUserSourceValidation(ctx, p.db, anthroveUserID, sourceID, valid) } func (p *postgresqlConnection) CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error { return postgres.CreateTagAlias(ctx, p.db, tagAliasName, tagID) } func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) { return postgres.GetAllTagAlias(ctx, p.db) } func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) { return postgres.GetAllTagAliasByTag(ctx, p.db, tagID) } func (p *postgresqlConnection) DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error { return postgres.DeleteTagAlias(ctx, p.db, tagAliasName) } func (p *postgresqlConnection) CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error { return postgres.CreateTagGroup(ctx, p.db, tagGroupName, tagID) } func (p *postgresqlConnection) GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error) { return postgres.GetAllTagGroup(ctx, p.db) } func (p *postgresqlConnection) GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error) { return postgres.GetAllTagGroupByTag(ctx, p.db, tagID) } func (p *postgresqlConnection) DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error { return postgres.DeleteTagGroup(ctx, p.db, tagGroupName) } func (p *postgresqlConnection) CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error { return postgres.CreateTag(ctx, p.db, tagName, tagType) } func (p *postgresqlConnection) GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) { return postgres.GetAllTagByTagsType(ctx, p.db, tagType) } func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error { return postgres.DeleteTag(ctx, p.db, tagName) } func (p *postgresqlConnection) CreateTagInBatchAndUpdate(ctx context.Context, tags []models.Tag, batchSize int) error { return postgres.CreateTagInBatchAndUpdate(ctx, p.db, tags, batchSize) } func (p *postgresqlConnection) CreateTagAliasInBatch(ctx context.Context, tagAliases []models.TagAlias, batchSize int) error { return postgres.CreateTagAliasInBatch(ctx, p.db, tagAliases, batchSize) } func (p *postgresqlConnection) CreateTagGroupInBatch(ctx context.Context, tagGroups []models.TagGroup, batchSize int) error { return postgres.CreateTagGroupInBatch(ctx, p.db, tagGroups, batchSize) } // HELPER 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 }