|
| 1 | +use sea_orm_migration::prelude::*; |
| 2 | + |
| 3 | +#[derive(DeriveMigrationName)] |
| 4 | +pub struct Migration; |
| 5 | + |
| 6 | +#[async_trait::async_trait] |
| 7 | +impl MigrationTrait for Migration { |
| 8 | + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { |
| 9 | + // Create OrionTasks |
| 10 | + manager |
| 11 | + .create_table( |
| 12 | + Table::create() |
| 13 | + .table(OrionTasks::Table) |
| 14 | + .if_not_exists() |
| 15 | + .col( |
| 16 | + ColumnDef::new(OrionTasks::Id) |
| 17 | + .uuid() |
| 18 | + .not_null() |
| 19 | + .primary_key(), |
| 20 | + ) |
| 21 | + .col(ColumnDef::new(OrionTasks::Changes).json_binary().not_null()) |
| 22 | + .col(ColumnDef::new(OrionTasks::RepoName).string().not_null()) |
| 23 | + .col(ColumnDef::new(OrionTasks::CL).string().not_null()) |
| 24 | + .col( |
| 25 | + ColumnDef::new(OrionTasks::CreatedAt) |
| 26 | + .timestamp_with_time_zone() |
| 27 | + .default(Expr::current_timestamp()) |
| 28 | + .not_null(), |
| 29 | + ) |
| 30 | + .to_owned(), |
| 31 | + ) |
| 32 | + .await?; |
| 33 | + |
| 34 | + // Create BuildEvents |
| 35 | + manager |
| 36 | + .create_table( |
| 37 | + Table::create() |
| 38 | + .table(BuildEvents::Table) |
| 39 | + .if_not_exists() |
| 40 | + .col( |
| 41 | + ColumnDef::new(BuildEvents::Id) |
| 42 | + .uuid() |
| 43 | + .not_null() |
| 44 | + .primary_key(), |
| 45 | + ) |
| 46 | + .col(ColumnDef::new(BuildEvents::Index).integer().not_null()) |
| 47 | + .col(ColumnDef::new(BuildEvents::TaskId).uuid().not_null()) |
| 48 | + .col(ColumnDef::new(BuildEvents::ExitCode).integer().null()) |
| 49 | + .col( |
| 50 | + ColumnDef::new(BuildEvents::LogOutputFile) |
| 51 | + .string() |
| 52 | + .not_null(), |
| 53 | + ) |
| 54 | + .col( |
| 55 | + ColumnDef::new(BuildEvents::StartAt) |
| 56 | + .timestamp_with_time_zone() |
| 57 | + .default(Expr::current_timestamp()), |
| 58 | + ) |
| 59 | + .col( |
| 60 | + ColumnDef::new(BuildEvents::EndAt) |
| 61 | + .timestamp_with_time_zone() |
| 62 | + .null(), |
| 63 | + ) |
| 64 | + .foreign_key( |
| 65 | + ForeignKey::create() |
| 66 | + .from(BuildEvents::Table, BuildEvents::TaskId) |
| 67 | + .to(OrionTasks::Table, OrionTasks::Id) |
| 68 | + .on_delete(ForeignKeyAction::Cascade) |
| 69 | + .on_update(ForeignKeyAction::Cascade), |
| 70 | + ) |
| 71 | + .to_owned(), |
| 72 | + ) |
| 73 | + .await?; |
| 74 | + |
| 75 | + // Create BuildTargets |
| 76 | + manager |
| 77 | + .create_table( |
| 78 | + Table::create() |
| 79 | + .table(BuildTargets::Table) |
| 80 | + .if_not_exists() |
| 81 | + .col( |
| 82 | + ColumnDef::new(BuildTargets::Id) |
| 83 | + .uuid() |
| 84 | + .not_null() |
| 85 | + .primary_key(), |
| 86 | + ) |
| 87 | + .col(ColumnDef::new(BuildTargets::TaskId).uuid().not_null()) |
| 88 | + .col(ColumnDef::new(BuildTargets::Path).json_binary().not_null()) |
| 89 | + .col(ColumnDef::new(BuildTargets::TargetState).text().not_null()) |
| 90 | + .foreign_key( |
| 91 | + ForeignKey::create() |
| 92 | + .from(BuildTargets::Table, BuildTargets::TaskId) |
| 93 | + .to(OrionTasks::Table, OrionTasks::Id) |
| 94 | + .on_delete(ForeignKeyAction::Cascade) |
| 95 | + .on_update(ForeignKeyAction::Cascade), |
| 96 | + ) |
| 97 | + .to_owned(), |
| 98 | + ) |
| 99 | + .await?; |
| 100 | + |
| 101 | + // Create indexes on foreign key columns for better join performance |
| 102 | + manager |
| 103 | + .create_index( |
| 104 | + Index::create() |
| 105 | + .name("idx_build_events_task_id") |
| 106 | + .table(BuildEvents::Table) |
| 107 | + .col(BuildEvents::TaskId) |
| 108 | + .to_owned(), |
| 109 | + ) |
| 110 | + .await?; |
| 111 | + manager |
| 112 | + .create_index( |
| 113 | + Index::create() |
| 114 | + .name("idx_build_targets_task_id") |
| 115 | + .table(BuildTargets::Table) |
| 116 | + .col(BuildTargets::TaskId) |
| 117 | + .to_owned(), |
| 118 | + ) |
| 119 | + .await?; |
| 120 | + |
| 121 | + Ok(()) |
| 122 | + } |
| 123 | + |
| 124 | + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { |
| 125 | + // Drop indexes created in `up` |
| 126 | + manager |
| 127 | + .drop_index( |
| 128 | + Index::drop() |
| 129 | + .name("idx_build_targets_task_id") |
| 130 | + .table(BuildTargets::Table) |
| 131 | + .to_owned(), |
| 132 | + ) |
| 133 | + .await?; |
| 134 | + manager |
| 135 | + .drop_index( |
| 136 | + Index::drop() |
| 137 | + .name("idx_build_events_task_id") |
| 138 | + .table(BuildEvents::Table) |
| 139 | + .to_owned(), |
| 140 | + ) |
| 141 | + .await?; |
| 142 | + // Drop tables created in `up` (children before parent) |
| 143 | + manager |
| 144 | + .drop_table(Table::drop().table(BuildTargets::Table).to_owned()) |
| 145 | + .await?; |
| 146 | + manager |
| 147 | + .drop_table(Table::drop().table(BuildEvents::Table).to_owned()) |
| 148 | + .await?; |
| 149 | + manager |
| 150 | + .drop_table(Table::drop().table(OrionTasks::Table).to_owned()) |
| 151 | + .await?; |
| 152 | + Ok(()) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +#[derive(DeriveIden)] |
| 157 | +enum OrionTasks { |
| 158 | + Table, |
| 159 | + Id, |
| 160 | + Changes, |
| 161 | + RepoName, |
| 162 | + CL, |
| 163 | + CreatedAt, |
| 164 | +} |
| 165 | + |
| 166 | +#[derive(DeriveIden)] |
| 167 | +enum BuildEvents { |
| 168 | + Table, |
| 169 | + Index, |
| 170 | + Id, |
| 171 | + TaskId, |
| 172 | + ExitCode, |
| 173 | + LogOutputFile, |
| 174 | + StartAt, |
| 175 | + EndAt, |
| 176 | +} |
| 177 | + |
| 178 | +#[derive(DeriveIden)] |
| 179 | +enum BuildTargets { |
| 180 | + Table, |
| 181 | + Id, |
| 182 | + TaskId, |
| 183 | + Path, |
| 184 | + TargetState, |
| 185 | +} |
0 commit comments