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"
S
songzhibin97 已提交
6

Sliver_Horn's avatar
fixed:  
Sliver_Horn 已提交
7
	adapter "github.com/casbin/gorm-adapter/v3"
8 9 10 11
	"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"
12 13
)

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

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

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

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

Mr.奇淼('s avatar
Mr.奇淼( 已提交
48 49
		example.ExaFile{},
		example.ExaCustomer{},
Sliver_Horn's avatar
Sliver_Horn 已提交
50 51
		example.ExaFileChunk{},
		example.ExaFileUploadAndDownload{},
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
}