migrator

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Copyright © 2023 Adharsh M dev@adharsh.in

Copyright © 2023 Adharsh M dev@adharsh.in

Copyright © 2023 Adharsh M dev@adharsh.in

Copyright © 2023 Adharsh M dev@adharsh.in

Copyright © 2023 Adharsh M dev@adharsh.in

Copyright © 2023 Adharsh M dev@adharsh.in

Index

Constants

This section is empty.

Variables

View Source
var CleanCmd = &cobra.Command{
	Use:   "clean",
	Short: "remove all unapplied migration files.",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {

		dryRun := cmd.Flag("dry").Value.String() == "true"

		workDir, dbType, logFile := sqlmigrator.DefaultContextConfig()
		ctx := sqlmigrator.NewContext(workDir, dbType, logFile, dryRun)
		ctx.LoadMigrationEntries()

		log.Println("cleaning unapplied migrations...")

		generator := &sqlmigrator.Generator{
			DryRun: dryRun,
		}

		removedFiles, err := generator.Clean(ctx)
		if err != nil {
			log.Fatal(err)
			return
		}

		displayCleanedFiles(removedFiles)
		err = ctx.WriteMigrationEntries()
		if err != nil {
			log.Println("error writing migration entries:", err)
			return
		}
		log.Println("cleaned migrations successfully.")

	},
}

CleanCmd represents the mkconfig command

View Source
var DownCmd = &cobra.Command{
	Use:   "down",
	Short: "perform backward migration from the files in the migrations folder",
	Args:  cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		dryRun := cmd.Flag("dry").Value.String() == "true"
		num := getNumberFromArgs(args, 0)

		workDir, dbType, logFile := sqlmigrator.DefaultContextConfig()
		ctx := sqlmigrator.NewContext(workDir, dbType, logFile, dryRun)
		ctx.LoadMigrationEntries()

		dbRepo := dbrepo.SelectDBRepo(dbType)
		migrator := sqlmigrator.NewMigrator(dbRepo)

		rolledBackMigrations, err := migrator.MigrateDown(ctx, num)
		if err != nil {
			log.Fatal(err)
			return
		}

		err = ctx.WriteMigrationEntries()
		if err != nil {
			log.Println("error writing migration entries:", err)
			return
		}

		displayRolledBack(rolledBackMigrations)
		log.Println("migrated to database successfully.")

	},
}

DownCmd represents the mkconfig command

View Source
var GenerateCmd = &cobra.Command{
	Use:   "generate",
	Short: "generate migration files.",
	Args:  cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {

		dryRun := cmd.Flag("dry").Value.String() == "true"
		fill := cmd.Flag("fill").Value.String() == "true"

		numToGenerate := getNumberFromArgs(args, 1)

		workDir, dbType, logFile := sqlmigrator.DefaultContextConfig()
		ctx := sqlmigrator.NewContext(workDir, dbType, logFile, dryRun)
		ctx.LoadMigrationEntries()
		generator := sqlmigrator.NewGenerator(migrationName, numToGenerate, fill)
		displayContextAndConfig(ctx, generator)

		log.Println("generating migrations...")
		generatedFiles, err := generator.Generate(ctx)
		if err != nil {
			log.Println("error generating migrations:", err)
			return
		}
		displayGeneratedFiles(generatedFiles)

		err = ctx.WriteMigrationEntries()
		if err != nil {
			log.Println("error writing migration entries:", err)
			return
		}
		log.Println("generated migrations successfully.")

	},
}
View Source
var HistoryCmd = &cobra.Command{
	Use:   "history",
	Short: "view the migration history of the database.",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {

		workDir, dbType, logFile := sqlmigrator.DefaultContextConfig()
		ctx := sqlmigrator.NewContext(workDir, dbType, logFile, false)

		dbRepo := dbrepo.SelectDBRepo(dbType)
		migrator := sqlmigrator.NewMigrator(dbRepo)

		history, err := migrator.MigrationHistory(ctx)
		if err != nil {
			log.Fatal(err)
			return
		}

		displayMigrationHistory(history)

	},
}

historyCmd represents the mkconfig command

View Source
var PurgeCmd = &cobra.Command{
	Use:   "purge",
	Short: "remove all migration files and the migration table from the database",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		log.Println("purging migrations...")
		rootDirectory := viper.GetString("migrator.workdir")
		err := os.RemoveAll(rootDirectory)
		if err != nil {
			log.Fatal(err)
			return
		}

		dbType := sqlmigrator.SelectDatabase(viper.GetString("migrator.dbtype"))
		dbrepo := dbrepo.SelectDBRepo(dbType)
		err = dbrepo.DeleteMigrationTable()
		if err != nil {
			log.Fatal(err)
			return
		}

		log.Println("purged migrations successfully.")
	},
}

PurgeCmd represents the mkconfig command

View Source
var UpCmd = &cobra.Command{
	Use:   "up",
	Short: "perform forward migration from the files in the migrations folder",
	Args:  cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {

		dryRun := cmd.Flag("dry").Value.String() == "true"
		num := getNumberFromArgs(args, 0)

		workDir, dbType, logFile := sqlmigrator.DefaultContextConfig()
		ctx := sqlmigrator.NewContext(workDir, dbType, logFile, dryRun)
		ctx.LoadMigrationEntries()

		dbRepo := dbrepo.SelectDBRepo(dbType)
		migrator := sqlmigrator.NewMigrator(dbRepo)

		committedMigration, err := migrator.MigrateUp(ctx, num)
		if err != nil {
			log.Fatal(err)
			return
		}

		err = ctx.WriteMigrationEntries()
		if err != nil {
			log.Println("error writing migration entries:", err)
			return
		}

		displayCommitted(committedMigration)
		log.Println("migrated to database successfully.")

	},
}

UpCmd represents the mkconfig command

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