Browse Source

schema change for #1274

tags/v2.4.0-rc1
Shivaram Lingamneni 3 years ago
parent
commit
4296ff02a4
2 changed files with 64 additions and 6 deletions
  1. 55
    1
      irc/database.go
  2. 9
    5
      irc/import.go

+ 55
- 1
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 = 18
28 28
 
29 29
 	keyCloakSecret = "crypto.cloak_secret"
30 30
 )
@@ -854,6 +854,55 @@ func schemaChangeV16ToV17(config *Config, tx *buntdb.Tx) error {
854 854
 	return nil
855 855
 }
856 856
 
857
+// #1274: we used to suspend accounts by deleting their "verified" key,
858
+// now we save some metadata under a new key
859
+func schemaChangeV17ToV18(config *Config, tx *buntdb.Tx) error {
860
+	now := time.Now().UTC()
861
+
862
+	exists := "account.exists "
863
+	suspended := "account.suspended "
864
+	verif := "account.verified "
865
+	verifCode := "account.verificationcode "
866
+
867
+	var accounts []string
868
+
869
+	tx.AscendGreaterOrEqual("", exists, func(key, value string) bool {
870
+		if !strings.HasPrefix(key, exists) {
871
+			return false
872
+		}
873
+		account := strings.TrimPrefix(key, exists)
874
+		_, verifiedErr := tx.Get(verif + account)
875
+		_, verifCodeErr := tx.Get(verifCode + account)
876
+		if verifiedErr != nil && verifCodeErr != nil {
877
+			// verified key not present, but there's no code either,
878
+			// this is a suspension
879
+			accounts = append(accounts, account)
880
+		}
881
+		return true
882
+	})
883
+
884
+	type accountSuspensionV18 struct {
885
+		TimeCreated time.Time
886
+		Duration    time.Duration
887
+		OperName    string
888
+		Reason      string
889
+	}
890
+
891
+	for _, account := range accounts {
892
+		var sus accountSuspensionV18
893
+		sus.TimeCreated = now
894
+		sus.OperName = "*"
895
+		sus.Reason = "[unknown]"
896
+		susBytes, err := json.Marshal(sus)
897
+		if err != nil {
898
+			return err
899
+		}
900
+		tx.Set(suspended+account, string(susBytes), nil)
901
+	}
902
+
903
+	return nil
904
+}
905
+
857 906
 func getSchemaChange(initialVersion int) (result SchemaChange, ok bool) {
858 907
 	for _, change := range allChanges {
859 908
 		if initialVersion == change.InitialVersion {
@@ -944,4 +993,9 @@ var allChanges = []SchemaChange{
944 993
 		TargetVersion:  17,
945 994
 		Changer:        schemaChangeV16ToV17,
946 995
 	},
996
+	{
997
+		InitialVersion: 17,
998
+		TargetVersion:  18,
999
+		Changer:        schemaChangeV17ToV18,
1000
+	},
947 1001
 }

+ 9
- 5
irc/import.go View File

@@ -15,6 +15,14 @@ import (
15 15
 	"github.com/oragono/oragono/irc/utils"
16 16
 )
17 17
 
18
+const (
19
+	// produce a hardcoded version of the database schema
20
+	// XXX instead of referencing, e.g., keyAccountExists, we should write in the string literal
21
+	// (to ensure that no matter what code changes happen elsewhere, we're still producing a
22
+	// db of the hardcoded version)
23
+	importDBSchemaVersion = 18
24
+)
25
+
18 26
 type userImport struct {
19 27
 	Name            string
20 28
 	Hash            string
@@ -66,11 +74,7 @@ func doImportDBGeneric(config *Config, dbImport databaseImport, credsType Creden
66 74
 		return fmt.Errorf("unsupported version of the db for import: version %d is required", requiredVersion)
67 75
 	}
68 76
 
69
-	// produce a hardcoded version of the database schema
70
-	// XXX instead of referencing, e.g., keyAccountExists, we should write in the string literal
71
-	// (to ensure that no matter what code changes happen elsewhere, we're still producing a
72
-	// db of the hardcoded version)
73
-	tx.Set(keySchemaVersion, "17", nil)
77
+	tx.Set(keySchemaVersion, strconv.Itoa(importDBSchemaVersion), nil)
74 78
 	tx.Set(keyCloakSecret, utils.GenerateSecretKey(), nil)
75 79
 
76 80
 	for username, userInfo := range dbImport.Users {

Loading…
Cancel
Save