Browse Source

refactor schema versions to be ints, not strings

tags/v2.4.0-rc1
Shivaram Lingamneni 3 years ago
parent
commit
dc456bd6a4
3 changed files with 117 additions and 115 deletions
  1. 113
    111
      irc/database.go
  2. 1
    1
      irc/mysql/history.go
  3. 3
    3
      irc/utils/args.go

+ 113
- 111
irc/database.go View File

@@ -24,7 +24,7 @@ const (
24 24
 	// 'version' of the database schema
25 25
 	keySchemaVersion = "db.version"
26 26
 	// latest schema of the db
27
-	latestDbSchema = "17"
27
+	latestDbSchema = 17
28 28
 
29 29
 	keyCloakSecret = "crypto.cloak_secret"
30 30
 )
@@ -32,14 +32,11 @@ const (
32 32
 type SchemaChanger func(*Config, *buntdb.Tx) error
33 33
 
34 34
 type SchemaChange struct {
35
-	InitialVersion string // the change will take this version
36
-	TargetVersion  string // and transform it into this version
35
+	InitialVersion int // the change will take this version
36
+	TargetVersion  int // and transform it into this version
37 37
 	Changer        SchemaChanger
38 38
 }
39 39
 
40
-// maps an initial version to a schema change capable of upgrading it
41
-var schemaChanges map[string]SchemaChange
42
-
43 40
 func checkDBReadyForInit(path string) error {
44 41
 	_, err := os.Stat(path)
45 42
 	if err == nil {
@@ -72,7 +69,7 @@ func initializeDB(path string) error {
72 69
 
73 70
 	err = store.Update(func(tx *buntdb.Tx) error {
74 71
 		// set schema version
75
-		tx.Set(keySchemaVersion, latestDbSchema, nil)
72
+		tx.Set(keySchemaVersion, strconv.Itoa(latestDbSchema), nil)
76 73
 		tx.Set(keyCloakSecret, utils.GenerateSecretKey(), nil)
77 74
 		return nil
78 75
 	})
@@ -100,9 +97,12 @@ func openDatabaseInternal(config *Config, allowAutoupgrade bool) (db *buntdb.DB,
100 97
 	}()
101 98
 
102 99
 	// read the current version string
103
-	var version string
104
-	err = db.View(func(tx *buntdb.Tx) error {
105
-		version, err = tx.Get(keySchemaVersion)
100
+	var version int
101
+	err = db.View(func(tx *buntdb.Tx) (err error) {
102
+		vStr, err := tx.Get(keySchemaVersion)
103
+		if err == nil {
104
+			version, err = strconv.Atoi(vStr)
105
+		}
106 106
 		return err
107 107
 	})
108 108
 	if err != nil {
@@ -130,11 +130,11 @@ func openDatabaseInternal(config *Config, allowAutoupgrade bool) (db *buntdb.DB,
130 130
 	}
131 131
 }
132 132
 
133
-func performAutoUpgrade(currentVersion string, config *Config) (err error) {
133
+func performAutoUpgrade(currentVersion int, config *Config) (err error) {
134 134
 	path := config.Datastore.Path
135
-	log.Printf("attempting to auto-upgrade schema from version %s to %s\n", currentVersion, latestDbSchema)
135
+	log.Printf("attempting to auto-upgrade schema from version %d to %d\n", currentVersion, latestDbSchema)
136 136
 	timestamp := time.Now().UTC().Format("2006-01-02-15:04:05.000Z")
137
-	backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp)
137
+	backupPath := fmt.Sprintf("%s.v%d.%s.bak", path, currentVersion, timestamp)
138 138
 	log.Printf("making a backup of current database at %s\n", backupPath)
139 139
 	err = utils.CopyFile(path, backupPath)
140 140
 	if err != nil {
@@ -164,29 +164,30 @@ func UpgradeDB(config *Config) (err error) {
164 164
 	}
165 165
 	defer store.Close()
166 166
 
167
-	var version string
167
+	var version int
168 168
 	err = store.Update(func(tx *buntdb.Tx) error {
169 169
 		for {
170
-			version, _ = tx.Get(keySchemaVersion)
171
-			change, schemaNeedsChange := schemaChanges[version]
172
-			if !schemaNeedsChange {
173
-				if version == latestDbSchema {
174
-					// success!
175
-					break
176
-				}
170
+			vStr, _ := tx.Get(keySchemaVersion)
171
+			version, _ = strconv.Atoi(vStr)
172
+			if version == latestDbSchema {
173
+				// success!
174
+				break
175
+			}
176
+			change, ok := getSchemaChange(version)
177
+			if !ok {
177 178
 				// unable to upgrade to the desired version, roll back
178 179
 				return &utils.IncompatibleSchemaError{CurrentVersion: version, RequiredVersion: latestDbSchema}
179 180
 			}
180
-			log.Println("attempting to update schema from version " + version)
181
+			log.Printf("attempting to update schema from version %d\n", version)
181 182
 			err := change.Changer(config, tx)
182 183
 			if err != nil {
183 184
 				return err
184 185
 			}
185
-			_, _, err = tx.Set(keySchemaVersion, change.TargetVersion, nil)
186
+			_, _, err = tx.Set(keySchemaVersion, strconv.Itoa(change.TargetVersion), nil)
186 187
 			if err != nil {
187 188
 				return err
188 189
 			}
189
-			log.Println("successfully updated schema to version " + change.TargetVersion)
190
+			log.Printf("successfully updated schema to version %d\n", change.TargetVersion)
190 191
 		}
191 192
 		return nil
192 193
 	})
@@ -853,93 +854,94 @@ func schemaChangeV16ToV17(config *Config, tx *buntdb.Tx) error {
853 854
 	return nil
854 855
 }
855 856
 
856
-func init() {
857
-	allChanges := []SchemaChange{
858
-		{
859
-			InitialVersion: "1",
860
-			TargetVersion:  "2",
861
-			Changer:        schemaChangeV1toV2,
862
-		},
863
-		{
864
-			InitialVersion: "2",
865
-			TargetVersion:  "3",
866
-			Changer:        schemaChangeV2ToV3,
867
-		},
868
-		{
869
-			InitialVersion: "3",
870
-			TargetVersion:  "4",
871
-			Changer:        schemaChangeV3ToV4,
872
-		},
873
-		{
874
-			InitialVersion: "4",
875
-			TargetVersion:  "5",
876
-			Changer:        schemaChangeV4ToV5,
877
-		},
878
-		{
879
-			InitialVersion: "5",
880
-			TargetVersion:  "6",
881
-			Changer:        schemaChangeV5ToV6,
882
-		},
883
-		{
884
-			InitialVersion: "6",
885
-			TargetVersion:  "7",
886
-			Changer:        schemaChangeV6ToV7,
887
-		},
888
-		{
889
-			InitialVersion: "7",
890
-			TargetVersion:  "8",
891
-			Changer:        schemaChangeV7ToV8,
892
-		},
893
-		{
894
-			InitialVersion: "8",
895
-			TargetVersion:  "9",
896
-			Changer:        schemaChangeV8ToV9,
897
-		},
898
-		{
899
-			InitialVersion: "9",
900
-			TargetVersion:  "10",
901
-			Changer:        schemaChangeV9ToV10,
902
-		},
903
-		{
904
-			InitialVersion: "10",
905
-			TargetVersion:  "11",
906
-			Changer:        schemaChangeV10ToV11,
907
-		},
908
-		{
909
-			InitialVersion: "11",
910
-			TargetVersion:  "12",
911
-			Changer:        schemaChangeV11ToV12,
912
-		},
913
-		{
914
-			InitialVersion: "12",
915
-			TargetVersion:  "13",
916
-			Changer:        schemaChangeV12ToV13,
917
-		},
918
-		{
919
-			InitialVersion: "13",
920
-			TargetVersion:  "14",
921
-			Changer:        schemaChangeV13ToV14,
922
-		},
923
-		{
924
-			InitialVersion: "14",
925
-			TargetVersion:  "15",
926
-			Changer:        schemaChangeV14ToV15,
927
-		},
928
-		{
929
-			InitialVersion: "15",
930
-			TargetVersion:  "16",
931
-			Changer:        schemaChangeV15ToV16,
932
-		},
933
-		{
934
-			InitialVersion: "16",
935
-			TargetVersion:  "17",
936
-			Changer:        schemaChangeV16ToV17,
937
-		},
938
-	}
939
-
940
-	// build the index
941
-	schemaChanges = make(map[string]SchemaChange)
857
+func getSchemaChange(initialVersion int) (result SchemaChange, ok bool) {
942 858
 	for _, change := range allChanges {
943
-		schemaChanges[change.InitialVersion] = change
859
+		if initialVersion == change.InitialVersion {
860
+			return result, true
861
+		}
944 862
 	}
863
+	return
864
+}
865
+
866
+var allChanges = []SchemaChange{
867
+	{
868
+		InitialVersion: 1,
869
+		TargetVersion:  2,
870
+		Changer:        schemaChangeV1toV2,
871
+	},
872
+	{
873
+		InitialVersion: 2,
874
+		TargetVersion:  3,
875
+		Changer:        schemaChangeV2ToV3,
876
+	},
877
+	{
878
+		InitialVersion: 3,
879
+		TargetVersion:  4,
880
+		Changer:        schemaChangeV3ToV4,
881
+	},
882
+	{
883
+		InitialVersion: 4,
884
+		TargetVersion:  5,
885
+		Changer:        schemaChangeV4ToV5,
886
+	},
887
+	{
888
+		InitialVersion: 5,
889
+		TargetVersion:  6,
890
+		Changer:        schemaChangeV5ToV6,
891
+	},
892
+	{
893
+		InitialVersion: 6,
894
+		TargetVersion:  7,
895
+		Changer:        schemaChangeV6ToV7,
896
+	},
897
+	{
898
+		InitialVersion: 7,
899
+		TargetVersion:  8,
900
+		Changer:        schemaChangeV7ToV8,
901
+	},
902
+	{
903
+		InitialVersion: 8,
904
+		TargetVersion:  9,
905
+		Changer:        schemaChangeV8ToV9,
906
+	},
907
+	{
908
+		InitialVersion: 9,
909
+		TargetVersion:  10,
910
+		Changer:        schemaChangeV9ToV10,
911
+	},
912
+	{
913
+		InitialVersion: 10,
914
+		TargetVersion:  11,
915
+		Changer:        schemaChangeV10ToV11,
916
+	},
917
+	{
918
+		InitialVersion: 11,
919
+		TargetVersion:  12,
920
+		Changer:        schemaChangeV11ToV12,
921
+	},
922
+	{
923
+		InitialVersion: 12,
924
+		TargetVersion:  13,
925
+		Changer:        schemaChangeV12ToV13,
926
+	},
927
+	{
928
+		InitialVersion: 13,
929
+		TargetVersion:  14,
930
+		Changer:        schemaChangeV13ToV14,
931
+	},
932
+	{
933
+		InitialVersion: 14,
934
+		TargetVersion:  15,
935
+		Changer:        schemaChangeV14ToV15,
936
+	},
937
+	{
938
+		InitialVersion: 15,
939
+		TargetVersion:  16,
940
+		Changer:        schemaChangeV15ToV16,
941
+	},
942
+	{
943
+		InitialVersion: 16,
944
+		TargetVersion:  17,
945
+		Changer:        schemaChangeV16ToV17,
946
+	},
945 947
 }

+ 1
- 1
irc/mysql/history.go View File

@@ -142,7 +142,7 @@ func (mysql *MySQL) fixSchemas() (err error) {
142 142
 		return
143 143
 	} else if err == nil && schema != latestDbSchema {
144 144
 		// TODO figure out what to do about schema changes
145
-		return &utils.IncompatibleSchemaError{CurrentVersion: schema, RequiredVersion: latestDbSchema}
145
+		return fmt.Errorf("incompatible schema: got %s, expected %s", schema, latestDbSchema)
146 146
 	} else if err != nil {
147 147
 		return err
148 148
 	}

+ 3
- 3
irc/utils/args.go View File

@@ -71,12 +71,12 @@ func SafeErrorParam(param string) string {
71 71
 }
72 72
 
73 73
 type IncompatibleSchemaError struct {
74
-	CurrentVersion  string
75
-	RequiredVersion string
74
+	CurrentVersion  int
75
+	RequiredVersion int
76 76
 }
77 77
 
78 78
 func (err *IncompatibleSchemaError) Error() string {
79
-	return fmt.Sprintf("Database requires update. Expected schema v%s, got v%s", err.RequiredVersion, err.CurrentVersion)
79
+	return fmt.Sprintf("Database requires update. Expected schema v%d, got v%d", err.RequiredVersion, err.CurrentVersion)
80 80
 }
81 81
 
82 82
 func NanoToTimestamp(nanotime int64) string {

Loading…
Cancel
Save