How to Create Database from Schema.Rb Without Initializing Rails

How to create database from schema.rb without initializing Rails?

You can add a check for the table existance in your initializer.

if TheModel.table_exists?
// do something with the model
end

how to run schema.rb?

Use the schema load task:

rake db:schema:load

From rake -T (expurgated version):

rake db:schema:dump  # Create db/schema.rb file usable with any AR-supported DB
rake db:schema:load # Load schema.rb file into DB

Difference between rake db:migrate db:reset and db:schema:load

  • db:migrate runs (single) migrations that have not run yet.

  • db:create creates the database

  • db:drop deletes the database

  • db:schema:load creates tables and columns within the existing database following schema.rb. This will delete existing data.

  • db:setup does db:create, db:schema:load, db:seed

  • db:reset does db:drop, db:setup

  • db:migrate:reset does db:drop, db:create, db:migrate

Typically, you would use db:migrate after having made changes to the schema via new migration files (this makes sense only if there is already data in the database). db:schema:load is used when you setup a new instance of your app.



For rails 3.2.12:

I just checked the source and the dependencies are like this now:

  • db:create creates the database for the current env

  • db:create:all creates the databases for all envs

  • db:drop drops the database for the current env

  • db:drop:all drops the databases for all envs

  • db:migrate runs migrations for the current env that have not run yet

  • db:migrate:up runs one specific migration

  • db:migrate:down rolls back one specific migration

  • db:migrate:status shows current migration status

  • db:rollback rolls back the last migration

  • db:forward advances the current schema version to the next one

  • db:seed (only) runs the db/seed.rb file

  • db:schema:load loads the schema into the current env's database

  • db:schema:dump dumps the current env's schema (and seems to create the db as well)

  • db:setup runs db:create db:schema:load db:seed

  • db:reset runs db:drop db:setup

  • db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration

  • db:migrate:reset runs db:drop db:create db:migrate

For further information please have a look at https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/railties/databases.rake (for Rails 3.2.x) and https://github.com/rails/rails/blob/v4.0.5/activerecord/lib/active_record/railties/databases.rake (for Rails 4.0.x)

Dynamic database-based routes in Rails preventing schema:load

An answer which my coworker found was to do a check via ActiveRecord::Base.connection to see if the database table existed before running the code.

It now looks like this:

if (Rails.env.development? || Rails.env.staging? || Rails.env.uat?) && ActiveRecord::Base.connection.table_exists?('countries')
Country.all.each do |country|
get "/#{country.locale}", to: 'home_pages#index', locale: country.locale
end
end

Can't run rake db:schema:load unless the database is already loaded

That's the problem with using arel on scope. It may affect the migration. The simple solution is to just go raw SQL.

scope :unread, where('read = false')

The longer answer is that the class is somehow loaded when running the migration (it's normally is not loaded). If you can find what causes the loading of that class during the migration and work around it, then you can still use arel_table in the scope. But probably it's not worth it.

How to dynamically set schema_format for custom db rake tasks in Rails?

Try this:

  task :set_custom_db_config_paths do
ENV['SCHEMA'] = 'db_other/schema.rb'
Rails.application.config.paths['db'] = ['other']
Rails.application.config.paths['db/migrate'] = ['db_other/migrate']
Rails.application.config.paths['config/database'] = ['config/database_other.yml']
Rails.application.config.active_record.schema_format = :ruby
end

Note the last line setting Rails.application.config.active_record.schema_format = :ruby



Related Topics



Leave a reply



Submit