forked from fleetdm/fleet
-
Notifications
You must be signed in to change notification settings - Fork 1
feat(openframe): stamp team_id on CDC-captured tables for shared-DB Debezium #77
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
Merged
Merged
Changes from all commits
Commits
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
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
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
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
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
47 changes: 47 additions & 0 deletions
47
server/datastore/mysql/migrations/openframe/20260722000001_AddTeamIdToCdcTables.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,47 @@ | ||
| package openframe | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "fmt" | ||
| ) | ||
|
|
||
| func init() { | ||
| MigrationClient.AddMigration(Up_20260722000001, Down_20260722000001) | ||
| } | ||
|
|
||
| // Up_20260722000001 adds a nullable `team_id` column to the four tables captured by the | ||
| // OpenFrame Debezium CDC pipeline: activity_past, activity_host_past, query_results and | ||
| // policy_membership. Under shared-database multitenancy one MySQL serves every tenant, so a | ||
| // CDC record must carry its own tenant discriminator — the connector's SMTs are stateless and | ||
| // cannot join host_id → hosts.team_id, and activity_past rows have no host reference at all. | ||
| // The column is stamped at write time from the request's team pin (fleet.OpenframeTeamID) or, | ||
| // for writes without a pinned context (async policy membership, host activities), from the | ||
| // host's own team via a scalar subselect. | ||
| // | ||
| // Rows written with the multitenancy flag off (or by unpinned background jobs) keep team_id | ||
| // NULL — byte-identical to pre-migration behavior. No index: nothing queries these tables by | ||
| // team; the column exists solely so the change-stream consumer can resolve the tenant. | ||
| // | ||
| // Intentionally NO foreign key to teams(id): a team deletion must not rewrite or cascade over | ||
| // millions of historical CDC rows. | ||
| // | ||
| // Idempotent. | ||
| func Up_20260722000001(tx *sql.Tx) error { | ||
| for _, table := range []string{"activity_past", "activity_host_past", "query_results", "policy_membership"} { | ||
| hasCol, err := columnExists(tx, table, "team_id") | ||
| if err != nil { | ||
| return fmt.Errorf("checking %s.team_id column: %w", table, err) | ||
| } | ||
| if hasCol { | ||
| continue | ||
| } | ||
| if _, err := tx.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN team_id INT UNSIGNED NULL", table)); err != nil { | ||
| return fmt.Errorf("adding team_id column to %s: %w", table, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func Down_20260722000001(tx *sql.Tx) error { | ||
| 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
Oops, something went wrong.
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.