aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.zed/tasks.json32
-rw-r--r--Cargo.toml7
-rw-r--r--entity/Cargo.toml10
-rw-r--r--entity/src/access_to_queue.rs46
-rw-r--r--entity/src/lib.rs9
-rw-r--r--entity/src/prelude.rs6
-rw-r--r--entity/src/queue_elements.rs50
-rw-r--r--entity/src/queues.rs51
-rw-r--r--entity/src/sea_orm_active_enums.rs16
-rw-r--r--entity/src/users.rs49
-rw-r--r--migration/Cargo.toml13
-rw-r--r--migration/src/lib.rs17
-rw-r--r--migration/src/m0_init_tables.rs192
-rw-r--r--migration/src/main.rs6
14 files changed, 504 insertions, 0 deletions
diff --git a/.zed/tasks.json b/.zed/tasks.json
index 4648b2c..e725a43 100644
--- a/.zed/tasks.json
+++ b/.zed/tasks.json
@@ -1,5 +1,37 @@
1[ 1[
2 { 2 {
3 "label": "Run migrations",
4 "command": "sea-orm-cli",
5 "args": ["migrate", "up"],
6
7 "env": {
8 "DATABASE_URL": "postgres://itmo_queue:itmo_queue@localhost/itmo_queue"
9 },
10
11 "use_new_terminal": false,
12 "allow_concurrent_runs": false,
13 "reveal": "no_focus",
14 "reveal_target": "dock",
15 "hide": "never",
16 "shell": "system"
17 },
18 {
19 "label": "Generate entity files",
20 "command": "sea-orm-cli",
21 "args": ["generate", "entity", "--lib", "--ignore-tables", "migrations", "-o", "entity/src/"],
22
23 "env": {
24 "DATABASE_URL": "postgres://itmo_queue:itmo_queue@localhost/itmo_queue"
25 },
26
27 "use_new_terminal": false,
28 "allow_concurrent_runs": false,
29 "reveal": "no_focus",
30 "reveal_target": "dock",
31 "hide": "never",
32 "shell": "system"
33 },
34 {
3 "label": "Run release server", 35 "label": "Run release server",
4 "command": "cargo", 36 "command": "cargo",
5 "args": ["run", "-r"], 37 "args": ["run", "-r"],
diff --git a/Cargo.toml b/Cargo.toml
index bd321fd..9b59ed0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,9 +2,16 @@
2name = "itmo_queue_server" 2name = "itmo_queue_server"
3version = "0.1.0" 3version = "0.1.0"
4edition = "2024" 4edition = "2024"
5publish = false
6
7[workspace]
8members = [".", "entity", "migration"]
5 9
6[dependencies] 10[dependencies]
7axum = "0.8.4" 11axum = "0.8.4"
12entity = { version = "0.1.0", path = "entity" }
13migration = { version = "0.1.0", path = "migration" }
14sea-orm = { version = "1.1.14", features = ["sqlx-postgres", "runtime-tokio-rustls"] }
8serde = { version = "1.0.219", features = ["derive"] } 15serde = { version = "1.0.219", features = ["derive"] }
9serde_json = "1.0.143" 16serde_json = "1.0.143"
10tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread"] } 17tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread"] }
diff --git a/entity/Cargo.toml b/entity/Cargo.toml
new file mode 100644
index 0000000..d212311
--- /dev/null
+++ b/entity/Cargo.toml
@@ -0,0 +1,10 @@
1[package]
2name = "entity"
3version = "0.1.0"
4edition = "2024"
5publish = false
6
7
8[dependencies]
9sea-orm = "1.1.14"
10serde = { version = "1.0.219", features = ["derive"] }
diff --git a/entity/src/access_to_queue.rs b/entity/src/access_to_queue.rs
new file mode 100644
index 0000000..9de03d5
--- /dev/null
+++ b/entity/src/access_to_queue.rs
@@ -0,0 +1,46 @@
1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14
2
3use sea_orm::entity::prelude::*;
4
5#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6#[sea_orm(table_name = "access_to_queue")]
7pub struct Model {
8 #[sea_orm(primary_key, auto_increment = false)]
9 pub user_id: i64,
10 #[sea_orm(primary_key, auto_increment = false)]
11 pub queue_id: i64,
12}
13
14#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
15pub enum Relation {
16 #[sea_orm(
17 belongs_to = "super::queues::Entity",
18 from = "Column::QueueId",
19 to = "super::queues::Column::Id",
20 on_update = "Cascade",
21 on_delete = "Cascade"
22 )]
23 Queues,
24 #[sea_orm(
25 belongs_to = "super::users::Entity",
26 from = "Column::UserId",
27 to = "super::users::Column::Id",
28 on_update = "Cascade",
29 on_delete = "Cascade"
30 )]
31 Users,
32}
33
34impl Related<super::queues::Entity> for Entity {
35 fn to() -> RelationDef {
36 Relation::Queues.def()
37 }
38}
39
40impl Related<super::users::Entity> for Entity {
41 fn to() -> RelationDef {
42 Relation::Users.def()
43 }
44}
45
46impl ActiveModelBehavior for ActiveModel {}
diff --git a/entity/src/lib.rs b/entity/src/lib.rs
new file mode 100644
index 0000000..1ca05d6
--- /dev/null
+++ b/entity/src/lib.rs
@@ -0,0 +1,9 @@
1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14
2
3pub mod prelude;
4
5pub mod access_to_queue;
6pub mod queue_elements;
7pub mod queues;
8pub mod sea_orm_active_enums;
9pub mod users;
diff --git a/entity/src/prelude.rs b/entity/src/prelude.rs
new file mode 100644
index 0000000..60f72f9
--- /dev/null
+++ b/entity/src/prelude.rs
@@ -0,0 +1,6 @@
1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14
2
3pub use super::access_to_queue::Entity as AccessToQueue;
4pub use super::queue_elements::Entity as QueueElements;
5pub use super::queues::Entity as Queues;
6pub use super::users::Entity as Users;
diff --git a/entity/src/queue_elements.rs b/entity/src/queue_elements.rs
new file mode 100644
index 0000000..7002da1
--- /dev/null
+++ b/entity/src/queue_elements.rs
@@ -0,0 +1,50 @@
1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14
2
3use super::sea_orm_active_enums::QueueElementStatusEnum;
4use sea_orm::entity::prelude::*;
5
6#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
7#[sea_orm(table_name = "queue_elements")]
8pub struct Model {
9 #[sea_orm(primary_key)]
10 pub id: i64,
11 pub queue_id: i64,
12 pub user_id: i64,
13 #[sea_orm(unique)]
14 pub position: i64,
15 pub status: QueueElementStatusEnum,
16}
17
18#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
19pub enum Relation {
20 #[sea_orm(
21 belongs_to = "super::queues::Entity",
22 from = "Column::QueueId",
23 to = "super::queues::Column::Id",
24 on_update = "Cascade",
25 on_delete = "Cascade"
26 )]
27 Queues,
28 #[sea_orm(
29 belongs_to = "super::users::Entity",
30 from = "Column::UserId",
31 to = "super::users::Column::Id",
32 on_update = "Cascade",
33 on_delete = "Cascade"
34 )]
35 Users,
36}
37
38impl Related<super::queues::Entity> for Entity {
39 fn to() -> RelationDef {
40 Relation::Queues.def()
41 }
42}
43
44impl Related<super::users::Entity> for Entity {
45 fn to() -> RelationDef {
46 Relation::Users.def()
47 }
48}
49
50impl ActiveModelBehavior for ActiveModel {}
diff --git a/entity/src/queues.rs b/entity/src/queues.rs
new file mode 100644
index 0000000..34b358a
--- /dev/null
+++ b/entity/src/queues.rs
@@ -0,0 +1,51 @@
1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14
2
3use sea_orm::entity::prelude::*;
4
5#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6#[sea_orm(table_name = "queues")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 pub owner_id: i64,
11 pub name: String,
12}
13
14#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
15pub enum Relation {
16 #[sea_orm(has_many = "super::access_to_queue::Entity")]
17 AccessToQueue,
18 #[sea_orm(has_many = "super::queue_elements::Entity")]
19 QueueElements,
20 #[sea_orm(
21 belongs_to = "super::users::Entity",
22 from = "Column::OwnerId",
23 to = "super::users::Column::Id",
24 on_update = "Cascade",
25 on_delete = "Cascade"
26 )]
27 Users,
28}
29
30impl Related<super::access_to_queue::Entity> for Entity {
31 fn to() -> RelationDef {
32 Relation::AccessToQueue.def()
33 }
34}
35
36impl Related<super::queue_elements::Entity> for Entity {
37 fn to() -> RelationDef {
38 Relation::QueueElements.def()
39 }
40}
41
42impl Related<super::users::Entity> for Entity {
43 fn to() -> RelationDef {
44 super::access_to_queue::Relation::Users.def()
45 }
46 fn via() -> Option<RelationDef> {
47 Some(super::access_to_queue::Relation::Queues.def().rev())
48 }
49}
50
51impl ActiveModelBehavior for ActiveModel {}
diff --git a/entity/src/sea_orm_active_enums.rs b/entity/src/sea_orm_active_enums.rs
new file mode 100644
index 0000000..2252a9f
--- /dev/null
+++ b/entity/src/sea_orm_active_enums.rs
@@ -0,0 +1,16 @@
1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14
2
3use sea_orm::entity::prelude::*;
4
5#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
6#[sea_orm(
7 rs_type = "String",
8 db_type = "Enum",
9 enum_name = "queue_element_status_enum"
10)]
11pub enum QueueElementStatusEnum {
12 #[sea_orm(string_value = "passed")]
13 Passed,
14 #[sea_orm(string_value = "waiting")]
15 Waiting,
16}
diff --git a/entity/src/users.rs b/entity/src/users.rs
new file mode 100644
index 0000000..b61d51b
--- /dev/null
+++ b/entity/src/users.rs
@@ -0,0 +1,49 @@
1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14
2
3use sea_orm::entity::prelude::*;
4
5#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
6#[sea_orm(table_name = "users")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 #[sea_orm(unique)]
11 pub login: String,
12 pub password: String,
13 pub password_issue_date: DateTime,
14 pub first_name: String,
15 pub last_name: String,
16}
17
18#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
19pub enum Relation {
20 #[sea_orm(has_many = "super::access_to_queue::Entity")]
21 AccessToQueue,
22 #[sea_orm(has_many = "super::queue_elements::Entity")]
23 QueueElements,
24 #[sea_orm(has_many = "super::queues::Entity")]
25 Queues,
26}
27
28impl Related<super::access_to_queue::Entity> for Entity {
29 fn to() -> RelationDef {
30 Relation::AccessToQueue.def()
31 }
32}
33
34impl Related<super::queue_elements::Entity> for Entity {
35 fn to() -> RelationDef {
36 Relation::QueueElements.def()
37 }
38}
39
40impl Related<super::queues::Entity> for Entity {
41 fn to() -> RelationDef {
42 super::access_to_queue::Relation::Queues.def()
43 }
44 fn via() -> Option<RelationDef> {
45 Some(super::access_to_queue::Relation::Users.def().rev())
46 }
47}
48
49impl ActiveModelBehavior for ActiveModel {}
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]
2name = "migration"
3version = "0.1.0"
4edition = "2024"
5publish = false
6
7[lib]
8name = "migration"
9path = "src/lib.rs"
10
11[dependencies]
12sea-orm-migration = { version = "1.1.14", features = ["sqlx-postgres", "runtime-tokio-rustls"] }
13tokio = { 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)]
2mod m0_init_tables;
3
4use sea_orm_migration::prelude::*;
5
6pub struct Migrator;
7
8#[async_trait::async_trait]
9impl 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 @@
1use sea_orm_migration::prelude::extension::postgres::Type;
2use sea_orm_migration::sea_orm::{EnumIter, Iterable};
3use sea_orm_migration::{prelude::*, schema::*};
4
5#[derive(DeriveIden)]
6enum Users {
7 Table,
8 Id,
9 Login,
10 Password,
11 PasswordIssueDate,
12 FirstName,
13 LastName,
14}
15
16#[derive(DeriveIden)]
17enum Queues {
18 Table,
19 Id,
20 OwnerId,
21 Name,
22}
23
24#[derive(DeriveIden)]
25enum AccessToQueue {
26 Table,
27 UserId,
28 QueueId,
29}
30
31#[derive(DeriveIden)]
32enum QueueElements {
33 Table,
34 Id,
35 QueueId,
36 UserId,
37 Position,
38 Status,
39}
40
41mod 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)]
56pub struct Migration;
57
58#[async_trait::async_trait]
59impl 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 @@
1use sea_orm_migration::prelude::*;
2
3#[tokio::main(flavor = "current_thread")]
4async fn main() {
5 cli::run_cli(migration::Migrator).await;
6}