Skip to content

Commit

Permalink
breaking change: config -> contexts.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed Nov 29, 2024
1 parent 3d0c077 commit 138e9e8
Show file tree
Hide file tree
Showing 41 changed files with 1,368 additions and 1,061 deletions.
35 changes: 0 additions & 35 deletions cmd/context.go

This file was deleted.

166 changes: 92 additions & 74 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,105 @@ package cmd

import (
"fmt"
"path"
"time"

"github.com/alexisvisco/amigo/pkg/amigo"
"github.com/alexisvisco/amigo/pkg/amigoconfig"
"github.com/alexisvisco/amigo/pkg/templates"
"github.com/alexisvisco/amigo/pkg/types"
"github.com/alexisvisco/amigo/pkg/utils"
"github.com/alexisvisco/amigo/pkg/utils/events"
"github.com/alexisvisco/amigo/pkg/utils/logger"
"github.com/spf13/cobra"
"path"
"time"
"gopkg.in/yaml.v3"
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize migrations folder and add the first migration file",
Run: wrapCobraFunc(func(cmd *cobra.Command, am amigo.Amigo, args []string) error {
if err := cmdCtx.ValidateDSN(); err != nil {
return err
}

// create the main file
logger.Info(events.FolderAddedEvent{FolderName: cmdCtx.MigrationFolder})

file, err := utils.CreateOrOpenFile(path.Join(cmdCtx.AmigoFolderPath, "main.go"))
if err != nil {
return fmt.Errorf("unable to open main.go file: %w", err)
}

err = am.GenerateMainFile(file)
if err != nil {
return err
}

logger.Info(events.FileAddedEvent{FileName: path.Join(cmdCtx.AmigoFolderPath, "main.go")})

// create the base schema version table
now := time.Now()
migrationFileName := fmt.Sprintf("%s_create_table_schema_version.go", now.UTC().Format(utils.FormatTime))
file, err = utils.CreateOrOpenFile(path.Join(cmdCtx.MigrationFolder, migrationFileName))
if err != nil {
return fmt.Errorf("unable to open migrations.go file: %w", err)
}

inUp, err := templates.GetInitCreateTableTemplate(templates.CreateTableData{Name: cmdCtx.SchemaVersionTable},
am.Driver == types.DriverUnknown)
if err != nil {
return err
}

err = am.GenerateMigrationFile(&amigo.GenerateMigrationFileParams{
Name: "create_table_schema_version",
Up: inUp,
Down: "// nothing to do to keep the schema version table",
Type: types.MigrationFileTypeClassic,
Now: now,
Writer: file,
UseSchemaImport: am.Driver != types.DriverUnknown,
UseFmtImport: am.Driver == types.DriverUnknown,
})
if err != nil {
return err
}
logger.Info(events.FileAddedEvent{FileName: path.Join(cmdCtx.MigrationFolder, migrationFileName)})

// create the migrations file where all the migrations will be stored
file, err = utils.CreateOrOpenFile(path.Join(cmdCtx.MigrationFolder, migrationsFile))
if err != nil {
return err
}

err = am.GenerateMigrationsFiles(file)
if err != nil {
return err
}

logger.Info(events.FileAddedEvent{FileName: path.Join(cmdCtx.MigrationFolder, migrationsFile)})

return nil
}),
}
func executeInit(
mainFilePath,
amigoFolder,
table,
migrationsFolder string,
) error {
// create the main file
logger.Info(events.FolderAddedEvent{FolderName: amigoFolder})

file, err := utils.CreateOrOpenFile(mainFilePath)
if err != nil {
return fmt.Errorf("unable to open main.go file: %w", err)
}

cfg := amigoconfig.NewConfig().
WithAmigoFolder(amigoFolder).
WithMigrationFolder(migrationsFolder).
WithSchemaVersionTable(table)

am := amigo.NewAmigo(cfg)

err = am.GenerateMainFile(file)
if err != nil {
return err
}

logger.Info(events.FileAddedEvent{FileName: mainFilePath})

// create the base schema version table
now := time.Now()
migrationFileName := fmt.Sprintf("%s_create_table_schema_version.go", now.UTC().Format(utils.FormatTime))
file, err = utils.CreateOrOpenFile(path.Join(cfg.MigrationFolder, migrationFileName))
if err != nil {
return fmt.Errorf("unable to open migrationsFolder.go file: %w", err)
}

inUp, err := templates.GetInitCreateTableTemplate(templates.CreateTableData{Name: table},
am.Driver == types.DriverUnknown)
if err != nil {
return err
}

err = am.GenerateMigrationFile(&amigo.GenerateMigrationFileParams{
Name: "create_table_schema_version",
Up: inUp,
Down: "// nothing to do to keep the schema version table",
Type: types.MigrationFileTypeClassic,
Now: now,
Writer: file,
UseSchemaImport: am.Driver != types.DriverUnknown,
UseFmtImport: am.Driver == types.DriverUnknown,
})
if err != nil {
return err
}
logger.Info(events.FileAddedEvent{FileName: path.Join(migrationsFolder, migrationFileName)})

// create the migrationsFolder file where all the migrationsFolder will be stored
file, err = utils.CreateOrOpenFile(path.Join(amigoFolder, "migrations.go"))
if err != nil {
return err
}

err = am.GenerateMigrationsFiles(file)
if err != nil {
return err
}

logger.Info(events.FileAddedEvent{FileName: path.Join(amigoFolder, migrationFileName)})

// write the context file
out, err := yaml.Marshal(amigoconfig.DefaultYamlConfig)
if err != nil {
return err
}

openFile, err := utils.CreateOrOpenFile(path.Join(amigoFolder, "contexts.yaml"))
if err != nil {
return fmt.Errorf("unable to open contexts.yaml file: %w", err)
}
defer openFile.Close()

_, err = openFile.WriteString(string(out))
if err != nil {
return fmt.Errorf("unable to write contexts.yaml file: %w", err)
}

func init() {
rootCmd.AddCommand(initCmd)
return nil
}
61 changes: 0 additions & 61 deletions cmd/migration.go

This file was deleted.

Loading

0 comments on commit 138e9e8

Please sign in to comment.