|
| 1 | +use sea_orm::DatabaseBackend; |
| 2 | +use sea_orm_migration::prelude::*; |
| 3 | + |
| 4 | +#[derive(DeriveMigrationName)] |
| 5 | +pub struct Migration; |
| 6 | + |
| 7 | +#[async_trait::async_trait] |
| 8 | +impl MigrationTrait for Migration { |
| 9 | + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { |
| 10 | + manager |
| 11 | + .create_table( |
| 12 | + Table::create() |
| 13 | + .table(DynamicSidebar::Table) |
| 14 | + .if_not_exists() |
| 15 | + .col( |
| 16 | + ColumnDef::new(DynamicSidebar::Id) |
| 17 | + .integer() |
| 18 | + .not_null() |
| 19 | + .auto_increment() |
| 20 | + .primary_key(), |
| 21 | + ) |
| 22 | + .col(ColumnDef::new(DynamicSidebar::PublicId).string().not_null()) |
| 23 | + .col(ColumnDef::new(DynamicSidebar::Label).string().not_null()) |
| 24 | + .col(ColumnDef::new(DynamicSidebar::Href).string().not_null()) |
| 25 | + .col(ColumnDef::new(DynamicSidebar::Visible).boolean().not_null()) |
| 26 | + .col( |
| 27 | + ColumnDef::new(DynamicSidebar::OrderIndex) |
| 28 | + .integer() |
| 29 | + .not_null(), |
| 30 | + ) |
| 31 | + .to_owned(), |
| 32 | + ) |
| 33 | + .await?; |
| 34 | + |
| 35 | + manager |
| 36 | + .create_index( |
| 37 | + Index::create() |
| 38 | + .unique() |
| 39 | + .name("idx_dynamic_sidebar_public_id_unique") |
| 40 | + .table(DynamicSidebar::Table) |
| 41 | + .col(DynamicSidebar::PublicId) |
| 42 | + .to_owned(), |
| 43 | + ) |
| 44 | + .await?; |
| 45 | + |
| 46 | + manager |
| 47 | + .create_index( |
| 48 | + Index::create() |
| 49 | + .unique() |
| 50 | + .name("idx_dynamic_sidebar_order_index") |
| 51 | + .table(DynamicSidebar::Table) |
| 52 | + .col(DynamicSidebar::OrderIndex) |
| 53 | + .to_owned(), |
| 54 | + ) |
| 55 | + .await?; |
| 56 | + |
| 57 | + let backend = manager.get_database_backend(); |
| 58 | + |
| 59 | + match backend { |
| 60 | + DatabaseBackend::Postgres => { |
| 61 | + let sql = r#" |
| 62 | + INSERT INTO dynamic_sidebar (public_id, label, href, visible, order_index) VALUES |
| 63 | + ('home', 'Home', '/posts', true, 0), |
| 64 | + ('inbox', 'Inbox', '/inbox', true, 1), |
| 65 | + ('chat', 'Chat', '/chat', true, 2), |
| 66 | + ('notes', 'Docs', '/notes', true, 3), |
| 67 | + ('calls', 'Calls', '/calls', true, 4), |
| 68 | + ('drafts', 'Drafts', '/drafts', true, 5), |
| 69 | + ('code', 'Code', '/code', true, 6), |
| 70 | + ('tags', 'Tags', '/code/tags', true, 7), |
| 71 | + ('cl', 'Change List', '/cl', true, 8), |
| 72 | + ('mq', 'Merge Queue', '/queue/main', true, 9), |
| 73 | + ('issue', 'Issue', '/issue', true, 10), |
| 74 | + ('rust', 'Rust', '/rust', true, 11); |
| 75 | + "#; |
| 76 | + |
| 77 | + manager.get_connection().execute_unprepared(sql).await?; |
| 78 | + } |
| 79 | + DatabaseBackend::Sqlite | DatabaseBackend::MySql => {} |
| 80 | + } |
| 81 | + |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | + |
| 85 | + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { |
| 86 | + manager |
| 87 | + .drop_table(Table::drop().table(DynamicSidebar::Table).to_owned()) |
| 88 | + .await |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(DeriveIden)] |
| 93 | +enum DynamicSidebar { |
| 94 | + Table, |
| 95 | + Id, |
| 96 | + PublicId, |
| 97 | + Label, |
| 98 | + Href, |
| 99 | + Visible, |
| 100 | + OrderIndex, |
| 101 | +} |
0 commit comments