diff --git a/server/cmd/rename-rollback/main.go b/server/cmd/rename-rollback/main.go deleted file mode 100644 index 8ad1834..0000000 --- a/server/cmd/rename-rollback/main.go +++ /dev/null @@ -1,87 +0,0 @@ -// Command rename-rollback reverses migration 0004. -// -// Run it only as part of a decision to revert the release that introduced the -// instance rename. It renames instance_id back to org_id and restores the two -// collection names. Like the migration, it only renames — it deletes no -// documents. -// -// rename-rollback -uri mongodb://host:27017 -db vantage -confirm -package main - -import ( - "context" - "flag" - "log" - "time" - - "github.com/mrhid6/vantage/server/internal/services" - "go.mongodb.org/mongo-driver/v2/bson" - "go.mongodb.org/mongo-driver/v2/mongo" - "go.mongodb.org/mongo-driver/v2/mongo/options" -) - -func main() { - uri := flag.String("uri", "mongodb://localhost:27017", "MongoDB URI") - dbName := flag.String("db", "vantage", "database name") - confirm := flag.Bool("confirm", false, "required; refuses to run without it") - flag.Parse() - - if !*confirm { - log.Fatal("refusing to run without -confirm") - } - - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) - defer cancel() - - client, err := mongo.Connect(options.Client().ApplyURI(*uri)) - if err != nil { - log.Fatalf("connect: %v", err) - } - defer client.Disconnect(ctx) - - db := client.Database(*dbName) - - // Drop indexes keyed on instance_id first, for the same reason the forward - // migration drops the org_id ones: a unique index treats the missing field - // as null and rejects the second document the rename touches. - for _, c := range services.ScopedCollections { - if err := services.DropIndexesKeyedOn(ctx, db, c, "instance_id"); err != nil { - log.Fatalf("%v", err) - } - } - - for _, c := range services.ScopedCollections { - res, err := db.Collection(c).UpdateMany(ctx, - bson.M{"instance_id": bson.M{"$exists": true}}, - bson.M{"$rename": bson.M{"instance_id": "org_id"}}, - ) - if err != nil { - log.Fatalf("rename instance_id in %s: %v", c, err) - } - if res.ModifiedCount > 0 { - log.Printf("%s: reverted %d document(s)", c, res.ModifiedCount) - } - } - - for _, r := range []struct{ from, to string }{ - {"instances", "orgs"}, - {"instance_oidc", "org_oidc"}, - } { - cmd := bson.D{ - {Key: "renameCollection", Value: *dbName + "." + r.from}, - {Key: "to", Value: *dbName + "." + r.to}, - } - if err := client.Database("admin").RunCommand(ctx, cmd).Err(); err != nil { - log.Printf("rename %s to %s: %v (continuing)", r.from, r.to, err) - continue - } - log.Printf("renamed collection %s to %s", r.from, r.to) - } - - // Remove the marker so a redeployed new binary re-runs the migration. - if _, err := db.Collection("migrations").DeleteOne(ctx, bson.M{"_id": "0004_org_to_instance"}); err != nil { - log.Printf("clear migration marker: %v", err) - } - - log.Println("rollback complete") -}