Stable Migrations 1.1.0

+ Finally implemented stable migrations for mysql
This commit is contained in:
Daniel Legt 2022-05-17 23:51:12 +03:00
parent 6268ee2b7f
commit 48cacb601d
8 changed files with 49 additions and 10 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ dev/*
.env
docker-compose.yaml
# Ignore the database file
dist/main.db
dist/main.db
dist/freepad

View File

@ -1,6 +1,6 @@
FROM alpine
LABEL version="1.0.0"
LABEL version="1.1.0"
# Copy the distribution files
COPY ./dist /app

View File

@ -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;

View File

@ -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;

View File

@ -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")
}

View File

@ -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")

View File

@ -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

View File

@ -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) {