-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'skip-migration-command' into main
- Loading branch information
Showing
10 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aleksanderaleksic/tgmigrate/common" | ||
"github.com/aleksanderaleksic/tgmigrate/config" | ||
"github.com/aleksanderaleksic/tgmigrate/history" | ||
"github.com/aleksanderaleksic/tgmigrate/migration" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type SkipCommand struct { | ||
Skipper *migration.Skipper | ||
} | ||
|
||
func (command *SkipCommand) GetCLICommand() *cli.Command { | ||
cmd := cli.Command{ | ||
Name: "skip", | ||
Aliases: nil, | ||
Usage: "Skips the provided migration", | ||
UsageText: "", | ||
Description: "This is useful if a migration is not able to complete successfully", | ||
ArgsUsage: "", | ||
Category: "", | ||
BashComplete: nil, | ||
Before: command.initialize, | ||
After: nil, | ||
Action: command.run, | ||
OnUsageError: nil, | ||
Subcommands: nil, | ||
Flags: nil, | ||
SkipFlagParsing: false, | ||
HideHelp: false, | ||
HideHelpCommand: false, | ||
Hidden: false, | ||
UseShortOptionHandling: false, | ||
HelpName: "", | ||
CustomHelpTemplate: "", | ||
} | ||
return &cmd | ||
} | ||
|
||
func (command *SkipCommand) run(c *cli.Context) error { | ||
migrationFileName := c.Args().First() | ||
if migrationFileName == "" { | ||
return fmt.Errorf("must include the migration file name as argument") | ||
} | ||
return command.Skipper.Skip(migrationFileName) | ||
} | ||
|
||
func (command *SkipCommand) initialize(c *cli.Context) error { | ||
cfg, err := config.GetConfigFile(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
chc := common.Cache{ | ||
ConfigFilePath: cfg.Path, | ||
} | ||
|
||
ctx := common.Context{ | ||
SkipUserInteraction: c.Bool("y"), | ||
DryRun: false, | ||
} | ||
|
||
historyInterface, err := history.GetHistoryInterface(*cfg, ctx, chc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s3Cfg := cfg.State.Config.(*config.S3StateConfig) | ||
awsSession, err := getAwsSession(s3Cfg.Region, s3Cfg.AssumeRole) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
skipper := migration.Skipper{ | ||
Context: &ctx, | ||
Config: cfg, | ||
HistoryInterface: historyInterface, | ||
S3StateClient: s3.New(awsSession), | ||
} | ||
|
||
command.Skipper = &skipper | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package migration | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aleksanderaleksic/tgmigrate/common" | ||
"github.com/aleksanderaleksic/tgmigrate/config" | ||
"github.com/aleksanderaleksic/tgmigrate/history" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
) | ||
|
||
type Skipper struct { | ||
Context *common.Context | ||
Config *config.Config | ||
S3StateClient *s3.S3 | ||
HistoryInterface history.History | ||
} | ||
|
||
func (r Skipper) Skip(migrationName string) error { | ||
hist, err := r.HistoryInterface.InitializeHistory() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
migrationFiles, err := GetMigrationFiles(r.Config.AbsoluteMigrationDir) | ||
if err != nil { | ||
return fmt.Errorf("could not get migration files from '%s', error: %s", r.Config.AbsoluteMigrationDir, err) | ||
} | ||
migrationFile := getMigration(migrationName, migrationFiles) | ||
if migrationFile == nil { | ||
return fmt.Errorf("migration file with name: '%s' was not found", migrationName) | ||
} | ||
|
||
for _, m := range hist.AppliedMigration { | ||
if m.Name == migrationName { | ||
return fmt.Errorf("migration file with name: '%s' is already applied", migrationName) | ||
} | ||
} | ||
for _, m := range hist.SkippedMigrations { | ||
if m.Name == migrationName { | ||
return fmt.Errorf("migration file with name: '%s' is already skipped", migrationName) | ||
} | ||
} | ||
|
||
r.HistoryInterface.StoreSkippedMigration(&history.SkippedStorageHistoryObject{ | ||
SchemaVersion: history.StorageHistoryObjectVersion, | ||
Skipped: common.JSONTime{}, | ||
Name: migrationName, | ||
}) | ||
|
||
r.HistoryInterface.WriteToStorage() | ||
|
||
return nil | ||
} | ||
|
||
func getMigration(migrationName string, migrations *[]File) *File { | ||
for _, migration := range *migrations { | ||
if migration.Metadata.FileName == migrationName { | ||
return &migration | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
{"schema_version":"v1","applied_migration":[],"failed_migration":[]} | ||
{ | ||
"schema_version": "v1", | ||
"applied_migration": [], | ||
"failed_migration": [] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.