diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2023-12-30 16:00:20 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2023-12-30 16:00:20 -0500 |
| commit | 809fc4639e58ca9ca082ce68b5c62b463b2f1e1d (patch) | |
| tree | 1cb7b9951a80509b827b8e5bd1eb2dd09e87aec1 /internal | |
| parent | Initial Commit (diff) | |
| download | cli-809fc4639e58ca9ca082ce68b5c62b463b2f1e1d.zip | |
Add initial code
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/commands/create.go | 45 | ||||
| -rw-r--r-- | internal/commands/migrate.go | 45 | ||||
| -rw-r--r-- | internal/config/config.go | 82 | ||||
| -rw-r--r-- | internal/db/db.go | 48 | ||||
| -rw-r--r-- | internal/models/migration.go | 3 | ||||
| -rw-r--r-- | internal/repositories/migration.go | 45 |
6 files changed, 268 insertions, 0 deletions
diff --git a/internal/commands/create.go b/internal/commands/create.go new file mode 100644 index 0000000..ab37eca --- /dev/null +++ b/internal/commands/create.go @@ -0,0 +1,45 @@ +package commands + +import ( + "context" + "flag" + "fmt" + "os" + + "github.com/eugenetriguba/bolt/internal/db" + "github.com/google/subcommands" +) + +type CreateCmd struct{} + +func (*CreateCmd) Name() string { return "create" } +func (*CreateCmd) Synopsis() string { return "Create a new database migration." } +func (*CreateCmd) Usage() string { + return `create: + Create a new database migration. + ` +} + +func (m *CreateCmd) SetFlags(f *flag.FlagSet) {} + +func (m *CreateCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + db, err := db.Connect() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + defer db.Close() + + sqlStatement := ` + CREATE TABLE IF NOT EXISTS applied_migration( + version CHARACTER(32) PRIMARY KEY NOT NULL + ); + ` + _, err = db.Exec(sqlStatement) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + + return subcommands.ExitSuccess +} diff --git a/internal/commands/migrate.go b/internal/commands/migrate.go new file mode 100644 index 0000000..fdbddfe --- /dev/null +++ b/internal/commands/migrate.go @@ -0,0 +1,45 @@ +package commands + +import ( + "context" + "flag" + "fmt" + "os" + + "github.com/eugenetriguba/bolt/internal/db" + "github.com/google/subcommands" +) + +type MigrateCmd struct{} + +func (*MigrateCmd) Name() string { return "migrate" } +func (*MigrateCmd) Synopsis() string { return "Migrate the database to the latest migration." } +func (*MigrateCmd) Usage() string { + return `migrate: + Migrate the database to the latest migration. + ` +} + +func (m *MigrateCmd) SetFlags(f *flag.FlagSet) {} + +func (m *MigrateCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + db, err := db.Connect() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + defer db.Close() + + sqlStatement := ` + CREATE TABLE IF NOT EXISTS applied_migration( + version CHARACTER(32) PRIMARY KEY NOT NULL + ); + ` + _, err = db.Exec(sqlStatement) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return subcommands.ExitFailure + } + + return subcommands.ExitSuccess +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..89acd23 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,82 @@ +package config + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/BurntSushi/toml" +) + +const configFileName = "bolt.toml" + +var config *Config + +type Config struct { + // Relative file path from the bolt configuration file. + // This is optional and will default to "migrations" if + // not specified. + MigrationsDir string `toml:"migrations_dir"` + + // Information related to how to connect to the database + // that is desired to run migrations against. + Connection connectionConfig +} + +type connectionConfig struct { + Host string + Port int + User string + Password string + DBName string + Driver string +} + +func NewConfig() (*Config, error) { + if config == nil { + filePath, err := findConfigFilePath() + if err != nil { + return nil, err + } + + config = &Config{} + _, err = toml.DecodeFile(filePath, &config) + if err != nil { + return nil, err + } + } + + return config, nil +} + +func findConfigFilePath() (filePath string, err error) { + dir, err := os.Getwd() + if err != nil { + return "", err + } + + for { + entries, err := os.ReadDir(dir) + if err != nil { + return "", err + } + + for _, e := range entries { + if e.Name() == configFileName { + return filepath.Join(dir, e.Name()), nil + } + } + + // TODO: This won't support Windows. + if dir == "/" { + errMsg := fmt.Sprintf( + "%s not found in current directory or any parent directories.", + configFileName, + ) + return "", errors.New(errMsg) + } + + dir = filepath.Dir(dir) + } +} diff --git a/internal/db/db.go b/internal/db/db.go new file mode 100644 index 0000000..df5934c --- /dev/null +++ b/internal/db/db.go @@ -0,0 +1,48 @@ +package db + +import ( + "database/sql" + "fmt" + + "github.com/eugenetriguba/bolt/internal/config" + _ "github.com/lib/pq" +) + +var db *sql.DB + +// Connect to the database using the driver and connection +// parameter that are specified in the bolt configuration file. +// +// Note that subsequent calls to Connect return the same database +// handle. +func Connect() (*sql.DB, error) { + if db == nil { + config, err := config.NewConfig() + if err != nil { + return nil, err + } + + connInfo := fmt.Sprintf( + "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", + config.Connection.Host, config.Connection.Port, config.Connection.User, + config.Connection.Password, config.Connection.DBName, + ) + newDb, err := sql.Open(config.Connection.Driver, connInfo) + if err != nil { + return nil, err + } + + // Note: `sql.Open` only validates the connection string we provided + // it is sane. It doesn't actually open up a connection to the database. + // For that, we ping the database to ensure the connection string is fully + // valid. + err = newDb.Ping() + if err != nil { + return nil, err + } + + db = newDb + } + + return db, nil +} diff --git a/internal/models/migration.go b/internal/models/migration.go new file mode 100644 index 0000000..a7e27aa --- /dev/null +++ b/internal/models/migration.go @@ -0,0 +1,3 @@ +package models + +type Migration struct{} diff --git a/internal/repositories/migration.go b/internal/repositories/migration.go new file mode 100644 index 0000000..488d838 --- /dev/null +++ b/internal/repositories/migration.go @@ -0,0 +1,45 @@ +package repositories + +import ( + "database/sql" +) + +type MigrationRepo struct { + // The database connection that will be used + // to look up migrations that have already been + // applied. + db *sql.DB +} + +// Create a new Migration Repo. +// +// This handles the interactions with applying +// and reverting migrations. +func NewMigrationRepo(db *sql.DB) (*MigrationRepo, error) { + _, err := db.Exec(` + CREATE TABLE IF NOT EXISTS bolt_migrations( + version CHARACTER(32) PRIMARY KEY NOT NULL + ); + `) + if err != nil { + return nil, err + } + + return &MigrationRepo{db: db}, nil +} + +func (m *MigrationRepo) Apply() error { + tx, err := m.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + sqlStatement := `INSERT INTO applied_migration(version) VALUES ($1);` + _, err = m.db.Exec(sqlStatement) + if err != nil { + return err + } + + return nil +} |