Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add id primary key to order_team_members table #210

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions db/migrate/20240311175613_add_id_to_order_team_members.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class AddIdToOrderTeamMembers < ActiveRecord::Migration[7.1]

def change
remove_index :order_team_members, column: :employee_id
remove_index :order_team_members, column: :order_id

reversible do |dir|
dir.up { deduplicate_entries }
end

add_column :order_team_members, :id, :primary_key
add_index :order_team_members, [:employee_id, :order_id], unique: true
end

private

def deduplicate_entries
execute <<~SQL
DELETE FROM "order_team_members"
WHERE "ctid" IN (
SELECT "ctid"
FROM (
SELECT
"ctid",
"employee_id",
"order_id",
row_number() OVER (
PARTITION BY "employee_id", "order_id"
ORDER BY "employee_id", "order_id"
) AS "rnum"
FROM "order_team_members"
) "t"
WHERE "t"."rnum" > 1
);
SQL
end

end
Loading