-
Couldn't load subscription status.
- Fork 58
fix(server): add deleted migration to allow following migrations to execute #1882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wilfredmulenga
wants to merge
9
commits into
main
Choose a base branch
from
fix/migration-script-key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−30
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf89cee
fix: adding deleted migration file to allow running of migrations
wilfredmulenga 2bbca6e
Fixes metadata update logic and renames migration function
wilfredmulenga 3ab53d4
Merge branch 'main' into fix/migration-script-key
wilfredmulenga a89497e
Merge branch 'main' into fix/migration-script-key
wilfredmulenga 593a040
Merge branch 'main' into fix/migration-script-key
wilfredmulenga 818d17b
Merge branch 'main' into fix/migration-script-key
wilfredmulenga 6f37857
Adds middleware package import for adapter layer
wilfredmulenga f30ccaf
Merge branch 'main' into fix/migration-script-key
wilfredmulenga 88a2949
Merge branch 'main' into fix/migration-script-key
wilfredmulenga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
server/internal/infrastructure/mongo/migration/251022100000_convert_topics_to_string.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package migration | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
| "strings" | ||
|
|
||
| "github.com/reearth/reearthx/mongox" | ||
| "go.mongodb.org/mongo-driver/bson" | ||
| "go.mongodb.org/mongo-driver/bson/primitive" | ||
| ) | ||
|
|
||
| func ConvertTopicsToString(ctx context.Context, c DBClient) error { | ||
| projectCol := c.WithCollection("project") | ||
| metadataCol := c.WithCollection("projectmetadata") | ||
|
|
||
| // First, add empty topics field to all projects | ||
| log.Printf("Adding empty topics field to project collection\n") | ||
| if _, err := projectCol.Client().UpdateMany(ctx, bson.M{}, bson.M{ | ||
| "$set": bson.M{ | ||
| "topics": "", | ||
| }, | ||
| }); err != nil { | ||
| log.Printf("Failed to add topics field to projects: %v\n", err) | ||
| return err | ||
| } | ||
| log.Printf("Successfully added empty topics field to all projects\n") | ||
|
|
||
| // Then convert topics from array to string in projectmetadata | ||
| return metadataCol.Find(ctx, bson.M{}, &mongox.BatchConsumer{ | ||
| Size: 1000, | ||
| Callback: func(rows []bson.Raw) error { | ||
| log.Printf("Processing batch of %d project metadata records\n", len(rows)) | ||
|
|
||
| for _, row := range rows { | ||
| var metadata map[string]interface{} | ||
| if err := bson.Unmarshal(row, &metadata); err != nil { | ||
| log.Printf("Error unmarshaling project metadata: %v\n", err) | ||
| continue | ||
| } | ||
|
|
||
| id, ok := metadata["project"].(string) | ||
| if !ok { | ||
| log.Printf("Skipping metadata with missing or invalid project id\n") | ||
| continue | ||
| } | ||
|
|
||
| // Check if topics field exists and is an array | ||
| if topicsField, exists := metadata["topics"]; exists { | ||
| var topicStrings []string | ||
| var shouldUpdate bool | ||
|
|
||
| switch topics := topicsField.(type) { | ||
| case []interface{}: | ||
| // Convert array of interfaces to string array | ||
| for _, topic := range topics { | ||
| if topicStr, ok := topic.(string); ok { | ||
| topicStrings = append(topicStrings, topicStr) | ||
| } | ||
| } | ||
| shouldUpdate = true | ||
| log.Printf("Found []interface{} topics for project %s\n", id) | ||
|
|
||
| case primitive.A: | ||
| // Handle MongoDB primitive array type | ||
| for _, topic := range topics { | ||
| if topicStr, ok := topic.(string); ok { | ||
| topicStrings = append(topicStrings, topicStr) | ||
| } | ||
| } | ||
| shouldUpdate = true | ||
| log.Printf("Found primitive.A topics for project %s\n", id) | ||
|
|
||
| case string: | ||
| // Already a string, skip | ||
| log.Printf("Topics already string format for project %s\n", id) | ||
|
|
||
| default: | ||
| log.Printf("Unexpected topics format for project %s: %T\n", id, topics) | ||
| } | ||
|
|
||
| if shouldUpdate { | ||
| // Join topics with comma and space | ||
| topicsString := strings.Join(topicStrings, ", ") | ||
|
|
||
| // Update the metadata record | ||
| updateFilter := bson.M{"project": id} | ||
| updateFields := bson.M{ | ||
| "$set": bson.M{ | ||
| "topics": topicsString, | ||
| }, | ||
| } | ||
|
|
||
| if _, err := metadataCol.Client().UpdateOne(ctx, updateFilter, updateFields); err != nil { | ||
| log.Printf("Failed to update topics for project metadata %s: %v\n", id, err) | ||
| continue | ||
| } | ||
|
|
||
| log.Printf("Converted topics from array to string for project %s: %s\n", id, topicsString) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| }) | ||
| } |
78 changes: 78 additions & 0 deletions
78
...er/internal/infrastructure/mongo/migration/251022100100_update_project_metadata_fields.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package migration | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| "github.com/reearth/reearthx/mongox" | ||
| "go.mongodb.org/mongo-driver/bson" | ||
| ) | ||
|
|
||
| func UpdateProjectMetadataFields(ctx context.Context, c DBClient) error { | ||
| projectCol := c.WithCollection("project") | ||
| metadataCol := c.WithCollection("projectmetadata") | ||
|
|
||
| return projectCol.Find(ctx, bson.M{}, &mongox.BatchConsumer{ | ||
| Size: 1000, | ||
| Callback: func(rows []bson.Raw) error { | ||
| log.Printf("Processing batch of %d projects\n", len(rows)) | ||
|
|
||
| ids := make([]string, 0, len(rows)) | ||
| newRows := make([]interface{}, 0, len(rows)) | ||
|
|
||
| for _, row := range rows { | ||
| var project map[string]interface{} | ||
| if err := bson.Unmarshal(row, &project); err != nil { | ||
| log.Printf("Error unmarshaling project: %v\n", err) | ||
| continue | ||
| } | ||
|
|
||
| id, ok := project["id"].(string) | ||
| if !ok { | ||
| log.Printf("Skipping project with missing or invalid id\n") | ||
| continue | ||
| } | ||
|
|
||
| // Remove unwanted fields if they exist in the project collection | ||
| for _, field := range []string{"topics", "star_count", "starred_by", "created_at"} { | ||
| if _, exists := project[field]; exists { | ||
| delete(project, field) | ||
| log.Printf("Removed field '%s' from project %s\n", field, id) | ||
| } | ||
| } | ||
|
|
||
| // Check if projectmetadata exists for this project and update if found | ||
| var existingMetadata bson.Raw | ||
| err := metadataCol.Client().FindOne(ctx, bson.M{"project": id}).Decode(&existingMetadata) | ||
|
|
||
| if err == nil { | ||
| // Existing metadata found, update the fields | ||
| updateFields := bson.M{ | ||
| "$set": bson.M{ | ||
| "topics": []string{}, | ||
wilfredmulenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "star_count": 0, | ||
| "starred_by": []string{}, | ||
| }, | ||
| } | ||
|
|
||
| if _, updateErr := metadataCol.Client().UpdateOne(ctx, bson.M{"project": id}, updateFields); updateErr != nil { | ||
| log.Printf("Failed to update metadata for project %s: %v\n", id, updateErr) | ||
| continue | ||
| } | ||
| log.Printf("Updated existing metadata for project %s in projectmetadata collection\n", id) | ||
| } else { | ||
| log.Printf("No existing metadata found for project %s, skipping metadata update\n", id) | ||
| } | ||
|
|
||
| ids = append(ids, id) | ||
| newRows = append(newRows, project) | ||
| log.Printf("Processed project %s\n", id) | ||
| } | ||
|
|
||
| if len(newRows) > 0 { | ||
| return projectCol.SaveAll(ctx, ids, newRows) | ||
| } | ||
| return nil | ||
| }, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 31 additions & 29 deletions
60
server/internal/infrastructure/mongo/migration/migrations.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.