feat(notification): add durable mail delivery state

This commit is contained in:
shira committed 2026-10-06 10:52:47 +03:00
1 parent 391c8a6cec
commit 2db585fbff
41 files changed
+4903 -108

No files matched your search

@@ -26,16 +26,21 @@ const (
policyMigrationVersion = 3
// consumerMigrationVersion creates consumerTableNames.
consumerMigrationVersion = 5
// deliveryMigrationVersion creates deliveryTableNames (mail requests,
// deliveries, and their attempts) and deliveryFunctionNames.
deliveryMigrationVersion = 6
)
var (
// Tables created by 00001, 00002, 00003, and 00005.
// Tables created by 00001, 00002, 00003, 00005, and 00006.
outboxTables = []string{"outbox_events"}
identityTableNames = []string{"users", "email_tokens", "sessions"}
policyTableNames = []string{"policy_versions"}
consumerTableNames = []string{"consumer_processed_events", "consumer_dead_letters"}
// Trigger functions created by 00003.
policyFunctionNames = []string{"policy_versions_refuse_change()", "policy_versions_stamp_creation()"}
deliveryTableNames = []string{"mail_requests", "deliveries", "delivery_attempts"}
// Trigger functions created by 00003 and 00006.
policyFunctionNames = []string{"policy_versions_refuse_change()", "policy_versions_stamp_creation()"}
deliveryFunctionNames = []string{"deliveries_refuse_content_change()", "delivery_attempts_refuse_update()"}
)
func TestPostgresMigrations(t *testing.T) {
@@ -65,8 +70,10 @@ func TestPostgresMigrations(t *testing.T) {
assertTablesExist(ctx, t, pool, true, policyTableNames...)
assertFunctionsExist(ctx, t, pool, true, policyFunctionNames...)
assertTablesExist(ctx, t, pool, true, consumerTableNames...)
assertTablesExist(ctx, t, pool, true, deliveryTableNames...)
assertFunctionsExist(ctx, t, pool, true, deliveryFunctionNames...)
identityRolledBack, policyRolledBack, consumerRolledBack := false, false, false
identityRolledBack, policyRolledBack, consumerRolledBack, deliveryRolledBack := false, false, false, false
for rolledBack := 0; ; rolledBack++ {
result, err := migrator.DownOne(ctx)
if errors.Is(err, postgresadapter.ErrNoMigrationToRollBack) {
@@ -78,6 +85,14 @@ func TestPostgresMigrations(t *testing.T) {
if err != nil {
t.Fatalf("DownOne() after %d rollbacks error = %v", rolledBack, err)
}
if result.Version == deliveryMigrationVersion {
// The delivery migration removes its tables and functions
// and leaves the identity tables they referenced.
assertTablesExist(ctx, t, pool, false, deliveryTableNames...)
assertFunctionsExist(ctx, t, pool, false, deliveryFunctionNames...)
assertTablesExist(ctx, t, pool, true, identityTableNames...)
deliveryRolledBack = true
}
if result.Version == consumerMigrationVersion {
// The consumer migration removes only its own tables.
assertTablesExist(ctx, t, pool, false, consumerTableNames...)
@@ -100,8 +115,9 @@ func TestPostgresMigrations(t *testing.T) {
identityRolledBack = true
}
}
if !identityRolledBack || !policyRolledBack || !consumerRolledBack {
t.Fatalf("migrations %d, %d, and %d were not all rolled back", identityMigrationVersion, policyMigrationVersion, consumerMigrationVersion)
if !identityRolledBack || !policyRolledBack || !consumerRolledBack || !deliveryRolledBack {
t.Fatalf("migrations %d, %d, %d, and %d were not all rolled back",
identityMigrationVersion, policyMigrationVersion, consumerMigrationVersion, deliveryMigrationVersion)
}
assertTablesExist(ctx, t, pool, false, outboxTables...)
assertTablesExist(ctx, t, pool, false, identityTableNames...)
@@ -141,6 +157,7 @@ func TestPostgresMigrations(t *testing.T) {
assertTablesExist(ctx, t, pool, true, policyTableNames...)
assertFunctionsExist(ctx, t, pool, true, policyFunctionNames...)
assertTablesExist(ctx, t, pool, true, consumerTableNames...)
assertTablesExist(ctx, t, pool, true, deliveryTableNames...)
assertAllApplied(ctx, t, migrator, total)
})