Skip to content

Commit

Permalink
Replace .env with .env.migrator to avoid conflict with standard .env …
Browse files Browse the repository at this point in the history
…in other projects and add full help
  • Loading branch information
olbrichattila committed Jul 14, 2024
1 parent 6c25262 commit cdf4176
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# (use the comment below if you use Visual Studio Code)
.vscode/
.env
.env.migrator
/migrations/
/data/
/build/
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Create migration SQL files

## Install.

If can install as a CLI tool:
```
go install github.com/olbrichattila/godbmigrator_cmd/cmd/migrator@latest
```


If you would like to build the migration into your application, please see:
https://github.com/olbrichattila/godbmigrator/

Expand Down Expand Up @@ -100,10 +108,10 @@ migrator report
The number of rollbacks and migrates are not mandatory.
If it is set, for rollbacks it only apply for the last rollback batch

## .env settings
## .env.migrator settings

If the .env does not exists, the applicaion will read the operating system environment variables.
If the .env file exists and the operating system variables are also set, the operating system variables are taking priority
If the .env.migrator does not exists, the applicaion will read the operating system environment variables.
If the .env.migrator file exists and the operating system variables are also set, the operating system variables are taking priority

Example setting variables in linux, command line:
```
Expand Down
85 changes: 85 additions & 0 deletions internal/display_help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package migrator

import "fmt"

func displayFullHelp() {
fmt.Printf(`
If the .env.migrator does not exists, the application will read the operating system environment variables.
If the .env.migrator file exists and the operating system variables are also set, the operating system variables are taking priority
Example setting variables in linux, command line:
------------------------------------------
export DB_CONNECTION=sqlite
export DB_DATABASE=./data/database.sqlite
------------------------------------------
Unset the variables can be done:
--------------------
unset DB_CONNECTION
unset DB_DATABASE
------------------
Create a .env file into your root directory
Examples:
sqlite
-------------------------------------
DB_CONNECTION=sqlite
DB_DATABASE=./data/database.sqlite
-------------------------------------
MySql
-------------------------------------
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=migrator
DB_USERNAME=root
DB_PASSWORD=password
-------------------------------------
Postgres
-------------------------------------
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=postgres
DB_USERNAME=postgres
DB_PASSWORD=postgres
# non mandatory, it defaults to disable
# possible values are: disable, require, verify-ca, verify-full, prefer, allow (depending on your setup)
DB_SSLMODE=disable
-------------------------------------
Firebird / Interbase
-------------------------------------
DB_CONNECTION=firebird
DB_HOST=127.0.0.1
DB_PORT=3050
DB_DATABASE=/opt/firebird/examples/empbuild/employee.fdb
DB_USERNAME=SYSDBA
DB_PASSWORD=masterkey
MIGRATOR_MIGRATION_PATH=./migrations/firebird
MIGRATOR_MIGRATION_PROVIDER=db
-------------------------------------
Setting migration path
The path by default is ./migrations
This can be overwritten by adding the following variable to your .env file
-------------------------------------
MIGRATOR_MIGRATION_PATH=./migrations/custom_path
-------------------------------------
Setting the migration provider in .env
It is possible to set the migration provider (see above, saves to database or json)
Possible values are:
-------------------------------------
MIGRATOR_MIGRATION_PROVIDER=json
MIGRATOR_MIGRATION_PROVIDER=db
-------------------------------------
`)
}
14 changes: 10 additions & 4 deletions internal/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

const (
envFileName = ".env.migrator"
defaultMigrationPath = "./migrations"
messageRollingBack = "Rolling back"
)
Expand All @@ -22,10 +23,10 @@ type migratorInterface interface {
AddNewMigrationFiles(string, string) error
}

// Init starts migration command, reads .env and command line arguments, and execute what was requested
// Init starts migration command, reads .env.migrator and command line arguments, and execute what was requested
func Init() {
if err := loadEnv(); err != nil {
fmt.Println("Error loading .env file:", err)
fmt.Printf("Error loading %s file:%s\n", err.Error(), envFileName)
return
}

Expand All @@ -49,6 +50,8 @@ func routeCommandLineParameters(args []string, migrationAdapter migratorInterfac
report(args, migrationAdapter, migrationInit)
case "add":
add(args, migrationAdapter)
case "help":
displayFullHelp()
default:
return fmt.Errorf("cannot find command")
}
Expand Down Expand Up @@ -145,6 +148,9 @@ Usage:
migrator report
migrator add <optional suffix>
For help how to set up:
migrator help
The number of rollbacks and migrates are not mandatory.
If it is set, for rollbacks it only apply for the last rollback batch
Expand Down Expand Up @@ -179,8 +185,8 @@ func fileExists(filename string) bool {
}

func loadEnv() error {
if fileExists("./.env") {
if err := godotenv.Load(); err != nil {
if fileExists(envFileName) {
if err := godotenv.Load(envFileName); err != nil {
return err
}
}
Expand Down
11 changes: 6 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ run-test:
install:
go build -o ./build/migrator .
switch-sqlite:
cp .env.sqlite.example .env
cp .env.sqlite.example .env.migrator
switch-mysql:
cp .env.mysql.example .env
cp .env.mysql.example .env.migrator
switch-pgsql:
cp .env.pgsql.example .env
cp .env.pgsql.example .env.migrator
switch-firebird:
cp .env.firebird.example .env
cp .env.firebird.example .env.migrator
lint:
gocritic check ./...
revive ./...
golint ./...
goconst ./...
golangci-lint run
go vet ./...
staticcheck ./...
staticcheck ./...

0 comments on commit cdf4176

Please sign in to comment.