Browse Source

修改为sqlite3数据库可运行

Gogs 1 year ago
parent
commit
3c280501cc
8 changed files with 66 additions and 21 deletions
  1. 6 0
      .gitignore
  2. 2 2
      admin/config.go
  3. 28 1
      admin/db.go
  4. 18 18
      admin/init_sql.go
  5. 2 0
      go.mod
  6. 7 0
      go.sum
  7. 3 0
      run.ps1
  8. BIN
      test.db

+ 6 - 0
.gitignore

@@ -0,0 +1,6 @@
+# 排除所有.class文件
+*.class
+
+# 排除目录
+build/
+bak/

+ 2 - 2
admin/config.go

@@ -90,9 +90,9 @@ func newPB() Config {
 	l10nBuilder := l10n.New()
 
 	pageBuilder := example.ConfigPageBuilder(db, "/admin/page_builder", ``, b.I18n())
-	pm := pageBuilder.Configure(b, db, l10nBuilder, ab)
+	pm := pageBuilder.Configure(b, db, l10nBuilder, ab, nil, nil)
 	tm := pageBuilder.ConfigTemplate(b, db)
-	cm := pageBuilder.ConfigCategory(b, db)
+	cm := pageBuilder.ConfigCategory(b, db, nil)
 
 	ab.RegisterModels(pm, tm, cm)
 

+ 28 - 1
admin/db.go

@@ -2,16 +2,43 @@ package admin
 
 import (
 	"os"
+	"strings"
 
 	"gorm.io/driver/postgres"
+	"gorm.io/driver/sqlite"
 	"gorm.io/gorm"
 	"gorm.io/gorm/logger"
 )
 
+var IsSqlite bool // 是否为Sqlite数据库
+
 func ConnectDB() (db *gorm.DB) {
 	var err error
+	// Ease 修改
+	// 自动判断是否为sqlite3数据库: 以.db结尾,则为sqlite3数据库连接; 否则为postgresql数据库
+	dbConnStr := os.Getenv("DB_PARAMS")
+	if strings.HasSuffix(dbConnStr, ".db") {
+		IsSqlite = true
+		db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{
+			DisableForeignKeyConstraintWhenMigrating: true, // 禁止gorm自动建立外键约束
+			SkipDefaultTransaction:                   true, //  跳过默认事务
+		})
+		db.Exec("PRAGMA journal_mode = OFF;")  // 设置数据库的日志模式: WAL/
+		db.Exec("PRAGMA synchronous = OFF;")   // 关闭写同步
+		db.Exec("PRAGMA cache_size = 8000;")   // 缓存大小(数据库文件页数): 1000000为页面缓存分配最大4GB的RAM
+		db.Exec("PRAGMA temp_store = MEMORY;") // 临时存储放到内存中(临时表和索引会存储在内存中)
+		// PRAGMA max_page_count 设置数据库文件最大的页数
+		// PRAGMA page_size 数据库的页面大小
+		sqlDB, err := db.DB()
+		if err != nil {
+			panic(err)
+		}
+		sqlDB.SetMaxIdleConns(10)  // 设置空闲连接池中连接的最大数量
+		sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量
 
-	db, err = gorm.Open(postgres.Open(os.Getenv("DB_PARAMS")))
+	} else {
+		db, err = gorm.Open(postgres.Open(dbConnStr)) // 通过环境变量连接数据库
+	}
 	if err != nil {
 		panic(err)
 	}

File diff suppressed because it is too large
+ 18 - 18
admin/init_sql.go


+ 2 - 0
go.mod

@@ -11,6 +11,7 @@ require (
 	github.com/theplant/htmlgo v1.0.3
 	golang.org/x/text v0.13.0
 	gorm.io/driver/postgres v1.5.2
+	gorm.io/driver/sqlite v1.4.4
 	gorm.io/gorm v1.25.4
 )
 
@@ -40,6 +41,7 @@ require (
 	github.com/jinzhu/now v1.1.5 // indirect
 	github.com/lib/pq v1.10.9 // indirect
 	github.com/markbates/goth v1.77.0 // indirect
+	github.com/mattn/go-sqlite3 v1.14.16 // indirect
 	github.com/ory/ladon v1.2.0 // indirect
 	github.com/ory/pagination v0.0.1 // indirect
 	github.com/pborman/uuid v1.2.1 // indirect

+ 7 - 0
go.sum

@@ -157,6 +157,7 @@ github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHo
 github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da/go.mod h1:ks+b9deReOc7jgqp+e7LuFiCBH6Rm5hL32cLcEAArb4=
 github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
 github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
+github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
 github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
 github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
 github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
@@ -176,6 +177,9 @@ github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
 github.com/markbates/going v1.0.0/go.mod h1:I6mnB4BPnEeqo85ynXIx1ZFLLbtiLHNXVgWeFO9OGOA=
 github.com/markbates/goth v1.77.0 h1:s3scqnWv/Zq/a5M766V0FKsLfOdFNdh/HEkuWCKbvT8=
 github.com/markbates/goth v1.77.0/go.mod h1:X6xdNgpapSENS0O35iTBBcMHoJDQDfI9bJl+APCkYMc=
+github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
+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/mrjones/oauth v0.0.0-20180629183705-f4e24b6d100c/go.mod h1:skjdDftzkFALcuGzYSklqYd8gvat6F1gZJ4YPVbkZpM=
 github.com/ory/ladon v1.2.0 h1:efIVtNkObNR/HL7nR5y17Lrw9c/wMwe56iKVDcRv3GY=
 github.com/ory/ladon v1.2.0/go.mod h1:25bNc/Glx/8xCH7MbItDxjvviAmFQ+aYxb1V1SE5wlg=
@@ -527,6 +531,9 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0=
 gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8=
+gorm.io/driver/sqlite v1.4.4 h1:gIufGoR0dQzjkyqDyYSCvsYR6fba1Gw5YKDqKeChxFc=
+gorm.io/driver/sqlite v1.4.4/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
+gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
 gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
 gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
 honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

+ 3 - 0
run.ps1

@@ -0,0 +1,3 @@
+$env:DB_PARAMS="./test.db"
+$env:LOGIN_SECRET="Ease"
+go run main.go

BIN
test.db


Some files were not shown because too many files changed in this diff