diff --git a/backend/go.mod b/backend/go.mod index 529ff3f..7351cc1 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -12,8 +12,8 @@ require ( github.com/google/uuid v1.6.0 github.com/jackc/pgx/v5 v5.7.2 github.com/joho/godotenv v1.5.1 - github.com/lib/pq v1.10.9 github.com/pressly/goose/v3 v3.24.1 + github.com/rabbitmq/amqp091-go v1.10.0 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 golang.org/x/crypto v0.31.0 diff --git a/backend/go.sum b/backend/go.sum index aa7bee7..720a7d9 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -48,8 +48,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= -github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -67,6 +65,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pressly/goose/v3 v3.24.1 h1:bZmxRco2uy5uu5Ng1MMVEfYsFlrMJI+e/VMXHQ3C4LY= github.com/pressly/goose/v3 v3.24.1/go.mod h1:rEWreU9uVtt0DHCyLzF9gRcWiiTF/V+528DV+4DORug= +github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw= +github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= @@ -99,6 +99,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= diff --git a/backend/internal/cmd/rollback.go b/backend/internal/cmd/rollback.go new file mode 100644 index 0000000..ae85d1c --- /dev/null +++ b/backend/internal/cmd/rollback.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "context" + "database/sql" + "fmt" + "log/slog" + "os" + "strconv" + + _ "github.com/jackc/pgx/v5/stdlib" + "github.com/pressly/goose/v3" + "github.com/spf13/cobra" + + "github.com/forfarm/backend/migrations" +) + +func RollbackCmd(ctx context.Context, dbDriver, dbSource string) *cobra.Command { + cmd := &cobra.Command{ + Use: "rollback [version]", + Short: "Rollback database migrations to a specific version", + Args: cobra.ExactArgs(1), // Ensure exactly one argument is provided + RunE: func(cmd *cobra.Command, args []string) error { + logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + + db, err := sql.Open(dbDriver, dbSource) + if err != nil { + return fmt.Errorf("failed to open database: %w", err) + } + defer db.Close() + + targetVersion := args[0] + targetVersionInt, err := strconv.Atoi(targetVersion) + if err != nil { + logger.Error("failed to convert version to integer", "version", targetVersion) + return err + } + targetVersionInt64 := int64(targetVersionInt) + + if err := goose.SetDialect(dbDriver); err != nil { + return fmt.Errorf("failed to set dialect: %w", err) + } + + if err := goose.DownTo(db, migrations.MigrationsDir, targetVersionInt64); err != nil { + return fmt.Errorf("failed to rollback to version %s: %w", targetVersion, err) + } + + logger.Info("Successfully rolled back to version", "version", targetVersion) + return nil + }, + } + + return cmd +} diff --git a/backend/internal/cmd/root.go b/backend/internal/cmd/root.go index df0b443..eced034 100644 --- a/backend/internal/cmd/root.go +++ b/backend/internal/cmd/root.go @@ -19,6 +19,7 @@ func Execute(ctx context.Context) int { rootCmd.AddCommand(APICmd(ctx)) rootCmd.AddCommand(MigrateCmd(ctx, "pgx", config.DATABASE_URL)) + rootCmd.AddCommand(RollbackCmd(ctx, "pgx", config.DATABASE_URL)) if err := rootCmd.Execute(); err != nil { return 1 diff --git a/backend/makefile b/backend/makefile index 49fbb06..dd79ec7 100644 --- a/backend/makefile +++ b/backend/makefile @@ -7,4 +7,7 @@ run: go run cmd/forfarm/main.go API migrate: - go run cmd/forfarm/main.go migrate \ No newline at end of file + go run cmd/forfarm/main.go migrate + +rollback: + go run cmd/forfarm/main.go rollback $(VERSION) \ No newline at end of file diff --git a/backend/migrations/embed.go b/backend/migrations/embed.go index 3e96d80..70280a3 100644 --- a/backend/migrations/embed.go +++ b/backend/migrations/embed.go @@ -6,3 +6,5 @@ import ( //go:embed *.sql var EmbedMigrations embed.FS + +const MigrationsDir = "migrations"