-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rsc: Create tables for tracking Blobs
- Loading branch information
Showing
9 changed files
with
300 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6 | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "blob")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: String, | ||
pub store: i32, | ||
pub workspace_path: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "super::blob_store::Entity", | ||
from = "Column::Store", | ||
to = "super::blob_store::Column::Id", | ||
on_update = "NoAction", | ||
on_delete = "Restrict" | ||
)] | ||
BlobStore, | ||
#[sea_orm(has_many = "super::job_blob::Entity")] | ||
JobBlob, | ||
} | ||
|
||
impl Related<super::blob_store::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::BlobStore.def() | ||
} | ||
} | ||
|
||
impl Related<super::job_blob::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::JobBlob.def() | ||
} | ||
} | ||
|
||
impl Related<super::job::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
super::job_blob::Relation::Job.def() | ||
} | ||
fn via() -> Option<RelationDef> { | ||
Some(super::job_blob::Relation::Blob.def().rev()) | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
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,25 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6 | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "blob_store")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
pub name: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm(has_many = "super::blob::Entity")] | ||
Blob, | ||
} | ||
|
||
impl Related<super::blob::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Blob.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6 | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "job_blob")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub job_id: i32, | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub blob_id: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "super::blob::Entity", | ||
from = "Column::BlobId", | ||
to = "super::blob::Column::Id", | ||
on_update = "NoAction", | ||
on_delete = "Restrict" | ||
)] | ||
Blob, | ||
#[sea_orm( | ||
belongs_to = "super::job::Entity", | ||
from = "Column::JobId", | ||
to = "super::job::Column::Id", | ||
on_update = "NoAction", | ||
on_delete = "Cascade" | ||
)] | ||
Job, | ||
} | ||
|
||
impl Related<super::blob::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Blob.def() | ||
} | ||
} | ||
|
||
impl Related<super::job::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Job.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
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
154 changes: 154 additions & 0 deletions
154
rust/migration/src/m20231213_194935_create_blob_table.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,154 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.create_table( | ||
Table::create() | ||
.table(BlobStore::Table) | ||
.col( | ||
ColumnDef::new(BlobStore::Id) | ||
.integer() | ||
.not_null() | ||
.auto_increment() | ||
.primary_key(), | ||
) | ||
.col(ColumnDef::new(BlobStore::Name).string().not_null()) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
// Insert basic backing stores since they must always exist. | ||
let insert = Query::insert() | ||
.into_table(BlobStore::Table) | ||
.columns([BlobStore::Id, BlobStore::Name]) | ||
.values_panic([1.into(), "local".into()]) | ||
.values_panic([2.into(), "artifactory".into()]) | ||
.values_panic([3.into(), "aws".into()]) | ||
.values_panic([4.into(), "gcs".into()]) | ||
.values_panic([5.into(), "azure".into()]) | ||
.to_owned(); | ||
|
||
manager.exec_stmt(insert).await?; | ||
|
||
manager | ||
.create_table( | ||
Table::create() | ||
.table(Blob::Table) | ||
.col( | ||
ColumnDef::new(Blob::Id) | ||
.string() | ||
.not_null() | ||
.primary_key(), | ||
) | ||
.col(ColumnDef::new(Blob::Store).integer().not_null()) | ||
.col(ColumnDef::new(Blob::WorkspacePath).string().not_null()) | ||
.foreign_key( | ||
ForeignKeyCreateStatement::new() | ||
.name("fk-store-blob_store") | ||
.from_tbl(Blob::Table) | ||
.from_col(Blob::Store) | ||
.to_tbl(BlobStore::Table) | ||
.to_col(BlobStore::Id) | ||
// All blobs for a given store must be manually | ||
// deleted before the blob store may be deleted. | ||
.on_delete(ForeignKeyAction::Restrict), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
manager | ||
.create_table( | ||
Table::create() | ||
.table(JobBlob::Table) | ||
.col( | ||
ColumnDef::new(JobBlob::JobId) | ||
.integer() | ||
.not_null(), | ||
) | ||
.col( | ||
ColumnDef::new(JobBlob::BlobId) | ||
.string() | ||
.not_null(), | ||
) | ||
.foreign_key( | ||
ForeignKeyCreateStatement::new() | ||
.name("fk-job_id-job") | ||
.from_tbl(JobBlob::Table) | ||
.from_col(JobBlob::JobId) | ||
.to_tbl(Job::Table) | ||
.to_col(Job::Id) | ||
// If the associated job is deleted (likely via eviction) | ||
// then the reference to the blob should be removed so the | ||
// blob may later be cleaned up. | ||
.on_delete(ForeignKeyAction::Cascade), | ||
) | ||
.foreign_key( | ||
ForeignKeyCreateStatement::new() | ||
.name("fk-blob_id-blob") | ||
.from_tbl(JobBlob::Table) | ||
.from_col(JobBlob::BlobId) | ||
.to_tbl(Blob::Table) | ||
.to_col(Blob::Id) | ||
// A blob must not be deleted if it is still referenced by | ||
// a job. Restrict such deletions. | ||
.on_delete(ForeignKeyAction::Restrict), | ||
) | ||
.primary_key(Index::create().col(JobBlob::JobId).col(JobBlob::BlobId)) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_table(Table::drop().table(JobBlob::Table).to_owned()) | ||
.await?; | ||
|
||
manager | ||
.drop_table(Table::drop().table(Blob::Table).to_owned()) | ||
.await?; | ||
|
||
manager | ||
.drop_table(Table::drop().table(BlobStore::Table).to_owned()) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum BlobStore { | ||
Table, | ||
Id, | ||
Name, | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Blob { | ||
Table, | ||
Id, | ||
Store, | ||
WorkspacePath, | ||
// Job relation | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Job { | ||
Table, | ||
Id, | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum JobBlob { | ||
Table, | ||
JobId, | ||
BlobId, | ||
} |
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