delete invalid project references before migrating

pull/7984/head
ulferts 5 years ago
parent a802f849aa
commit 19138e5066
No known key found for this signature in database
GPG Key ID: A205708DE1284017
  1. 14
      db/migrate/20191216135213_join_table_for_active_activities.rb

@ -9,6 +9,7 @@ class JoinTableForActiveActivities < ActiveRecord::Migration[6.0]
def up def up
create_join_table create_join_table
delete_invalid_project_activities
link_time_entries_to_root_activities link_time_entries_to_root_activities
fill_new_join_table fill_new_join_table
delete_inherited_activities delete_inherited_activities
@ -33,6 +34,19 @@ class JoinTableForActiveActivities < ActiveRecord::Migration[6.0]
name: 'index_teap_on_project_id_and_activity_id' name: 'index_teap_on_project_id_and_activity_id'
end end
# Delete all references from enumerations to projects which point to no longer
# existing projects.
def delete_invalid_project_activities
ActiveRecord::Base.connection.exec_query(
<<-SQL
DELETE FROM enumerations
USING enumerations AS enums
LEFT OUTER JOIN projects on enums.project_id = projects.id
WHERE enums.id = enumerations.id AND enums.type='TimeEntryActivity' AND projects.id IS NULL AND enums.project_id IS NOT NULL
SQL
)
end
def link_time_entries_to_root_activities def link_time_entries_to_root_activities
ActiveRecord::Base.connection.exec_query( ActiveRecord::Base.connection.exec_query(
<<-SQL <<-SQL

Loading…
Cancel
Save