Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support sqlite3 #375

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/klauspost/compress v1.5.0 // indirect
github.com/klauspost/cpuid v1.2.1 // indirect
github.com/lib/pq v1.1.1
github.com/mattn/go-sqlite3 v1.14.16
github.com/ouqiang/goutil v1.1.1
github.com/rakyll/statik v0.1.6
github.com/sirupsen/logrus v1.4.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/ouqiang/goutil v1.1.1 h1:r5EKn1jw6vBnj0sLrk3XWeQGxG5ftjGQ3ftuiiO2DYE=
github.com/ouqiang/goutil v1.1.1/go.mod h1:QrB1Ky4uGqcixxOx55MXweI3IA6nDZ0NtLMXbMfkur4=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand Down
2 changes: 2 additions & 0 deletions internal/models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func getDbEngineDSN(setting *setting.Setting) string {
setting.Db.Host,
setting.Db.Port,
setting.Db.Database)
case "sqlite3":
dsn = setting.Db.Database
}

return dsn
Expand Down
84 changes: 84 additions & 0 deletions internal/models/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package models

import (
"os"
"testing"

_ "github.com/mattn/go-sqlite3"
"github.com/ouqiang/gocron/internal/modules/app"
"github.com/ouqiang/gocron/internal/modules/logger"
"github.com/ouqiang/gocron/internal/modules/setting"
"github.com/stretchr/testify/assert"
)

func TestDSN(t *testing.T) {
cfg := &setting.Setting{
Db: struct {
Engine string
Host string
Port int
User string
Password string
Database string
Prefix string
Charset string
MaxIdleConns int
MaxOpenConns int
}{Engine: "sqlite3", Database: "./test.db"},
AllowIps: "",
AppName: "",
ApiKey: "",
ApiSecret: "",
ApiSignEnable: false,
EnableTLS: false,
CAFile: "",
CertFile: "",
KeyFile: "",
ConcurrencyQueue: 0,
AuthSecret: "",
}
dsn := getDbEngineDSN(cfg)
t.Log("dsn: ", dsn)
assert.NotEmpty(t, dsn)
}

func TestCreateDb(t *testing.T) {
logger.InitLogger()
cfg := &setting.Setting{
Db: struct {
Engine string
Host string
Port int
User string
Password string
Database string
Prefix string
Charset string
MaxIdleConns int
MaxOpenConns int
}{Engine: "sqlite3", Database: "./test.db"},
AllowIps: "",
AppName: "",
ApiKey: "",
ApiSecret: "",
ApiSignEnable: false,
EnableTLS: false,
CAFile: "",
CertFile: "",
KeyFile: "",
ConcurrencyQueue: 0,
AuthSecret: "",
}
app.Setting = cfg

os.Remove(cfg.Db.Database)
defer os.Remove(cfg.Db.Database)

// 创建数据库并初始化
Db = CreateDb()
assert.NotNil(t, Db)

migrate := new(Migration)
err := migrate.Install("hello")
assert.Nil(t, err)
}
3 changes: 2 additions & 1 deletion internal/routers/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"

_ "github.com/mattn/go-sqlite3"
macaron "gopkg.in/macaron.v1"

"github.com/go-macaron/binding"
Expand All @@ -20,7 +21,7 @@ import (
// 系统安装

type InstallForm struct {
DbType string `binding:"In(mysql,postgres)"`
DbType string `binding:"In(mysql,postgres,sqlite3)"`
DbHost string `binding:"Required;MaxSize(50)"`
DbPort int `binding:"Required;Range(1,65535)"`
DbUsername string `binding:"Required;MaxSize(50)"`
Expand Down
8 changes: 7 additions & 1 deletion web/vue/src/pages/install/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
:value="item.value">
</el-option>
</el-select>
<label title="使用sqlite3时需要在数据库名称中配置文件路径,主机名/端口/用户名/密码随意">❓</label>
</el-form-item>
<el-row>
<el-col :span="12">
Expand Down Expand Up @@ -143,11 +144,16 @@ export default {
{
value: 'postgres',
label: 'PostgreSql'
},
{
value: 'sqlite3',
label: 'sqlite3'
}
],
default_ports: {
'mysql': 3306,
'postgres': 5432
'postgres': 5432,
'sqlite3': 1
}
}
},
Expand Down