From b65e942f24a2e7660087d0b712ffe164eb871740 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 4 Jun 2024 13:28:14 +0200 Subject: [PATCH] feat(db): embed migration files Signed-off-by: SoXX --- .../001_create_enum_Rating_2024_06_04.sql | 0 .../002_create_enum_TagType_2024_06_04.sql | 0 .../003_create_table_Post_2024_06_04.sql | 0 .../004_create_table_Source_2024_06_04.sql | 0 .../005_create_table_Tag_2024_06_04.sql | 0 .../006_create_table_User_2024_06_04.sql | 0 ..._create_table_PostReference_2024_06_04.sql | 0 .../008_create_table_TagAlias_2024_06_04.sql | 0 .../009_create_table_TagGroup_2024_06_04.sql | 0 ...0_create_table_UserFavorite_2024_06_04.sql | 0 ...011_create_table_UserSource_2024_06_04.sql | 0 pkg/database/postgres.go | 21 +++++++++++++------ 12 files changed, 15 insertions(+), 6 deletions(-) rename {db => pkg/database}/migrations/001_create_enum_Rating_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/002_create_enum_TagType_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/003_create_table_Post_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/004_create_table_Source_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/005_create_table_Tag_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/006_create_table_User_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/007_create_table_PostReference_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/008_create_table_TagAlias_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/009_create_table_TagGroup_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/010_create_table_UserFavorite_2024_06_04.sql (100%) rename {db => pkg/database}/migrations/011_create_table_UserSource_2024_06_04.sql (100%) diff --git a/db/migrations/001_create_enum_Rating_2024_06_04.sql b/pkg/database/migrations/001_create_enum_Rating_2024_06_04.sql similarity index 100% rename from db/migrations/001_create_enum_Rating_2024_06_04.sql rename to pkg/database/migrations/001_create_enum_Rating_2024_06_04.sql diff --git a/db/migrations/002_create_enum_TagType_2024_06_04.sql b/pkg/database/migrations/002_create_enum_TagType_2024_06_04.sql similarity index 100% rename from db/migrations/002_create_enum_TagType_2024_06_04.sql rename to pkg/database/migrations/002_create_enum_TagType_2024_06_04.sql diff --git a/db/migrations/003_create_table_Post_2024_06_04.sql b/pkg/database/migrations/003_create_table_Post_2024_06_04.sql similarity index 100% rename from db/migrations/003_create_table_Post_2024_06_04.sql rename to pkg/database/migrations/003_create_table_Post_2024_06_04.sql diff --git a/db/migrations/004_create_table_Source_2024_06_04.sql b/pkg/database/migrations/004_create_table_Source_2024_06_04.sql similarity index 100% rename from db/migrations/004_create_table_Source_2024_06_04.sql rename to pkg/database/migrations/004_create_table_Source_2024_06_04.sql diff --git a/db/migrations/005_create_table_Tag_2024_06_04.sql b/pkg/database/migrations/005_create_table_Tag_2024_06_04.sql similarity index 100% rename from db/migrations/005_create_table_Tag_2024_06_04.sql rename to pkg/database/migrations/005_create_table_Tag_2024_06_04.sql diff --git a/db/migrations/006_create_table_User_2024_06_04.sql b/pkg/database/migrations/006_create_table_User_2024_06_04.sql similarity index 100% rename from db/migrations/006_create_table_User_2024_06_04.sql rename to pkg/database/migrations/006_create_table_User_2024_06_04.sql diff --git a/db/migrations/007_create_table_PostReference_2024_06_04.sql b/pkg/database/migrations/007_create_table_PostReference_2024_06_04.sql similarity index 100% rename from db/migrations/007_create_table_PostReference_2024_06_04.sql rename to pkg/database/migrations/007_create_table_PostReference_2024_06_04.sql diff --git a/db/migrations/008_create_table_TagAlias_2024_06_04.sql b/pkg/database/migrations/008_create_table_TagAlias_2024_06_04.sql similarity index 100% rename from db/migrations/008_create_table_TagAlias_2024_06_04.sql rename to pkg/database/migrations/008_create_table_TagAlias_2024_06_04.sql diff --git a/db/migrations/009_create_table_TagGroup_2024_06_04.sql b/pkg/database/migrations/009_create_table_TagGroup_2024_06_04.sql similarity index 100% rename from db/migrations/009_create_table_TagGroup_2024_06_04.sql rename to pkg/database/migrations/009_create_table_TagGroup_2024_06_04.sql diff --git a/db/migrations/010_create_table_UserFavorite_2024_06_04.sql b/pkg/database/migrations/010_create_table_UserFavorite_2024_06_04.sql similarity index 100% rename from db/migrations/010_create_table_UserFavorite_2024_06_04.sql rename to pkg/database/migrations/010_create_table_UserFavorite_2024_06_04.sql diff --git a/db/migrations/011_create_table_UserSource_2024_06_04.sql b/pkg/database/migrations/011_create_table_UserSource_2024_06_04.sql similarity index 100% rename from db/migrations/011_create_table_UserSource_2024_06_04.sql rename to pkg/database/migrations/011_create_table_UserSource_2024_06_04.sql diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 77870f4..f8773e6 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -3,6 +3,7 @@ package database import ( "context" "database/sql" + "embed" "fmt" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" @@ -10,10 +11,14 @@ import ( "git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels" _ "github.com/lib/pq" migrate "github.com/rubenv/sql-migrate" + log "github.com/sirupsen/logrus" gormPostgres "gorm.io/driver/postgres" "gorm.io/gorm" ) +//go:embed migrations/*.sql +var embedMigrations embed.FS + type postgresqlConnection struct { db *gorm.DB } @@ -142,20 +147,24 @@ func (p postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl stri func (p postgresqlConnection) migrateDatabase(connectionString string) error { dialect := "postgres" - migrations := &migrate.FileMigrationSource{ - Dir: "db/migrations", - } + migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} db, err := sql.Open(dialect, connectionString) if err != nil { - return err + return fmt.Errorf("postgres migration: %v", err) } n, err := migrate.Exec(db, dialect, migrations, migrate.Up) if err != nil { - return err + return fmt.Errorf("postgres migration: %v", err) + } + if n != 0 { + log.Infof("postgres migration: applied %d migrations!", n) + + } else { + log.Info("postgres migration: nothing to migrate") + } - fmt.Printf("Applied %d migrations!\n", n) return nil }