Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error checking and logging to migrations #300

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/draconctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func initializeConfig(cmd *cobra.Command) error {

// Bind to environment variables
// Works great for simple config names, but needs help for names
// like --favorite-color which we fix in the bindFlags function
// like --favorite-color which we fix in the visitall function
v.AutomaticEnv()

if !slices.Contains(supportedLogFormats, strings.ToLower(loggingFormat)) {
Expand Down
3 changes: 3 additions & 0 deletions cmd/draconctl/migrations/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migrations
import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/golang-migrate/migrate/v4"
Expand Down Expand Up @@ -34,6 +35,8 @@ func applyMigrations(cmd *cobra.Command, args []string) error {
if migrationsCmdConfig.migratiosnPath == "" {
return fmt.Errorf("you need to provide a path to the migrations that will be applied")
}
slog.Info("applying migrations", "migrations path:", migrationsCmdConfig.migratiosnPath)

dirFS := os.DirFS(migrationsCmdConfig.migratiosnPath)

dbURL, err := db.ParseConnectionStr(migrationsCmdConfig.connStr)
Expand Down
2 changes: 2 additions & 0 deletions cmd/draconctl/migrations/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"

"github.com/golang-migrate/migrate/v4"
Expand Down Expand Up @@ -32,6 +33,7 @@ func inspectMigrations(cmd *cobra.Command, args []string) error {
if migrationsCmdConfig.migratiosnPath == "" {
return fmt.Errorf("you need to provide a path to the migrations that will be applied")
}
slog.Info("inspecting migrations", "migrations path:", migrationsCmdConfig.migratiosnPath)
dirFS := os.DirFS(migrationsCmdConfig.migratiosnPath)

dbURL, err := db.ParseConnectionStr(migrationsCmdConfig.connStr)
Expand Down
2 changes: 2 additions & 0 deletions cmd/draconctl/migrations/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migrations
import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/golang-migrate/migrate/v4"
Expand Down Expand Up @@ -30,6 +31,7 @@ func revertMigrations(cmd *cobra.Command, args []string) error {
if migrationsCmdConfig.migratiosnPath == "" {
return fmt.Errorf("you need to provide a path to the migrations that will be applied")
}
slog.Info("reverting migrations", "migrations path:", migrationsCmdConfig.migratiosnPath)
dirFS := os.DirFS(migrationsCmdConfig.migratiosnPath)

dbURL, err := db.ParseConnectionStr(migrationsCmdConfig.connStr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
- apply
- --url
- "postgresql://{{ .Values.database.auth.username }}:{{ .Values.database.auth.password }}@{{ .Values.database.host }}/{{ .Values.database.auth.database }}?{{ .Values.database.auth.querystringargs}}"
- ${DRACONCTL_MIGRATIONS_PATH}
resources:
{{- toYaml .Values.resources | nindent 12 }}
serviceAccountName: {{ include "deduplication_db_migrations.fullname" . }}
Expand Down
3 changes: 3 additions & 0 deletions pkg/db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Migrations struct {

func (m *Migrations) ListAvailable(migrationsDir fs.FS) (*bindata.AssetSource, error) {
var assetNames []string
if _, err := fs.Stat(migrationsDir, "."); err != nil {
return nil, fmt.Errorf("could not find migrations directory: %w", err)
}
if err := fs.WalkDir(migrationsDir, ".", func(path string, info fs.DirEntry, err error) error {
if !info.IsDir() {
assetNames = append(assetNames, info.Name())
Expand Down
6 changes: 3 additions & 3 deletions tests/migrations/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ services:
- |
apt-get update && apt-get install -y jq &&
echo "------------------------------ Starting tests ------------------------------" &&
/bin/draconctl migrations inspect --url postgres://postgres:password@postgres:5432?sslmode=disable --json | tee | jq -eM '. | select(.Version == 0)' &&
/bin/draconctl migrations inspect --url postgres://postgres:password@postgres:5432?sslmode=disable --json |grep -v 'level=' | tee | jq -eM '. | select(.Version == 0)' &&
echo "------------------------------ Applying migrations ------------------------------" &&
/bin/draconctl migrations apply --url postgres://postgres:password@postgres:5432?sslmode=disable &&
/bin/draconctl migrations inspect --url postgres://postgres:password@postgres:5432?sslmode=disable --json | tee | jq -eM '. | select(.Version > 0)' &&
/bin/draconctl migrations inspect --url postgres://postgres:password@postgres:5432?sslmode=disable --json |grep -v 'level=' | tee | jq -eM '. | select(.Version > 0)' &&
echo "------------------------------ Reverting migrations ------------------------------" &&
/bin/draconctl migrations revert --url postgres://postgres:password@postgres:5432?sslmode=disable &&
/bin/draconctl migrations inspect --url postgres://postgres:password@postgres:5432?sslmode=disable --json | tee | jq -eM '. | select(.Version == 0)'
/bin/draconctl migrations inspect --url postgres://postgres:password@postgres:5432?sslmode=disable --json |grep -v 'level=' | tee | jq -eM '. | select(.Version == 0)'

networks:
test-migrations:
Loading