mirror of
https://github.com/lus/pasty.git
synced 2023-08-10 21:13:09 +03:00
75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"errors"
|
|
"github.com/golang-migrate/migrate/v4"
|
|
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/lus/pasty/internal/pastes"
|
|
"github.com/lus/pasty/internal/storage"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrations embed.FS
|
|
|
|
type Driver struct {
|
|
dsn string
|
|
connPool *pgxpool.Pool
|
|
pastes *pasteRepository
|
|
}
|
|
|
|
var _ storage.Driver = (*Driver)(nil)
|
|
|
|
func New(dsn string) *Driver {
|
|
return &Driver{
|
|
dsn: dsn,
|
|
}
|
|
}
|
|
|
|
func (driver *Driver) Initialize(ctx context.Context) error {
|
|
pool, err := pgxpool.New(ctx, driver.dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Info().Msg("Performing PostgreSQL database migrations...")
|
|
source, err := iofs.New(migrations, "migrations")
|
|
if err != nil {
|
|
pool.Close()
|
|
return err
|
|
}
|
|
migrator, err := migrate.NewWithSourceInstance("iofs", source, driver.dsn)
|
|
if err != nil {
|
|
pool.Close()
|
|
return err
|
|
}
|
|
defer func() {
|
|
_, _ = migrator.Close()
|
|
}()
|
|
if err := migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
|
pool.Close()
|
|
return err
|
|
}
|
|
|
|
driver.connPool = pool
|
|
driver.pastes = &pasteRepository{
|
|
connPool: pool,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (driver *Driver) Close() error {
|
|
driver.pastes = nil
|
|
driver.connPool.Close()
|
|
driver.connPool = nil
|
|
return nil
|
|
}
|
|
|
|
func (driver *Driver) Pastes() pastes.Repository {
|
|
return driver.pastes
|
|
}
|