From 316b5ac49f7593d7bea4c6bc00a61989aff99428 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:59:57 +0900 Subject: [PATCH 01/13] =?UTF-8?q?chore:=20=E3=83=9E=E3=82=A4=E3=82=B0?= =?UTF-8?q?=E3=83=AC=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=A7ORM?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E3=82=8F=E3=81=AA=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/migration/src/lib.rs | 18 +- .../src/m20220101_000001_create_table.rs | 224 ++++++++++++------ .../src/m20221211_211233_form_questions.rs | 98 -------- .../m20230219_143118_create_form_choices.rs | 60 ----- ...083950_crate_form_response_period_table.rs | 71 ------ .../m20230622_053919_create_form_webhook.rs | 58 ----- .../m20230811_062425_create_answer_tables.rs | 131 ---------- ...908_140907_create_default_answer_titles.rs | 64 ----- .../src/m20231008_135425_create_user_table.rs | 46 ---- 9 files changed, 148 insertions(+), 622 deletions(-) delete mode 100644 server/migration/src/m20221211_211233_form_questions.rs delete mode 100644 server/migration/src/m20230219_143118_create_form_choices.rs delete mode 100644 server/migration/src/m20230614_083950_crate_form_response_period_table.rs delete mode 100644 server/migration/src/m20230622_053919_create_form_webhook.rs delete mode 100644 server/migration/src/m20230811_062425_create_answer_tables.rs delete mode 100644 server/migration/src/m20230908_140907_create_default_answer_titles.rs delete mode 100644 server/migration/src/m20231008_135425_create_user_table.rs diff --git a/server/migration/src/lib.rs b/server/migration/src/lib.rs index 58d9a5af..2c605afb 100644 --- a/server/migration/src/lib.rs +++ b/server/migration/src/lib.rs @@ -1,28 +1,12 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_create_table; -mod m20221211_211233_form_questions; -mod m20230219_143118_create_form_choices; -mod m20230614_083950_crate_form_response_period_table; -mod m20230622_053919_create_form_webhook; -mod m20230811_062425_create_answer_tables; -mod m20230908_140907_create_default_answer_titles; -mod m20231008_135425_create_user_table; pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![ - Box::new(m20231008_135425_create_user_table::Migration), - Box::new(m20220101_000001_create_table::Migration), - Box::new(m20221211_211233_form_questions::Migration), - Box::new(m20230219_143118_create_form_choices::Migration), - Box::new(m20230614_083950_crate_form_response_period_table::Migration), - Box::new(m20230622_053919_create_form_webhook::Migration), - Box::new(m20230811_062425_create_answer_tables::Migration), - Box::new(m20230908_140907_create_default_answer_titles::Migration), - ] + vec![Box::new(m20220101_000001_create_table::Migration)] } } diff --git a/server/migration/src/m20220101_000001_create_table.rs b/server/migration/src/m20220101_000001_create_table.rs index 7a324d3c..7291029b 100644 --- a/server/migration/src/m20220101_000001_create_table.rs +++ b/server/migration/src/m20220101_000001_create_table.rs @@ -1,6 +1,7 @@ use sea_orm_migration::prelude::*; +use sea_orm_migration::sea_orm::Statement; -use crate::m20231008_135425_create_user_table::UsersTable; +use crate::sea_orm::DatabaseBackend; #[derive(DeriveMigrationName)] pub struct Migration; @@ -8,84 +9,153 @@ 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(FormMetaDataTable::FormMetaData) - .if_not_exists() - .col( - ColumnDef::new(FormMetaDataTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col(ColumnDef::new(FormMetaDataTable::Title).string().not_null()) - .col(ColumnDef::new(FormMetaDataTable::Description).string()) - .col( - ColumnDef::new(FormMetaDataTable::CreatedAt) - .timestamp() - .not_null() - .extra("DEFAULT CURRENT_TIMESTAMP".to_string()), - ) - .col( - ColumnDef::new(FormMetaDataTable::CreatedBy) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-created-user-id") - .from( - FormMetaDataTable::FormMetaData, - FormMetaDataTable::CreatedBy, - ) - .to(UsersTable::Users, UsersTable::Id), - ) - .col( - ColumnDef::new(FormMetaDataTable::UpdatedAt) - .timestamp() - .not_null() - .extra("DEFAULT CURRENT_TIMESTAMP".to_string()), - ) - .col( - ColumnDef::new(FormMetaDataTable::UpdatedBy) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-updated-user-id") - .from( - FormMetaDataTable::FormMetaData, - FormMetaDataTable::UpdatedBy, - ) - .to(UsersTable::Users, UsersTable::Id), - ) - .to_owned(), - ) - .await + let connection = manager.get_connection(); + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS users( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + uuid CHAR(36) NOT NULL UNIQUE KEY, + name VARCHAR(16) NOT NULL + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS form_meta_data( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + title TEXT NOT NULL, + description TEXT NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + created_by INT NOT NULL, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_by INT NOT NULL, + FOREIGN KEY fk_form_meta_data_created_by(created_by) REFERENCES users(id), + FOREIGN KEY fk_form_meta_data_updated_by(updated_by) REFERENCES users(id) + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS form_questions( + question_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + form_id INT NOT NULL, + title TEXT NOT NULL, + description TEXT, + question_type ENUM('TEXT', 'SINGLE', 'MULTIPLE'), + is_required BOOL DEFAULT FALSE, + FOREIGN KEY fk_form_questions_form_id(form_id) REFERENCES form_meta_data(id) + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS form_choices( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + question_id INT NOT NULL, + choice TEXT NOT NULL, + FOREIGN KEY fk_form_choices_question_id(question_id) REFERENCES form_questions(question_id) + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS response_period( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + form_id INT NOT NULL, + start_at DATETIME NOT NULL, + end_at DATETIME NOT NULL, + FOREIGN KEY fk_response_period_form_id(form_id) REFERENCES form_meta_data(id) + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS form_webhooks( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + form_id INT NOT NULL, + url TEXT NOT NULL, + FOREIGN KEY fk_form_webhooks_form_id(form_id) REFERENCES form_meta_data(id) + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS answers( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + form_id INT NOT NULL, + user INT NOT NULL, + title TEXT, + time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY fk_answers_form_id(form_id) REFERENCES form_meta_data(id), + FOREIGN KEY fk_answers_user(user) REFERENCES users(id) + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS real_answers( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + answer_id INT NOT NULL, + question_id INT NOT NULL, + answer TEXT NOT NULL, + FOREIGN KEY fk_real_answers_answer_id(answer_id) REFERENCES answers(id), + FOREIGN KEY fk_real_answers_quesiton_id(question_id) REFERENCES form_questions(question_id) + )", + )) + .await?; + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r"CREATE TABLE IF NOT EXISTS default_answer_titles( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + form_id INT NOT NULL, + title TEXT NOT NULL, + FOREIGN KEY fk_default_answer_titles_form_id(form_id) REFERENCES form_meta_data(id) + )", + )) + .await?; + + Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(FormMetaDataTable::FormMetaData) - .to_owned(), - ) - .await - } -} + let connection = manager.get_connection(); + + connection + .execute(Statement::from_string( + DatabaseBackend::MySql, + r" + DROP TABLE IF EXISTS + users, + form_meta_data, + form_questions, + form_choices, + response_period, + form_webhooks, + answers, + real_answers, + default_answer_titles; + ", + )) + .await?; -#[derive(Iden)] -pub enum FormMetaDataTable { - FormMetaData, - Id, - Title, - Description, - CreatedAt, - CreatedBy, - UpdatedAt, - UpdatedBy, + Ok(()) + } } diff --git a/server/migration/src/m20221211_211233_form_questions.rs b/server/migration/src/m20221211_211233_form_questions.rs deleted file mode 100644 index a723c9e3..00000000 --- a/server/migration/src/m20221211_211233_form_questions.rs +++ /dev/null @@ -1,98 +0,0 @@ -use std::borrow::BorrowMut; - -use sea_orm_migration::prelude::*; - -use crate::{m20220101_000001_create_table::FormMetaDataTable, ColumnType::Enum}; - -#[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(FormQuestionsTable::FormQuestions) - .if_not_exists() - .col( - ColumnDef::new(FormQuestionsTable::QuestionId) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(FormQuestionsTable::FormId) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-form_key") - .from( - FormQuestionsTable::FormQuestions, - FormQuestionsTable::FormId, - ) - .to(FormMetaDataTable::FormMetaData, FormMetaDataTable::Id), - ) - .col( - ColumnDef::new(FormQuestionsTable::Title) - .string() - .not_null(), - ) - .col(ColumnDef::new(FormQuestionsTable::Description).string()) - .col( - ColumnDef::new_with_type( - FormQuestionsTable::QuestionType.into_iden(), - Enum { - name: FormQuestionsTable::QuestionType.into_iden(), - variants: vec![ - QuestionType::Text.into_iden(), - QuestionType::Multiple.into_iden(), - QuestionType::Single.into_iden(), - ], - }, - ) - .not_null() - .borrow_mut(), - ) - .col( - ColumnDef::new(FormQuestionsTable::IsRequired) - .boolean() - .default(false) - .not_null(), - ) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(FormQuestionsTable::FormQuestions) - .to_owned(), - ) - .await - } -} - -#[derive(Iden)] -pub enum FormQuestionsTable { - FormQuestions, - QuestionId, - FormId, - Title, - Description, - QuestionType, - IsRequired, -} - -#[derive(Iden)] -enum QuestionType { - Text, - Single, - Multiple, -} diff --git a/server/migration/src/m20230219_143118_create_form_choices.rs b/server/migration/src/m20230219_143118_create_form_choices.rs deleted file mode 100644 index 0a2a0a8f..00000000 --- a/server/migration/src/m20230219_143118_create_form_choices.rs +++ /dev/null @@ -1,60 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::m20221211_211233_form_questions::FormQuestionsTable; - -#[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(FormChoicesTable::FormChoices) - .if_not_exists() - .col( - ColumnDef::new(FormChoicesTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(FormChoicesTable::QuestionId) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-question_id") - .from(FormChoicesTable::FormChoices, FormChoicesTable::QuestionId) - .to( - FormQuestionsTable::FormQuestions, - FormQuestionsTable::QuestionId, - ), - ) - .col(ColumnDef::new(FormChoicesTable::Choice).string().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(FormChoicesTable::FormChoices) - .to_owned(), - ) - .await - } -} - -#[derive(Iden)] -enum FormChoicesTable { - FormChoices, - Id, - QuestionId, - Choice, -} diff --git a/server/migration/src/m20230614_083950_crate_form_response_period_table.rs b/server/migration/src/m20230614_083950_crate_form_response_period_table.rs deleted file mode 100644 index bb8db166..00000000 --- a/server/migration/src/m20230614_083950_crate_form_response_period_table.rs +++ /dev/null @@ -1,71 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::m20220101_000001_create_table::FormMetaDataTable; - -#[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(FormResponsePeriodTable::ResponsePeriod) - .if_not_exists() - .col( - ColumnDef::new(FormResponsePeriodTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(FormResponsePeriodTable::FormId) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-form_key_from_response_period") - .from( - FormResponsePeriodTable::ResponsePeriod, - FormResponsePeriodTable::FormId, - ) - .to(FormMetaDataTable::FormMetaData, FormMetaDataTable::Id), - ) - .col( - ColumnDef::new(FormResponsePeriodTable::StartAt) - .date_time() - .not_null(), - ) - .col( - ColumnDef::new(FormResponsePeriodTable::EndAt) - .date_time() - .not_null(), - ) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(FormResponsePeriodTable::ResponsePeriod) - .to_owned(), - ) - .await - } -} - -/// Learn more at https://docs.rs/sea-query#iden -#[derive(Iden)] -enum FormResponsePeriodTable { - ResponsePeriod, - Id, - FormId, - StartAt, - EndAt, -} diff --git a/server/migration/src/m20230622_053919_create_form_webhook.rs b/server/migration/src/m20230622_053919_create_form_webhook.rs deleted file mode 100644 index 911cb00c..00000000 --- a/server/migration/src/m20230622_053919_create_form_webhook.rs +++ /dev/null @@ -1,58 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::m20220101_000001_create_table::FormMetaDataTable; - -#[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(FormWebhookTable::FormWebhooks) - .if_not_exists() - .col( - ColumnDef::new(FormWebhookTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(FormWebhookTable::FormId) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-form_key_form_webhooks") - .from(FormWebhookTable::FormWebhooks, FormWebhookTable::FormId) - .to(FormMetaDataTable::FormMetaData, FormMetaDataTable::Id), - ) - .col(ColumnDef::new(FormWebhookTable::Url).string().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(FormWebhookTable::FormWebhooks) - .to_owned(), - ) - .await - } -} - -/// Learn more at https://docs.rs/sea-query#iden -#[derive(Iden)] -enum FormWebhookTable { - FormWebhooks, - Id, - FormId, - Url, -} diff --git a/server/migration/src/m20230811_062425_create_answer_tables.rs b/server/migration/src/m20230811_062425_create_answer_tables.rs deleted file mode 100644 index fe819897..00000000 --- a/server/migration/src/m20230811_062425_create_answer_tables.rs +++ /dev/null @@ -1,131 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::{ - m20220101_000001_create_table::FormMetaDataTable, - m20221211_211233_form_questions::FormQuestionsTable, - m20231008_135425_create_user_table::UsersTable, -}; - -#[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(AnswersTable::Answers) - .if_not_exists() - .col( - ColumnDef::new(AnswersTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col(ColumnDef::new(AnswersTable::FormId).integer().not_null()) - .foreign_key( - ForeignKey::create() - .name("fk-form-id-from-answers") - .from(AnswersTable::Answers, AnswersTable::FormId) - .to(FormMetaDataTable::FormMetaData, FormMetaDataTable::Id), - ) - .col(ColumnDef::new(AnswersTable::User).integer().not_null()) - .foreign_key( - ForeignKey::create() - .name("fk-answer-user-id") - .from(AnswersTable::Answers, AnswersTable::User) - .to(UsersTable::Users, UsersTable::Id), - ) - .col( - ColumnDef::new(AnswersTable::Title) - .string() - .not_null() - .default("未設定"), - ) - .col( - ColumnDef::new(AnswersTable::TimeStamp) - .timestamp() - .not_null() - .extra("DEFAULT CURRENT_TIMESTAMP".to_string()), - ) - .to_owned(), - ) - .await?; - - manager - .create_table( - Table::create() - .table(RealAnswersTable::RealAnswers) - .if_not_exists() - .col( - ColumnDef::new(RealAnswersTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(RealAnswersTable::AnswerId) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-answer-id") - .from(RealAnswersTable::RealAnswers, RealAnswersTable::AnswerId) - .to(AnswersTable::Answers, AnswersTable::Id), - ) - .col( - ColumnDef::new(RealAnswersTable::QuestionId) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-question-id-from-real-answers") - .from(RealAnswersTable::RealAnswers, RealAnswersTable::QuestionId) - .to( - FormQuestionsTable::FormQuestions, - FormQuestionsTable::QuestionId, - ), - ) - .col(ColumnDef::new(RealAnswersTable::Answer).string().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table(Table::drop().table(AnswersTable::Answers).to_owned()) - .await?; - manager - .drop_table( - Table::drop() - .table(RealAnswersTable::RealAnswers) - .to_owned(), - ) - .await - } -} - -#[derive(DeriveIden)] -enum AnswersTable { - Answers, - Id, - FormId, - User, - Title, - TimeStamp, -} - -#[derive(DeriveIden)] -enum RealAnswersTable { - RealAnswers, - Id, - AnswerId, - QuestionId, - Answer, -} diff --git a/server/migration/src/m20230908_140907_create_default_answer_titles.rs b/server/migration/src/m20230908_140907_create_default_answer_titles.rs deleted file mode 100644 index 55cb5bc3..00000000 --- a/server/migration/src/m20230908_140907_create_default_answer_titles.rs +++ /dev/null @@ -1,64 +0,0 @@ -use sea_orm_migration::prelude::*; - -use crate::m20220101_000001_create_table::FormMetaDataTable; - -#[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(DefaultAnswerTitlesTable::DefaultAnswerTitles) - .if_not_exists() - .col( - ColumnDef::new(DefaultAnswerTitlesTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(DefaultAnswerTitlesTable::FormId) - .integer() - .not_null(), - ) - .foreign_key( - ForeignKey::create() - .name("fk-form_key_default_answer_title") - .from( - DefaultAnswerTitlesTable::DefaultAnswerTitles, - DefaultAnswerTitlesTable::FormId, - ) - .to(FormMetaDataTable::FormMetaData, FormMetaDataTable::Id), - ) - .col( - ColumnDef::new(DefaultAnswerTitlesTable::Title) - .string() - .not_null(), - ) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table( - Table::drop() - .table(DefaultAnswerTitlesTable::DefaultAnswerTitles) - .to_owned(), - ) - .await - } -} - -#[derive(DeriveIden)] -enum DefaultAnswerTitlesTable { - DefaultAnswerTitles, - Id, - FormId, - Title, -} diff --git a/server/migration/src/m20231008_135425_create_user_table.rs b/server/migration/src/m20231008_135425_create_user_table.rs deleted file mode 100644 index 26915cf3..00000000 --- a/server/migration/src/m20231008_135425_create_user_table.rs +++ /dev/null @@ -1,46 +0,0 @@ -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(UsersTable::Users) - .if_not_exists() - .col( - ColumnDef::new(UsersTable::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col( - ColumnDef::new(UsersTable::Uuid) - .uuid() - .unique_key() - .not_null(), - ) - .col(ColumnDef::new(UsersTable::Name).string().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table(Table::drop().table(UsersTable::Users).to_owned()) - .await - } -} - -#[derive(DeriveIden)] -pub enum UsersTable { - Users, - Id, - Uuid, - Name, -} From 5c96865e2d7637d569ba3c01b83c047bcb818d0a Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 01:15:26 +0900 Subject: [PATCH 02/13] =?UTF-8?q?chore:=20form=E3=81=AE=E4=B8=80=E8=A6=A7?= =?UTF-8?q?=E3=82=92=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=A7ORM=E3=82=92=E4=BD=BF=E3=81=86=E3=81=AE=E3=82=92?= =?UTF-8?q?=E3=82=84=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/infra/resource/src/database/form.rs | 174 +++++++++--------- server/infra/resource/src/database/user.rs | 2 +- server/infra/resource/src/dto.rs | 1 + .../src/m20220101_000001_create_table.rs | 3 +- 4 files changed, 89 insertions(+), 91 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 4ccd769a..a0fe1459 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -1,4 +1,5 @@ use async_trait::async_trait; +use chrono::{DateTime, Utc}; use domain::{ form::models::{ DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, @@ -15,14 +16,13 @@ use entities::{ use errors::infra::{InfraError, InfraError::FormNotFound}; use futures::{stream, stream::StreamExt}; use itertools::Itertools; -use num_traits::cast::FromPrimitive; use regex::Regex; use sea_orm::{ sea_query::{Expr, SimpleExpr}, ActiveEnum, ActiveModelTrait, ActiveValue, ActiveValue::Set, ColumnTrait, ConnectionTrait, DatabaseBackend, DbErr, EntityTrait, ModelTrait, QueryFilter, - QueryOrder, QuerySelect, Statement, + Statement, }; use crate::{ @@ -44,7 +44,7 @@ impl FormDatabase for ConnectionPool { .execute(Statement::from_sql_and_values( DatabaseBackend::MySql, "INSERT INTO form_meta_data (title, description, created_by, updated_by) - SELECT ?, ?, users.id, users.id FROM users WHERE uuid = UUID_TO_BIN(?)", + SELECT ?, ?, users.id, users.id FROM users WHERE uuid = ?", [ title.title().to_owned().into(), description.to_owned().into(), @@ -62,107 +62,106 @@ impl FormDatabase for ConnectionPool { &self, OffsetAndLimit { offset, limit }: OffsetAndLimit, ) -> Result, InfraError> { - let forms = FormMetaData::find() - .order_by_asc(form_meta_data::Column::Id) - .offset(offset.and_then(u64::from_i32)) - .limit(limit.and_then(u64::from_i32)) - .all(&self.pool) + let forms = self + .pool + .query_all(Statement::from_string( + DatabaseBackend::MySql, + format!(r"SELECT form_meta_data.id AS form_id, form_meta_data.title AS form_title, description, created_at, updated_at, url, start_at, end_at, default_answer_titles.title + FROM form_meta_data + LEFT JOIN form_webhooks ON form_meta_data.id = form_webhooks.form_id + LEFT JOIN response_period ON form_meta_data.id = response_period.form_id + LEFT JOIN default_answer_titles ON form_meta_data.id = default_answer_titles.form_id + ORDER BY form_meta_data.id + {} {}", + limit.map(|value| format!("LIMIT {}", value)).unwrap_or_default(), + offset.map(|value| format!("OFFSET {}", value)).unwrap_or_default()), + )) .await?; - let form_ids = forms.iter().map(|form| form.id).collect_vec(); - - let all_questions = FormQuestions::find() - .filter(Expr::col(form_questions::Column::FormId).is_in(form_ids.to_owned())) - .all(&self.pool) + let questions = self + .pool + .query_all(Statement::from_string( + DatabaseBackend::MySql, + r"SELECT form_id, question_id, title, description, question_type, is_required FROM form_questions" + )) .await?; - let question_ids = all_questions - .iter() - .map(|question| question.question_id) - .collect_vec(); - - let all_choices = FormChoices::find() - .filter(Expr::col(form_choices::Column::QuestionId).is_in(question_ids)) - .all(&self.pool) + let choices = self + .pool + .query_all(Statement::from_string( + DatabaseBackend::MySql, + r"SELECT question_id, choice FROM form_choices", + )) .await?; - let all_periods = entities::response_period::Entity::find() - .filter(Expr::col(response_period::Column::FormId).is_in(form_ids.to_owned())) - .all(&self.pool) - .await?; + let form_id_with_questions = questions + .into_iter() + .map(|rs| { + let question_id: i32 = rs.try_get("", "question_id")?; - let all_webhook_urls = FormWebhooks::find() - .filter(Expr::col(form_webhooks::Column::FormId).is_in(form_ids.to_owned())) - .all(&self.pool) - .await?; + let choices = choices + .iter() + .filter_map(|rs| { + let choice_question_id: i32 = rs.try_get("", "question_id").ok()?; - let all_default_answer_title = DefaultAnswerTitles::find() - .filter(Expr::col(default_answer_titles::Column::FormId).is_in(form_ids.to_owned())) - .all(&self.pool) - .await?; + if choice_question_id == question_id { + let choice: Result = rs.try_get("", "choice"); - Ok(forms - .into_iter() - .map(|form| { - let questions = all_questions - .iter() - .filter(|question| question.form_id == form.id) - .map(|question| { - let choices = all_choices - .iter() - .filter(|choice| choice.question_id == question.question_id) - .cloned() - .map(|choice| choice.choice) - .collect_vec(); - - QuestionDto { - id: question.question_id.to_owned(), - title: question.title.to_owned(), - description: question.description.to_owned(), - question_type: question.question_type.to_string(), - choices, - is_required: question.is_required != 0, + choice.ok() + } else { + None } }) - .collect::>(); + .collect_vec(); + + let form_id: i32 = rs.try_get("", "form_id")?; + + Ok(( + form_id, + QuestionDto { + id: question_id, + title: rs.try_get("", "title")?, + description: rs.try_get("", "description")?, + question_type: rs.try_get("", "question_type")?, + choices, + is_required: rs.try_get("", "is_required")?, + }, + )) + }) + .collect::, DbErr>>()?; + + forms + .into_iter() + .map(|rs| { + let form_id: i32 = rs.try_get("", "form_id")?; - let response_period = all_periods + let questions = form_id_with_questions .iter() - .filter(|period| period.form_id == form.id) - .map(|period| { - Some(( - period.start_at.to_owned().and_utc(), - period.end_at.to_owned().and_utc(), - )) + .cloned() + .filter_map(|(question_form_id, questions)| { + if question_form_id == form_id { + Some(questions) + } else { + None + } }) - .next() - .unwrap_or_default(); + .collect_vec(); - let webhook_url = all_webhook_urls - .iter() - .filter(|webhook_url| webhook_url.form_id == form.id) - .map(|webhook_url| Some(webhook_url.url.to_owned())) - .next() - .unwrap_or_default(); + let start_at: Option> = rs.try_get("", "start_at")?; + let end_at: Option> = rs.try_get("", "end_at")?; - let default_answer_title = all_default_answer_title - .iter() - .filter(|default_answer_title| default_answer_title.form_id == form.id) - .map(|default_answer_title| default_answer_title.title.to_owned()) - .next(); - - FormDto { - id: form.id, - title: form.title, - description: form.description, + Ok(FormDto { + id: form_id, + title: rs.try_get("", "form_title")?, + description: rs.try_get("", "description")?, questions, - metadata: (form.created_at, form.updated_at), - response_period, - webhook_url, - default_answer_title, - } + metadata: (rs.try_get("", "created_at")?, rs.try_get("", "updated_at")?), + response_period: start_at.zip(end_at), + webhook_url: rs.try_get("", "url")?, + default_answer_title: rs.try_get("", "default_answer_titles.title")?, + }) }) - .collect()) + .collect() } #[tracing::instrument] @@ -413,8 +412,7 @@ impl FormDatabase for ConnectionPool { .pool .execute(Statement::from_sql_and_values( DatabaseBackend::MySql, - "INSERT INTO answers (form_id, user, title) VALUES (?, (SELECT id FROM users \ - WHERE uuid = UUID_TO_BIN(?)), ?)", + r"INSERT INTO answers (form_id, user, title) VALUES (?, (SELECT id FROM users WHERE uuid = ?), ?)", [ answer.form_id.to_owned().into(), answer.uuid.to_string().into(), diff --git a/server/infra/resource/src/database/user.rs b/server/infra/resource/src/database/user.rs index 15d53c1e..e9a16df5 100644 --- a/server/infra/resource/src/database/user.rs +++ b/server/infra/resource/src/database/user.rs @@ -11,7 +11,7 @@ impl UserDatabase for ConnectionPool { self.pool .execute(Statement::from_sql_and_values( DatabaseBackend::MySql, - "INSERT INTO users (uuid, name) VALUES (UUID_TO_BIN(?), ?) + "INSERT INTO users (uuid, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name)", [user.id.to_string().into(), user.name.to_owned().into()], diff --git a/server/infra/resource/src/dto.rs b/server/infra/resource/src/dto.rs index 3c89ffcd..99859c55 100644 --- a/server/infra/resource/src/dto.rs +++ b/server/infra/resource/src/dto.rs @@ -2,6 +2,7 @@ use chrono::{DateTime, Utc}; use domain::form::models::{FormSettings, ResponsePeriod}; use uuid::Uuid; +#[derive(Clone)] pub struct QuestionDto { pub id: i32, pub title: String, diff --git a/server/migration/src/m20220101_000001_create_table.rs b/server/migration/src/m20220101_000001_create_table.rs index 7291029b..00a8691a 100644 --- a/server/migration/src/m20220101_000001_create_table.rs +++ b/server/migration/src/m20220101_000001_create_table.rs @@ -1,5 +1,4 @@ -use sea_orm_migration::prelude::*; -use sea_orm_migration::sea_orm::Statement; +use sea_orm_migration::{prelude::*, sea_orm::Statement}; use crate::sea_orm::DatabaseBackend; From 35252ef223d2ac5aa9128835adbf97e33eae2318 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 01:38:28 +0900 Subject: [PATCH 03/13] =?UTF-8?q?refactor:=20ConnectionPool=E3=81=ABrawQue?= =?UTF-8?q?ry=E3=81=AE=E3=83=A9=E3=83=83=E3=83=91=E3=83=BC=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infra/resource/src/database/connection.rs | 47 +++++++++++- server/infra/resource/src/database/form.rs | 73 +++++++------------ 2 files changed, 72 insertions(+), 48 deletions(-) diff --git a/server/infra/resource/src/database/connection.rs b/server/infra/resource/src/database/connection.rs index 06e38dd9..04c498e1 100644 --- a/server/infra/resource/src/database/connection.rs +++ b/server/infra/resource/src/database/connection.rs @@ -1,6 +1,9 @@ use async_trait::async_trait; use migration::MigratorTrait; -use sea_orm::{Database, DatabaseConnection, DatabaseTransaction, TransactionTrait}; +use sea_orm::{ + ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, DatabaseTransaction, DbErr, + ExecResult, QueryResult, Statement, TransactionTrait, Value, +}; use crate::database::{ components::DatabaseComponents, @@ -37,6 +40,48 @@ impl ConnectionPool { Ok(()) } + + pub async fn query_all(&self, sql: &str) -> Result, DbErr> { + self.pool + .query_all(Statement::from_string(DatabaseBackend::MySql, sql)) + .await + } + + pub async fn query_all_and_values( + &self, + sql: &str, + values: I, + ) -> Result, DbErr> + where + I: IntoIterator, + { + self.pool + .query_all(Statement::from_sql_and_values( + DatabaseBackend::MySql, + sql, + values, + )) + .await + } + + pub async fn execute(&self, sql: &str) -> Result { + self.pool + .execute(Statement::from_string(DatabaseBackend::MySql, sql)) + .await + } + + pub async fn execute_and_values(&self, sql: &str, values: I) -> Result + where + I: IntoIterator, + { + self.pool + .execute(Statement::from_sql_and_values( + DatabaseBackend::MySql, + sql, + values, + )) + .await + } } #[async_trait] diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index a0fe1459..90a93fd4 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -21,8 +21,7 @@ use sea_orm::{ sea_query::{Expr, SimpleExpr}, ActiveEnum, ActiveModelTrait, ActiveValue, ActiveValue::Set, - ColumnTrait, ConnectionTrait, DatabaseBackend, DbErr, EntityTrait, ModelTrait, QueryFilter, - Statement, + ColumnTrait, DbErr, EntityTrait, ModelTrait, QueryFilter, }; use crate::{ @@ -40,9 +39,7 @@ impl FormDatabase for ConnectionPool { user: User, ) -> Result { let form_id = self - .pool - .execute(Statement::from_sql_and_values( - DatabaseBackend::MySql, + .execute_and_values( "INSERT INTO form_meta_data (title, description, created_by, updated_by) SELECT ?, ?, users.id, users.id FROM users WHERE uuid = ?", [ @@ -50,7 +47,7 @@ impl FormDatabase for ConnectionPool { description.to_owned().into(), user.id.to_string().into(), ], - )) + ) .await? .last_insert_id() as i32; @@ -63,10 +60,8 @@ impl FormDatabase for ConnectionPool { OffsetAndLimit { offset, limit }: OffsetAndLimit, ) -> Result, InfraError> { let forms = self - .pool - .query_all(Statement::from_string( - DatabaseBackend::MySql, - format!(r"SELECT form_meta_data.id AS form_id, form_meta_data.title AS form_title, description, created_at, updated_at, url, start_at, end_at, default_answer_titles.title + .query_all( + &format!(r"SELECT form_meta_data.id AS form_id, form_meta_data.title AS form_title, description, created_at, updated_at, url, start_at, end_at, default_answer_titles.title FROM form_meta_data LEFT JOIN form_webhooks ON form_meta_data.id = form_webhooks.form_id LEFT JOIN response_period ON form_meta_data.id = response_period.form_id @@ -75,23 +70,17 @@ impl FormDatabase for ConnectionPool { {} {}", limit.map(|value| format!("LIMIT {}", value)).unwrap_or_default(), offset.map(|value| format!("OFFSET {}", value)).unwrap_or_default()), - )) + ) .await?; let questions = self - .pool - .query_all(Statement::from_string( - DatabaseBackend::MySql, + .query_all( r"SELECT form_id, question_id, title, description, question_type, is_required FROM form_questions" - )) + ) .await?; let choices = self - .pool - .query_all(Statement::from_string( - DatabaseBackend::MySql, - r"SELECT question_id, choice FROM form_choices", - )) + .query_all(r"SELECT question_id, choice FROM form_choices") .await?; let form_id_with_questions = questions @@ -409,16 +398,14 @@ impl FormDatabase for ConnectionPool { ); let id = self - .pool - .execute(Statement::from_sql_and_values( - DatabaseBackend::MySql, + .execute_and_values( r"INSERT INTO answers (form_id, user, title) VALUES (?, (SELECT id FROM users WHERE uuid = ?), ?)", [ answer.form_id.to_owned().into(), answer.uuid.to_string().into(), embed_title.into(), ], - )) + ) .await? .last_insert_id(); @@ -434,41 +421,33 @@ impl FormDatabase for ConnectionPool { }) .collect_vec(); - self.pool - .execute(Statement::from_sql_and_values( - DatabaseBackend::MySql, - format!( - "INSERT INTO real_answers (answer_id, question_id, answer) VALUES {}", - vec!["(?, ?, ?)"; params.len()].iter().join(", ") - ), - params - .iter() - .flatten() - .map(|value| value.into()) - .collect_vec(), - )) - .await?; + self.execute_and_values( + &format!( + "INSERT INTO real_answers (answer_id, question_id, answer) VALUES {}", + vec!["(?, ?, ?)"; params.len()].iter().join(", ") + ), + params + .iter() + .flatten() + .map(|value| value.into()) + .collect_vec(), + ) + .await?; Ok(()) } async fn get_all_answers(&self) -> Result, InfraError> { let answers = self - .pool - .query_all(Statement::from_string( - DatabaseBackend::MySql, + .query_all( "SELECT form_id, answers.id AS answer_id, title, uuid, time_stamp FROM answers INNER JOIN users ON answers.user = users.id ORDER BY answers.time_stamp", - )) + ) .await?; let real_answers = self - .pool - .query_all(Statement::from_string( - DatabaseBackend::MySql, - "SELECT answer_id, question_id, answer FROM real_answers", - )) + .query_all("SELECT answer_id, question_id, answer FROM real_answers") .await?; answers From 4f34e9e3096cb17a24014c43ea56e60e2a9a8393 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 03:41:58 +0900 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20batch=5Finsert=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infra/resource/src/database/connection.rs | 32 +++++++++++++++++++ server/infra/resource/src/database/form.rs | 15 +++------ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/server/infra/resource/src/database/connection.rs b/server/infra/resource/src/database/connection.rs index 04c498e1..a229ca4d 100644 --- a/server/infra/resource/src/database/connection.rs +++ b/server/infra/resource/src/database/connection.rs @@ -1,5 +1,7 @@ use async_trait::async_trait; +use itertools::Itertools; use migration::MigratorTrait; +use regex::Regex; use sea_orm::{ ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, DatabaseTransaction, DbErr, ExecResult, QueryResult, Statement, TransactionTrait, Value, @@ -82,6 +84,36 @@ impl ConnectionPool { )) .await } + + pub async fn batch_insert(&self, sql: &str, params: I) -> Result + where + I: IntoIterator, + { + let regex = Regex::new(r"\((\?,\s*)+\?\)").unwrap(); + let insert_part_opt = regex.find(sql); + + assert!( + insert_part_opt.is_some(), + "SQL insert params must be exists." + ); + + let insert_part = insert_part_opt.unwrap().as_str(); + + let params_vec = params.into_iter().collect::>(); + + self.pool + .execute(Statement::from_sql_and_values( + DatabaseBackend::MySql, + sql.replace( + insert_part, + &vec![insert_part; params_vec.len() / insert_part.matches('?').count()] + .iter() + .join(", "), + ), + params_vec, + )) + .await + } } #[async_trait] diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 90a93fd4..bdeb584c 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -412,7 +412,7 @@ impl FormDatabase for ConnectionPool { let params = answer .answers .into_iter() - .map(|answer| { + .flat_map(|answer| { vec![ id.to_string(), answer.question_id.to_string(), @@ -421,16 +421,9 @@ impl FormDatabase for ConnectionPool { }) .collect_vec(); - self.execute_and_values( - &format!( - "INSERT INTO real_answers (answer_id, question_id, answer) VALUES {}", - vec!["(?, ?, ?)"; params.len()].iter().join(", ") - ), - params - .iter() - .flatten() - .map(|value| value.into()) - .collect_vec(), + self.batch_insert( + "INSERT INTO real_answers (answer_id, question_id, answer) VALUES (?, ?, ?)", + params.iter().map(|value| value.into()), ) .await?; From ba56a4f8f9e8d8f82fd5e01335f1f5ac9fc1c66a Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 14:03:45 +0900 Subject: [PATCH 05/13] =?UTF-8?q?chore:=20=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=A0=E3=81=AE=E3=83=87=E3=83=BC=E3=82=BF=E3=82=92=E5=8F=96?= =?UTF-8?q?=E5=BE=97=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86=E3=81=A7ORM?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E3=81=86=E3=81=AE=E3=82=92=E3=82=84=E3=82=81?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/infra/resource/src/database/form.rs | 138 ++++++++++----------- 1 file changed, 67 insertions(+), 71 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index bdeb584c..7366b7ad 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -14,7 +14,6 @@ use entities::{ sea_orm_active_enums::QuestionType, }; use errors::infra::{InfraError, InfraError::FormNotFound}; -use futures::{stream, stream::StreamExt}; use itertools::Itertools; use regex::Regex; use sea_orm::{ @@ -155,82 +154,79 @@ impl FormDatabase for ConnectionPool { #[tracing::instrument] async fn get(&self, form_id: FormId) -> Result { - let target_form = FormMetaData::find() - .filter(Expr::col(form_meta_data::Column::Id).eq(form_id.to_owned())) - .all(&self.pool) - .await? - .first() - .ok_or(FormNotFound { - id: form_id.to_owned(), - })? - .to_owned(); + let form_query = self + .query_all_and_values( + r"SELECT form_meta_data.id AS form_id, form_meta_data.title AS form_title, description, created_at, updated_at, url, start_at, end_at, default_answer_titles.title + FROM form_meta_data + LEFT JOIN form_webhooks ON form_meta_data.id = form_webhooks.form_id + LEFT JOIN response_period ON form_meta_data.id = response_period.form_id + LEFT JOIN default_answer_titles ON form_meta_data.id = default_answer_titles.form_id + WHERE form_meta_data.id = ?", + [form_id.to_owned().into()] + ) + .await?; - let form_questions = stream::iter( - FormQuestions::find() - .filter(Expr::col(form_questions::Column::FormId).eq(form_id.to_owned())) - .all(&self.pool) - .await?, - ) - .then(move |question| async move { - let choices = FormChoices::find() - .filter(Expr::col(form_choices::Column::QuestionId).eq(question.question_id)) - .all(&self.pool) - .await? - .into_iter() - .map(|choice| choice.choice) - .collect_vec(); - - Ok::(QuestionDto { - id: question.question_id.to_owned(), - title: question.title.to_owned(), - description: question.description.to_owned(), - question_type: question.question_type.to_string(), - choices, - is_required: question.is_required != 0, - }) - }) - .collect::>>() - .await - .into_iter() - .collect::, _>>()?; + let form = form_query.first().ok_or(FormNotFound { + id: form_id.to_owned(), + })?; - let response_period = entities::response_period::Entity::find() - .filter(Expr::col(response_period::Column::FormId).eq(target_form.id)) - .all(&self.pool) - .await? - .first() - .map(|period| { - Some(( - period.start_at.to_owned().and_utc(), - period.end_at.to_owned().and_utc(), - )) - }) - .unwrap_or_default(); + let questions = self + .query_all_and_values( + r"SELECT question_id, title, description, question_type, is_required FROM form_questions WHERE form_id = ?", + [form_id.to_owned().into()] + ) + .await?; - let webhook_url = FormWebhooks::find() - .filter(Expr::col(form_webhooks::Column::FormId).eq(target_form.id)) - .all(&self.pool) - .await? - .first() - .map(|webhook_url_model| Some(webhook_url_model.url.to_owned())) - .unwrap_or_default(); + let choices = self + .query_all(r"SELECT question_id, choice FROM form_choices") + .await?; - let default_answer_title = DefaultAnswerTitles::find() - .filter(Expr::col(default_answer_titles::Column::FormId).eq(target_form.id)) - .all(&self.pool) - .await? - .first() - .map(|answer_title_setting| answer_title_setting.title.to_owned()); + let questions = questions + .into_iter() + .map(|rs| { + let question_id: i32 = rs.try_get("", "question_id")?; + + let choices = choices + .iter() + .filter_map(|rs| { + let choice_question_id: i32 = rs.try_get("", "question_id").ok()?; + + if choice_question_id == question_id { + let choice: Result = rs.try_get("", "choice"); + + choice.ok() + } else { + None + } + }) + .collect_vec(); + + Ok(QuestionDto { + id: question_id, + title: rs.try_get("", "title")?, + description: rs.try_get("", "description")?, + question_type: rs.try_get("", "question_type")?, + choices, + is_required: rs.try_get("", "is_required")?, + }) + }) + .collect::, DbErr>>()?; + + let start_at: Option> = form.try_get("", "start_at")?; + let end_at: Option> = form.try_get("", "end_at")?; Ok(FormDto { - id: target_form.id, - title: target_form.title, - description: target_form.description, - questions: form_questions, - metadata: (target_form.created_at, target_form.updated_at), - response_period, - webhook_url, - default_answer_title, + id: form_id.to_owned(), + title: form.try_get("", "form_title")?, + description: form.try_get("", "description")?, + questions, + metadata: ( + form.try_get("", "created_at")?, + form.try_get("", "updated_at")?, + ), + response_period: start_at.zip(end_at), + webhook_url: form.try_get("", "url")?, + default_answer_title: form.try_get("", "default_answer_titles.title")?, }) } From 9810a18b9d0ee979f5e648878db6543fbd35358c Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 14:10:40 +0900 Subject: [PATCH 06/13] =?UTF-8?q?chore:=20form=5Fid=E3=81=A8question=5Fid?= =?UTF-8?q?=E3=81=AE=E5=A4=96=E9=83=A8=E3=82=AD=E3=83=BC=E3=81=ABON=20DELE?= =?UTF-8?q?TE=20CASCADE=E3=82=92=E3=81=A4=E3=81=91=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migration/src/m20220101_000001_create_table.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/server/migration/src/m20220101_000001_create_table.rs b/server/migration/src/m20220101_000001_create_table.rs index 00a8691a..2c8ec2f4 100644 --- a/server/migration/src/m20220101_000001_create_table.rs +++ b/server/migration/src/m20220101_000001_create_table.rs @@ -48,7 +48,7 @@ impl MigrationTrait for Migration { description TEXT, question_type ENUM('TEXT', 'SINGLE', 'MULTIPLE'), is_required BOOL DEFAULT FALSE, - FOREIGN KEY fk_form_questions_form_id(form_id) REFERENCES form_meta_data(id) + FOREIGN KEY fk_form_questions_form_id(form_id) REFERENCES form_meta_data(id) ON DELETE CASCADE )", )) .await?; @@ -60,7 +60,7 @@ impl MigrationTrait for Migration { id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, question_id INT NOT NULL, choice TEXT NOT NULL, - FOREIGN KEY fk_form_choices_question_id(question_id) REFERENCES form_questions(question_id) + FOREIGN KEY fk_form_choices_question_id(question_id) REFERENCES form_questions(question_id) ON DELETE CASCADE )", )) .await?; @@ -73,7 +73,7 @@ impl MigrationTrait for Migration { form_id INT NOT NULL, start_at DATETIME NOT NULL, end_at DATETIME NOT NULL, - FOREIGN KEY fk_response_period_form_id(form_id) REFERENCES form_meta_data(id) + FOREIGN KEY fk_response_period_form_id(form_id) REFERENCES form_meta_data(id) ON DELETE CASCADE )", )) .await?; @@ -85,7 +85,7 @@ impl MigrationTrait for Migration { id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, form_id INT NOT NULL, url TEXT NOT NULL, - FOREIGN KEY fk_form_webhooks_form_id(form_id) REFERENCES form_meta_data(id) + FOREIGN KEY fk_form_webhooks_form_id(form_id) REFERENCES form_meta_data(id) ON DELETE CASCADE )", )) .await?; @@ -99,7 +99,7 @@ impl MigrationTrait for Migration { user INT NOT NULL, title TEXT, time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY fk_answers_form_id(form_id) REFERENCES form_meta_data(id), + FOREIGN KEY fk_answers_form_id(form_id) REFERENCES form_meta_data(id) ON DELETE CASCADE, FOREIGN KEY fk_answers_user(user) REFERENCES users(id) )", )) @@ -114,7 +114,7 @@ impl MigrationTrait for Migration { question_id INT NOT NULL, answer TEXT NOT NULL, FOREIGN KEY fk_real_answers_answer_id(answer_id) REFERENCES answers(id), - FOREIGN KEY fk_real_answers_quesiton_id(question_id) REFERENCES form_questions(question_id) + FOREIGN KEY fk_real_answers_quesiton_id(question_id) REFERENCES form_questions(question_id) ON DELETE CASCADE )", )) .await?; @@ -126,7 +126,7 @@ impl MigrationTrait for Migration { id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, form_id INT NOT NULL, title TEXT NOT NULL, - FOREIGN KEY fk_default_answer_titles_form_id(form_id) REFERENCES form_meta_data(id) + FOREIGN KEY fk_default_answer_titles_form_id(form_id) REFERENCES form_meta_data(id) ON DELETE CASCADE )", )) .await?; From 1d48941cafbbb0e9e3d160f9f25006b0f04e84ff Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 14:30:21 +0900 Subject: [PATCH 07/13] =?UTF-8?q?chore:=20=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=A0=E3=81=AE=E3=83=87=E3=83=BC=E3=82=BF=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86=E3=81=A7ORM?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E3=81=86=E3=81=AE=E3=82=92=E3=82=84=E3=82=81?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/infra/resource/src/database/form.rs | 40 ++++------------------ 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 7366b7ad..ffb230a2 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -20,7 +20,7 @@ use sea_orm::{ sea_query::{Expr, SimpleExpr}, ActiveEnum, ActiveModelTrait, ActiveValue, ActiveValue::Set, - ColumnTrait, DbErr, EntityTrait, ModelTrait, QueryFilter, + ColumnTrait, DbErr, EntityTrait, QueryFilter, }; use crate::{ @@ -232,39 +232,11 @@ impl FormDatabase for ConnectionPool { #[tracing::instrument] async fn delete(&self, form_id: FormId) -> Result { - let target_form = FormMetaData::find_by_id(form_id.to_owned()) - .all(&self.pool) - .await? - .first() - .ok_or(FormNotFound { - id: form_id.to_owned(), - })? - .to_owned(); - - let question_ids = FormQuestions::find() - .filter(Expr::col(form_questions::Column::FormId).eq(form_id.to_owned())) - .all(&self.pool) - .await? - .iter() - .map(|question| question.question_id) - .collect_vec(); - - FormChoices::delete_many() - .filter(Expr::col(form_choices::Column::QuestionId).is_in(question_ids)) - .exec(&self.pool) - .await?; - - response_period::Entity::delete_many() - .filter(Expr::col(response_period::Column::FormId).eq(form_id.to_owned())) - .exec(&self.pool) - .await?; - - FormQuestions::delete_many() - .filter(Expr::col(form_questions::Column::FormId).eq(form_id.to_owned())) - .exec(&self.pool) - .await?; - - target_form.delete(&self.pool).await?; + self.execute_and_values( + "DELETE FROM form_meta_data WHERE id = ?", + [form_id.to_owned().into()], + ) + .await?; Ok(form_id) } From 497fc1b5c0c07bde236d73a1dda8f463a5831e17 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 14:59:27 +0900 Subject: [PATCH 08/13] =?UTF-8?q?chore:=20=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=A0=E3=81=AE=E3=83=87=E3=83=BC=E3=82=BF=E3=82=92=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86=E3=81=A7ORM?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E3=81=86=E3=81=AE=E3=82=92=E3=82=84=E3=82=81?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/infra/resource/src/database/form.rs | 125 +++++++++------------ 1 file changed, 54 insertions(+), 71 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index ffb230a2..ce6c50c8 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -8,19 +8,15 @@ use domain::{ user::models::User, }; use entities::{ - default_answer_titles, form_choices, form_meta_data, form_questions, form_webhooks, - prelude::{DefaultAnswerTitles, FormChoices, FormMetaData, FormQuestions, FormWebhooks}, - response_period, + default_answer_titles, form_choices, form_questions, + prelude::{DefaultAnswerTitles, FormChoices, FormQuestions}, sea_orm_active_enums::QuestionType, }; use errors::infra::{InfraError, InfraError::FormNotFound}; use itertools::Itertools; use regex::Regex; use sea_orm::{ - sea_query::{Expr, SimpleExpr}, - ActiveEnum, ActiveModelTrait, ActiveValue, - ActiveValue::Set, - ColumnTrait, DbErr, EntityTrait, QueryFilter, + sea_query::Expr, ActiveEnum, ActiveValue, ActiveValue::Set, DbErr, EntityTrait, QueryFilter, }; use crate::{ @@ -254,81 +250,68 @@ impl FormDatabase for ConnectionPool { ) -> Result<(), InfraError> { let current_form = self.get(form_id.to_owned().into()).await?; - FormMetaData::update_many() - .filter(form_meta_data::Column::Id.eq(form_id.to_owned())) - .col_expr( - form_meta_data::Column::Title, - Expr::value( - title - .map(|title| title.into_inner()) - .unwrap_or(current_form.title), - ), - ) - .col_expr( - form_meta_data::Column::Description, - Expr::value( - description - .map(|description| description.into_inner()) - .unwrap_or(current_form.description), - ), - ) - .col_expr( - form_meta_data::Column::UpdatedAt, - SimpleExpr::from(Expr::current_timestamp()), - ) - .exec(&self.pool) - .await?; + self.execute_and_values( + r"UPDATE form_meta_data SET title = ?, description = ?, update_at = CURRENT_TIMESTAMP WHERE id = ?", + [ + title + .map(|title| title.into_inner()) + .unwrap_or(current_form.title) + .into(), + description + .map(|description| description.into_inner()) + .unwrap_or(current_form.description) + .into(), + form_id.to_owned().into(), + ], + ) + .await?; if let Some(response_period) = response_period { - response_period::Entity::update_many() - .filter(response_period::Column::FormId.eq(form_id.to_owned())) - .col_expr( - response_period::Column::StartAt, - Expr::value(response_period.start_at), - ) - .col_expr( - response_period::Column::EndAt, - Expr::value(response_period.end_at), - ) - .exec(&self.pool) - .await?; + self.execute_and_values( + "UPDATE response_period SET start_at = ?, end_at = ? WHERE form_id = ?", + [ + response_period.start_at.into(), + response_period.end_at.into(), + form_id.to_owned().into(), + ], + ) + .await?; } if current_form.webhook_url.is_some() { - FormWebhooks::update_many() - .filter(form_webhooks::Column::FormId.eq(form_id.to_owned())) - .col_expr( - form_webhooks::Column::Url, - Expr::value(webhook.and_then(|url| url.webhook_url)), - ) - .exec(&self.pool) - .await?; + self.execute_and_values( + "UPDATE form_webhooks SET url = ? WHERE form_id = ?", + [ + webhook.and_then(|url| url.webhook_url).into(), + form_id.to_owned().into(), + ], + ) + .await?; } else if let Some(webhook_url) = webhook.and_then(|url| url.webhook_url) { - form_webhooks::ActiveModel { - id: ActiveValue::NotSet, - form_id: Set(form_id.to_owned()), - url: Set(webhook_url), - } - .insert(&self.pool) + self.execute_and_values( + "INSERT INTO form_webhooks (form_id, url) VALUES (?, ?)", + [form_id.to_owned().into(), webhook_url.into()], + ) .await?; } if current_form.default_answer_title.is_some() && default_answer_title.is_some() { - DefaultAnswerTitles::update_many() - .filter(default_answer_titles::Column::FormId.eq(form_id.to_owned())) - .col_expr( - default_answer_titles::Column::Title, - Expr::value(default_answer_title.unwrap().unwrap_or_default()), - ) - .exec(&self.pool) - .await?; + self.execute_and_values( + "UPDATE default_answer_titles SET title = ?, WHERE form_id = ?", + [ + default_answer_title.unwrap().unwrap_or_default().into(), + form_id.to_owned().into(), + ], + ) + .await?; } else if let Some(default_answer_title) = default_answer_title { - default_answer_titles::ActiveModel { - id: ActiveValue::NotSet, - form_id: Set(form_id.to_owned()), - title: Set(default_answer_title.unwrap_or_default()), - } - .insert(&self.pool) + self.execute_and_values( + "INSERT INTO default_answer_titles (form_id, title) VALUES (?, ?)", + [ + form_id.to_owned().into(), + default_answer_title.unwrap_or_default().into(), + ], + ) .await?; } From c6a8e66975544e48560adf15895d6b222dcf4a7a Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 15:12:44 +0900 Subject: [PATCH 09/13] =?UTF-8?q?chore:=20=E5=8F=97=E3=81=91=E4=BB=98?= =?UTF-8?q?=E3=81=91=E3=81=9F=E5=9B=9E=E7=AD=94=E3=82=92=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86=E3=81=A7ORM=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E3=81=86=E3=81=AE=E3=82=92=E3=82=84=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/infra/resource/src/database/form.rs | 31 +++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index ce6c50c8..6f55ed21 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -8,16 +8,14 @@ use domain::{ user::models::User, }; use entities::{ - default_answer_titles, form_choices, form_questions, - prelude::{DefaultAnswerTitles, FormChoices, FormQuestions}, + form_choices, form_questions, + prelude::{FormChoices, FormQuestions}, sea_orm_active_enums::QuestionType, }; use errors::infra::{InfraError, InfraError::FormNotFound}; use itertools::Itertools; use regex::Regex; -use sea_orm::{ - sea_query::Expr, ActiveEnum, ActiveValue, ActiveValue::Set, DbErr, EntityTrait, QueryFilter, -}; +use sea_orm::{ActiveEnum, ActiveValue, ActiveValue::Set, DbErr, EntityTrait}; use crate::{ database::{components::FormDatabase, connection::ConnectionPool}, @@ -321,15 +319,22 @@ impl FormDatabase for ConnectionPool { async fn post_answer(&self, answer: PostedAnswers) -> Result<(), InfraError> { let regex = Regex::new(r"\$\d+").unwrap(); + let default_answer_title_query_result = self + .query_all_and_values( + r"SELECT title FROM default_answer_titles WHERE form_id = ?", + [answer.form_id.to_owned().into()], + ) + .await?; + + let default_answer_title: Option = default_answer_title_query_result + .first() + .ok_or(FormNotFound { + id: answer.form_id.to_owned(), + })? + .try_get("", "title")?; + let default_answer_title = DefaultAnswerTitle { - default_answer_title: DefaultAnswerTitles::find() - .filter( - Expr::col(default_answer_titles::Column::FormId).eq(answer.form_id.to_owned()), - ) - .all(&self.pool) - .await? - .first() - .map(|answer_title_setting| answer_title_setting.title.to_owned()), + default_answer_title, } .unwrap_or_default(); From 455c05cd16e9040f1db9b780c958782a055e8fe8 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:08:07 +0900 Subject: [PATCH 10/13] =?UTF-8?q?chore:=20=E8=B3=AA=E5=95=8F=E3=82=92?= =?UTF-8?q?=E4=BD=9C=E6=88=90=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86=E3=81=A7?= =?UTF-8?q?ORM=E3=82=92=E4=BD=BF=E3=81=86=E3=81=AE=E3=82=92=E3=82=84?= =?UTF-8?q?=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infra/resource/src/database/connection.rs | 35 ++++++----- server/infra/resource/src/database/form.rs | 59 ++++++++----------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/server/infra/resource/src/database/connection.rs b/server/infra/resource/src/database/connection.rs index a229ca4d..59daa8c9 100644 --- a/server/infra/resource/src/database/connection.rs +++ b/server/infra/resource/src/database/connection.rs @@ -97,22 +97,29 @@ impl ConnectionPool { "SQL insert params must be exists." ); - let insert_part = insert_part_opt.unwrap().as_str(); - let params_vec = params.into_iter().collect::>(); - self.pool - .execute(Statement::from_sql_and_values( - DatabaseBackend::MySql, - sql.replace( - insert_part, - &vec![insert_part; params_vec.len() / insert_part.matches('?').count()] - .iter() - .join(", "), - ), - params_vec, - )) - .await + if params_vec.is_empty() { + // NOTE: paramsが空だったら適当なSELECTを実行してINSERT文の構築失敗を阻止する + self.pool + .execute(Statement::from_string(DatabaseBackend::MySql, "SELECT 1")) + .await + } else { + let insert_part = insert_part_opt.unwrap().as_str(); + + self.pool + .execute(Statement::from_sql_and_values( + DatabaseBackend::MySql, + sql.replace( + insert_part, + &vec![insert_part; params_vec.len() / insert_part.matches('?').count()] + .iter() + .join(", "), + ), + params_vec, + )) + .await + } } } diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 6f55ed21..0141d570 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -7,15 +7,10 @@ use domain::{ }, user::models::User, }; -use entities::{ - form_choices, form_questions, - prelude::{FormChoices, FormQuestions}, - sea_orm_active_enums::QuestionType, -}; use errors::infra::{InfraError, InfraError::FormNotFound}; use itertools::Itertools; use regex::Regex; -use sea_orm::{ActiveEnum, ActiveValue, ActiveValue::Set, DbErr, EntityTrait}; +use sea_orm::DbErr; use crate::{ database::{components::FormDatabase, connection::ConnectionPool}, @@ -432,26 +427,23 @@ impl FormDatabase for ConnectionPool { &self, form_question_update_schema: FormQuestionUpdateSchema, ) -> Result<(), InfraError> { - let question_active_values = form_question_update_schema - .questions - .iter() - .map(|question| { - QuestionType::try_from_value(&question.question_type.to_string().to_lowercase()) - .map(|question_type| form_questions::ActiveModel { - question_id: ActiveValue::NotSet, - form_id: Set(form_question_update_schema.form_id.to_owned()), - title: Set(question.title.to_owned()), - description: Set(question.description.to_owned()), - question_type: Set(question_type), - is_required: Set(i8::from(question.is_required().to_owned())), - }) - }) - .collect::, _>>()?; - - let last_insert_id = FormQuestions::insert_many(question_active_values) - .exec(&self.pool) + let last_insert_id = self.batch_insert( + r"INSERT INTO form_questions (form_id, title, description, question_type, is_required) VALUES (?, ?, ?, ?, ?)", + form_question_update_schema + .questions + .iter() + .flat_map(|question| + vec![ + form_question_update_schema.form_id.to_owned().into(), + question.title.to_owned().into(), + question.description.to_owned().into(), + question.question_type.to_string().into(), + question.is_required().to_owned().into() + ] + ).collect_vec() + ) .await? - .last_insert_id; + .last_insert_id(); let choices_active_values = form_question_update_schema .questions @@ -466,21 +458,16 @@ impl FormDatabase for ConnectionPool { .choices .iter() .cloned() - .map(|choice| form_choices::ActiveModel { - id: ActiveValue::NotSet, - question_id: Set(question_id), - choice: Set(choice), - }) + .flat_map(|choice| vec![question_id.to_string(), choice]) .collect_vec() }) .collect_vec(); - if !choices_active_values.is_empty() { - // NOTE: insert_manyに渡すvecが空だとinsertに失敗する - FormChoices::insert_many(choices_active_values) - .exec(&self.pool) - .await?; - } + self.batch_insert( + "INSERT INTO form_choices (question_id, choice) VALUES (?, ?)", + choices_active_values.into_iter().map(|value| value.into()), + ) + .await?; Ok(()) } From 3e34456a61a951c6c580071980db9a9b2e0a2cba Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:16:02 +0900 Subject: [PATCH 11/13] =?UTF-8?q?chore:=20entity=E3=82=92=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E3=81=AB=E5=BC=95=E3=81=A3=E5=89=A5=E3=81=8C=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CONTRIBUTING.md | 6 -- server/Cargo.lock | 11 --- server/Cargo.toml | 1 - server/Makefile.toml | 4 - server/infra/entities/Cargo.toml | 20 ----- server/infra/entities/Makefile.toml | 1 - server/infra/entities/src/answers.rs | 56 ------------- .../entities/src/default_answer_title.rs | 32 -------- .../entities/src/default_answer_titles.rs | 32 -------- server/infra/entities/src/form_choices.rs | 32 -------- server/infra/entities/src/form_meta_data.rs | 78 ------------------- server/infra/entities/src/form_questions.rs | 53 ------------- server/infra/entities/src/form_webhooks.rs | 32 -------- server/infra/entities/src/mod.rs | 14 ---- server/infra/entities/src/prelude.rs | 9 --- server/infra/entities/src/question_types.rs | 17 ---- server/infra/entities/src/real_answers.rs | 47 ----------- server/infra/entities/src/response_period.rs | 33 -------- .../entities/src/sea_orm_active_enums.rs | 14 ---- server/infra/entities/src/users.rs | 27 ------- server/infra/resource/Cargo.toml | 1 - server/migration/Cargo.toml | 1 - 22 files changed, 521 deletions(-) delete mode 100644 server/infra/entities/Cargo.toml delete mode 100644 server/infra/entities/Makefile.toml delete mode 100644 server/infra/entities/src/answers.rs delete mode 100644 server/infra/entities/src/default_answer_title.rs delete mode 100644 server/infra/entities/src/default_answer_titles.rs delete mode 100644 server/infra/entities/src/form_choices.rs delete mode 100644 server/infra/entities/src/form_meta_data.rs delete mode 100644 server/infra/entities/src/form_questions.rs delete mode 100644 server/infra/entities/src/form_webhooks.rs delete mode 100644 server/infra/entities/src/mod.rs delete mode 100644 server/infra/entities/src/prelude.rs delete mode 100644 server/infra/entities/src/question_types.rs delete mode 100644 server/infra/entities/src/real_answers.rs delete mode 100644 server/infra/entities/src/response_period.rs delete mode 100644 server/infra/entities/src/sea_orm_active_enums.rs delete mode 100644 server/infra/entities/src/users.rs diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 552f4a70..765ca464 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -72,11 +72,6 @@ server ドメイン(seichi-portal)を表現するのに必要な構造体およびドメイン固有ロジック(構造体の impl)を置くクレートです。 リポジトリのトレイトの定義もここに置きます(リポジトリはドメイン固有型を返す必要があることに注意してください)。 -### infra/entities - -SeaORM によって生成されたクレートです。 -マイグレーションをした場合は、データベースを起動し `makers generate-entity` をすると更新できます。 - ### infra/resource 外部リソースを扱うクレートです。 @@ -134,5 +129,4 @@ cargo ワークスペースで共通のタスクはワークスペースのト | lint | cargo clippy -- -D warnings | clippy によるコードチェックを行います | | format | cargo fmt | rustfmt によるコード整形を行います | | pretty | fix -> test -> lint -> format の順に実行します | 上記 4 つをすべて実行します、push の前に行うことが推奨されます | -| generate-entity | sea-orm-cli generate entity --database-url (DB接続リンク) -o infra/entities/src | sea-orm-cliによるエンティティファイルの生成を行います | | generate-migrate-file <ファイル名> | sea-orm-cli migrate generate <ファイル名> | sea-orm-cliによるデータベースマイグレーションファイルを生成します。 | diff --git a/server/Cargo.lock b/server/Cargo.lock index 1ed3cc53..7af50306 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -906,15 +906,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "entities" -version = "0.1.0" -dependencies = [ - "cargo-husky", - "sea-orm", - "serde", -] - [[package]] name = "entrypoint" version = "0.1.0" @@ -1682,7 +1673,6 @@ version = "0.1.0" dependencies = [ "async-std", "cargo-husky", - "entities", "sea-orm-migration", ] @@ -2390,7 +2380,6 @@ dependencies = [ "async-trait", "chrono", "domain", - "entities", "envy", "errors", "futures", diff --git a/server/Cargo.toml b/server/Cargo.toml index 1b7a46dc..2d4ae67b 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -5,7 +5,6 @@ members = [ "domain", "entrypoint", "errors", - "infra/entities", "infra/outgoing", "infra/resource", "migration", diff --git a/server/Makefile.toml b/server/Makefile.toml index 035a06d9..3bb6e59b 100644 --- a/server/Makefile.toml +++ b/server/Makefile.toml @@ -6,10 +6,6 @@ default_to_workspace = false __CARGO_FIX_YOLO=1 REPOSITORY_ROOT = { script = ["git rev-parse --show-superproject-working-tree --show-toplevel"] } -[tasks.generate-entity] -command = "sea-orm-cli" -args = [ "generate", "entity", "--database-url", "mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_HOST}/${MYSQL_DATABASE}", "-o", "infra/entities/src" ] - [tasks.generate-migrate-file] command = "sea-orm-cli" args = [ "migrate", "generate", "${@}" ] diff --git a/server/infra/entities/Cargo.toml b/server/infra/entities/Cargo.toml deleted file mode 100644 index 1518bc57..00000000 --- a/server/infra/entities/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "entities" -version = "0.1.0" -edition = "2021" - -[lib] -name = "entities" -path = "src/mod.rs" - -[dependencies] -sea-orm = { workspace = true } -serde = { workspace = true } - -[package.metadata.cargo-udeps.ignore] -development = ["cargo-husky"] - -[dev-dependencies.cargo-husky] -version = "1" -default-features = false # Disable features which are enabled by default -features = ["user-hooks"] diff --git a/server/infra/entities/Makefile.toml b/server/infra/entities/Makefile.toml deleted file mode 100644 index 8a447a88..00000000 --- a/server/infra/entities/Makefile.toml +++ /dev/null @@ -1 +0,0 @@ -extend = "../../Makefile.common.toml" diff --git a/server/infra/entities/src/answers.rs b/server/infra/entities/src/answers.rs deleted file mode 100644 index da5890da..00000000 --- a/server/infra/entities/src/answers.rs +++ /dev/null @@ -1,56 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "answers")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub form_id: i32, - pub user: i32, - pub title: String, - pub time_stamp: DateTimeUtc, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::form_meta_data::Entity", - from = "Column::FormId", - to = "super::form_meta_data::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormMetaData, - #[sea_orm(has_many = "super::real_answers::Entity")] - RealAnswers, - #[sea_orm( - belongs_to = "super::users::Entity", - from = "Column::User", - to = "super::users::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - Users, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormMetaData.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::RealAnswers.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Users.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/default_answer_title.rs b/server/infra/entities/src/default_answer_title.rs deleted file mode 100644 index f8559db5..00000000 --- a/server/infra/entities/src/default_answer_title.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "default_answer_title")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub form_id: i32, - pub title: Option, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::form_meta_data::Entity", - from = "Column::FormId", - to = "super::form_meta_data::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormMetaData, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormMetaData.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/default_answer_titles.rs b/server/infra/entities/src/default_answer_titles.rs deleted file mode 100644 index c576a188..00000000 --- a/server/infra/entities/src/default_answer_titles.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "default_answer_titles")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub form_id: i32, - pub title: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::form_meta_data::Entity", - from = "Column::FormId", - to = "super::form_meta_data::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormMetaData, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormMetaData.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/form_choices.rs b/server/infra/entities/src/form_choices.rs deleted file mode 100644 index 261f1a1b..00000000 --- a/server/infra/entities/src/form_choices.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "form_choices")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub question_id: i32, - pub choice: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::form_questions::Entity", - from = "Column::QuestionId", - to = "super::form_questions::Column::QuestionId", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormQuestions, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormQuestions.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/form_meta_data.rs b/server/infra/entities/src/form_meta_data.rs deleted file mode 100644 index 7e7bb281..00000000 --- a/server/infra/entities/src/form_meta_data.rs +++ /dev/null @@ -1,78 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "form_meta_data")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub title: String, - pub description: Option, - pub created_at: DateTimeUtc, - pub created_by: i32, - pub updated_at: DateTimeUtc, - pub updated_by: i32, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm(has_many = "super::answers::Entity")] - Answers, - #[sea_orm(has_many = "super::default_answer_titles::Entity")] - DefaultAnswerTitles, - #[sea_orm(has_many = "super::form_questions::Entity")] - FormQuestions, - #[sea_orm(has_many = "super::form_webhooks::Entity")] - FormWebhooks, - #[sea_orm(has_many = "super::response_period::Entity")] - ResponsePeriod, - #[sea_orm( - belongs_to = "super::users::Entity", - from = "Column::CreatedBy", - to = "super::users::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - Users2, - #[sea_orm( - belongs_to = "super::users::Entity", - from = "Column::UpdatedBy", - to = "super::users::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - Users1, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Answers.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::DefaultAnswerTitles.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormQuestions.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormWebhooks.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::ResponsePeriod.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/form_questions.rs b/server/infra/entities/src/form_questions.rs deleted file mode 100644 index eff0a305..00000000 --- a/server/infra/entities/src/form_questions.rs +++ /dev/null @@ -1,53 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -use super::sea_orm_active_enums::QuestionType; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "form_questions")] -pub struct Model { - #[sea_orm(primary_key)] - pub question_id: i32, - pub form_id: i32, - pub title: String, - pub description: Option, - pub question_type: QuestionType, - pub is_required: i8, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm(has_many = "super::form_choices::Entity")] - FormChoices, - #[sea_orm( - belongs_to = "super::form_meta_data::Entity", - from = "Column::FormId", - to = "super::form_meta_data::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormMetaData, - #[sea_orm(has_many = "super::real_answers::Entity")] - RealAnswers, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormChoices.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormMetaData.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::RealAnswers.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/form_webhooks.rs b/server/infra/entities/src/form_webhooks.rs deleted file mode 100644 index 39327327..00000000 --- a/server/infra/entities/src/form_webhooks.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "form_webhooks")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub form_id: i32, - pub url: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::form_meta_data::Entity", - from = "Column::FormId", - to = "super::form_meta_data::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormMetaData, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormMetaData.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/mod.rs b/server/infra/entities/src/mod.rs deleted file mode 100644 index 5ea0f3b4..00000000 --- a/server/infra/entities/src/mod.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -pub mod prelude; - -pub mod answers; -pub mod default_answer_titles; -pub mod form_choices; -pub mod form_meta_data; -pub mod form_questions; -pub mod form_webhooks; -pub mod real_answers; -pub mod response_period; -pub mod sea_orm_active_enums; -pub mod users; diff --git a/server/infra/entities/src/prelude.rs b/server/infra/entities/src/prelude.rs deleted file mode 100644 index da7a825c..00000000 --- a/server/infra/entities/src/prelude.rs +++ /dev/null @@ -1,9 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -pub use super::{ - answers::Entity as Answers, default_answer_titles::Entity as DefaultAnswerTitles, - form_choices::Entity as FormChoices, form_meta_data::Entity as FormMetaData, - form_questions::Entity as FormQuestions, form_webhooks::Entity as FormWebhooks, - real_answers::Entity as RealAnswers, response_period::Entity as ResponsePeriod, - users::Entity as Users, -}; diff --git a/server/infra/entities/src/question_types.rs b/server/infra/entities/src/question_types.rs deleted file mode 100644 index 59bf5862..00000000 --- a/server/infra/entities/src/question_types.rs +++ /dev/null @@ -1,17 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "question_types")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - #[sea_orm(unique)] - pub question_type: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/real_answers.rs b/server/infra/entities/src/real_answers.rs deleted file mode 100644 index f37ab646..00000000 --- a/server/infra/entities/src/real_answers.rs +++ /dev/null @@ -1,47 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "real_answers")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub answer_id: i32, - pub question_id: i32, - pub answer: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::answers::Entity", - from = "Column::AnswerId", - to = "super::answers::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - Answers, - #[sea_orm( - belongs_to = "super::form_questions::Entity", - from = "Column::QuestionId", - to = "super::form_questions::Column::QuestionId", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormQuestions, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Answers.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormQuestions.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/response_period.rs b/server/infra/entities/src/response_period.rs deleted file mode 100644 index 9acd29d4..00000000 --- a/server/infra/entities/src/response_period.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "response_period")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - pub form_id: i32, - pub start_at: DateTime, - pub end_at: DateTime, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::form_meta_data::Entity", - from = "Column::FormId", - to = "super::form_meta_data::Column::Id", - on_update = "NoAction", - on_delete = "NoAction" - )] - FormMetaData, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::FormMetaData.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/sea_orm_active_enums.rs b/server/infra/entities/src/sea_orm_active_enums.rs deleted file mode 100644 index 468cd251..00000000 --- a/server/infra/entities/src/sea_orm_active_enums.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, DeriveDisplay)] -#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "question_type")] -pub enum QuestionType { - #[sea_orm(string_value = "text")] - Text, - #[sea_orm(string_value = "multiple")] - Multiple, - #[sea_orm(string_value = "single")] - Single, -} diff --git a/server/infra/entities/src/users.rs b/server/infra/entities/src/users.rs deleted file mode 100644 index e9220b41..00000000 --- a/server/infra/entities/src/users.rs +++ /dev/null @@ -1,27 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] -#[sea_orm(table_name = "users")] -pub struct Model { - #[sea_orm(primary_key)] - pub id: i32, - #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(16)))")] - pub uuid: Vec, - pub name: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm(has_many = "super::answers::Entity")] - Answers, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Answers.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/resource/Cargo.toml b/server/infra/resource/Cargo.toml index 547366fb..8e6e9e5e 100644 --- a/server/infra/resource/Cargo.toml +++ b/server/infra/resource/Cargo.toml @@ -8,7 +8,6 @@ anyhow = { workspace = true } async-trait = { workspace = true } chrono = { workspace = true } domain = { path = "../../domain" } -entities = { path = "../entities" } envy = { workspace = true } errors = { path = "../../errors" } futures = { workspace = true } diff --git a/server/migration/Cargo.toml b/server/migration/Cargo.toml index cd10da88..7e384b1f 100644 --- a/server/migration/Cargo.toml +++ b/server/migration/Cargo.toml @@ -10,7 +10,6 @@ path = "src/lib.rs" [dependencies] async-std = { version = "^1.12.0", features = ["attributes", "tokio1"] } -entities = { path = "../infra/entities" } [dependencies.sea-orm-migration] version = "^0.12.4" From feb82d220def93bd889dec0aa788016809062888 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sun, 22 Oct 2023 01:11:09 +0900 Subject: [PATCH 12/13] =?UTF-8?q?refactor:=20batch=5Finsert=E3=81=A7Result?= =?UTF-8?q?,=20DbErr>=E3=82=92=E8=BF=94=E3=81=99?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infra/resource/src/database/connection.rs | 33 +++++++++---------- server/infra/resource/src/database/form.rs | 4 ++- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/server/infra/resource/src/database/connection.rs b/server/infra/resource/src/database/connection.rs index 59daa8c9..23942481 100644 --- a/server/infra/resource/src/database/connection.rs +++ b/server/infra/resource/src/database/connection.rs @@ -85,7 +85,7 @@ impl ConnectionPool { .await } - pub async fn batch_insert(&self, sql: &str, params: I) -> Result + pub async fn batch_insert(&self, sql: &str, params: I) -> Result, DbErr> where I: IntoIterator, { @@ -100,25 +100,24 @@ impl ConnectionPool { let params_vec = params.into_iter().collect::>(); if params_vec.is_empty() { - // NOTE: paramsが空だったら適当なSELECTを実行してINSERT文の構築失敗を阻止する - self.pool - .execute(Statement::from_string(DatabaseBackend::MySql, "SELECT 1")) - .await + Ok(None) } else { let insert_part = insert_part_opt.unwrap().as_str(); - self.pool - .execute(Statement::from_sql_and_values( - DatabaseBackend::MySql, - sql.replace( - insert_part, - &vec![insert_part; params_vec.len() / insert_part.matches('?').count()] - .iter() - .join(", "), - ), - params_vec, - )) - .await + Ok(Some( + self.pool + .execute(Statement::from_sql_and_values( + DatabaseBackend::MySql, + sql.replace( + insert_part, + &vec![insert_part; params_vec.len() / insert_part.matches('?').count()] + .iter() + .join(", "), + ), + params_vec, + )) + .await?, + )) } } } diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 0141d570..121c8178 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -443,6 +443,7 @@ impl FormDatabase for ConnectionPool { ).collect_vec() ) .await? + .unwrap_or_default() .last_insert_id(); let choices_active_values = form_question_update_schema @@ -467,7 +468,8 @@ impl FormDatabase for ConnectionPool { "INSERT INTO form_choices (question_id, choice) VALUES (?, ?)", choices_active_values.into_iter().map(|value| value.into()), ) - .await?; + .await? + .unwrap_or_default(); Ok(()) } From 19f50968807834144f3117637692fd3a4659a96b Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sun, 22 Oct 2023 01:15:36 +0900 Subject: [PATCH 13/13] style: apply pretty --- server/infra/resource/src/database/form.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 121c8178..39eb522a 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -443,7 +443,7 @@ impl FormDatabase for ConnectionPool { ).collect_vec() ) .await? - .unwrap_or_default() + .unwrap() .last_insert_id(); let choices_active_values = form_question_update_schema @@ -468,8 +468,7 @@ impl FormDatabase for ConnectionPool { "INSERT INTO form_choices (question_id, choice) VALUES (?, ?)", choices_active_values.into_iter().map(|value| value.into()), ) - .await? - .unwrap_or_default(); + .await?; Ok(()) }