From 48cacb601de4737e45c1b9fee25e73c865fa6ab9 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Tue, 17 May 2022 23:51:12 +0300 Subject: [PATCH] Stable Migrations 1.1.0 + Finally implemented stable migrations for mysql --- .gitignore | 3 ++- Dockerfile | 2 +- build.sh | 2 +- rundev.sh | 6 +++++- src/main.go | 11 ++++++++++- src/models/database/database.go | 10 ++++++++++ src/models/database/database_migrations.go | 21 ++++++++++++++++++++- src/routes/routes_api.go | 4 ---- 8 files changed, 49 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 979f3a1..b33ca54 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ dev/* .env docker-compose.yaml # Ignore the database file -dist/main.db \ No newline at end of file +dist/main.db +dist/freepad diff --git a/Dockerfile b/Dockerfile index 786cfa1..125fcf4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine -LABEL version="1.0.0" +LABEL version="1.1.0" # Copy the distribution files COPY ./dist /app diff --git a/build.sh b/build.sh index 27fd13e..9dbfffd 100755 --- a/build.sh +++ b/build.sh @@ -10,7 +10,7 @@ MYDIR=`pwd`; cd src; # Build echo "Building..." -export GIN_MODE=release; +export RELEASE_MODE=1; CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o ../dist/freepad . # Go back! cd $MYDIR; \ No newline at end of file diff --git a/rundev.sh b/rundev.sh index 2731176..92c2ff7 100755 --- a/rundev.sh +++ b/rundev.sh @@ -3,13 +3,16 @@ echo "Removing old"; rm dist/freepad; +# Yeah, this is my solution +export DOMAIN_BASE CACHE_MAP_LIMIT API_BAN_LIMIT DATABASE_DRIVER MYSQL_ROOT_PASSWORD MYSQL_DATABASE MYSQL_USER MYSQL_PASSWORD MYSQL_URL MYSQL_PORT + # Remember current path MYDIR=`pwd`; # Go into src cd src; # Build echo "Building..." -export GIN_MODE=debug; +unset RELEASE_MODE; go build -o ../dist/freepad . # Go back! cd $MYDIR; @@ -18,4 +21,5 @@ MYPATH=`pwd`; cd dist +source ../.env ./freepad && cd $MYPATH; \ No newline at end of file diff --git a/src/main.go b/src/main.go index 760b4c4..212f771 100644 --- a/src/main.go +++ b/src/main.go @@ -1,13 +1,19 @@ package main import ( + "os" + + "github.com/JustKato/FreePad/models/database" "github.com/JustKato/FreePad/routes" "github.com/gin-gonic/gin" ) func main() { - gin.SetMode(gin.ReleaseMode) + _, isRelease := os.LookupEnv("RELEASE_MODE") + if isRelease { + gin.SetMode(gin.ReleaseMode) + } // Initialize the router router := gin.Default() @@ -25,6 +31,9 @@ func main() { // TODO: Sockets: https://gist.github.com/supanadit/f6de65fc5896e8bb0c4656e451387d0f + // Try and run migrations + database.MigrateMysql() + router.Run(":8080") } diff --git a/src/models/database/database.go b/src/models/database/database.go index 48b1cf0..51e56d4 100644 --- a/src/models/database/database.go +++ b/src/models/database/database.go @@ -70,6 +70,16 @@ func GetLiteConn() (*sql.DB, error) { return db, nil } +func GetMysqlString() string { + + user := os.Getenv("MYSQL_USER") + password := os.Getenv("MYSQL_PASSWORD") + dburl := os.Getenv("MYSQL_URL") + dbname := os.Getenv("MYSQL_DATABASE") + + return fmt.Sprintf("mysql://%s:%s@tcp(%s:3306)/%s", user, password, dburl, dbname) +} + func GetMysqlConn() (*sql.DB, error) { user := os.Getenv("MYSQL_USER") diff --git a/src/models/database/database_migrations.go b/src/models/database/database_migrations.go index fb4ada1..8ae607d 100644 --- a/src/models/database/database_migrations.go +++ b/src/models/database/database_migrations.go @@ -5,10 +5,29 @@ import ( "github.com/golang-migrate/migrate/v4" - _ "github.com/golang-migrate/migrate/v4/database/sqlite" + _ "github.com/golang-migrate/migrate/v4/database/mysql" _ "github.com/golang-migrate/migrate/v4/source/file" ) +func MigrateMysql() error { + + m, err := migrate.New( + "file://db/migrations/", + GetMysqlString(), + ) + if err != nil { + return err + } + + // Migrate + err = m.Up() + if err != nil { + return err + } + + return m.Run() +} + // Run migrations to ensure tables exist func MigrationUpdate() *migrate.Logger { // Get the path to the sqlite database diff --git a/src/routes/routes_api.go b/src/routes/routes_api.go index 7a9c691..a5624c8 100644 --- a/src/routes/routes_api.go +++ b/src/routes/routes_api.go @@ -6,7 +6,6 @@ import ( "github.com/JustKato/FreePad/controllers/post" "github.com/JustKato/FreePad/helper" - "github.com/JustKato/FreePad/models/database" "github.com/JustKato/FreePad/types" "github.com/gin-gonic/gin" ) @@ -67,9 +66,6 @@ func ApiRoutes(route *gin.RouterGroup) { // Add in health checks route.GET("/health", healthCheck) - route.POST("/test", func(ctx *gin.Context) { - ctx.JSON(200, database.MigrationUpdate()) - }) } func healthCheck(ctx *gin.Context) {