migrate

package
v0.0.0-...-1c65c5f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApplyCmd = &cobra.Command{
	Use:   "apply",
	Short: "apply migrations",
	Run: func(cmd *cobra.Command, args []string) {
		cmdPath := checkAtlas()
		c := exec.Command(cmdPath, "migrate", "apply",
			"--dir", fmt.Sprintf("file://%s", migrationsDir),
			"--url", db.GetMigrateDSN(),
		)

		out, err := c.CombinedOutput()
		if err != nil {
			logger.Errorf("apply migrations failed: %s", out)
			os.Exit(1)
		}
		logger.Infof("apply migrations successfully: %s", out)
	},
}
View Source
var GenerateCmd = &cobra.Command{
	Use:   "generate",
	Short: "generate migrations",
	Run: func(cmd *cobra.Command, args []string) {
		ctx := context.Background()

		dir, err := atlas.NewLocalDir(migrationsDir)
		if err != nil {
			logger.Errorf("failed creating atlas migration directory: %v", err)
			os.Exit(1)
		}

		opts := []schema.MigrateOption{
			schema.WithDir(dir),
			schema.WithMigrationMode(schema.ModeInspect),
			schema.WithDialect(viper.GetString("db.type")),
			schema.WithDropColumn(true),
			schema.WithDropIndex(true),
			schema.WithForeignKeys(false),
			schema.WithFormatter(atlas.DefaultFormatter),
		}

		err = migrate.NamedDiff(ctx, db.GetMigrateDSN(), migrationName, opts...)
		if err != nil {
			logger.Fatalf("failed generating migration file: %v", err)
			os.Exit(1)
		}
		logger.Infof("generate migration %s successfully", migrationName)
	},
}
View Source
var HashCmd = &cobra.Command{
	Use:   "hash",
	Short: "update migrations hash",
	Run: func(cmd *cobra.Command, args []string) {
		cmdPath := checkAtlas()
		c := exec.Command(cmdPath, "migrate", "hash",
			"--dir", fmt.Sprintf("file://%s", migrationsDir),
		)

		out, err := c.CombinedOutput()
		if err != nil {
			logger.Errorf("update migrations hash failed: %s", out)
			os.Exit(1)
		}
		logger.Infof("update migrations hash successfully")
	},
}
View Source
var LintCmd = &cobra.Command{
	Use:   "lint",
	Short: "lint migrations",
	Run: func(cmd *cobra.Command, args []string) {
		cmdPath := checkAtlas()
		if lintLatest < 1 {
			lintLatest = 1
		}
		c := exec.Command(cmdPath, "migrate", "lint",
			"--dev-url", "docker://mysql/8/test",
			"--dir", fmt.Sprintf("file://%s", migrationsDir),
			"--latest", strconv.Itoa(lintLatest),
		)

		out, err := c.CombinedOutput()
		if err != nil {
			logger.Errorf("lint migrations failed: %s", out)
			os.Exit(1)
		}
		logger.Infof("lint migrations successfully")
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL