1
0
Fork 0

added a new sitetype for sites that need access to the pool outside of requests

Dieser Commit ist enthalten in:
Sebastian Tobie 2022-11-14 10:37:23 +01:00
Ursprung 1b64551582
Commit 0f9fded567
1 geänderte Dateien mit 26 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -132,23 +132,26 @@ func (m *Middleware) PostSetup(sites []any) (err error) {
db *pgxpool.Pool db *pgxpool.Pool
) )
for _, s := range sites { for _, s := range sites {
site, ok := s.(MigrationSite)
if !ok { if site, ok := s.(MigrationSite); ok {
continue
} db, ok = m.databases[site.Database()]
db, ok = m.databases[site.Database()] if !ok {
if !ok { return fmt.Errorf("Failed to get the database. The Databasepool %s does not exist", site.Database())
return fmt.Errorf("Failed to get the database. The Databasepool %s does not exist", site.Database()) }
} conn, err = db.Acquire(context.TODO())
conn, err = db.Acquire(context.TODO()) if err != nil {
if err != nil { return
return }
} defer conn.Release()
defer conn.Release() mig, err = site.Migrations(conn.Conn())
mig, err = site.Migrations(conn.Conn()) err = mig.Migrate(context.TODO())
err = mig.Migrate(context.TODO()) if err != nil {
if err != nil { return
return }
if poolsite, ok := s.(PoolSite); ok {
poolsite.Pool(m.databases[site.Database()])
}
} }
} }
return return
@ -165,6 +168,12 @@ type MigrationSite interface {
Migrations(*pgx.Conn) (*migrate.Migrator, error) Migrations(*pgx.Conn) (*migrate.Migrator, error)
} }
// PoolSite is an interface for site that need access to the pool outside of requests
type PoolSite interface {
MigrationSite
Pool(*pgxpool.Pool)
}
// SetupMigrator sets up the migrator to migrate the database. // SetupMigrator sets up the migrator to migrate the database.
func SetupMigrator(prefix string, connection *pgx.Conn, migrations fs.FS) (mig *migrate.Migrator, err error) { func SetupMigrator(prefix string, connection *pgx.Conn, migrations fs.FS) (mig *migrate.Migrator, err error) {
mig, err = migrate.NewMigratorEx( mig, err = migrate.NewMigratorEx(