Ruby on Rails 2.3.5: Filling a prod and devel database with data (migration or binding?)

I need to populate my production database application with data in specific tables. This is before anyone even touches the application. This data will also be required in development mode, as required for testing. Adaptations are generally suitable for testing data, but what is the “best practice” for Ruby on Rails to send this data to a live database also when creating db?

Ultimately, this is a two-part question, which I assume.

1) What is the best way to upload test data to my development database, it would be about 1,000 elements. Is it through migration or through fixtures? The reason is that this is a different answer than the question below: in the development there are certain fields in the tables that I would like to make random. In production, these fields will begin with the same value 0.

2) What is the best way to load the source db with the live data that I need in it, is it also using migration or armature?

I think the answer is in the seeds, as described here: http://lptf.blogspot.com/2009/09/seed-data-in-rails-234.html , but I need a seed method for development and seed for production In addition, why use Fixtures if seeding is available? When does one seed occur and when is the appliance used?

+3
source share
2 answers

Typically, lights are used to provide your tests with data, not to populate the data in your database. You can - and some people, like the links you point to - use lights for this purpose.

The lights are fine, but using Ruby gives us some advantages: for example, the ability to read from a CSV file and fill out records based on this data set. Or read from the YAML binding file if you really want to: starting from your start with a programming language, your options are wide open from there.

db/seed.rb RAILS_ENV .

db: seed , : - -... , ( ActiveRecord find_or_create_by...() ).

Bootstrapper, DSL RAILS_ENV , , .

- , . Ruby (db/bootstrapdata/) Arild Shirazi required gem ( ) .

. -, (, ).

: ( , , Rails, . ).

+3

, , .

, ,

  desc 'Export the data objects to Fixtures from data in an existing 
  database.  Defaults to development database.  Set RAILS_ENV to override.'
  task :export => :environment do
    sql  = "SELECT * FROM %s"
    skip_tables = ["schema_info"]
    export_tables = [
      "roles", 
      "roles_users", 
      "roles_utilities",
      "user_filters", 
      "users",
      "utilities"
    ]

    time_now = Time.now.strftime("%Y_%h_%d_%H%M")
    folder = "#{RAILS_ROOT}/db/fixtures/#{time_now}/"
    FileUtils.mkdir_p folder
    puts "Exporting data to #{folder}"

    ActiveRecord::Base.establish_connection(:development)
    export_tables.each do |table_name|
      i = "000"
      File.open("#{folder}/#{table_name}.yml", 'w') do |file|
        data = ActiveRecord::Base.connection.select_all(sql % table_name)
        file.write data.inject({}) { |hash, record|
          hash["#{table_name}_#{i.succ!}"] = record 
          hash }.to_yaml
      end
    end
  end

  desc "Import the models that have YAML files in 
  db/fixture/defaults or from a specified path."
  task :import do
    location = 'db/fixtures/default' 
    puts ""
    puts "enter import path [#{location}]"
    location_in = STDIN.gets.chomp
    location = location_in unless location_in.blank?
    ENV['FIXTURES_PATH'] = location
    puts "Importing data from #{ENV['FIXTURES_PATH']}"
    Rake::Task["db:fixtures:load"].invoke
  end
0

Source: https://habr.com/ru/post/1744261/


All Articles