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
Rails: Serializing Objects in a Database
Using a Ruby Script to Login to a Website via Https
Adding an Instance Variable to a Class in Ruby
Array.Include? Multiple Values
How to Generate a Random Date in Ruby
Creating an Empty File in Ruby: "Touch" Equivalent
"Ago" Date/Time Functions in Ruby/Rails
Which Ruby Memoize Pattern Does Activesupport::Memoizable Refer To
How to Check from Ruby Whether a Process with a Certain Pid Is Running
Building a Hash in a Conditional Way
Is There a Good Admin Generator for Ruby on Rails
Object.Valid? Returns False But Object.Errors.Full_Messages Is Empty
Rails Flash Message Remains for Two Page Loads
How to Validate the Date Such That It Is After Today in Rails