From 17deb5f382b4f9157679cf6c1f5fb4cb493eb4e8 Mon Sep 17 00:00:00 2001 From: "witold.brzozowski" Date: Wed, 23 Oct 2024 09:19:12 +0200 Subject: [PATCH 1/7] Add logs to user synchronizer --- .../fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java b/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java index fb57ed4..ad22c37 100644 --- a/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java +++ b/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java @@ -17,6 +17,7 @@ import javax.naming.directory.SearchResult; import java.time.LocalDateTime; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.Stream; @Component @@ -33,6 +34,11 @@ public class ActiveDirectoryUserSynchronizer { public void addNewUsers() { var dbUsers = userRepository.findAllAccountNames(); + LOGGER.info("Existing account names: {}", dbUsers.stream().sorted().collect(Collectors.joining(", "))); + + var adUsers = pickUsersFromActiveDirectory(); + LOGGER.info("AD account names: {}", adUsers.stream().map(user -> ActiveDirectoryUtils.pickAttribute(user, Attribute.ACCOUNT_NAME)).sorted().collect(Collectors.joining(", "))); + pickUsersFromActiveDirectory().stream() .filter(user -> !dbUsers.contains(ActiveDirectoryUtils.pickAttribute(user, Attribute.ACCOUNT_NAME))) .map(userMapper::mapNewUser) From 12e30527695e77f3aebb99406f9b6694459acb67 Mon Sep 17 00:00:00 2001 From: "witold.brzozowski" Date: Wed, 23 Oct 2024 09:37:26 +0200 Subject: [PATCH 2/7] Remove duplicate users --- .../fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java | 6 ------ .../resources/scripts/U3_5_0_7__remove_duplicate_users.sql | 1 + .../scripts/U3_5_0_8__make_account_name_unique.sql | 1 + .../resources/scripts/V3_5_0_7__remove_duplicate_users.sql | 3 +++ .../scripts/V3_5_0_8__make_account_name_unique.sql | 2 ++ 5 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 src/main/resources/scripts/U3_5_0_7__remove_duplicate_users.sql create mode 100644 src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql create mode 100644 src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql create mode 100644 src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql diff --git a/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java b/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java index ad22c37..fb57ed4 100644 --- a/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java +++ b/src/main/java/info/fingo/urlopia/user/ActiveDirectoryUserSynchronizer.java @@ -17,7 +17,6 @@ import javax.naming.directory.SearchResult; import java.time.LocalDateTime; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.Stream; @Component @@ -34,11 +33,6 @@ public class ActiveDirectoryUserSynchronizer { public void addNewUsers() { var dbUsers = userRepository.findAllAccountNames(); - LOGGER.info("Existing account names: {}", dbUsers.stream().sorted().collect(Collectors.joining(", "))); - - var adUsers = pickUsersFromActiveDirectory(); - LOGGER.info("AD account names: {}", adUsers.stream().map(user -> ActiveDirectoryUtils.pickAttribute(user, Attribute.ACCOUNT_NAME)).sorted().collect(Collectors.joining(", "))); - pickUsersFromActiveDirectory().stream() .filter(user -> !dbUsers.contains(ActiveDirectoryUtils.pickAttribute(user, Attribute.ACCOUNT_NAME))) .map(userMapper::mapNewUser) diff --git a/src/main/resources/scripts/U3_5_0_7__remove_duplicate_users.sql b/src/main/resources/scripts/U3_5_0_7__remove_duplicate_users.sql new file mode 100644 index 0000000..a10e7e1 --- /dev/null +++ b/src/main/resources/scripts/U3_5_0_7__remove_duplicate_users.sql @@ -0,0 +1 @@ +-- no rollback script for this \ No newline at end of file diff --git a/src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql b/src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql new file mode 100644 index 0000000..6685adb --- /dev/null +++ b/src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql @@ -0,0 +1 @@ +ALTER TABLE users DROP CONSTRAINT IF EXISTS users_account_name_unique; \ No newline at end of file diff --git a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql new file mode 100644 index 0000000..3838020 --- /dev/null +++ b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql @@ -0,0 +1,3 @@ +WITH CTE AS (SELECT id, ROW_NUMBER() OVER (PARTITION BY account_name ORDER BY id) AS rn FROM users) +DELETE FROM users +WHERE id IN (SELECT id FROM CTE WHERE rn > 1); \ No newline at end of file diff --git a/src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql b/src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql new file mode 100644 index 0000000..87a8cb5 --- /dev/null +++ b/src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql @@ -0,0 +1,2 @@ +ALTER TABLE users +ADD CONSTRAINT users_account_name_unique UNIQUE (account_name); \ No newline at end of file From 3deab140a13de082b029e700f1d0bd70ea2bb23a Mon Sep 17 00:00:00 2001 From: "witold.brzozowski" Date: Wed, 23 Oct 2024 10:12:17 +0200 Subject: [PATCH 3/7] Add cascade delete constraints --- .../fingo/urlopia/acceptance/Acceptance.java | 2 +- .../days/model/AutomaticVacationDay.java | 2 +- .../fingo/urlopia/history/HistoryLog.java | 4 +-- .../info/fingo/urlopia/request/Request.java | 2 +- .../java/info/fingo/urlopia/team/Team.java | 2 +- .../V3_5_0_7__remove_duplicate_users.sql | 27 +++++++++++++++++++ 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/info/fingo/urlopia/acceptance/Acceptance.java b/src/main/java/info/fingo/urlopia/acceptance/Acceptance.java index 74c48a0..901a090 100644 --- a/src/main/java/info/fingo/urlopia/acceptance/Acceptance.java +++ b/src/main/java/info/fingo/urlopia/acceptance/Acceptance.java @@ -18,7 +18,7 @@ public class Acceptance { @JoinColumn(nullable = false) private Request request; - @OneToOne + @OneToOne(cascade = CascadeType.ALL) @JoinColumn(nullable = false) private User leader; diff --git a/src/main/java/info/fingo/urlopia/api/v2/automatic/vacation/days/model/AutomaticVacationDay.java b/src/main/java/info/fingo/urlopia/api/v2/automatic/vacation/days/model/AutomaticVacationDay.java index d17c897..1efd474 100644 --- a/src/main/java/info/fingo/urlopia/api/v2/automatic/vacation/days/model/AutomaticVacationDay.java +++ b/src/main/java/info/fingo/urlopia/api/v2/automatic/vacation/days/model/AutomaticVacationDay.java @@ -30,7 +30,7 @@ public class AutomaticVacationDay { @Column(nullable = false) private Double nextYearHoursProposition = 0.0; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(nullable = false) private User user; diff --git a/src/main/java/info/fingo/urlopia/history/HistoryLog.java b/src/main/java/info/fingo/urlopia/history/HistoryLog.java index f95462b..8e8854e 100644 --- a/src/main/java/info/fingo/urlopia/history/HistoryLog.java +++ b/src/main/java/info/fingo/urlopia/history/HistoryLog.java @@ -23,11 +23,11 @@ public class HistoryLog { // TODO: Think about removing all relations from log @Column(nullable = false) private LocalDateTime created; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(nullable = false) private User user; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL) private User decider; // TODO: change to list of deciders @ManyToOne diff --git a/src/main/java/info/fingo/urlopia/request/Request.java b/src/main/java/info/fingo/urlopia/request/Request.java index a94996a..b48bc18 100644 --- a/src/main/java/info/fingo/urlopia/request/Request.java +++ b/src/main/java/info/fingo/urlopia/request/Request.java @@ -27,7 +27,7 @@ public class Request { @Column(nullable = false) private LocalDateTime modified; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(nullable = false) private User requester; diff --git a/src/main/java/info/fingo/urlopia/team/Team.java b/src/main/java/info/fingo/urlopia/team/Team.java index 8a0b7f8..3c42210 100644 --- a/src/main/java/info/fingo/urlopia/team/Team.java +++ b/src/main/java/info/fingo/urlopia/team/Team.java @@ -15,7 +15,7 @@ public class Team { @Column(nullable = false) private String adName; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL) private User leader; @ManyToMany(cascade = CascadeType.PERSIST) diff --git a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql index 3838020..8847911 100644 --- a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql +++ b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql @@ -1,3 +1,30 @@ +ALTER TABLE acceptances DROP CONSTRAINT acceptances_leader_id_fkey; +ALTER TABLE acceptances ADD CONSTRAINT acceptances_leader_id_fkey FOREIGN KEY (leader_id) REFERENCES users(id) ON DELETE CASCADE; + +ALTER TABLE automatic_vacation_days DROP CONSTRAINT automatic_vacation_days_user_id_fkey; +ALTER TABLE automatic_vacation_days ADD CONSTRAINT automatic_vacation_days_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; + +ALTER TABLE history_logs DROP CONSTRAINT history_logs_user_id_fkey; +ALTER TABLE history_logs ADD CONSTRAINT history_logs_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; + +ALTER TABLE history_logs DROP CONSTRAINT history_logs_decider_id_fkey; +ALTER TABLE history_logs ADD CONSTRAINT history_logs_user_id_fkey FOREIGN KEY (decider_id) REFERENCES users(id) ON DELETE SET NULL; + +ALTER TABLE presence_confirmations DROP CONSTRAINT presence_confirmations_user_id_fkey; +ALTER TABLE presence_confirmations ADD CONSTRAINT presence_confirmations_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; + +ALTER TABLE requests DROP CONSTRAINT requests_requester_id_fkey; +ALTER TABLE requests ADD CONSTRAINT requests_requester_id_fkey FOREIGN KEY (requester_id) REFERENCES users(id) ON DELETE CASCADE; + +ALTER TABLE teams DROP CONSTRAINT teams_leader_id_fkey; +ALTER TABLE teams ADD CONSTRAINT teams_leader_id_fkey FOREIGN KEY (leader_id) REFERENCES users(id) ON DELETE SET NULL; + +ALTER TABLE user_working_hours_preference DROP CONSTRAINT user_working_hours_preference_user_id_fkey; +ALTER TABLE user_working_hours_preference ADD CONSTRAINT user_working_hours_preference_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; + +ALTER TABLE users_teams DROP CONSTRAINT users_teams_user_id_fkey; +ALTER TABLE users_teams ADD CONSTRAINT users_teams_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; + WITH CTE AS (SELECT id, ROW_NUMBER() OVER (PARTITION BY account_name ORDER BY id) AS rn FROM users) DELETE FROM users WHERE id IN (SELECT id FROM CTE WHERE rn > 1); \ No newline at end of file From 62287fb3ad68cc157052800498f85f0aa2859146 Mon Sep 17 00:00:00 2001 From: "witold.brzozowski" Date: Wed, 23 Oct 2024 10:19:31 +0200 Subject: [PATCH 4/7] Fix --- src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql index 8847911..4dde1c2 100644 --- a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql +++ b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql @@ -8,7 +8,7 @@ ALTER TABLE history_logs DROP CONSTRAINT history_logs_user_id_fkey; ALTER TABLE history_logs ADD CONSTRAINT history_logs_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; ALTER TABLE history_logs DROP CONSTRAINT history_logs_decider_id_fkey; -ALTER TABLE history_logs ADD CONSTRAINT history_logs_user_id_fkey FOREIGN KEY (decider_id) REFERENCES users(id) ON DELETE SET NULL; +ALTER TABLE history_logs ADD CONSTRAINT history_logs_decider_id_fkey FOREIGN KEY (decider_id) REFERENCES users(id) ON DELETE SET NULL; ALTER TABLE presence_confirmations DROP CONSTRAINT presence_confirmations_user_id_fkey; ALTER TABLE presence_confirmations ADD CONSTRAINT presence_confirmations_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; From c12dc65ff7573e9b1c186fce38eca9d3986347c3 Mon Sep 17 00:00:00 2001 From: "witold.brzozowski" Date: Wed, 23 Oct 2024 10:47:30 +0200 Subject: [PATCH 5/7] Fix script --- .../scripts/V3_5_0_7__remove_duplicate_users.sql | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql index 4dde1c2..5032acc 100644 --- a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql +++ b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql @@ -1,6 +1,9 @@ ALTER TABLE acceptances DROP CONSTRAINT acceptances_leader_id_fkey; ALTER TABLE acceptances ADD CONSTRAINT acceptances_leader_id_fkey FOREIGN KEY (leader_id) REFERENCES users(id) ON DELETE CASCADE; +ALTER TABLE acceptances DROP CONSTRAINT acceptances_request_id_fkey; +ALTER TABLE acceptances ADD CONSTRAINT acceptances_request_id_fkey FOREIGN KEY (request_id) REFERENCES requests(id) ON DELETE CASCADE; + ALTER TABLE automatic_vacation_days DROP CONSTRAINT automatic_vacation_days_user_id_fkey; ALTER TABLE automatic_vacation_days ADD CONSTRAINT automatic_vacation_days_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; @@ -10,6 +13,12 @@ ALTER TABLE history_logs ADD CONSTRAINT history_logs_user_id_fkey FOREIGN KEY (u ALTER TABLE history_logs DROP CONSTRAINT history_logs_decider_id_fkey; ALTER TABLE history_logs ADD CONSTRAINT history_logs_decider_id_fkey FOREIGN KEY (decider_id) REFERENCES users(id) ON DELETE SET NULL; +ALTER TABLE history_logs DROP CONSTRAINT history_logs_prev_history_log_id_fkey; +ALTER TABLE history_logs ADD CONSTRAINT history_logs_prev_history_log_id_fkey FOREIGN KEY (prev_history_log_id) REFERENCES history_logs(id) ON DELETE SET NULL; + +ALTER TABLE history_logs DROP CONSTRAINT history_logs_request_id_fkey; +ALTER TABLE history_logs ADD CONSTRAINT history_logs_request_id_fkey FOREIGN KEY (request_id) REFERENCES requests(id) ON DELETE SET NULL; + ALTER TABLE presence_confirmations DROP CONSTRAINT presence_confirmations_user_id_fkey; ALTER TABLE presence_confirmations ADD CONSTRAINT presence_confirmations_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; @@ -25,6 +34,9 @@ ALTER TABLE user_working_hours_preference ADD CONSTRAINT user_working_hours_pref ALTER TABLE users_teams DROP CONSTRAINT users_teams_user_id_fkey; ALTER TABLE users_teams ADD CONSTRAINT users_teams_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; -WITH CTE AS (SELECT id, ROW_NUMBER() OVER (PARTITION BY account_name ORDER BY id) AS rn FROM users) +ALTER TABLE users_teams DROP CONSTRAINT users_teams_team_id_fkey; +ALTER TABLE users_teams ADD CONSTRAINT users_teams_team_id_fkey FOREIGN KEY (team_id) REFERENCES teams(name) ON DELETE CASCADE; + +WITH CTE AS (SELECT id, ROW_NUMBER() OVER (PARTITION BY account_name ORDER BY id) AS rn FROM users WHERE active = TRUE) DELETE FROM users WHERE id IN (SELECT id FROM CTE WHERE rn > 1); \ No newline at end of file From 3a6b8de3e9ed715f3eac5602dd12bcd0812bdce8 Mon Sep 17 00:00:00 2001 From: "witold.brzozowski" Date: Wed, 23 Oct 2024 10:56:56 +0200 Subject: [PATCH 6/7] Change account name unique constraint to index --- .../resources/scripts/U3_5_0_8__make_account_name_unique.sql | 2 +- .../resources/scripts/V3_5_0_8__make_account_name_unique.sql | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql b/src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql index 6685adb..484b84c 100644 --- a/src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql +++ b/src/main/resources/scripts/U3_5_0_8__make_account_name_unique.sql @@ -1 +1 @@ -ALTER TABLE users DROP CONSTRAINT IF EXISTS users_account_name_unique; \ No newline at end of file +DROP INDEX IF EXISTS users_account_name_unique_index; \ No newline at end of file diff --git a/src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql b/src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql index 87a8cb5..1c82a94 100644 --- a/src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql +++ b/src/main/resources/scripts/V3_5_0_8__make_account_name_unique.sql @@ -1,2 +1,3 @@ -ALTER TABLE users -ADD CONSTRAINT users_account_name_unique UNIQUE (account_name); \ No newline at end of file +CREATE UNIQUE INDEX users_account_name_unique_index +ON users (account_name) +WHERE account_name IS NOT NULL; From 11791214669b2d4a94f18b0c5a7eff7b2f9164a8 Mon Sep 17 00:00:00 2001 From: "witold.brzozowski" Date: Wed, 23 Oct 2024 11:06:21 +0200 Subject: [PATCH 7/7] Fix --- src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql index 5032acc..9f31659 100644 --- a/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql +++ b/src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql @@ -37,6 +37,6 @@ ALTER TABLE users_teams ADD CONSTRAINT users_teams_user_id_fkey FOREIGN KEY (use ALTER TABLE users_teams DROP CONSTRAINT users_teams_team_id_fkey; ALTER TABLE users_teams ADD CONSTRAINT users_teams_team_id_fkey FOREIGN KEY (team_id) REFERENCES teams(name) ON DELETE CASCADE; -WITH CTE AS (SELECT id, ROW_NUMBER() OVER (PARTITION BY account_name ORDER BY id) AS rn FROM users WHERE active = TRUE) +WITH CTE AS (SELECT id, ROW_NUMBER() OVER (PARTITION BY account_name ORDER BY id) AS rn FROM users) DELETE FROM users WHERE id IN (SELECT id FROM CTE WHERE rn > 1); \ No newline at end of file