Module: Fixturama::Config

Defined in:
lib/fixturama/config.rb

Overview

Configures the gem to exclude conflicts between ids auto-generated by database and hardcoded in fixtures

Constant Summary collapse

SEQUENCE_BOOST_SCRIPT =
<<~SQL.freeze
  DO $$
  DECLARE
  seq_name TEXT;

  BEGIN
    FOR seq_name IN (select table_name from information_schema.tables where
      table_catalog='%<db_name>s' and table_schema='public') LOOP
    BEGIN
      EXECUTE ' \
        SELECT setval('''||seq_name||'_id_seq''::regclass, %<min_id>s); ';
    EXCEPTION
      WHEN undefined_table THEN
        NULL;
    END;
    END LOOP;
  END$$;
SQL

Class Method Summary collapse

Class Method Details

.start_ids_from(value) ⇒ Boolean



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fixturama/config.rb', line 13

def start_ids_from(value)
  require "active_record"

  db_name = ActiveRecord::Base.connection_config[:database]
  sql = format(SEQUENCE_BOOST_SCRIPT, db_name: db_name, min_id: value.to_i)
  ActiveRecord::Base.connection.execute(sql)

  true
rescue LoadError
  false
end