Przeglądaj źródła

server infrastructure for auto upgrades

Matthew R. Kasun 3 lat temu
rodzic
commit
a10ee3af66
2 zmienionych plików z 69 dodań i 0 usunięć
  1. 62 0
      database/database.go
  2. 7 0
      main.go

+ 62 - 0
database/database.go

@@ -87,6 +87,68 @@ const FETCH_ALL = "fetchall"
 // CLOSE_DB - graceful close of db const
 const CLOSE_DB = "closedb"
 
+// MigrationTable - maps function to update database table to new schema
+type MigrationTable struct {
+	Name string
+	Op   UpdateTable
+}
+
+//UpdateTable - func to migrate a database table to new schema
+type UpdateTable func(string)
+
+// MigrationTables - schema update functions for database tables
+// Usage:  declare new UpdateTable func and update operation in table
+// Example:  To add new field to UsersTable (models.User has been updated with NewField)
+//func UserTableMigration (table string) {
+//	var data models.User
+//	collection, err := FetchRecords(table)
+//	if err != nil {
+//		logger.Log(0, "could not update table", table, err.Error())
+//		return
+//	}
+//	for key, value := range collection {
+//		if err := json.Unmarshal([]byte(value), &data); err != nil {
+//			logger.Log(0, "failed to unmarshal database table", table, err.Error())
+//		}
+//		if data.NewField == "" {
+//			data.NewFied = defaultvalue
+//			newRecord, err := json.Marshal(data)
+//			if err != nil {
+//				logger.Log(0, "failed to marshal new data for table", table, err.Error())
+//				continue
+//			}
+//			if err := Insert(key, string(newRecord), table); err != nil {
+//				logger.Log(0, "failed to update", table, ".NewField", err.Error())
+//				continue
+//			}
+//		}
+//	}
+//}
+//var Tables []MigrationTable = []MigrationTable{
+//	...
+//	{Name: SERVER_UUID_TABLE_NAME, Op: NilUpdate},
+//	{Name: USERS_TABLE_NAME, Op: UserTableMigration},
+//}
+
+var MigrationTables []MigrationTable = []MigrationTable{
+	{Name: DELETED_NODES_TABLE_NAME, Op: NilUpdate},
+	{Name: DNS_TABLE_NAME, Op: NilUpdate},
+	{Name: EXT_CLIENT_TABLE_NAME, Op: NilUpdate},
+	{Name: GENERATED_TABLE_NAME, Op: NilUpdate},
+	{Name: NETWORKS_TABLE_NAME, Op: NilUpdate},
+	{Name: NODES_TABLE_NAME, Op: NilUpdate},
+	{Name: NODE_ACLS_TABLE_NAME, Op: NilUpdate},
+	{Name: PEERS_TABLE_NAME, Op: NilUpdate},
+	{Name: SERVERCONF_TABLE_NAME, Op: NilUpdate},
+	{Name: SERVER_UUID_TABLE_NAME, Op: NilUpdate},
+	{Name: USERS_TABLE_NAME, Op: NilUpdate},
+}
+
+// NilUpdate - function to be called when table schema is not being updated
+func NilUpdate(string) {
+	return
+}
+
 func getCurrentDB() map[string]interface{} {
 	switch servercfg.GetDB() {
 	case "rqlite":

+ 7 - 0
main.go

@@ -72,6 +72,7 @@ func initialize() { // Client Mode Prereq Check
 		logger.FatalLog("Error connecting to database")
 	}
 	logger.Log(0, "database successfully connected")
+	updateDatabase()
 	logic.SetJWTSecret()
 
 	err = logic.TimerCheckpoint()
@@ -253,3 +254,9 @@ func genCerts() error {
 	}
 	return nil
 }
+
+func updateDatabase() {
+	for _, table := range database.MigrationTables {
+		table.Op(table.Name)
+	}
+}