-
Notifications
You must be signed in to change notification settings - Fork 6
refactor(metadata-db): decouple physical tables from storage paths #1696
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
shiyasmohd
merged 3 commits into
main
from
shiyasmohd/decouple-phy-tables-in-metadata-db
Feb 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e4e678d
refactor(metadata-db): decouple physical tables from storage paths
shiyasmohd 8a16ba1
docs(data-store): update docs with physical table revisions architecture
shiyasmohd b0de304
refactor(metadata-db): rename mark_inactive_by_table_id to mark_inact…
shiyasmohd 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,3 +19,4 @@ build/ | |
| .idea | ||
| .vscode/ | ||
| .amp/ | ||
| .agents/settings.local.json | ||
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
141 changes: 141 additions & 0 deletions
141
crates/core/metadata-db/migrations/20260204061026_split_physical_tables.sql
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,141 @@ | ||
| -- Migration: Split physical_tables into physical_tables (meta) and physical_table_revisions | ||
| -- | ||
| -- This migration decouples table identity from storage paths, enabling: | ||
| -- - Data sharing across datasets with same spec | ||
| -- - External data import (link any directory to any table) | ||
| -- | ||
| -- Changes: | ||
| -- 1. Create physical_table_revisions table (preserves existing IDs for FK compatibility) | ||
| -- 2. Update file_metadata and gc_manifest FKs to point to revisions | ||
| -- 3. Transform physical_tables into a meta table with active_revision_id pointer | ||
| -- 4. Populate relationships between tables | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 1: Create physical_table_revisions table | ||
| -- ============================================================================= | ||
| -- Using GENERATED BY DEFAULT to allow explicit ID insertion during migration | ||
| -- This preserves location_id values that file_metadata and gc_manifest reference | ||
|
|
||
| CREATE TABLE physical_table_revisions ( | ||
| id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
| created_at TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'utc'), | ||
| updated_at TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'utc'), | ||
| path TEXT NOT NULL UNIQUE, | ||
| writer BIGINT REFERENCES jobs(id) ON DELETE SET NULL, | ||
| metadata JSONB NOT NULL DEFAULT ('{}'::jsonb) | ||
| ); | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 2: Copy revision data with existing IDs | ||
| -- ============================================================================= | ||
| -- Critical: This preserves the location_id values that file_metadata references | ||
|
|
||
| INSERT INTO physical_table_revisions (id, created_at, updated_at, path, writer) | ||
| SELECT id, created_at, created_at AS updated_at, path, writer | ||
| FROM physical_tables; | ||
|
|
||
| -- Reset the identity sequence to continue from max ID (only if table has rows) | ||
| DO $$ | ||
| BEGIN | ||
| IF EXISTS (SELECT 1 FROM physical_table_revisions LIMIT 1) THEN | ||
| PERFORM setval( | ||
| pg_get_serial_sequence('physical_table_revisions', 'id'), | ||
| (SELECT MAX(id) FROM physical_table_revisions) | ||
| ); | ||
| END IF; | ||
| END $$; | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 3: Update file_metadata FK to point to physical_table_revisions | ||
| -- ============================================================================= | ||
|
|
||
| ALTER TABLE file_metadata | ||
| DROP CONSTRAINT IF EXISTS file_metadata_location_id_fkey; | ||
|
|
||
| ALTER TABLE file_metadata | ||
| ADD CONSTRAINT file_metadata_location_id_fkey | ||
| FOREIGN KEY (location_id) | ||
| REFERENCES physical_table_revisions(id) | ||
| ON DELETE CASCADE; | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 4: Update gc_manifest FK to point to physical_table_revisions | ||
| -- ============================================================================= | ||
|
|
||
| ALTER TABLE gc_manifest | ||
| DROP CONSTRAINT IF EXISTS gc_manifest_location_id_fkey; | ||
|
|
||
| ALTER TABLE gc_manifest | ||
| ADD CONSTRAINT gc_manifest_location_id_fkey | ||
| FOREIGN KEY (location_id) | ||
| REFERENCES physical_table_revisions(id) | ||
| ON DELETE CASCADE; | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 5: Rename old table and create new physical_tables (meta) | ||
| -- ============================================================================= | ||
|
|
||
| ALTER TABLE physical_tables RENAME TO physical_tables_old; | ||
|
|
||
| CREATE TABLE physical_tables ( | ||
| id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, | ||
| created_at TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'utc'), | ||
| updated_at TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'utc'), | ||
| dataset_namespace TEXT NOT NULL, | ||
| dataset_name TEXT NOT NULL, | ||
| table_name TEXT NOT NULL, | ||
| manifest_hash TEXT NOT NULL, | ||
| active_revision_id BIGINT REFERENCES physical_table_revisions(id) ON DELETE SET NULL | ||
| ); | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 6: Populate physical_tables (meta) from old data | ||
| -- ============================================================================= | ||
| -- Insert one row per unique (dataset_namespace, dataset_name, table_name, manifest_hash) combination | ||
| -- Set active_revision_id to the revision that was marked active (if any) | ||
|
|
||
| INSERT INTO physical_tables ( | ||
| created_at, | ||
| updated_at, | ||
| dataset_namespace, | ||
| dataset_name, | ||
| table_name, | ||
| manifest_hash, | ||
| active_revision_id | ||
| ) | ||
| SELECT | ||
| MIN(created_at) AS created_at, | ||
| MIN(created_at) AS updated_at, | ||
| dataset_namespace, | ||
| dataset_name, | ||
| table_name, | ||
| manifest_hash, | ||
| (SELECT id FROM physical_tables_old pt2 | ||
| WHERE pt2.manifest_hash = physical_tables_old.manifest_hash | ||
| AND pt2.table_name = physical_tables_old.table_name | ||
| AND pt2.active = true | ||
| LIMIT 1) AS active_revision_id | ||
| FROM physical_tables_old | ||
| GROUP BY dataset_namespace, dataset_name, table_name, manifest_hash; | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 7: Create indexes | ||
| -- ============================================================================= | ||
|
|
||
| -- Unique constraint: one physical_table per (dataset_namespace, dataset_name, manifest_hash, table_name) | ||
| CREATE UNIQUE INDEX unique_manifest_table | ||
| ON physical_tables (dataset_namespace, dataset_name, manifest_hash, table_name); | ||
|
|
||
| -- Index for active_revision_id | ||
| CREATE INDEX idx_physical_tables_active_revision_id | ||
| ON physical_tables (active_revision_id) WHERE active_revision_id IS NOT NULL; | ||
|
|
||
| -- ============================================================================= | ||
| -- STEP 8: Cleanup | ||
| -- ============================================================================= | ||
|
|
||
| -- Drop the old partial unique index (no longer needed) | ||
| DROP INDEX IF EXISTS unique_active_per_manifest_table; | ||
|
|
||
| -- Drop the old table | ||
| DROP TABLE physical_tables_old; |
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
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.
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.