-
-
Notifications
You must be signed in to change notification settings - Fork 62
feat(eap-items): add version column to eap_items_1 #8424
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
+95
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
95 changes: 95 additions & 0 deletions
95
snuba/snuba_migrations/events_analytics_platform/0064_add_version_column.py
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,95 @@ | ||
| from snuba.clickhouse.columns import Column, UInt | ||
| from snuba.clusters.storage_sets import StorageSetKey | ||
| from snuba.migrations import migration, operations | ||
| from snuba.migrations.columns import MigrationModifiers as Modifiers | ||
| from snuba.migrations.operations import OperationTarget | ||
|
|
||
| storage_set = StorageSetKey.EVENTS_ANALYTICS_PLATFORM | ||
| table_name_prefix = "eap_items_1" | ||
|
|
||
| new_column_name = "version" | ||
|
|
||
| # The local and distributed tables deliberately carry *different* DEFAULT | ||
| # expressions. This is not an oversight; see below. | ||
|
|
||
| # ADD COLUMN does not rewrite existing parts, so every row written before this | ||
| # migration has no `version` on disk and the local DEFAULT is what gets | ||
| # evaluated when those rows are read or merged. Once eap_items moves to | ||
| # ReplacingMergeTree(version), a non-deterministic local default such as | ||
| # now64() would hand those legacy rows a version of "whenever the merge | ||
| # happened", which is always newer than a genuine write -- so a stale legacy row | ||
| # would silently win over a real update. `0` is therefore the sentinel for | ||
| # "written before versioning existed", and it deterministically loses to | ||
| # everything else. | ||
| local_column: Column[Modifiers] = Column( | ||
| new_column_name, | ||
| UInt( | ||
| 64, | ||
| modifiers=Modifiers( | ||
| default="0", | ||
| codecs=["ZSTD(1)"], | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
| # Inserts arrive through the distributed table, and a Distributed table | ||
| # evaluates its own DEFAULT expressions on the initiator node and then ships the | ||
| # materialized value to the shard -- the local DEFAULT never fires for that | ||
| # path. (Declaring the column here *without* a default would not fall through to | ||
| # the local default either; it would ship an explicit 0.) So this is the default | ||
| # that actually assigns a version to live writes, until the consumer starts | ||
| # populating the column explicitly. | ||
| dist_column: Column[Modifiers] = Column( | ||
| new_column_name, | ||
| UInt( | ||
| 64, | ||
| modifiers=Modifiers( | ||
| default="toUnixTimestamp64Milli(now64(3))", | ||
| codecs=["ZSTD(1)"], | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
| # `version` is intentionally not added to eap_items_1_dist_ro. It is internal | ||
| # bookkeeping for the ReplacingMergeTree cutover and is absent from the storage | ||
| # YAML, so nothing the query layer generates can reference it. Migration 0056 | ||
| # creates the _dist_ro tables before this migration runs, so fresh and existing | ||
| # deployments both end up without the column there -- no environment drift. | ||
| # If we ever want to read `version` through the read-only routing path, it | ||
| # should be added in its own migration. | ||
|
|
||
|
|
||
| class Migration(migration.ClickhouseNodeMigration): | ||
| blocking = False | ||
|
|
||
| def forwards_ops(self) -> list[operations.SqlOperation]: | ||
| return [ | ||
| operations.AddColumn( | ||
| storage_set=storage_set, | ||
| table_name=f"{table_name_prefix}_local", | ||
| column=local_column, | ||
| target=OperationTarget.LOCAL, | ||
| ), | ||
| operations.AddColumn( | ||
| storage_set=storage_set, | ||
| table_name=f"{table_name_prefix}_dist", | ||
| column=dist_column, | ||
| target=OperationTarget.DISTRIBUTED, | ||
| ), | ||
| ] | ||
|
|
||
| def backwards_ops(self) -> list[operations.SqlOperation]: | ||
| return [ | ||
| operations.DropColumn( | ||
| storage_set=storage_set, | ||
| table_name=f"{table_name_prefix}_dist", | ||
| column_name=new_column_name, | ||
| target=OperationTarget.DISTRIBUTED, | ||
| ), | ||
| operations.DropColumn( | ||
| storage_set=storage_set, | ||
| table_name=f"{table_name_prefix}_local", | ||
| column_name=new_column_name, | ||
| target=OperationTarget.LOCAL, | ||
| ), | ||
| ] | ||
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.