fleet/server/goose/goose.go
Lucas Manuel Rodriguez 38b8c9cc58
Move external dependency goose to monorepo (#15859)
#15555

Probably the best way to review this is commit by commit:
- First commit does the actual moving.
- Second commit fixes golangci-lint issues (in the least effort way to
avoid refactoring or rearrangement of some of the code).
- Third commit moves a printf to before the migration step is executed.
In the past some customers hitting migration issues (like migration
steps hanging or taking long to execute) and wanted to know which one
was it. The only way to know was to look at the repository and looking
for the next migration after the last one logged.

Checks:
- [X] Manual QA for all new/changed functionality

Manual tests:
- `make fleet && make db-reset`.
- Adding a new migration via `make migration name=Foobar` and then
running `./build/fleet prepare db`.
- Enrolling a new device to Fleet (smoke test).
2024-01-02 17:52:00 -03:00

77 lines
1.7 KiB
Go

package goose
import (
"database/sql"
"errors"
"fmt"
)
var (
minVersion = int64(0)
maxVersion = int64((1 << 63) - 1)
)
// Client stores the migration state and preferences. Prefer interacting with
// the Goose API through a Client struct created with New rather than using the
// global Client and functions.
type Client struct {
// TableName is the name of the table used to store migration status
// for this client.
TableName string
// Dialect is the SqlDialect to use.
Dialect SqlDialect
// Migrations is the list of migrations.
Migrations Migrations
}
func New(tableName string, dialect SqlDialect) *Client {
return &Client{
TableName: tableName,
Dialect: dialect,
}
}
func Run(command string, db *sql.DB, dir string, args ...string) error {
switch command {
case "up":
if err := globalGoose.Up(db, dir); err != nil {
return err
}
case "up-by-one":
if err := globalGoose.UpByOne(db, dir); err != nil {
return err
}
case "create":
if len(args) == 0 {
return errors.New("create must be of form: goose [OPTIONS] DRIVER DBSTRING create NAME [go|sql]")
}
migrationType := "go"
if len(args) == 2 {
migrationType = args[1]
}
if err := Create(db, dir, args[0], migrationType); err != nil {
return err
}
case "down":
if err := globalGoose.Down(db, dir); err != nil {
return err
}
case "redo":
if err := globalGoose.Redo(db, dir); err != nil {
return err
}
case "status":
if err := globalGoose.Status(db, dir); err != nil {
return err
}
case "version":
if err := globalGoose.Version(db, dir); err != nil {
return err
}
default:
return fmt.Errorf("%q: no such command", command)
}
return nil
}