How can I create a production database in Rails 3 and load a schema to it?
I tried the following approaches...
I.
rake db:create Rails.env='production' && rake db:schema:load Rails.env='production'
II.
# config/environment.rb
# Set the rails environment
Rails.env='production'
rake db:create && rake db:schema:load
... but neither of them works.
Thanks.
Debian GNU/Linux 5.0.6;
Rails 3.0.0;
Sqlite3 3.7.2.
You can set the rails env off of the environment variable RAILS_ENV
RAILS_ENV=production bundle exec rake db:create db:schema:load
should work
Shouldn't this be
RAILS_ENV=production bundle exec rake db:create db:schema:load
Related
I have an application in rails 4.2.7.1, when I run the db:migrate in test in works perfectly
➜ bundle exec rake db:drop RAILS_ENV=test
➜ bundle exec rake db:ceate RAILS_ENV=test
➜ bundle exec rake db:migrate RAILS_ENV=test
But when I ran it in development, only the last migration or beginning from a drop database after executing firs migration I get this:
➜ bundle exec rake db:migrate -- --trace
== 20191112204156 MyNewModel: migrating ==============================
-- create_table(:global_clients)
-> 0.0121s
== 20191112204156 MyNewModel: migrated (0.0122s) =====================
rake aborted!
SystemStackError: stack level too deep
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I'm using ruby 2.1.6, and mysql 0.318 and later 0.4.10, I did rvm implode to start it again and nothing happens. I do not know how to solve it, even I do not know how to debug it. So any tips will be good.
It's a problem with dthe difference from rais test and development environments
this is what I get when I run the project
Hi, I'm working on a project.
When I run my project, I got this problem:
Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=development
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?
(connection)
I've already tried several solutions below:
1)
rake db:drop
rake db:create
rake db:migrate
2) bundle exec rake db:migrate
3) bin/rake db:migrate RAILS_ENV=development
but they didn't work, and I got the same error over and over again.
What can I do?
Your first try was close, you need to do
rake db:drop
rake db:create
rake db:schema:load
Check this article it may be helpful.
http://icebergist.com/posts/rake-db-migrate-vs-rake-db-schema-load/
$ rm db/schema.rb
$ bundle exec rake db:drop
$ bundle exec rake db:create
$ bundle exec rake db:migrate
Or just rake db:reset. That always works for me, when I get stuck.
What is the difference between rake db:create and rake db:create:all?
Both are equally used in order to create a database for a Rails application.
The most exhaustive information on rake for Rails I could find is at tutorialpoint but the above commands are missing.
rake db:create:all creates all the databases for the application (which are defined in database.yml)
rake db:create creates the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
FYI: http://jacopretorius.net/2014/02/all-rails-db-rake-tasks-and-what-they-do.html
One creates the DB for the current environment.
One creates the DB for all environments.
If you run rake -T | grep db, you will see :
rake db:create
# Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV
# (use db:create:all to create all databases in the config)
What's the difference between adding a RAILS_ENV before or after a rake task? Here are samples from my staging environments:
Adding RAILS_ENV after rake task.
This raised an error, and the reason for this is accepting development environment as by default and not taking devutility as the environment.
$bundle exec rake -T RAILS_ENV=devutility
$rake aborted!
$cannot load such file -- rack/bug
Adding RAILS_ENV before rake task
This works and lists all the rake task available.
$RAILS_ENV=devutility bundle exec rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake bourbon:install[sass_path] # Move files to the Rails assets directory
rake ci # Continuous Integration build (simplecov-rcov and deploy)
rake cucumber # Alias for cucumber:ok
rake cucumber:all # Run all features
rake cucumber:ok # Run features that should pass
rake cucumber:rerun # Record failing features and run only them if any exist
rake cucumber:wip # Run features that are being worked on
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:data:dump ....................
..............
RAILS_ENV is an environment variable that needs to be available before running your rake task.
When you do:
RAILS_ENV=devutility bundle exec rake -T
It has the same affect as:
export RAILS_ENV=devutility
bundle exec rake -T
RAILS_ENV is not an argument to rake as it may appear, it's part of the environment available to Ruby though it's ENV constant.
Utterly confused at this mess:
rake db:drop
>
rake db:create
> my_database already exists
rake db:migrate
> unknown database my_database
Appreciate any insight.
That's because first time when u do rake db:create it creates two databases one is development and other is test. then when u do rake db:drop it drops the database, but it only drops the development database not the test database. so try removing the test database explicitly and everything should be fine.
Try :
rake db:drop
rake db:create RAILS_ENV=development
rake db:migrate RAILS_ENV=development
if getting same error then open mysql terminal and create database manually :
CREATE DATABASE database_name;
then run
rake db:migrate