Skip to content

Commit

Permalink
refactor: Command reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-dee7 committed Aug 14, 2023
1 parent aeae2fe commit f93cdba
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 193 deletions.
86 changes: 83 additions & 3 deletions cmd/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ import (
"github.com/urfave/cli/v2"
)

const CategoryMigrations = "Migrations"

func NewMigrationsCommand() *cli.Command {
return &cli.Command{
Name: "migrations",
Aliases: []string{"m"},
Usage: "Run database migrations for OpenRegistry data store",
Name: "migrations",
Aliases: []string{"m"},
Usage: "Run database migrations for OpenRegistry data store",
Category: CategoryMigrations,
Description: `Perform migrations for the OpenRegistry database like database initialisation, migrations,
rollback, reset, etc`,
UsageText: `OpenRegistry CLI provides a collection of commands for running database migrations.
Examples:
1. openregistry migrations init --openregistry-db-dsn=<openregistry_db_dsn> --admin-db-dsn=<admin_db_dsn>
2. openregistry migrations run --openregistry-db-dsn=<openregistry_db_dsn>
3. openregistry migrations generate --openregistry-db-dsn=<openregistry_db_dsn>
4. openregistry migrations reset --openregistry-db-dsn=<openregistry_db_dsn>`,
Subcommands: []*cli.Command{
newDatabaseInitCommand(),
newMigrationsRunCommand(),
Expand Down Expand Up @@ -278,3 +289,72 @@ func parseDatabaseFlags(ctx *cli.Context) *databaseOptions {
adminPassword: adminPassword,
}
}

func getOpenRegistryDatabaseCmdFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "openregistry-db-dsn",
Required: false,
},
&cli.StringFlag{
Name: "database",
Value: "open_registry",
Required: false,
},
&cli.StringFlag{
Name: "host",
Value: "0.0.0.0",
Required: false,
},
&cli.StringFlag{
Name: "port",
Value: "5432",
Required: false,
},
&cli.StringFlag{
Name: "username",
Value: "open_registry_user",
DefaultText: "open_registry_user",
Required: false,
},
&cli.StringFlag{
Name: "password",
Value: "",
Required: false,
},
&cli.DurationFlag{
Name: "timeout",
Value: time.Second * 60,
Required: false,
},
&cli.BoolFlag{
Name: "insecure",
Value: false,
Required: false,
},
}
}

func getAdminDatabaseFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "admin-db-dsn",
Required: false,
},
&cli.StringFlag{
Name: "admin-db",
Value: "postgres",
Required: false,
},
&cli.StringFlag{
Name: "admin-db-username",
Value: "postgres",
Required: false,
},
&cli.StringFlag{
Name: "admin-db-password",
Value: "",
Required: false,
},
}
}
85 changes: 46 additions & 39 deletions cmd/migrationsGenerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,53 @@ import (

func newMigrationsGenrateCommand() *cli.Command {
return &cli.Command{
Name: "generate",
Usage: "Generate database migration files",
Aliases: []string{"gen", "ge"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "openregistry-db-dsn",
Value: "postgres://localhost:5432/open_registry",
Required: true,
},
&cli.StringFlag{
Name: "name",
Value: "column_name",
Required: true,
},
&cli.StringFlag{
Name: "operation",
Value: "add",
Required: true,
Aliases: []string{"op"},
Usage: "What will this migration do? Add column/s or remove them?",
},
},
Action: func(ctx *cli.Context) error {
name := ctx.String("name")
operation := ctx.String("operation")
opts := parseDatabaseFlags(ctx)
connector := getDBConnectorFromCtx(false, opts)
db := getOpenRegistryDB(connector)
migrator := migrations.NewMigrator(db)
Name: "generate",
Usage: "Generate database migration files",
Aliases: []string{"gen", "ge"},
Category: CategoryMigrations,
Flags: getMigrationGenerateCmdFlags(),
Action: migrationGenerateCmd,
}
}

migrationFile, err := migrator.CreateGoMigration(
ctx.Context,
fmt.Sprintf("%s_%s", operation, name),
migrate.WithPackageName("migrations"),
)
if err != nil {
return err
}
color.Green("Migration file created at - \"%s\" ✔︎", migrationFile.Path)
return nil
func migrationGenerateCmd(ctx *cli.Context) error {
name := ctx.String("name")
operation := ctx.String("operation")
opts := parseDatabaseFlags(ctx)
connector := getDBConnectorFromCtx(false, opts)
db := getOpenRegistryDB(connector)
migrator := migrations.NewMigrator(db)

migrationFile, err := migrator.CreateGoMigration(
ctx.Context,
fmt.Sprintf("%s_%s", operation, name),
migrate.WithPackageName("migrations"),
)
if err != nil {
return err
}
color.Green("Migration file created at - \"%s\" ✔︎", migrationFile.Path)
return nil
}

func getMigrationGenerateCmdFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "openregistry-db-dsn",
Value: "postgres://localhost:5432/open_registry",
Required: true,
},
&cli.StringFlag{
Name: "name",
Value: "column_name",
Required: true,
},
&cli.StringFlag{
Name: "operation",
Value: "add",
Required: true,
Aliases: []string{"op"},
Usage: "What will this migration do? Add column/s or remove them?",
},
}
}
152 changes: 45 additions & 107 deletions cmd/migrationsInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"errors"
"time"

"github.com/containerish/OpenRegistry/store/v2/migrations"
"github.com/fatih/color"
Expand All @@ -11,117 +10,56 @@ import (

func newDatabaseInitCommand() *cli.Command {
return &cli.Command{
Name: "init",
Usage: "Initialise the database, create tables, roles, indexes, etc",
Flags: getInitCmdFlags(),
Action: func(ctx *cli.Context) error {
opts := parseDatabaseFlags(ctx)
openRegistryDB, err := createOpenRegistryDatabase(ctx, opts)
if err != nil {
return err
}

migrator := migrations.NewMigrator(openRegistryDB)
err = migrator.Init(ctx.Context)
if err != nil {
return errors.New(
color.RedString("Tables=migration_locks Created=❌ Error=%s", err),
)
}
color.Green(`Table "open_registry_migration_locks" created ✔︎`)
color.Green(`Table "open_registry_migrations" created ✔︎`)
Name: "init",
Usage: "Initialise the database, create tables, roles, indexes, etc",
Category: CategoryMigrations,
Flags: append(getOpenRegistryDatabaseCmdFlags(), getAdminDatabaseFlags()...),
Action: databaseInitCmd,
}
}

if err = createOpenRegistryTables(ctx, openRegistryDB); err != nil {
return err
}
func databaseInitCmd(ctx *cli.Context) error {
opts := parseDatabaseFlags(ctx)
openRegistryDB, err := createOpenRegistryDatabase(ctx, opts)
if err != nil {
return err
}

_, err = openRegistryDB.ExecContext(
ctx.Context,
"alter table repositories add constraint fk_owner_id foreign key (owner_id) references users(id)",
)
if err != nil {
return errors.New(
color.RedString("Table=repositories Alter=❌ Error=%s", err),
)
}
color.Green(`Alter "owner_id" to add FK constraint done ✔︎`)
migrator := migrations.NewMigrator(openRegistryDB)
err = migrator.Init(ctx.Context)
if err != nil {
return errors.New(
color.RedString("Tables=migration_locks Created=❌ Error=%s", err),
)
}
color.Green(`Table "open_registry_migration_locks" created ✔︎`)
color.Green(`Table "open_registry_migrations" created ✔︎`)

_, err = openRegistryDB.ExecContext(
ctx.Context,
"alter table image_manifests add unique (reference,repository_id)",
)
if err != nil {
return errors.New(
color.RedString("Table=image_manifests Alter=❌ Error=%s", err),
)
}
color.Green(`Alter "reference,repository_id" to add unique group constraint done ✔︎`)
if err = createOpenRegistryTables(ctx, openRegistryDB); err != nil {
return err
}

return nil
},
_, err = openRegistryDB.ExecContext(
ctx.Context,
"alter table repositories add constraint fk_owner_id foreign key (owner_id) references users(id)",
)
if err != nil {
return errors.New(
color.RedString("Table=repositories Alter=❌ Error=%s", err),
)
}
}
color.Green(`Alter "owner_id" to add FK constraint done ✔︎`)

func getInitCmdFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "openregistry-db-dsn",
Required: false,
},
&cli.StringFlag{
Name: "admin-db-dsn",
Required: false,
},
&cli.StringFlag{
Name: "admin-db",
Value: "postgres",
Required: false,
},
&cli.StringFlag{
Name: "admin-db-username",
Value: "postgres",
Required: false,
},
&cli.StringFlag{
Name: "admin-db-password",
Value: "",
Required: false,
},
&cli.StringFlag{
Name: "database",
Value: "open_registry",
Required: false,
},
&cli.StringFlag{
Name: "host",
Value: "0.0.0.0",
Required: false,
},
&cli.StringFlag{
Name: "port",
Value: "5432",
Required: false,
},
&cli.StringFlag{
Name: "username",
Value: "open_registry_user",
DefaultText: "open_registry_user",
Required: false,
},
&cli.StringFlag{
Name: "password",
Value: "",
Required: false,
},
&cli.DurationFlag{
Name: "timeout",
Value: time.Second * 60,
Required: false,
},
&cli.BoolFlag{
Name: "insecure",
Value: false,
Required: false,
},
_, err = openRegistryDB.ExecContext(
ctx.Context,
"alter table image_manifests add unique (reference,repository_id)",
)
if err != nil {
return errors.New(
color.RedString("Table=image_manifests Alter=❌ Error=%s", err),
)
}
color.Green(`Alter "reference,repository_id" to add unique group constraint done ✔︎`)

return nil
}
Loading

0 comments on commit f93cdba

Please sign in to comment.