Top Level Namespace

Defined Under Namespace

Modules: Padrino Classes: String, TemporaryString

Constant Summary collapse

RACK_ENV =

Defines our constants

ENV['RACK_ENV'] ||= 'development'
PADRINO_ROOT =
File.expand_path('..', __dir__)

Instance Method Summary collapse

Instance Method Details

#catch_error(type, error, config) ⇒ Object



363
364
365
366
367
368
369
370
371
372
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 363

def catch_error(type, error, config)
  $stderr.puts(*error.backtrace)
  $stderr.puts error.inspect
  case type
  when :create
    $stderr.puts "Couldn't create database for #{config.inspect}"
  when :drop
    $stderr.puts "Couldn't drop #{config[:database]}"
  end
end

#configuration_hash(configuration) ⇒ Object



453
454
455
456
457
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 453

def configuration_hash(configuration)
  return configuration if less_than_active_record_6_0?
  config = less_than_active_record_6_1? ? configuration.config : configuration.configuration_hash
  config.with_indifferent_access
end

#drop_database(config) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 337

def drop_database(config)
  case config[:adapter]
  when 'mysql', 'mysql2', 'em_mysql2', 'jdbcmysql'
    ActiveRecord::Base.establish_connection(config)
    ActiveRecord::Base.connection.drop_database config[:database]
  when /^sqlite/
    require 'pathname'
    path = Pathname.new(config[:database])
    file = path.absolute? ? path.to_s : Padrino.root(path)

    FileUtils.rm(file)
  when 'postgresql'
    ActiveRecord::Base.establish_connection(config.merge(database: 'postgres', schema_search_path: 'public'))
    ActiveRecord::Base.connection.drop_database config[:database]
  end
end

#dump_schemaObject



399
400
401
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 399

def dump_schema
  Rake::Task['ar:schema:dump'].invoke if ActiveRecord::Base.schema_format == :ruby
end

#firebird_db_string(config) ⇒ Object



359
360
361
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 359

def firebird_db_string(config)
  FireRuby::Database.db_string_for(config.symbolize_keys)
end

#less_than_active_record_5_2?Boolean

Returns:

  • (Boolean)


407
408
409
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 407

def less_than_active_record_5_2?
  ActiveRecord.version < Gem::Version.create('5.2.0')
end

#less_than_active_record_6_0?Boolean

Returns:

  • (Boolean)


411
412
413
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 411

def less_than_active_record_6_0?
  ActiveRecord.version < Gem::Version.create('6.0.0')
end

#less_than_active_record_6_1?Boolean

Returns:

  • (Boolean)


415
416
417
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 415

def less_than_active_record_6_1?
  ActiveRecord.version < Gem::Version.create('6.1.0')
end

#migrate_as(type) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 374

def migrate_as(type)
  version = env_migration_version
  raise 'MIGRATION_VERSION is required' unless version

  if less_than_active_record_5_2?
    ActiveRecord::Migrator.run(type, 'db/migrate/', version)
  else
    migration_context.run(type, version)
  end

  dump_schema
end

#migration_contextObject



419
420
421
422
423
424
425
426
427
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 419

def migration_context
  if less_than_active_record_6_0?
    ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
  else
    connection = ActiveRecord::Base.connection
    connection = connection.pool unless connection.respond_to?(:migration_context)
    connection.migration_context
  end
end

#move_as(type) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 387

def move_as(type)
  step = ENV['STEP'] ? ENV['STEP'].to_i : 1

  if less_than_active_record_5_2?
    ActiveRecord::Migrator.send(type, 'db/migrate/', step)
  else
    migration_context.send(type, step)
  end

  dump_schema
end

#resolve_structure_sqlObject



403
404
405
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 403

def resolve_structure_sql
  "#{Padrino.root}/db/#{Padrino.env}_structure.sql"
end

#rootObject

Require initializers before all other dependencies. Dependencies from 'config' folder are NOT re-required on reload.



45
# File 'lib/padrino-gen/generators/project/config/boot.rb', line 45

Padrino.dependency_paths.unshift Padrino.root('config/initializers/*.rb')

#set_firebird_env(config) ⇒ Object



354
355
356
357
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 354

def set_firebird_env(config)
  ENV['ISC_USER']     = config[:username].to_s if config[:username]
  ENV['ISC_PASSWORD'] = config[:password].to_s if config[:password]
end

#with_all_databases(&block) ⇒ Object



443
444
445
446
447
448
449
450
451
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 443

def with_all_databases(&block)
  if less_than_active_record_6_0?
    ActiveRecord::Base.configurations.each_value(&block)
  else
    ActiveRecord::Base.configurations.configs_for.each do |db_config|
      block.call(configuration_hash(db_config))
    end
  end
end

#with_database(env_name) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/padrino-gen/padrino-tasks/activerecord.rb', line 429

def with_database(env_name)
  if less_than_active_record_6_0?
    config = ActiveRecord::Base.configurations.with_indifferent_access[env_name]

    yield config
  else
    db_configs = ActiveRecord::Base.configurations.configs_for(env_name: env_name.to_s)

    db_configs.each do |db_config|
      yield configuration_hash(db_config)
    end
  end
end