mirror of
https://github.com/JustKato/FreePad.git
synced 2026-02-24 00:00:46 +02:00
Stable Migrations 1.1.0
+ Finally implemented stable migrations for mysql
This commit is contained in:
11
src/main.go
11
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")
|
||||
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user