From ea1bd27aa88c73da30de7fda6d735d8632c691ed Mon Sep 17 00:00:00 2001 From: zicla Date: Tue, 4 Dec 2018 21:34:33 +0800 Subject: [PATCH] Add something for install. --- .../{dashboard.sql => tank20_dashboard.sql} | 0 ...ad_token.sql => tank20_download_token.sql} | 0 .../{footprint.sql => tank20_footprint.sql} | 0 ...image_cache.sql => tank20_image_cache.sql} | 0 build/db/{matter.sql => tank20_matter.sql} | 0 .../{preference.sql => tank20_preference.sql} | 0 build/db/{session.sql => tank20_session.sql} | 0 ...load_token.sql => tank20_upload_token.sql} | 0 build/db/{user.sql => tank20_user.sql} | 0 rest/base_model.go | 13 +- rest/install_controller.go | 121 ++++++++++++++++-- rest/install_model.go | 11 ++ 12 files changed, 133 insertions(+), 12 deletions(-) rename build/db/{dashboard.sql => tank20_dashboard.sql} (100%) rename build/db/{download_token.sql => tank20_download_token.sql} (100%) rename build/db/{footprint.sql => tank20_footprint.sql} (100%) rename build/db/{image_cache.sql => tank20_image_cache.sql} (100%) rename build/db/{matter.sql => tank20_matter.sql} (100%) rename build/db/{preference.sql => tank20_preference.sql} (100%) rename build/db/{session.sql => tank20_session.sql} (100%) rename build/db/{upload_token.sql => tank20_upload_token.sql} (100%) rename build/db/{user.sql => tank20_user.sql} (100%) create mode 100644 rest/install_model.go diff --git a/build/db/dashboard.sql b/build/db/tank20_dashboard.sql similarity index 100% rename from build/db/dashboard.sql rename to build/db/tank20_dashboard.sql diff --git a/build/db/download_token.sql b/build/db/tank20_download_token.sql similarity index 100% rename from build/db/download_token.sql rename to build/db/tank20_download_token.sql diff --git a/build/db/footprint.sql b/build/db/tank20_footprint.sql similarity index 100% rename from build/db/footprint.sql rename to build/db/tank20_footprint.sql diff --git a/build/db/image_cache.sql b/build/db/tank20_image_cache.sql similarity index 100% rename from build/db/image_cache.sql rename to build/db/tank20_image_cache.sql diff --git a/build/db/matter.sql b/build/db/tank20_matter.sql similarity index 100% rename from build/db/matter.sql rename to build/db/tank20_matter.sql diff --git a/build/db/preference.sql b/build/db/tank20_preference.sql similarity index 100% rename from build/db/preference.sql rename to build/db/tank20_preference.sql diff --git a/build/db/session.sql b/build/db/tank20_session.sql similarity index 100% rename from build/db/session.sql rename to build/db/tank20_session.sql diff --git a/build/db/upload_token.sql b/build/db/tank20_upload_token.sql similarity index 100% rename from build/db/upload_token.sql rename to build/db/tank20_upload_token.sql diff --git a/build/db/user.sql b/build/db/tank20_user.sql similarity index 100% rename from build/db/user.sql rename to build/db/tank20_user.sql diff --git a/rest/base_model.go b/rest/base_model.go index a2cbd8b..dcf6aef 100644 --- a/rest/base_model.go +++ b/rest/base_model.go @@ -1,13 +1,18 @@ package rest import ( - "time" - "reflect" "math" + "reflect" + "time" ) type Time time.Time +type IBase interface { + //返回其对应的数据库表名 + TableName() string +} + type Base struct { Uuid string `gorm:"primary_key" json:"uuid"` Sort int64 `json:"sort"` @@ -27,6 +32,10 @@ func (this *Base) Map() map[string]interface{} { return data } +func (Base) TableName() string { + return TABLE_PREFIX + "base" +} + //分页类 type Pager struct { Page int `json:"page"` diff --git a/rest/install_controller.go b/rest/install_controller.go index 31ec57a..087f58a 100644 --- a/rest/install_controller.go +++ b/rest/install_controller.go @@ -1,11 +1,15 @@ package rest import ( + "fmt" "github.com/jinzhu/gorm" + "go/build" + "io/ioutil" "net/http" "strconv" ) +//安装程序的接口,只有安装阶段可以访问。 type InstallController struct { BaseController uploadTokenDao *UploadTokenDao @@ -60,14 +64,13 @@ func (this *InstallController) RegisterRoutes() map[string]func(writer http.Resp //每个Controller需要主动注册自己的路由。 routeMap["/api/install/verify"] = this.Wrap(this.Verify, USER_ROLE_GUEST) - routeMap["/api/install/table/dashboard"] = this.Wrap(this.InstallTableDashboard, USER_ROLE_GUEST) + routeMap["/api/install/table/info/list"] = this.Wrap(this.InstallTableInfoList, USER_ROLE_GUEST) return routeMap } -//验证数据库连接 -func (this *InstallController) Verify(writer http.ResponseWriter, request *http.Request) *WebResult { - +//获取数据库连接 +func (this *InstallController) openDbConnection(writer http.ResponseWriter, request *http.Request) *gorm.DB { mysqlPortStr := request.FormValue("mysqlPort") mysqlHost := request.FormValue("mysqlHost") mysqlSchema := request.FormValue("mysqlSchema") @@ -82,23 +85,121 @@ func (this *InstallController) Verify(writer http.ResponseWriter, request *http. } mysqlUrl := GetMysqlUrl(mysqlPort, mysqlHost, mysqlSchema, mysqlUsername, mysqlPassword) - this.logger.Info("验证MySQL连接性 %s", mysqlUrl) + + this.logger.Info("连接MySQL %s", mysqlUrl) var err error = nil db, err := gorm.Open("mysql", mysqlUrl) this.PanicError(err) + return db + +} + +//关闭数据库连接 +func (this *InstallController) closeDbConnection(db *gorm.DB) { + + if db != nil { + err := db.Close() + if err != nil { + this.logger.Error("关闭数据库连接出错 %v", err) + } + } +} + +//根据表名获取建表SQL语句 +func (this *InstallController) getCreateSQLFromFile(tableName string) string { + + //1. 从当前安装目录db下去寻找建表文件。 + homePath := GetHomePath() + filePath := homePath + "/db/" + tableName + ".sql" + exists, err := PathExists(filePath) + if err != nil { + this.PanicServer("从安装目录判断建表语句文件是否存在时出错!") + } + + //2. 从GOPATH下面去找,因为可能是开发环境 + if !exists { + + this.logger.Info("GOPATH = %s", build.Default.GOPATH) + + filePath1 := filePath + filePath = build.Default.GOPATH + "/src/tank/build/db/" + tableName + ".sql" + exists, err = PathExists(filePath) + if err != nil { + this.PanicServer("从GOPATH判断建表语句文件是否存在时出错!") + } + + if !exists { + this.PanicServer(fmt.Sprintf("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath)) + } + } + + //读取文件内容. + bytes, err := ioutil.ReadFile(filePath) + this.PanicError(err) + + return string(bytes) +} + +//根据表名获取建表SQL语句 +func (this *InstallController) getCreateSQLFromDb(db *gorm.DB, base IBase) (bool, string) { + + var hasTable = true + var tableName = base.TableName() + hasTable = db.HasTable(base) + if !hasTable { + return false, "" + } + + // Scan + type Result struct { + Table string + CreateTable string + } + + //读取建表语句。 + var result = &Result{} + db1 := db.Exec("SHOW CREATE TABLE " + tableName).Scan(result) + this.PanicError(db1.Error) + + return true, result.CreateTable +} + +//验证数据库连接 +func (this *InstallController) Verify(writer http.ResponseWriter, request *http.Request) *WebResult { + + db := this.openDbConnection(writer, request) + defer this.closeDbConnection(db) + this.logger.Info("Ping一下数据库") - err = db.DB().Ping() + err := db.DB().Ping() this.PanicError(err) return this.Success("OK") - } -//安装dashboard表 -func (this *InstallController) InstallTableDashboard(writer http.ResponseWriter, request *http.Request) *WebResult { +//获取需要安装的数据库表 +func (this *InstallController) InstallTableInfoList(writer http.ResponseWriter, request *http.Request) *WebResult { - return this.Success("") + var tableNames = []IBase{&Dashboard{}, &DownloadToken{}, &Footprint{}, &ImageCache{}, &Matter{}, &Preference{}, &Session{}, UploadToken{}, &User{}} + var installTableInfos []*InstallTableInfo + + db := this.openDbConnection(writer, request) + defer this.closeDbConnection(db) + + for _, iBase := range tableNames { + + exist, sql := this.getCreateSQLFromDb(db, iBase) + installTableInfos = append(installTableInfos, &InstallTableInfo{ + Name: iBase.TableName(), + CreateSql: this.getCreateSQLFromFile(iBase.TableName()), + TableExist: exist, + ExistCreateSql: sql, + }) + + } + + return this.Success(installTableInfos) } diff --git a/rest/install_model.go b/rest/install_model.go new file mode 100644 index 0000000..0aa7f82 --- /dev/null +++ b/rest/install_model.go @@ -0,0 +1,11 @@ +package rest + +/** + * 表名对应的表结构 + */ +type InstallTableInfo struct { + Name string `json:"name"` + CreateSql string `json:"createSql"` + TableExist bool `json:"tableExist"` + ExistCreateSql string `json:"existCreateSql"` +}