sys_initdb.go 1.9 KB
Newer Older
1
package system
2 3

import (
Sliver_Horn's avatar
fixed:  
Sliver_Horn 已提交
4 5
	"database/sql"
	"fmt"
Sliver_Horn's avatar
fixed:  
Sliver_Horn 已提交
6
	adapter "github.com/casbin/gorm-adapter/v3"
7 8 9 10
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
11 12
)

Sliver_Horn's avatar
Sliver_Horn 已提交
13
type InitDBService struct{}
14

Sliver_Horn's avatar
Sliver_Horn 已提交
15 16 17 18
// InitDB 创建数据库并初始化 总入口
// Author [piexlmax](https://github.com/piexlmax)
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
19
func (initDBService *InitDBService) InitDB(conf request.InitDB) error {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
20 21 22 23
	switch conf.DBType {
	case "mysql":
		return initDBService.initMsqlDB(conf)
	case "pgsql":
Sliver_Horn's avatar
Sliver_Horn 已提交
24
		return initDBService.initPgsqlDB(conf)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
25 26 27 28
	default:
		return initDBService.initMsqlDB(conf)
	}
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
29

Sliver_Horn's avatar
Sliver_Horn 已提交
30
// initTables 初始化表
Sliver_Horn's avatar
fixed:  
Sliver_Horn 已提交
31
// Author [SliverHorn](https://github.com/SliverHorn)
Sliver_Horn's avatar
Sliver_Horn 已提交
32 33
func (initDBService *InitDBService) initTables() error {
	return global.GVA_DB.AutoMigrate(
Mr.奇淼('s avatar
Mr.奇淼( 已提交
34
		system.SysApi{},
Sliver_Horn's avatar
Sliver_Horn 已提交
35
		system.SysUser{},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
36
		system.SysBaseMenu{},
Sliver_Horn's avatar
Sliver_Horn 已提交
37
		system.SysAuthority{},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
38 39
		system.JwtBlacklist{},
		system.SysDictionary{},
Sliver_Horn's avatar
Sliver_Horn 已提交
40 41
		system.SysAutoCodeHistory{},
		system.SysOperationRecord{},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
42
		system.SysDictionaryDetail{},
Sliver_Horn's avatar
Sliver_Horn 已提交
43 44
		system.SysBaseMenuParameter{},

Sliver_Horn's avatar
fixed:  
Sliver_Horn 已提交
45 46
		adapter.CasbinRule{},

Mr.奇淼('s avatar
Mr.奇淼( 已提交
47 48
		example.ExaFile{},
		example.ExaCustomer{},
Sliver_Horn's avatar
Sliver_Horn 已提交
49 50
		example.ExaFileChunk{},
		example.ExaFileUploadAndDownload{},
51
	)
S
songzhibin97 已提交
52

53
}
Sliver_Horn's avatar
fixed:  
Sliver_Horn 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

// createDatabase 创建数据库(mysql)
// Author [SliverHorn](https://github.com/SliverHorn)
// Author: [songzhibin97](https://github.com/songzhibin97)

func (initDBService *InitDBService) createDatabase(dsn string, driver string, createSql string) error {
	db, err := sql.Open(driver, dsn)
	if err != nil {
		return err
	}
	defer func(db *sql.DB) {
		err = db.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(db)
	if err = db.Ping(); err != nil {
		return err
	}
	_, err = db.Exec(createSql)
	return err
}