migration.gno
3.20 Kb · 138 lines
1package user
2
3import (
4 "gno.land/p/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v2/accesscontrol"
5 "gno.land/p/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v2/validate"
6 "gno.land/r/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v2/admin"
7)
8
9var (
10 migrationMigratorPkgPath string
11 migrationExportCompleted bool
12 migrationStateCleaned bool
13)
14
15type MigrationConfig struct {
16 ListLimit int
17 Frozen bool
18 TotalUsers int
19 NextUserID uint32
20}
21
22func GetMigrationConfig() MigrationConfig {
23 assertMigrationStateAvailable()
24 return MigrationConfig{
25 ListLimit: listLimit,
26 Frozen: frozen,
27 TotalUsers: userStore.Total(),
28 NextUserID: userStore.NextID(),
29 }
30}
31
32func GetMigrationUserStore(cur realm) *UserStore {
33 assertCanExposeMigrationStore(0, cur)
34 return userStore
35}
36
37func GetMigrationCreateValidator(cur realm) *validate.Validator {
38 assertCanExposeMigrationStore(0, cur)
39 return createValidator
40}
41
42func GetMigrationUpdateValidator(cur realm) *validate.Validator {
43 assertCanExposeMigrationStore(0, cur)
44 return updateValidator
45}
46
47func SetMigrationMigrator(cur realm, pkgPath string) {
48 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
49 if pkgPath == "" {
50 panic("migration migrator is required")
51 }
52 migrationMigratorPkgPath = pkgPath
53}
54
55func ClearMigrationMigrator(cur realm) {
56 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
57 migrationMigratorPkgPath = ""
58}
59
60func SetMigrationExportCompleted(cur realm, completed bool) {
61 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
62 assertMigrationStateAvailable()
63 migrationExportCompleted = completed
64}
65
66func IsMigrationExportCompleted() bool {
67 return migrationExportCompleted
68}
69
70func IsMigrationStateCleaned() bool {
71 return migrationStateCleaned
72}
73
74func CleanupMigrationState(cur realm) {
75 assertCanCleanupMigrationState(0, cur)
76
77 userStore = nil
78 createValidator = nil
79 updateValidator = nil
80
81 migrationStateCleaned = true
82}
83
84func assertCanExposeMigrationStore(_ int, rlm realm) {
85 accesscontrol.AssertCurrentRealm(0, rlm)
86 accesscontrol.AssertIsAdminOrigin(admin.IsAdmin)
87 assertMigrationMigratorConfigured()
88 assertMigrationMigratorCodeRealm(0, rlm)
89 assertMigrationMigratorRealm(0, rlm)
90 assertCanExposeMigrationUserStore()
91}
92
93func assertMigrationMigratorConfigured() {
94 if migrationMigratorPkgPath == "" {
95 panic("migration migrator not set")
96 }
97}
98
99func assertMigrationMigratorCodeRealm(_ int, rlm realm) {
100 if rlm.Previous().PkgPath() == "" {
101 panic("migration migrator must be code realm")
102 }
103}
104
105func assertMigrationMigratorRealm(_ int, rlm realm) {
106 if rlm.Previous().PkgPath() != migrationMigratorPkgPath {
107 panic("migration migrator access required")
108 }
109}
110
111func assertMigrationStateAvailable() {
112 if migrationStateCleaned {
113 panic("migration state cleaned")
114 }
115}
116
117func assertCanCleanupMigrationState(_ int, rlm realm) {
118 accesscontrol.AssertIsAdmin(0, rlm, admin.IsAdmin)
119 if !frozen {
120 panic("contract must be frozen")
121 }
122 if !migrationExportCompleted {
123 panic("migration not completed")
124 }
125 if migrationStateCleaned {
126 panic("migration state already cleaned")
127 }
128}
129
130func assertCanExposeMigrationUserStore() {
131 assertMigrationStateAvailable()
132 if !frozen {
133 panic("contract must be frozen")
134 }
135 if migrationExportCompleted {
136 panic("migration already completed")
137 }
138}