-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(meta): support time travel query on a per-table basis
- Loading branch information
Showing
13 changed files
with
304 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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
200 changes: 200 additions & 0 deletions
200
src/meta/model_v2/migration/src/m20240820_081248_add_time_travel_per_table_epoch.rs
This file contains 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,200 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
const TABLE_NAME: &str = "hummock_epoch_to_version"; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
// modify PK | ||
match manager.get_database_backend() { | ||
sea_orm::DatabaseBackend::MySql => { | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(HummockEpochToVersion::Table) | ||
.add_column( | ||
ColumnDef::new(HummockEpochToVersion::TableId).big_integer(), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
manager | ||
.get_connection() | ||
.execute(sea_orm::Statement::from_string( | ||
sea_orm::DatabaseBackend::MySql, | ||
format!( | ||
"ALTER TABLE {TABLE_NAME} | ||
DROP PRIMARY KEY, | ||
ADD PRIMARY KEY (epoch, table_id)" | ||
), | ||
)) | ||
.await?; | ||
} | ||
sea_orm::DatabaseBackend::Postgres => { | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(HummockEpochToVersion::Table) | ||
.add_column( | ||
ColumnDef::new(HummockEpochToVersion::TableId).big_integer(), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
manager | ||
.get_connection() | ||
.execute(sea_orm::Statement::from_string( | ||
sea_orm::DatabaseBackend::Postgres, | ||
format!("ALTER TABLE {TABLE_NAME} DROP CONSTRAINT {TABLE_NAME}_pkey"), | ||
)) | ||
.await?; | ||
manager | ||
.get_connection() | ||
.execute(sea_orm::Statement::from_string( | ||
sea_orm::DatabaseBackend::Postgres, | ||
format!("ALTER TABLE {TABLE_NAME} ADD PRIMARY KEY (epoch, table_id)"), | ||
)) | ||
.await?; | ||
} | ||
sea_orm::DatabaseBackend::Sqlite => { | ||
// sqlite is not for prod usage, so recreating the table is fine. | ||
manager | ||
.drop_table( | ||
sea_orm_migration::prelude::Table::drop() | ||
.table(HummockEpochToVersion::Table) | ||
.if_exists() | ||
.cascade() | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
manager | ||
.create_table( | ||
Table::create() | ||
.table(HummockEpochToVersion::Table) | ||
.if_not_exists() | ||
.col( | ||
ColumnDef::new(HummockEpochToVersion::Epoch) | ||
.big_integer() | ||
.not_null() | ||
.primary_key(), | ||
) | ||
.col( | ||
ColumnDef::new(HummockEpochToVersion::TableId) | ||
.big_integer() | ||
.not_null() | ||
.primary_key(), | ||
) | ||
.col( | ||
ColumnDef::new(HummockEpochToVersion::VersionId) | ||
.big_integer() | ||
.not_null(), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
// The downgrade for MySql and Postgres may not work due to PK confliction. | ||
match manager.get_database_backend() { | ||
sea_orm::DatabaseBackend::MySql => { | ||
manager | ||
.get_connection() | ||
.execute(sea_orm::Statement::from_string( | ||
sea_orm::DatabaseBackend::MySql, | ||
format!("ALTER TABLE {TABLE_NAME} DROP PRIMARY KEY"), | ||
)) | ||
.await?; | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(HummockEpochToVersion::Table) | ||
.drop_column(HummockEpochToVersion::TableId) | ||
.to_owned(), | ||
) | ||
.await?; | ||
manager | ||
.get_connection() | ||
.execute(sea_orm::Statement::from_string( | ||
sea_orm::DatabaseBackend::MySql, | ||
format!("ALTER TABLE {TABLE_NAME} ADD PRIMARY KEY (epoch)"), | ||
)) | ||
.await?; | ||
} | ||
sea_orm::DatabaseBackend::Postgres => { | ||
manager | ||
.get_connection() | ||
.execute(sea_orm::Statement::from_string( | ||
sea_orm::DatabaseBackend::Postgres, | ||
format!("ALTER TABLE {TABLE_NAME} DROP CONSTRAINT {TABLE_NAME}_pkey"), | ||
)) | ||
.await?; | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(HummockEpochToVersion::Table) | ||
.drop_column(HummockEpochToVersion::TableId) | ||
.to_owned(), | ||
) | ||
.await?; | ||
manager | ||
.get_connection() | ||
.execute(sea_orm::Statement::from_string( | ||
sea_orm::DatabaseBackend::Postgres, | ||
format!( | ||
"ALTER TABLE {TABLE_NAME} ADD PRIMARY KEY (epoch)" | ||
), | ||
)) | ||
.await?; | ||
} | ||
sea_orm::DatabaseBackend::Sqlite => { | ||
manager | ||
.drop_table( | ||
sea_orm_migration::prelude::Table::drop() | ||
.table(HummockEpochToVersion::Table) | ||
.if_exists() | ||
.cascade() | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
manager | ||
.create_table( | ||
Table::create() | ||
.table(HummockEpochToVersion::Table) | ||
.if_not_exists() | ||
.col( | ||
ColumnDef::new(HummockEpochToVersion::Epoch) | ||
.big_integer() | ||
.not_null() | ||
.primary_key(), | ||
) | ||
.col( | ||
ColumnDef::new(HummockEpochToVersion::VersionId) | ||
.big_integer() | ||
.not_null(), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum HummockEpochToVersion { | ||
Table, | ||
Epoch, | ||
TableId, | ||
VersionId, | ||
} |
This file contains 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 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 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.