diff --git a/db/migrate/20150416234411_create_event_types_for_all_existing_events.rb b/db/migrate/20150416234411_create_event_types_for_all_existing_events.rb index 0e963fe..b12b5ea 100644 --- a/db/migrate/20150416234411_create_event_types_for_all_existing_events.rb +++ b/db/migrate/20150416234411_create_event_types_for_all_existing_events.rb @@ -1,15 +1,18 @@ +class EventType < ActiveRecord::Base +end + class CreateEventTypesForAllExistingEvents < ActiveRecord::Migration def up - event_types = execute 'SELECT DISTINCT(events.type) as type_name, tracks.conference_id, events.created_at, events.updated_at FROM events INNER JOIN tracks ON tracks.id = events.track_id WHERE events.type NOT NULL GROUP BY type_name;' + event_types = execute 'SELECT events.type AS type_name, tracks.conference_id, MIN(events.created_at) AS created_at FROM events + INNER JOIN tracks ON tracks.id = events.track_id + WHERE events.type IS NOT NULL + GROUP BY type_name, tracks.conference_id;' event_types.each do |type| - execute "INSERT INTO event_types (conference_id, created_at, updated_at) - VALUES (#{type['conference_id']}, '#{type['created_at']}', '#{type['updated_at']}');" - - event_type_id = execute('SELECT MAX(id) AS last_id FROM event_types;').first['last_id'] + event_type = EventType.create! conference_id: type['conference_id'], created_at: type['created_at'], updated_at: type['created_at'] execute "INSERT INTO event_type_translations (event_type_id, locale, created_at, updated_at, name, description) - VALUES (#{event_type_id}, 'en', '#{type['created_at']}', '#{type['created_at']}', '#{type['type_name']}', 'None');" + VALUES (#{event_type.id}, 'en', '#{event_type.created_at}', '#{event_type.created_at}', '#{type['type_name']}', 'None');" end end diff --git a/db/migrate/20150417002233_populate_event_type_of_existing_events.rb b/db/migrate/20150417002233_populate_event_type_of_existing_events.rb index 46f01cb..84e8e1d 100644 --- a/db/migrate/20150417002233_populate_event_type_of_existing_events.rb +++ b/db/migrate/20150417002233_populate_event_type_of_existing_events.rb @@ -1,6 +1,6 @@ class PopulateEventTypeOfExistingEvents < ActiveRecord::Migration def up - event_ids = execute('SELECT id FROM events WHERE type NOT NULL;').map { |row| row['id'] }; + event_ids = execute('SELECT id FROM events WHERE type IS NOT NULL;').map { |row| row['id'] }; event_ids.each do |id| event_type_id = execute("SELECT ett.event_type_id FROM events JOIN event_type_translations AS ett ON events.type = ett.name WHERE events.id = #{id};").first['event_type_id']