92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package db
|
|
|
|
// ================================================================
|
|
// Schema 版本声明
|
|
// ================================================================
|
|
|
|
type SchemaVersionDef struct {
|
|
Version string
|
|
TableName string
|
|
Columns map[string]ColumnDef
|
|
}
|
|
|
|
type ColumnDef struct {
|
|
Type string
|
|
NotNull bool
|
|
Default string
|
|
Primary bool
|
|
}
|
|
|
|
var schemaHistory = []SchemaVersionDef{
|
|
// v1: 初始版本
|
|
{
|
|
Version: "v1",
|
|
TableName: "proxies",
|
|
Columns: map[string]ColumnDef{
|
|
"id": {Type: "INTEGER", Primary: true},
|
|
"name": {Type: "TEXT", NotNull: true},
|
|
"type": {Type: "TEXT", NotNull: true, Default: "'tcp'"},
|
|
"local_ip": {Type: "TEXT", NotNull: true},
|
|
"local_port": {Type: "INTEGER", NotNull: true},
|
|
"remote_port": {Type: "INTEGER", NotNull: true},
|
|
"enabled": {Type: "INTEGER", NotNull: true, Default: "1"},
|
|
"created_at": {Type: "DATETIME", Default: "CURRENT_TIMESTAMP"},
|
|
"updated_at": {Type: "DATETIME", Default: "CURRENT_TIMESTAMP"},
|
|
},
|
|
},
|
|
// v2: 当前版本
|
|
{
|
|
Version: "v2",
|
|
TableName: "proxies",
|
|
Columns: map[string]ColumnDef{
|
|
"id": {Type: "INTEGER", Primary: true},
|
|
"name": {Type: "TEXT", NotNull: true},
|
|
"type": {Type: "TEXT", NotNull: true, Default: "'tcp'"},
|
|
"local_ip": {Type: "TEXT", NotNull: true},
|
|
"local_port": {Type: "INTEGER", NotNull: true},
|
|
"remote_port": {Type: "INTEGER", NotNull: true},
|
|
"enabled": {Type: "INTEGER", NotNull: true, Default: "1"},
|
|
"created_at": {Type: "DATETIME", Default: "CURRENT_TIMESTAMP"},
|
|
"updated_at": {Type: "DATETIME", Default: "CURRENT_TIMESTAMP"},
|
|
},
|
|
},
|
|
}
|
|
|
|
func getSchemaDef(version string) *SchemaVersionDef {
|
|
for _, def := range schemaHistory {
|
|
if def.Version == version {
|
|
return &def
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getLatestSchemaDef() *SchemaVersionDef {
|
|
if len(schemaHistory) == 0 {
|
|
return nil
|
|
}
|
|
return &schemaHistory[len(schemaHistory)-1]
|
|
}
|
|
|
|
func schemaVersionsEqual(v1, v2 *SchemaVersionDef) bool {
|
|
if v1 == nil || v2 == nil {
|
|
return false
|
|
}
|
|
if v1.TableName != v2.TableName {
|
|
return false
|
|
}
|
|
if len(v1.Columns) != len(v2.Columns) {
|
|
return false
|
|
}
|
|
for name, col1 := range v1.Columns {
|
|
col2, ok := v2.Columns[name]
|
|
if !ok {
|
|
return false
|
|
}
|
|
if col1.Type != col2.Type || col1.NotNull != col2.NotNull || col1.Default != col2.Default {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|