f2c534f636
- mongo-driver v2 connection with bounded startup retry + /readyz gate - idempotent creation of all §4.9 indexes - UpdateVersioned: version-filtered updates, conflict vs not-found errors - AES-256-GCM Seal/Open for ticketing credentials, base64(nonce|ct) - GridFS file store: MIME sniffing, MAX_UPLOAD_MB cap, sha256 metadata - HMAC-signed short-lived file URL tokens (§5.1, 1h TTL) - integration tests against compose Mongo via docker-compose.test.yml overlay Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
73 lines
2.9 KiB
Go
73 lines
2.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
)
|
|
|
|
// EnsureIndexes creates every index from spec §4.9. CreateMany is idempotent:
|
|
// existing identical indexes are no-ops, so this runs safely at every startup.
|
|
func EnsureIndexes(ctx context.Context, db *mongo.Database) error {
|
|
plan := map[string][]mongo.IndexModel{
|
|
"users": {
|
|
{Keys: bson.D{{Key: "email", Value: 1}}, Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: "auth.oidc.issuer", Value: 1}, {Key: "auth.oidc.subject", Value: 1}},
|
|
Options: options.Index().SetUnique(true).SetSparse(true)},
|
|
},
|
|
"customers": {
|
|
{Keys: bson.D{{Key: "name", Value: 1}}, Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: "consultantIds", Value: 1}}},
|
|
},
|
|
"pools": {
|
|
{Keys: bson.D{{Key: "consultantId", Value: 1}, {Key: "developerId", Value: 1}},
|
|
Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: "developerId", Value: 1}}},
|
|
},
|
|
"tasks": {
|
|
{Keys: bson.D{{Key: "customerId", Value: 1}, {Key: "status", Value: 1}, {Key: "updatedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "consultantId", Value: 1}, {Key: "status", Value: 1}, {Key: "updatedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "assignee.userId", Value: 1}, {Key: "status", Value: 1}}},
|
|
{Keys: bson.D{{Key: "rootId", Value: 1}}},
|
|
{Keys: bson.D{{Key: "parentId", Value: 1}}},
|
|
{Keys: bson.D{{Key: "external.system", Value: 1}, {Key: "external.key", Value: 1}, {Key: "customerId", Value: 1}},
|
|
Options: options.Index().SetUnique(true).SetSparse(true)},
|
|
{Keys: bson.D{{Key: "title", Value: "text"}, {Key: "description", Value: "text"}}},
|
|
},
|
|
"bountyAwards": {
|
|
{Keys: bson.D{{Key: "developerId", Value: 1}, {Key: "awardedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "consultantId", Value: 1}, {Key: "awardedAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "customerId", Value: 1}, {Key: "awardedAt", Value: -1}}},
|
|
},
|
|
"conversations": {
|
|
{Keys: bson.D{{Key: "participantIds", Value: 1}, {Key: "lastMessageAt", Value: -1}}},
|
|
{Keys: bson.D{{Key: "kind", Value: 1}, {Key: "customerId", Value: 1}}},
|
|
},
|
|
"messages": {
|
|
// ULID _id is time-ordered, so this serves history pagination.
|
|
{Keys: bson.D{{Key: "conversationId", Value: 1}, {Key: "_id", Value: 1}}},
|
|
},
|
|
"notifications": {
|
|
{Keys: bson.D{{Key: "userId", Value: 1}, {Key: "readAt", Value: 1}, {Key: "createdAt", Value: -1}}},
|
|
},
|
|
"sessions": {
|
|
{Keys: bson.D{{Key: "expiresAt", Value: 1}}, Options: options.Index().SetExpireAfterSeconds(0)},
|
|
{Keys: bson.D{{Key: "userId", Value: 1}}},
|
|
},
|
|
"auditLog": {
|
|
{Keys: bson.D{{Key: "at", Value: -1}}},
|
|
{Keys: bson.D{{Key: "entityId", Value: 1}, {Key: "at", Value: -1}}},
|
|
},
|
|
}
|
|
|
|
for coll, models := range plan {
|
|
if _, err := db.Collection(coll).Indexes().CreateMany(ctx, models); err != nil {
|
|
return fmt.Errorf("create indexes for %s: %w", coll, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|