textutils: move colors to textutils
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
||||
)
|
||||
|
||||
type DBConfig struct {
|
||||
@@ -22,20 +24,6 @@ type DBConfig struct {
|
||||
version_number uint
|
||||
}
|
||||
|
||||
// Colors for terminal output
|
||||
const (
|
||||
ColorReset = "\033[0m"
|
||||
ColorBlack = "\033[30m"
|
||||
ColorRed = "\033[31m"
|
||||
ColorGreen = "\033[32m"
|
||||
ColorYellow = "\033[33m"
|
||||
ColorBlue = "\033[34m"
|
||||
ColorPurple = "\033[35m"
|
||||
ColorCyan = "\033[36m"
|
||||
ColorGray = "\033[37m"
|
||||
ColorWhite = "\033[97m"
|
||||
)
|
||||
|
||||
func Init(schema *string, migrationsList *[]string) DBConfig {
|
||||
return DBConfig{
|
||||
sql_schema: schema,
|
||||
@@ -96,11 +84,11 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
|
||||
}
|
||||
|
||||
if c.version_number > version {
|
||||
fmt.Print(ColorYellow)
|
||||
fmt.Print(textutils.ColorYellow)
|
||||
fmt.Printf("================================================\n")
|
||||
fmt.Printf("Database version is out of date. Upgrading database from version %d to version %d!\n", version,
|
||||
c.version_number)
|
||||
fmt.Print(ColorReset)
|
||||
fmt.Print(textutils.ColorReset)
|
||||
c.UpgradeFromXToY(db, version, c.version_number)
|
||||
}
|
||||
|
||||
@@ -110,9 +98,9 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
|
||||
// UpgradeFromXToY runs all the migrations from version X to version Y, and update the `database_version` table's `version_number`
|
||||
func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
|
||||
for i := x; i < y; i++ {
|
||||
fmt.Print(ColorCyan)
|
||||
fmt.Print(textutils.ColorCyan)
|
||||
fmt.Println((*c.migrations)[i])
|
||||
fmt.Print(ColorReset)
|
||||
fmt.Print(textutils.ColorReset)
|
||||
|
||||
// Execute the migration in a transaction
|
||||
tx, err := db.Beginx()
|
||||
@@ -125,14 +113,14 @@ func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Print(ColorYellow)
|
||||
fmt.Print(textutils.ColorYellow)
|
||||
fmt.Printf("Now at database schema version %d.\n", i+1)
|
||||
fmt.Print(ColorReset)
|
||||
fmt.Print(textutils.ColorReset)
|
||||
}
|
||||
fmt.Print(ColorGreen)
|
||||
fmt.Print(textutils.ColorGreen)
|
||||
fmt.Printf("================================================\n")
|
||||
fmt.Printf("Database version has been upgraded to version %d.\n", y)
|
||||
fmt.Print(ColorReset)
|
||||
fmt.Print(textutils.ColorReset)
|
||||
}
|
||||
|
||||
type VersionMismatchError struct {
|
||||
|
||||
Reference in New Issue
Block a user