2024-06-28 12:16:28 +00:00
|
|
|
package database
|
2024-06-03 19:31:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
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-07-02 08:11:42 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/internal/postgres"
|
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/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-07-01 19:38:09 +00:00
|
|
|
gormLogger "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-28 12:16:28 +00:00
|
|
|
func NewPostgresqlConnection() OtterSpace {
|
2024-06-03 19:31:44 +00:00
|
|
|
return &postgresqlConnection{
|
2024-06-24 11:13:32 +00:00
|
|
|
db: nil,
|
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
|
2024-07-01 19:38:09 +00:00
|
|
|
var logLevel gormLogger.LogLevel
|
|
|
|
|
2024-06-24 10:59:51 +00:00
|
|
|
if config.SSL {
|
|
|
|
localSSL = "require"
|
|
|
|
} else {
|
|
|
|
localSSL = "disable"
|
|
|
|
}
|
|
|
|
|
2024-06-24 12:28:09 +00:00
|
|
|
p.debug = config.Debug
|
|
|
|
|
2024-06-24 10:59:51 +00:00
|
|
|
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
|
|
|
|
|
2024-07-01 19:38:09 +00:00
|
|
|
if p.debug {
|
|
|
|
logLevel = gormLogger.Info
|
|
|
|
} else {
|
|
|
|
logLevel = gormLogger.Silent
|
|
|
|
}
|
|
|
|
|
|
|
|
dbLogger := gormLogger.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), gormLogger.Config{
|
2024-06-21 21:48:18 +00:00
|
|
|
SlowThreshold: 200 * time.Millisecond,
|
2024-07-01 19:38:09 +00:00
|
|
|
LogLevel: logLevel,
|
2024-06-21 21:48:18 +00:00
|
|
|
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-24 11:13:32 +00:00
|
|
|
log.Infof("OtterSpace: database connection established")
|
|
|
|
|
|
|
|
err = p.migrateDatabase(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("OtterSpace: migration compleate")
|
2024-06-04 11:44:15 +00:00
|
|
|
|
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-07-03 20:57:04 +00:00
|
|
|
func (p *postgresqlConnection) CreatePostInBatch(ctx context.Context, db *gorm.DB, anthrovePost []models.Post, batchSize int) error {
|
|
|
|
return postgres.CreatePostInBatch(ctx, p.db, anthrovePost, batchSize)
|
|
|
|
}
|
|
|
|
|
2024-06-28 12:16:28 +00:00
|
|
|
func (p *postgresqlConnection) CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error {
|
2024-06-23 19:23:38 +00:00
|
|
|
return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 12:16:28 +00:00
|
|
|
func (p *postgresqlConnection) CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error {
|
2024-06-27 20:05:21 +00:00
|
|
|
return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, postURL, config)
|
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-28 10:15:51 +00:00
|
|
|
func (p *postgresqlConnection) CheckIfUserHasPostAsFavorite(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
|
2024-06-23 20:35:46 +00:00
|
|
|
return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 20:07:34 +00:00
|
|
|
func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) {
|
|
|
|
return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePostID)
|
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) {
|
2024-07-01 19:38:09 +00:00
|
|
|
return postgres.GetPostBySourceURL(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-28 10:15:51 +00:00
|
|
|
func (p *postgresqlConnection) GetAllUserSources(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-28 10:15:51 +00:00
|
|
|
func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) {
|
2024-06-23 20:35:46 +00:00
|
|
|
return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 12:16:28 +00:00
|
|
|
func (p *postgresqlConnection) GetAllUsers(ctx context.Context) ([]models.User, error) {
|
2024-06-28 10:15:51 +00:00
|
|
|
return postgres.GetAllUsers(ctx, p.db)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 10:15:51 +00:00
|
|
|
func (p *postgresqlConnection) GetAllUserFavoritesWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
|
2024-06-23 19:23:38 +00:00
|
|
|
return postgres.GetUserFavoriteWithPagination(ctx, p.db, anthroveUserID, skip, limit)
|
2024-06-03 19:31:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 10:15:51 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTagsFromUser(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
|
2024-06-23 19:23:38 +00:00
|
|
|
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-28 13:13:40 +00:00
|
|
|
// NEW FUNCTIONS
|
2024-06-27 20:17:25 +00:00
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error {
|
2024-07-01 11:42:53 +00:00
|
|
|
return postgres.UpdateUserSourceScrapeTimeInterval(ctx, p.db, anthroveUserID, sourceID, scrapeTime)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error {
|
2024-07-01 11:42:53 +00:00
|
|
|
return postgres.UpdateUserSourceLastScrapeTime(ctx, p.db, anthroveUserID, sourceID, lastScrapeTime)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error {
|
2024-07-01 11:42:53 +00:00
|
|
|
return postgres.UpdateUserSourceValidation(ctx, p.db, anthroveUserID, sourceID, valid)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error {
|
|
|
|
return postgres.CreateTagAlias(ctx, p.db, tagAliasName, tagID)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) {
|
2024-06-28 20:14:27 +00:00
|
|
|
return postgres.GetAllTagAlias(ctx, p.db)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) {
|
2024-06-28 20:27:31 +00:00
|
|
|
return postgres.GetAllTagAliasByTag(ctx, p.db, tagID)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error {
|
2024-06-28 20:27:31 +00:00
|
|
|
return postgres.DeleteTagAlias(ctx, p.db, tagAliasName)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 20:39:12 +00:00
|
|
|
func (p *postgresqlConnection) CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error {
|
|
|
|
return postgres.CreateTagGroup(ctx, p.db, tagGroupName, tagID)
|
2024-06-27 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error) {
|
2024-06-28 20:39:12 +00:00
|
|
|
return postgres.GetAllTagGroup(ctx, p.db)
|
2024-06-28 08:43:55 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error) {
|
2024-06-28 20:39:12 +00:00
|
|
|
return postgres.GetAllTagGroupByTag(ctx, p.db, tagID)
|
2024-06-28 08:43:55 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
func (p *postgresqlConnection) DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error {
|
2024-06-28 20:39:12 +00:00
|
|
|
return postgres.DeleteTagGroup(ctx, p.db, tagGroupName)
|
2024-06-28 08:43:55 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 20:06:36 +00:00
|
|
|
func (p *postgresqlConnection) CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error {
|
|
|
|
return postgres.CreateTag(ctx, p.db, tagName, tagType)
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:29:37 +00:00
|
|
|
func (p *postgresqlConnection) GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) {
|
|
|
|
return postgres.GetAllTagByTagsType(ctx, p.db, tagType)
|
2024-07-01 20:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error {
|
2024-07-01 20:15:43 +00:00
|
|
|
return postgres.DeleteTag(ctx, p.db, tagName)
|
2024-07-01 20:06:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 20:35:42 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-28 13:13:40 +00:00
|
|
|
// HELPER
|
|
|
|
|
2024-06-24 11:13:32 +00:00
|
|
|
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) 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
|
|
|
|
2024-06-24 11:13:32 +00:00
|
|
|
db, err := dbPool.DB()
|
2024-06-04 07:33:35 +00:00
|
|
|
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
|
|
|
|
}
|