+ Started work on Sqlite

+ Started work on moving migrations into code instead of Dockerfile
This commit is contained in:
Daniel Legt 2022-05-17 03:20:31 +03:00
parent af6a4676e3
commit c8dde83c05
10 changed files with 1815 additions and 4 deletions

View File

@ -1,3 +1,5 @@
DATABASE_DRIVER=sqlite # sqlite,mysql
MYSQL_ROOT_PASSWORD=example-dev MYSQL_ROOT_PASSWORD=example-dev
MYSQL_DATABASE=freepad MYSQL_DATABASE=freepad
MYSQL_USER=freepad MYSQL_USER=freepad

View File

@ -0,0 +1 @@
DROP table t_posts;

View File

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS "t_posts" (
"id" INTEGER,
"name" TEXT,
"content" TEXT,
PRIMARY KEY("id" AUTOINCREMENT)
);

View File

@ -89,10 +89,13 @@ func Create(name string, content string) (*Post, error) {
} }
// Check if we can cache this element // Check if we can cache this element
if len(postMap) < helper.GetCacheMapLimit() { if len(postMap) > helper.GetCacheMapLimit() {
// Reset Cache
postMap = make(map[string]Post)
}
// Set the post by name // Set the post by name
postMap[name] = myPost postMap[name] = myPost
}
// Add the post to the database // Add the post to the database
db, err := database.GetConn() db, err := database.GetConn()

View File

@ -5,12 +5,14 @@ go 1.15
require ( require (
github.com/gin-gonic/gin v1.7.7 github.com/gin-gonic/gin v1.7.7
github.com/go-sql-driver/mysql v1.6.0 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/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // 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/stretchr/testify v1.7.0 // indirect
github.com/ulule/limiter/v3 v3.10.0 github.com/ulule/limiter/v3 v3.10.0
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect 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 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
) )

1700
src/go.sum

File diff suppressed because it is too large Load Diff

BIN
src/main.db Normal file

Binary file not shown.

View File

@ -9,7 +9,63 @@ import (
_ "github.com/go-sql-driver/mysql" _ "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) { 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") user := os.Getenv("MYSQL_USER")
password := os.Getenv("MYSQL_PASSWORD") password := os.Getenv("MYSQL_PASSWORD")

View File

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

View File

@ -6,6 +6,7 @@ import (
"github.com/JustKato/FreePad/controllers/post" "github.com/JustKato/FreePad/controllers/post"
"github.com/JustKato/FreePad/helper" "github.com/JustKato/FreePad/helper"
"github.com/JustKato/FreePad/models/database"
"github.com/JustKato/FreePad/types" "github.com/JustKato/FreePad/types"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -61,6 +62,10 @@ func ApiRoutes(route *gin.RouterGroup) {
// Add in health checks // Add in health checks
route.GET("/health", healthCheck) route.GET("/health", healthCheck)
route.POST("/test", func(ctx *gin.Context) {
ctx.JSON(200, database.MigrationUpdate())
})
} }
func healthCheck(ctx *gin.Context) { func healthCheck(ctx *gin.Context) {