-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathorg_scoped_migration_test.go
More file actions
335 lines (301 loc) · 11.3 KB
/
org_scoped_migration_test.go
File metadata and controls
335 lines (301 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package persistence
import (
"context"
"database/sql"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
"github.com/google/uuid"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/modules/cockroachdb"
gormpg "gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// TestOrgScopedMigrations_CockroachDB validates that org-scoped account migrations
// run correctly on CockroachDB and that all indexes and constraints work as expected.
func TestOrgScopedMigrations_CockroachDB(t *testing.T) {
if testing.Short() {
t.Skip("Skipping CockroachDB migration test in short mode")
}
// Start CockroachDB container directly so we can get the connection string
// for both raw SQL (migrations) and GORM (entity operations)
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
crdbContainer, err := cockroachdb.Run(ctx,
"cockroachdb/cockroach:v24.3.0",
cockroachdb.WithDatabase("test_db"),
cockroachdb.WithUser("root"),
cockroachdb.WithInsecure(),
)
require.NoError(t, err, "Failed to start CockroachDB container")
defer func() {
_ = crdbContainer.Terminate(context.Background())
}()
connConfig, err := crdbContainer.ConnectionConfig(ctx)
require.NoError(t, err)
connStr := connConfig.ConnString()
// Open raw SQL connection for migrations
sqlDB, err := sql.Open("pgx", connStr)
require.NoError(t, err)
defer sqlDB.Close()
// Apply all current-account migrations in order
applyCurrentAccountMigrations(t, sqlDB)
// Open GORM connection for entity operations
gormDB, err := gorm.Open(gormpg.Open(connStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
require.NoError(t, err)
t.Run("MigrationsAppliedSuccessfully", func(t *testing.T) {
// Verify org_party_id column exists
var count int64
err := gormDB.Raw(`
SELECT COUNT(*) FROM information_schema.columns
WHERE table_name = 'account' AND column_name = 'org_party_id'
`).Scan(&count).Error
require.NoError(t, err)
assert.Equal(t, int64(1), count, "org_party_id column should exist")
})
t.Run("IndexesCreated", func(t *testing.T) {
for _, indexName := range []string{
"idx_account_participant_syndicate",
"idx_account_syndicate_participants",
} {
var count int64
err := gormDB.Raw(`
SELECT COUNT(*) FROM pg_indexes
WHERE tablename = 'account' AND indexname = ?
`, indexName).Scan(&count).Error
require.NoError(t, err)
assert.Equal(t, int64(1), count, "Index %s should exist", indexName)
}
})
t.Run("ScopeIntegrityIndexDropped", func(t *testing.T) {
// Migration 20260416000001 dropped idx_account_syndicate_scope_integrity to allow
// multiple accounts per (party_id, org_party_id, instrument_code) — required by
// utility billing patterns (e.g. separate GBP accounts for electricity and gas
// from the same supplier).
var count int64
err := gormDB.Raw(`
SELECT COUNT(*) FROM pg_indexes
WHERE tablename = 'account' AND indexname = 'idx_account_syndicate_scope_integrity'
`).Scan(&count).Error
require.NoError(t, err)
assert.Equal(t, int64(0), count, "idx_account_syndicate_scope_integrity should have been dropped")
})
t.Run("CanCreateOrgScopedAccount", func(t *testing.T) {
partyID := uuid.New()
orgPartyID := uuid.New()
now := time.Now()
entity := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-ORG-001",
AccountIdentification: "GB82WEST11111111111111",
AccountType: "current",
InstrumentCode: "GBP",
Dimension: "CURRENCY",
Status: "active",
PartyID: partyID,
OrgPartyID: &orgPartyID,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err := gormDB.Create(entity).Error
require.NoError(t, err, "Should create org-scoped account")
var retrieved CurrentAccountEntity
err = gormDB.First(&retrieved, "id = ?", entity.ID).Error
require.NoError(t, err)
require.NotNil(t, retrieved.OrgPartyID)
assert.Equal(t, orgPartyID, *retrieved.OrgPartyID)
})
t.Run("PersonalAccountHasNullOrgPartyID", func(t *testing.T) {
now := time.Now()
entity := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-PERSONAL-001",
AccountIdentification: "GB82WEST22222222222222",
AccountType: "current",
InstrumentCode: "GBP",
Dimension: "CURRENCY",
Status: "active",
PartyID: uuid.New(),
OrgPartyID: nil,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err := gormDB.Create(entity).Error
require.NoError(t, err, "Should create personal account with NULL org_party_id")
var retrieved CurrentAccountEntity
err = gormDB.First(&retrieved, "id = ?", entity.ID).Error
require.NoError(t, err)
assert.Nil(t, retrieved.OrgPartyID, "Personal account should have NULL org_party_id")
})
t.Run("MultipleAccountsPerPartyOrgInstrumentAllowed", func(t *testing.T) {
// Utility billing case: a customer has separate GBP billing accounts for electricity
// and gas with the same supplier. After migration 20260416000001, both succeed and
// uniqueness is preserved only by account_identification.
partyID := uuid.New()
orgPartyID := uuid.New()
now := time.Now()
entity1 := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-UNIQ-001",
AccountIdentification: "PPM-ELEC-DEMO-001",
AccountType: "current",
InstrumentCode: "GBP",
Dimension: "CURRENCY",
Status: "active",
PartyID: partyID,
OrgPartyID: &orgPartyID,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err := gormDB.Create(entity1).Error
require.NoError(t, err, "First org-scoped GBP account should succeed")
entity2 := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-UNIQ-002",
AccountIdentification: "PPM-GAS-DEMO-001",
AccountType: "current",
InstrumentCode: "GBP",
Dimension: "CURRENCY",
Status: "active",
PartyID: partyID,
OrgPartyID: &orgPartyID,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err = gormDB.Create(entity2).Error
assert.NoError(t, err, "Second GBP account for same (party, org) should succeed (multi-service billing)")
entity3 := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-UNIQ-003",
AccountIdentification: "PPM-EUR-DEMO-001",
AccountType: "current",
InstrumentCode: "EUR",
Dimension: "CURRENCY",
Status: "active",
PartyID: partyID,
OrgPartyID: &orgPartyID,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err = gormDB.Create(entity3).Error
assert.NoError(t, err, "Different instrument_code for same (party_id, org_party_id) should succeed")
// Account-identifier uniqueness is still enforced by idx_account_account_identification.
dup := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-UNIQ-004",
AccountIdentification: "PPM-ELEC-DEMO-001",
AccountType: "current",
InstrumentCode: "GBP",
Dimension: "CURRENCY",
Status: "active",
PartyID: partyID,
OrgPartyID: &orgPartyID,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err = gormDB.Create(dup).Error
require.Error(t, err, "Duplicate account_identification must still be rejected")
assert.Contains(t, err.Error(), "duplicate key", "rejection should be a uniqueness violation")
assert.Contains(t, err.Error(), "account_identification", "rejection must reference idx_account_account_identification, not some other constraint")
})
t.Run("UniqueConstraintDoesNotAffectPersonalAccounts", func(t *testing.T) {
partyID := uuid.New()
now := time.Now()
// Two personal accounts (NULL org_party_id) with same party and instrument_code
// should succeed because the partial unique index only applies WHERE org_party_id IS NOT NULL
entity1 := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-PERS-DUP-001",
AccountIdentification: "GB82WEST66666666666666",
AccountType: "current",
InstrumentCode: "GBP",
Dimension: "CURRENCY",
Status: "active",
PartyID: partyID,
OrgPartyID: nil,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err := gormDB.Create(entity1).Error
require.NoError(t, err)
entity2 := &CurrentAccountEntity{
ID: uuid.New(),
AccountID: "ACC-PERS-DUP-002",
AccountIdentification: "GB82WEST77777777777777",
AccountType: "savings",
InstrumentCode: "GBP",
Dimension: "CURRENCY",
Status: "active",
PartyID: partyID,
OrgPartyID: nil,
OverdraftLimit: 0,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: "system",
UpdatedBy: "system",
}
err = gormDB.Create(entity2).Error
assert.NoError(t, err, "Multiple personal accounts with same party+instrument_code should be allowed")
})
}
// applyCurrentAccountMigrations runs all SQL migration files for the current-account service.
func applyCurrentAccountMigrations(t *testing.T, db *sql.DB) {
t.Helper()
// Find project root by traversing up for go.mod
dir, err := os.Getwd()
require.NoError(t, err)
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
break
}
parent := filepath.Dir(dir)
require.NotEqual(t, dir, parent, "Could not find project root (no go.mod)")
dir = parent
}
migrationDir := filepath.Join(dir, "services", "current-account", "migrations")
entries, err := os.ReadDir(migrationDir)
require.NoError(t, err, "Failed to read migration directory")
var migrations []string
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".sql") {
migrations = append(migrations, filepath.Join(migrationDir, entry.Name()))
}
}
sort.Strings(migrations)
for _, path := range migrations {
content, err := os.ReadFile(path)
require.NoError(t, err, "Failed to read migration: %s", path)
_, err = db.ExecContext(context.Background(), string(content))
require.NoError(t, err, "Failed to apply migration %s", filepath.Base(path))
t.Logf("Applied migration: %s", filepath.Base(path))
}
}