Fix migrations that populates the event types

This commit is contained in:
Petko Bordjukov 2015-08-10 19:05:34 +03:00
parent 4d37c817e6
commit 1c876bfeb7
2 changed files with 10 additions and 7 deletions

View File

@ -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

View File

@ -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']