mirror of https://github.com/JustKato/FreePad.git
+ Started work on Sqlite
+ Started work on moving migrations into code instead of Dockerfile
This commit is contained in:
parent
af6a4676e3
commit
c8dde83c05
|
@ -1,3 +1,5 @@
|
|||
DATABASE_DRIVER=sqlite # sqlite,mysql
|
||||
|
||||
MYSQL_ROOT_PASSWORD=example-dev
|
||||
MYSQL_DATABASE=freepad
|
||||
MYSQL_USER=freepad
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
DROP table t_posts;
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE IF NOT EXISTS "t_posts" (
|
||||
"id" INTEGER,
|
||||
"name" TEXT,
|
||||
"content" TEXT,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
|
@ -89,11 +89,14 @@ func Create(name string, content string) (*Post, error) {
|
|||
}
|
||||
|
||||
// Check if we can cache this element
|
||||
if len(postMap) < helper.GetCacheMapLimit() {
|
||||
// Set the post by name
|
||||
postMap[name] = myPost
|
||||
if len(postMap) > helper.GetCacheMapLimit() {
|
||||
// Reset Cache
|
||||
postMap = make(map[string]Post)
|
||||
}
|
||||
|
||||
// Set the post by name
|
||||
postMap[name] = myPost
|
||||
|
||||
// Add the post to the database
|
||||
db, err := database.GetConn()
|
||||
if err != nil {
|
||||
|
|
|
@ -5,12 +5,14 @@ go 1.15
|
|||
require (
|
||||
github.com/gin-gonic/gin v1.7.7
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/golang-migrate/migrate v3.5.4+incompatible
|
||||
github.com/golang-migrate/migrate/v4 v4.15.2
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/go-cmp v0.5.6 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.13 // indirect
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
github.com/ulule/limiter/v3 v3.10.0
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
|
||||
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
|
1700
src/go.sum
1700
src/go.sum
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
@ -9,7 +9,63 @@ import (
|
|||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// Declare the default database driver
|
||||
const defaultDatabaseDriver string = "sqlite"
|
||||
|
||||
// Declare the valid database drivers
|
||||
var validDatabaseDrivers []string = []string{"sqlite", "mysql"}
|
||||
|
||||
// Get the database type to use
|
||||
func getDbType() string {
|
||||
// Grab the environment variable
|
||||
db, test := os.LookupEnv(`DATABASE_DRIVER`)
|
||||
|
||||
// Check if the test has failed
|
||||
if !test {
|
||||
return defaultDatabaseDriver
|
||||
}
|
||||
|
||||
for _, v := range validDatabaseDrivers {
|
||||
// Check if the provided database corresponds to this entry
|
||||
if v == db {
|
||||
// This is a valid database type
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
// No matches
|
||||
return defaultDatabaseDriver
|
||||
}
|
||||
|
||||
func GetConn() (*sql.DB, error) {
|
||||
// Check what kind of database we are looking for
|
||||
dbConnType := getDbType()
|
||||
|
||||
if dbConnType == `mysql` {
|
||||
return GetMysqlConn()
|
||||
} else {
|
||||
return GetLiteConn()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func GetSqliteDatabasePath() string {
|
||||
return "main.db"
|
||||
}
|
||||
|
||||
func GetLiteConn() (*sql.DB, error) {
|
||||
// Declare the database file name
|
||||
dbFile := GetSqliteDatabasePath()
|
||||
|
||||
db, err := sql.Open("sqlite3", dbFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func GetMysqlConn() (*sql.DB, error) {
|
||||
|
||||
user := os.Getenv("MYSQL_USER")
|
||||
password := os.Getenv("MYSQL_PASSWORD")
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
|
||||
_ "github.com/golang-migrate/migrate/v4/database/sqlite"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
)
|
||||
|
||||
// Run migrations to ensure tables exist
|
||||
func MigrationUpdate() *migrate.Logger {
|
||||
// Get the path to the sqlite database
|
||||
databasePath := fmt.Sprintf("sqlite://%s", GetSqliteDatabasePath())
|
||||
|
||||
// Try and create a new migration
|
||||
m, err := migrate.New(
|
||||
"file://../db/migrations_sqlite",
|
||||
databasePath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
// End the whole thing if migrations fail
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Run the update
|
||||
err = m.Up()
|
||||
m.Run()
|
||||
|
||||
m.Force(1)
|
||||
|
||||
return &m.Log
|
||||
|
||||
}
|
|
@ -6,6 +6,7 @@ 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"
|
||||
)
|
||||
|
@ -61,6 +62,10 @@ 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) {
|
||||
|
|
Loading…
Reference in New Issue