diff options
| author | Tolmachev Igor <me@igorek.dev> | 2025-08-26 21:13:53 +0900 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2025-08-26 21:13:53 +0900 |
| commit | dc33fa8416ce6b447494c6efdf46518da37ac1cc (patch) | |
| tree | 6bf069f70c953f14b3a7e84ed4a7bbcde3de7012 /migration | |
| parent | 9cb5689c87978c4e05e87f631ebf92a626d583b0 (diff) | |
| download | queue_server-dc33fa8416ce6b447494c6efdf46518da37ac1cc.tar.gz queue_server-dc33fa8416ce6b447494c6efdf46518da37ac1cc.zip | |
Add database migration and entities
Diffstat (limited to 'migration')
| -rw-r--r-- | migration/Cargo.toml | 13 | ||||
| -rw-r--r-- | migration/src/lib.rs | 17 | ||||
| -rw-r--r-- | migration/src/m0_init_tables.rs | 192 | ||||
| -rw-r--r-- | migration/src/main.rs | 6 |
4 files changed, 228 insertions, 0 deletions
diff --git a/migration/Cargo.toml b/migration/Cargo.toml new file mode 100644 index 0000000..bb33242 --- /dev/null +++ b/migration/Cargo.toml | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | [package] | ||
| 2 | name = "migration" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2024" | ||
| 5 | publish = false | ||
| 6 | |||
| 7 | [lib] | ||
| 8 | name = "migration" | ||
| 9 | path = "src/lib.rs" | ||
| 10 | |||
| 11 | [dependencies] | ||
| 12 | sea-orm-migration = { version = "1.1.14", features = ["sqlx-postgres", "runtime-tokio-rustls"] } | ||
| 13 | tokio = { version = "1.47.1", features = ["macros", "rt"] } | ||
diff --git a/migration/src/lib.rs b/migration/src/lib.rs new file mode 100644 index 0000000..ef740fc --- /dev/null +++ b/migration/src/lib.rs | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #![deny(dead_code)] | ||
| 2 | mod m0_init_tables; | ||
| 3 | |||
| 4 | use sea_orm_migration::prelude::*; | ||
| 5 | |||
| 6 | pub struct Migrator; | ||
| 7 | |||
| 8 | #[async_trait::async_trait] | ||
| 9 | impl MigratorTrait for Migrator { | ||
| 10 | fn migration_table_name() -> DynIden { | ||
| 11 | Alias::new("migrations").into_iden() | ||
| 12 | } | ||
| 13 | |||
| 14 | fn migrations() -> Vec<Box<dyn MigrationTrait>> { | ||
| 15 | vec![Box::new(m0_init_tables::Migration)] | ||
| 16 | } | ||
| 17 | } | ||
diff --git a/migration/src/m0_init_tables.rs b/migration/src/m0_init_tables.rs new file mode 100644 index 0000000..576f45b --- /dev/null +++ b/migration/src/m0_init_tables.rs | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | use sea_orm_migration::prelude::extension::postgres::Type; | ||
| 2 | use sea_orm_migration::sea_orm::{EnumIter, Iterable}; | ||
| 3 | use sea_orm_migration::{prelude::*, schema::*}; | ||
| 4 | |||
| 5 | #[derive(DeriveIden)] | ||
| 6 | enum Users { | ||
| 7 | Table, | ||
| 8 | Id, | ||
| 9 | Login, | ||
| 10 | Password, | ||
| 11 | PasswordIssueDate, | ||
| 12 | FirstName, | ||
| 13 | LastName, | ||
| 14 | } | ||
| 15 | |||
| 16 | #[derive(DeriveIden)] | ||
| 17 | enum Queues { | ||
| 18 | Table, | ||
| 19 | Id, | ||
| 20 | OwnerId, | ||
| 21 | Name, | ||
| 22 | } | ||
| 23 | |||
| 24 | #[derive(DeriveIden)] | ||
| 25 | enum AccessToQueue { | ||
| 26 | Table, | ||
| 27 | UserId, | ||
| 28 | QueueId, | ||
| 29 | } | ||
| 30 | |||
| 31 | #[derive(DeriveIden)] | ||
| 32 | enum QueueElements { | ||
| 33 | Table, | ||
| 34 | Id, | ||
| 35 | QueueId, | ||
| 36 | UserId, | ||
| 37 | Position, | ||
| 38 | Status, | ||
| 39 | } | ||
| 40 | |||
| 41 | mod queue_element_status { | ||
| 42 | use super::*; | ||
| 43 | |||
| 44 | #[derive(DeriveIden)] | ||
| 45 | #[sea_orm(iden = "queue_element_status_enum")] | ||
| 46 | pub struct Iden; | ||
| 47 | |||
| 48 | #[derive(Iden, EnumIter)] | ||
| 49 | pub enum Items { | ||
| 50 | Passed, | ||
| 51 | Waiting, | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | #[derive(DeriveMigrationName)] | ||
| 56 | pub struct Migration; | ||
| 57 | |||
| 58 | #[async_trait::async_trait] | ||
| 59 | impl MigrationTrait for Migration { | ||
| 60 | async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| 61 | manager | ||
| 62 | .create_table( | ||
| 63 | Table::create() | ||
| 64 | .table(Users::Table) | ||
| 65 | .if_not_exists() | ||
| 66 | .col(pk_auto(Users::Id).big_integer()) | ||
| 67 | .col(string(Users::Login).unique_key()) | ||
| 68 | .col(string(Users::Password)) | ||
| 69 | .col(timestamp(Users::PasswordIssueDate)) | ||
| 70 | .col(string(Users::FirstName)) | ||
| 71 | .col(string(Users::LastName)) | ||
| 72 | .to_owned(), | ||
| 73 | ) | ||
| 74 | .await?; | ||
| 75 | |||
| 76 | manager | ||
| 77 | .create_table( | ||
| 78 | Table::create() | ||
| 79 | .table(Queues::Table) | ||
| 80 | .if_not_exists() | ||
| 81 | .col(pk_auto(Queues::Id).big_integer()) | ||
| 82 | .col(big_integer(Queues::OwnerId)) | ||
| 83 | .foreign_key( | ||
| 84 | ForeignKey::create() | ||
| 85 | .from(Queues::Table, Queues::OwnerId) | ||
| 86 | .to(Users::Table, Users::Id) | ||
| 87 | .on_delete(ForeignKeyAction::Cascade) | ||
| 88 | .on_update(ForeignKeyAction::Cascade), | ||
| 89 | ) | ||
| 90 | .col(string(Queues::Name)) | ||
| 91 | .to_owned(), | ||
| 92 | ) | ||
| 93 | .await?; | ||
| 94 | |||
| 95 | manager | ||
| 96 | .create_table( | ||
| 97 | Table::create() | ||
| 98 | .table(AccessToQueue::Table) | ||
| 99 | .if_not_exists() | ||
| 100 | .col(big_integer(AccessToQueue::UserId)) | ||
| 101 | .col(big_integer(AccessToQueue::QueueId)) | ||
| 102 | .foreign_key( | ||
| 103 | ForeignKey::create() | ||
| 104 | .from(AccessToQueue::Table, AccessToQueue::UserId) | ||
| 105 | .to(Users::Table, Users::Id) | ||
| 106 | .on_delete(ForeignKeyAction::Cascade) | ||
| 107 | .on_update(ForeignKeyAction::Cascade), | ||
| 108 | ) | ||
| 109 | .foreign_key( | ||
| 110 | ForeignKey::create() | ||
| 111 | .from(AccessToQueue::Table, AccessToQueue::QueueId) | ||
| 112 | .to(Queues::Table, Queues::Id) | ||
| 113 | .on_delete(ForeignKeyAction::Cascade) | ||
| 114 | .on_update(ForeignKeyAction::Cascade), | ||
| 115 | ) | ||
| 116 | .primary_key( | ||
| 117 | Index::create() | ||
| 118 | .col(AccessToQueue::UserId) | ||
| 119 | .col(AccessToQueue::QueueId), | ||
| 120 | ) | ||
| 121 | .to_owned(), | ||
| 122 | ) | ||
| 123 | .await?; | ||
| 124 | |||
| 125 | manager | ||
| 126 | .create_type( | ||
| 127 | Type::create() | ||
| 128 | .as_enum(queue_element_status::Iden) | ||
| 129 | .values(queue_element_status::Items::iter()) | ||
| 130 | .to_owned(), | ||
| 131 | ) | ||
| 132 | .await?; | ||
| 133 | |||
| 134 | manager | ||
| 135 | .create_table( | ||
| 136 | Table::create() | ||
| 137 | .table(QueueElements::Table) | ||
| 138 | .if_not_exists() | ||
| 139 | .col(pk_auto(QueueElements::Id).big_integer()) | ||
| 140 | .col(big_integer(QueueElements::QueueId)) | ||
| 141 | .col(big_integer(QueueElements::UserId)) | ||
| 142 | .col( | ||
| 143 | big_integer(QueueElements::Position) | ||
| 144 | .unique_key() | ||
| 145 | .auto_increment(), | ||
| 146 | ) | ||
| 147 | .col(enumeration( | ||
| 148 | QueueElements::Status, | ||
| 149 | queue_element_status::Iden, | ||
| 150 | queue_element_status::Items::iter(), | ||
| 151 | )) | ||
| 152 | .foreign_key( | ||
| 153 | ForeignKey::create() | ||
| 154 | .from(QueueElements::Table, QueueElements::UserId) | ||
| 155 | .to(Users::Table, Users::Id) | ||
| 156 | .on_delete(ForeignKeyAction::Cascade) | ||
| 157 | .on_update(ForeignKeyAction::Cascade), | ||
| 158 | ) | ||
| 159 | .foreign_key( | ||
| 160 | ForeignKey::create() | ||
| 161 | .from(QueueElements::Table, QueueElements::QueueId) | ||
| 162 | .to(Queues::Table, Queues::Id) | ||
| 163 | .on_delete(ForeignKeyAction::Cascade) | ||
| 164 | .on_update(ForeignKeyAction::Cascade), | ||
| 165 | ) | ||
| 166 | .to_owned(), | ||
| 167 | ) | ||
| 168 | .await?; | ||
| 169 | |||
| 170 | Ok(()) | ||
| 171 | } | ||
| 172 | |||
| 173 | async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| 174 | manager | ||
| 175 | .drop_table(Table::drop().table(Users::Table).to_owned()) | ||
| 176 | .await?; | ||
| 177 | |||
| 178 | manager | ||
| 179 | .drop_table(Table::drop().table(Queues::Table).to_owned()) | ||
| 180 | .await?; | ||
| 181 | |||
| 182 | manager | ||
| 183 | .drop_table(Table::drop().table(AccessToQueue::Table).to_owned()) | ||
| 184 | .await?; | ||
| 185 | |||
| 186 | manager | ||
| 187 | .drop_table(Table::drop().table(QueueElements::Table).to_owned()) | ||
| 188 | .await?; | ||
| 189 | |||
| 190 | Ok(()) | ||
| 191 | } | ||
| 192 | } | ||
diff --git a/migration/src/main.rs b/migration/src/main.rs new file mode 100644 index 0000000..56190ac --- /dev/null +++ b/migration/src/main.rs | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | use sea_orm_migration::prelude::*; | ||
| 2 | |||
| 3 | #[tokio::main(flavor = "current_thread")] | ||
| 4 | async fn main() { | ||
| 5 | cli::run_cli(migration::Migrator).await; | ||
| 6 | } | ||
