Skip to content

Commit

Permalink
Remove duplicate users (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
boavenn authored Oct 31, 2024
1 parent 9c84cef commit 07fcb7d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Acceptance {
@JoinColumn(nullable = false)
private Request request;

@OneToOne
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(nullable = false)
private User leader;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/info/fingo/urlopia/history/HistoryLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/info/fingo/urlopia/request/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Request {
@Column(nullable = false)
private LocalDateTime modified;

@ManyToOne
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(nullable = false)
private User requester;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/info/fingo/urlopia/team/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- no rollback script for this
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS users_account_name_unique_index;
42 changes: 42 additions & 0 deletions src/main/resources/scripts/V3_5_0_7__remove_duplicate_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;

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_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;

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;

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)
DELETE FROM users
WHERE id IN (SELECT id FROM CTE WHERE rn > 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE UNIQUE INDEX users_account_name_unique_index
ON users (account_name)
WHERE account_name IS NOT NULL;

0 comments on commit 07fcb7d

Please # to comment.