github.com/akkaraju-satvik/dbmap@v0.0.3-0.20240414054547-f818701a74f0/cmd/create-migration.go (about)

     1  /*
     2  Copyright © 2024 NAME HERE <EMAIL ADDRESS>
     3  */
     4  package cmd
     5  
     6  import (
     7  	"database/sql"
     8  	"fmt"
     9  	"os"
    10  	"time"
    11  
    12  	"github.com/akkaraju-satvik/dbmap/config"
    13  	"github.com/akkaraju-satvik/dbmap/queries"
    14  	"github.com/fatih/color"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var createMigrationErrorStrings = map[string]string{
    19  	"config":         "Error reading config file\n %s",
    20  	"migrationDir":   "Error creating migration directory\n %s",
    21  	"migrationFiles": "Error creating migration files\n %s",
    22  	"database":       "Error connecting to the database\n %s",
    23  	"migration":      "Error creating migration\n %s",
    24  }
    25  
    26  var createMigrationCmd = &cobra.Command{
    27  	Use:   "create-migration",
    28  	Short: "Create a new migration in the migrations directory specified in the config file",
    29  	Long: `Creates a new migration in the migrations directory specified in the config file.
    30  
    31  The migration will have an up.sql and down.sql file with placeholders for the migration queries.`,
    32  	Run: func(cmd *cobra.Command, args []string) {
    33  		configFilePath := cmd.Flag("config-file").Value.String()
    34  		config, err := config.Read(configFilePath)
    35  		if err != nil {
    36  			color.Red(createMigrationErrorStrings["config"], err.Error())
    37  			os.Exit(1)
    38  		}
    39  		err = createMigration(config)
    40  		if err != nil {
    41  			color.Red(err.Error())
    42  			os.Exit(1)
    43  		}
    44  	},
    45  }
    46  
    47  func createMigration(config config.Config) error {
    48  	currentTime := fmt.Sprintf("%d", time.Now().Unix())
    49  	migrationDir := config.MigrationsDir + "/" + currentTime
    50  	if err := os.MkdirAll(migrationDir, 0755); err != nil {
    51  		return fmt.Errorf(createMigrationErrorStrings["migrationDir"], err.Error())
    52  	}
    53  	err := createMigrationFiles(migrationDir)
    54  	if err != nil {
    55  		return fmt.Errorf(createMigrationErrorStrings["migrationFiles"], err.Error())
    56  	}
    57  	db, err := sql.Open("postgres", config.DbURL)
    58  	if err != nil {
    59  		removeMigrationSetup(migrationDir)
    60  		return fmt.Errorf(createMigrationErrorStrings["database"], err.Error())
    61  	}
    62  	_, err = db.Exec(queries.CreateMigrationQuery, currentTime)
    63  	if err != nil {
    64  		removeMigrationSetup(migrationDir)
    65  		return fmt.Errorf(createMigrationErrorStrings["migration"], err.Error())
    66  	}
    67  	db.Close()
    68  	color.Green("Migration created successfully")
    69  	return nil
    70  }
    71  
    72  func init() {
    73  	createMigrationCmd.Flags().StringP("config-file", "c", "dbmap.config.yaml", "config file to use")
    74  	rootCmd.AddCommand(createMigrationCmd)
    75  }
    76  
    77  func createMigrationFiles(migrationDir string) error {
    78  	upFile, err := os.Create(migrationDir + "/up.sql")
    79  	if err != nil {
    80  		removeMigrationSetup(migrationDir)
    81  		return fmt.Errorf("error creating migration file\n %s", err.Error())
    82  	}
    83  	fmt.Fprintf(upFile, "-- Write your UP migration here\n\n")
    84  	upFile.Close()
    85  	downFile, err := os.Create(migrationDir + "/down.sql")
    86  	if err != nil {
    87  		removeMigrationSetup(migrationDir)
    88  		return fmt.Errorf("error creating migration file\n %s", err.Error())
    89  	}
    90  	fmt.Fprintf(downFile, "-- Write your DOWN migration here\n\n")
    91  	downFile.Close()
    92  	return nil
    93  }
    94  
    95  func removeMigrationSetup(migrationDir string) {
    96  	os.RemoveAll(migrationDir)
    97  }