From 4a73e14bbfb71f6fa32b8f6bbba8696a8b4363d6 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Fri, 22 Jul 2022 16:00:57 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:crm=5Fdata=5Fsync=E3=81=AE=E6=96=B0?= =?UTF-8?q?=E8=A6=8F=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_data_sync.sql | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 rds_mysql/stored_procedure/crm_data_sync.sql diff --git a/rds_mysql/stored_procedure/crm_data_sync.sql b/rds_mysql/stored_procedure/crm_data_sync.sql new file mode 100644 index 00000000..c039a18a --- /dev/null +++ b/rds_mysql/stored_procedure/crm_data_sync.sql @@ -0,0 +1,41 @@ +-- A5M2で実行時にSQL区切り文字を「;」以外にすること +-- $$から始まる文字は後からREPLACEする文字を示す独自ルール +-- crm_data_syncストアドプロシージャは、同一セッション内での並列処理を実行することができない +CREATE PROCEDURE crm_data_sync(target_table VARCHAR(255), target_table_all VARCHAR(255), target_column VARCHAR(255)) +BEGIN + -- 例外処理 + -- エラーが発生した場合に一時テーブルの削除を実施 + DECLARE EXIT HANDLER FOR SQLEXCEPTION + BEGIN + GET DIAGNOSTICS CONDITION 1 + @error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT; + ROLLBACK; + SIGNAL SQLSTATE '45000' + SET MYSQL_ERRNO = @error_state, MESSAGE_TEXT = @error_msg; + END; + + SET @error_state = NULL, @error_msg = NULL; + START TRANSACTION; + + -- ①-1 Salesforce側で物理削除されたデータを検出し更新する + SET @update_end_datetime = ' + UPDATE $$target_table$$ tt + LEFT JOIN $$target_table_all$$ tta + ON tt.id = tta.id + AND tt.$$target_column$$ = tta.$$target_column$$ + SET + tt.end_datetime = CURRENT_TIMESTAMP() + , tt.upd_user = CURRENT_USER() + , tt.upd_date = CURRENT_TIMESTAMP() + WHERE + tta.id IS NULL + AND tt.end_datetime = "9999-12-31 00:00:00" + '; + SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table$$", target_table); + SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table_all$$", target_table_all); + SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_column$$", target_column); + PREPARE update_end_datetime_stmt from @update_end_datetime; + EXECUTE update_end_datetime_stmt; + + COMMIT; +END \ No newline at end of file From 016e4465e5ac3576aa023962205d055464bb13eb Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Tue, 26 Jul 2022 10:32:23 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:EXISTS=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_data_sync.sql | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_data_sync.sql b/rds_mysql/stored_procedure/crm_data_sync.sql index c039a18a..01d27b9a 100644 --- a/rds_mysql/stored_procedure/crm_data_sync.sql +++ b/rds_mysql/stored_procedure/crm_data_sync.sql @@ -20,16 +20,21 @@ BEGIN -- ①-1 Salesforce側で物理削除されたデータを検出し更新する SET @update_end_datetime = ' UPDATE $$target_table$$ tt - LEFT JOIN $$target_table_all$$ tta - ON tt.id = tta.id - AND tt.$$target_column$$ = tta.$$target_column$$ SET - tt.end_datetime = CURRENT_TIMESTAMP() - , tt.upd_user = CURRENT_USER() - , tt.upd_date = CURRENT_TIMESTAMP() + tt.end_datetime = CURRENT_TIMESTAMP () + , tt.upd_user = CURRENT_USER () + , tt.upd_date = CURRENT_TIMESTAMP () WHERE - tta.id IS NULL - AND tt.end_datetime = "9999-12-31 00:00:00" + tt.end_datetime = "9999-12-31 00:00:00" + AND NOT EXISTS ( + SELECT + * + FROM + $$target_table_all$$ tta + WHERE + tt.id = tta.id + AND tt.$$target_column$$ = tta.$$target_column$$ + ) '; SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table$$", target_table); SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table_all$$", target_table_all); From 76750b86b3e8f76ef340f2814cf5240c58bab7bf Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Tue, 26 Jul 2022 11:33:32 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:EXISTS=E3=81=AE=E4=B8=AD=E8=BA=AB?= =?UTF-8?q?=E3=81=AESELECT=E5=8F=A5=E3=82=92ID=E3=81=AE=E3=81=BF=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_data_sync.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_data_sync.sql b/rds_mysql/stored_procedure/crm_data_sync.sql index 01d27b9a..155bfb99 100644 --- a/rds_mysql/stored_procedure/crm_data_sync.sql +++ b/rds_mysql/stored_procedure/crm_data_sync.sql @@ -28,7 +28,7 @@ BEGIN tt.end_datetime = "9999-12-31 00:00:00" AND NOT EXISTS ( SELECT - * + tt.id FROM $$target_table_all$$ tta WHERE @@ -37,8 +37,8 @@ BEGIN ) '; SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table$$", target_table); - SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table_all$$", target_table_all); - SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_column$$", target_column); + SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table_all$$", target_table_all); + SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_column$$", target_column); PREPARE update_end_datetime_stmt from @update_end_datetime; EXECUTE update_end_datetime_stmt; From 66b79a3115b35a84a6aaf9867567233985d3559d Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Tue, 26 Jul 2022 11:43:03 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=E5=8F=82=E7=85=A7ID=E3=82=92tta?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_data_sync.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rds_mysql/stored_procedure/crm_data_sync.sql b/rds_mysql/stored_procedure/crm_data_sync.sql index 155bfb99..e9986c0f 100644 --- a/rds_mysql/stored_procedure/crm_data_sync.sql +++ b/rds_mysql/stored_procedure/crm_data_sync.sql @@ -28,7 +28,7 @@ BEGIN tt.end_datetime = "9999-12-31 00:00:00" AND NOT EXISTS ( SELECT - tt.id + tta.id FROM $$target_table_all$$ tta WHERE