Sha256: 48463fc38cefc18103e638493373fe6fd7c265378ccfa9cdad5d7a16982aa448

Contents?: true

Size: 596 Bytes

Versions: 9

Compression:

Stored size: 596 Bytes

Contents

# frozen_string_literal: true

PgEventstore.connection.with do |conn|
  types = conn.exec('select type from events group by type').to_a.map { |attrs| attrs['type'] }
  types.each.with_index(1) do |type, index|
    id = conn.exec_params('SELECT id FROM event_types WHERE type = $1', [type]).to_a.first['id']
    id ||= conn.exec_params('INSERT INTO event_types (type) VALUES ($1) RETURNING *', [type]).to_a.first['id']
    conn.exec_params('UPDATE events SET event_type_id = $1 WHERE type = $2 AND event_type_id IS NULL', [id, type])
    puts "Processed #{index} types of #{types.size}"
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pg_eventstore-0.6.0 db/migrations/4_populate_event_types.rb
pg_eventstore-0.5.3 db/migrations/4_populate_event_types.rb
pg_eventstore-0.5.2 db/migrations/4_populate_event_types.rb
pg_eventstore-0.5.0 db/migrations/4_populate_event_types.rb
pg_eventstore-0.4.0 db/migrations/4_populate_event_types.rb
pg_eventstore-0.3.0 db/migrations/4_populate_event_types.rb
pg_eventstore-0.2.6 db/migrations/4_populate_event_types.rb
pg_eventstore-0.2.5 db/migrations/4_populate_event_types.rb
pg_eventstore-0.2.4 db/migrations/4_populate_event_types.rb