migratorCmds

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: MIT Imports: 10 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) {
		rootDirectory := viper.GetString("migrator.workdir")
		dbChoice := viper.GetString("migrator.database")

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

		dbType := migrator.SelectDatabase(dbChoice)
		log.Println("selected database: ", dbType)

		extention := migrator.SelectExtention(dbType)
		subDirectory := migrator.SelectSubDirectory(dbType)
		fsRepo := fsrepo.NewFSRepo(filepath.Join(rootDirectory, subDirectory), extention)

		dbRepo := selectDbRepo(dbType)

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

		config := &migrator.MigratorConfig{
			DryRun: dryRun,

			FSRepo: fsRepo,
			DBRepo: dbRepo,
		}

		_, err := migrator.Clean(config)
		if err != nil {
			log.Fatal(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) {
		rootDirectory := viper.GetString("migrator.workdir")
		dbChoice := viper.GetString("migrator.database")

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

		numToMigrate := getNumberFromArgs(args, 0)

		dbType := migrator.SelectDatabase(dbChoice)
		log.Println("selected database: ", dbType)

		extention := migrator.SelectExtention(dbType)
		subDirectory := migrator.SelectSubDirectory(dbType)
		fsRepo := fsrepo.NewFSRepo(filepath.Join(rootDirectory, subDirectory), extention)

		dbRepo := selectDbRepo(dbType)

		log.Println("Applying migrations down...")

		config := &migrator.MigratorConfig{
			NumToMigrate: numToMigrate,
			DryRun:       dryRun,

			FSRepo: fsRepo,
			DBRepo: dbRepo,
		}

		_, err := migrator.MigrateDown(config)
		if err != nil {
			log.Fatal(err)
			return
		}

		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) {
		rootDirectory := viper.GetString("migrator.workdir")
		dbChoice := viper.GetString("migrator.database")

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

		numToGenerate := getNumberFromArgs(args, 1)

		dbType := migrator.SelectDatabase(dbChoice)
		log.Println("selected database: ", dbType)

		extention := migrator.SelectExtention(dbType)
		subDirectory := migrator.SelectSubDirectory(dbType)
		fsRepo := fsrepo.NewFSRepo(filepath.Join(rootDirectory, subDirectory), extention)

		log.Println("Generating migration files...")

		config := migrator.GeneratorConfig{
			Name:          migrationName,
			NumToGenerate: numToGenerate,
			DryRun:        dryRun,
			Fill:          fill,
			FSRepo:        fsRepo,
		}

		err := migrator.Generate(config)
		if err != nil {
			log.Fatal(err)
			return
		}

		log.Println("Migration files generated 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) {
		dbChoice := viper.GetString("migrator.database")

		dbType := migrator.SelectDatabase(dbChoice)

		dbRepo := selectDbRepo(dbType)

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

	},
}

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) {
		rootDirectory := viper.GetString("migrator.workdir")
		dbChoice := viper.GetString("migrator.database")

		dbType := migrator.SelectDatabase(dbChoice)
		log.Println("selected database: ", dbType)

		extention := migrator.SelectExtention(dbType)
		subDirectory := migrator.SelectSubDirectory(dbType)
		fsRepo := fsrepo.NewFSRepo(filepath.Join(rootDirectory, subDirectory), extention)

		dbRepo := selectDbRepo(dbType)

		log.Println("Removing migration table...")

		err := dbRepo.DeleteMigrationTable()
		if err != nil {
			log.Fatal(err)
			return
		}

		log.Println("Removed migration table successfully.")

		log.Println("Removing migration files")

		err = fsRepo.DeleteMigrationDirectory()
		if err != nil {
			log.Fatal(err)
			return
		}

		log.Println("Removed migration files 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) {
		rootDirectory := viper.GetString("migrator.workdir")
		dbChoice := viper.GetString("migrator.database")
		log.Println("selected database: ", dbChoice)

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

		numToMigrate := getNumberFromArgs(args, 0)

		dbType := migrator.SelectDatabase(dbChoice)

		extention := migrator.SelectExtention(dbType)
		subDirectory := migrator.SelectSubDirectory(dbType)
		fsRepo := fsrepo.NewFSRepo(filepath.Join(rootDirectory, subDirectory), extention)

		dbRepo := selectDbRepo(dbType)

		log.Println("Applying migrations up...")

		config := &migrator.MigratorConfig{
			NumToMigrate: numToMigrate,
			DryRun:       dryRun,

			FSRepo: fsRepo,
			DBRepo: dbRepo,
		}

		_, err := migrator.MigrateUp(config)
		if err != nil {
			log.Fatal(err)
			return
		}

		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