Skip to content

Commit

Permalink
Use go:embed for migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottminns committed Nov 7, 2024
1 parent b15a67c commit 6c77aee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,9 @@ USER appuser

# Copy the executable from the "build" stage.
COPY --from=build /bin/server /bin/
COPY ./migrations ./migrations
COPY ./templates ./templates
COPY ./static ./static

ENV MIGRATIONS_URL=file://migrations

# Expose the port that the application listens on.
EXPOSE 8080

Expand Down
15 changes: 9 additions & 6 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"html/template"
"io/fs"
"log/slog"
"net/http"
"os"
Expand All @@ -18,13 +19,14 @@ import (
)

type App struct {
logger *slog.Logger
router *http.ServeMux
db *pgxpool.Pool
rdb *redis.Client
logger *slog.Logger
router *http.ServeMux
db *pgxpool.Pool
rdb *redis.Client
migrations fs.FS
}

func New(logger *slog.Logger) *App {
func New(logger *slog.Logger, migrations fs.FS) *App {
router := http.NewServeMux()

redisAddr, exists := os.LookupEnv("REDIS_ADDR")
Expand All @@ -38,13 +40,14 @@ func New(logger *slog.Logger) *App {
rdb: redis.NewClient(&redis.Options{
Addr: redisAddr,
}),
migrations: migrations,
}

return app
}

func (a *App) Start(ctx context.Context) error {
db, err := database.Connect(ctx, a.logger)
db, err := database.Connect(ctx, a.logger, a.migrations)
if err != nil {
return fmt.Errorf("failed to connect to db: %w", err)
}
Expand Down
17 changes: 9 additions & 8 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"log/slog"
"os"

"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jackc/pgx/v5/pgxpool"

"github.com/dreamsofcode-io/guestbook/internal/config"
Expand Down Expand Up @@ -60,7 +61,7 @@ func dbURL() (string, error) {
return cfg.URL(), nil
}

func Connect(ctx context.Context, logger *slog.Logger) (*pgxpool.Pool, error) {
func Connect(ctx context.Context, logger *slog.Logger, migrations fs.FS) (*pgxpool.Pool, error) {
config, err := loadConfig()
if err != nil {
return nil, err
Expand All @@ -73,17 +74,17 @@ func Connect(ctx context.Context, logger *slog.Logger) (*pgxpool.Pool, error) {

logger.Debug("Running migrations")

migrationsURL, exists := os.LookupEnv("MIGRATIONS_PATH")
if !exists {
migrationsURL = "file://migrations"
}

url, err := dbURL()
if err != nil {
return nil, err
}

migrator, err := migrate.New(migrationsURL, url)
source, err := iofs.New(migrations, "migrations")
if err != nil {
return nil, fmt.Errorf("failed to create source: %w", err)
}

migrator, err := migrate.NewWithSourceInstance("iofs", source, url)
if err != nil {
return nil, fmt.Errorf("migrate new: %s", err)
}
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"embed"
"log/slog"
"os"
"os/signal"
Expand All @@ -10,14 +11,17 @@ import (
"github.com/joho/godotenv"
)

//go:embed migrations/*.sql
var migrations embed.FS

func main() {
godotenv.Load()

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

a := app.New(logger)
a := app.New(logger, migrations)

if err := a.Start(ctx); err != nil {
logger.Error("failed to start server", slog.Any("error", err))
Expand Down

0 comments on commit 6c77aee

Please sign in to comment.