The following command is supposed to create a new database.
rails db:create
Where is this function defined? Or is this a pre-packaged function in rails?
It's in the databases.rake file of the framework:
namespace :create do
task all: :load_config do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
ActiveRecord::Tasks::DatabaseTasks.for_each do |spec_name|
desc "Create #{spec_name} database for current environment"
task spec_name => :load_config do
db_config = ActiveRecord::DatabaseConfigurations.config_for_env_and_spec(Rails.env, spec_name)
ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
end
end
end
Whenever you doubt or want to know where a task has been defined, you can use the rails -W (or rake) command, passing the task:
$ rails -W db:create
rails db:create /path/databases.rake:26:in `block in <top (required)>'
rails db:create:all /path/databases.rake:20:in `block (2 levels) in <top (required)>'
Note it was introduced in the version 0.9 of Rake. This might or might not work depending on the versions which you're working with.
Related
On Rails 6 (6.1.4.1) we had a RakeFile that would run a subset of tests. For example:
# lib/tasks/carrier.rake
namespace :test do
task carriers: "test:prepare" do
$: << "test"
test_files = FileList["test/models/carrier_test.rb",
"test/controllers/admin/carriers/**/*_test.rb",
"test/system/admin/carriers/**/*_test.rb"]
Rails::TestUnit::Runner.run(test_files)
end
end
This would execute just fine when called:
rails test:carriers
However, somewhere along the way, something changed and we began seeing errors when trying to run our RakeFile test tasks. (I haven't tracked down exactly what changed and when it changed -- perhaps it was part of the Rails 7 release.) Here's the error we began seeing:
rails aborted!
NameError: uninitialized constant Shoulda
Shoulda::Matchers.configure do |config|
^^^^^^^
/path/test/test_helper.rb:15:in `<main>'
/path/test/models/carrier_test.rb:1:in `<main>'
/path/lib/tasks/carriers.rake:11:in `block (2 levels) in <main>'
Tasks: TOP => test:carriers
(See full trace by running task with --trace)
The error appeared with no changes to our tests or environment configuration. (Running a full rake worked just fine.)
When reviewing the source code for Rails::TestUnit::Runner, I came across rake_run. Simply replacing Rails::TestUnit::Runner.run with Rails::TestUnit::Runner.rake_run addressed the issue (no other changes required):
# lib/tasks/carrier.rake
namespace :test do
task carriers: "test:prepare" do
$: << "test"
test_files = FileList["test/models/carrier_test.rb",
"test/controllers/admin/carriers/**/*_test.rb",
"test/system/admin/carriers/**/*_test.rb"]
Rails::TestUnit::Runner.rake_run(test_files)
end
end
I have a rake task which is called by a third party service. In my application I need to set Customer before starting the application. I could not access the model variable from the rake task. But this happens only in Production not in Development. When I run
RAILS_ENV = 'production' rake auth:tester
I get the followng error.
rake aborted!
Unable to autoload constant CUSTOMER, expected /home/suganya/academics/tsg-mcds-server/app/models/customer.rb to define it
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:495:in `load_missing_constant'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:184:in `const_missing'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:11:in `xml'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:38:in `set_customer_settings'
/home/suganya/academics/tsg-mcds-server/config/initializers/customer_config.rb:710:in `<top (required)>'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
/home/suganya/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.2.7.1/lib/rails/engine.rb:652:in `block in load_config_initializer'
I have referred all the related questions. When I enable in poduction.rb
config.threadsafe!
config.dependency_loading = true if $rails_rake_task
I get the following error.
undefined method `threadsafe!' for #<Rails::Application::Configuration:0x00000004aebdf8>
Also I tried
Rails.application.eager_load!
this is my rake task:
namespace :authentication do
desc "Automatically runs authentication tester"
task :tester => :environment do
Rake.application.rake_require "#{Rails.root}/config/initialize_customer_in_development.rb"
begin
AuthenticationTester.perform
rescue
SystemEvent.error(50049, "Authentication Tester Finished Unsuccessfully.")
end
end
end
Nothing helped. please help to solve this problem.
I'm a beginning rails student at OneMonth learning stripe payments. Right now, I'm learning how to create a product table and seed the database. Everything's been smooth so far until I encountered a problem after running rake db: seed
Davids-MBP-2:one_month_stripe_payments DKP$ rake db:seed
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/validations.rb:79:in raise_record_invalid'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/validations.rb:43:insave!'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/attribute_methods/dirty.rb:29:in save!'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:inblock in save!'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/transactions.rb:351:in block in with_transaction_returning_status'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:inblock in transaction'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in within_new_transaction'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:intransaction'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in transaction'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/transactions.rb:348:inwith_transaction_returning_status'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:in save!'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/persistence.rb:51:increate!'
/Users/DKP/Desktop/one_month/one_month_stripe_payments/db/seeds.rb:8:in <top (required)>'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:inload'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in block in load'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:inload_dependency'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in load'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/railties-4.2.5/lib/rails/engine.rb:547:inload_seed'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb:250:in load_seed'
/Users/DKP/.rvm/gems/ruby-2.3.0#global/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:183:inblock (2 levels) in '
/Users/DKP/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in eval'
/Users/DKP/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
I've tried rake db: reset, then rake:db migrate, then rake db: seed again. But i still have the same error.
Could anyone help me with this problem please? I'm feeling stuck and I'm not sure what to do.
Thanks so much!
When you do rake db:reset, what rails does is rake db:drop -> rake db:create -> rake db:migrate -> rake db:seed.
You DO NOT need to rake db:seed again.
you can check your console by doing rails c and see if you seed data is already inside. Product.all
'Email has already been taken' means you have a record with the same email already present.
Do Rails console and check if the records are there or not.
If not, do rake db:drop -> rake db:create -> rake db:migrate -> rake db:seed. This will solve your problem.
Some of my rSpec tests are failing and I suspect it's due to the test database not being cleared out. When I try to run rake db:test:prepare, I'm getting the error below (i'm in Rails 3.2). Clearly, this looks like some kind of Postgres issue, looks like the rake task is trying to drop the test database so it can repopulate it. Production database schema loads fine.
I've looked for the rake task in lib/tasks/ but I haven't been able to find it. Anyone have any idea on what's going on?
osx_user-> rake db:test:prepare
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
rake aborted!
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: DROP DATABASE IF EXISTS ""
^
: DROP DATABASE IF EXISTS ""
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `async_exec'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `block in execute'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activesupport-3.2.14/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `execute'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/connection_adapters/postgresql_adapter.rb:772:in `drop_database'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/railties/databases.rake:623:in `drop_database'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/railties/databases.rake:532:in `block (3 levels) in <top (required)>'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-3.2.14/lib/active_record/railties/databases.rake:559:in `block (3 levels) in <top (required)>'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `eval'
/Users/osx_user/.rvm/gems/ruby-1.9.3-p547/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)
Figured it out. My database.yml file for the development environment was set like this:
database: <%= ENV['DATABASE_NAME_TEST'] %>
The rake task was looking for an environmental variable that did not exist.
I have a naked rails 3 app with one model, generated using rails g model User.
I've added a factory (using factory_girl_rails):
Factory.define :user do |f|
f.email "test#test.com"
f.password "blah"
f.password_confirmation "blah"
f.display_name "neezer"
end
Then I've added one test:
require 'spec_helper'
describe User do
subject { Factory :user }
it "can be created from a factory" do
subject.should_not be_nil
subject.should be_kind_of User
end
end
Then I migrate my database using rake db:migrate.
Then I run the test using rspec spec, and the test fails with the following:
Failures:
1) User can be created from a factory
Failure/Error: subject { Factory :user }
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
I'm confused, because I did just migrate my database, and my schema.db file reflects that there is a users table present, so what gives?
I know this is a beginner question, but banging my head against a wall isn't working...
factory_girl (1.3.3)
factory_girl_rails (1.0.1)
rails (3.0.5)
rspec-rails (2.5.0)
sqlite3 (1.3.3)
Try to execute
rake db:test:prepare
This should fix your tests db.
The point here is that rspec command doesn't execute migrations on your test database. and rake db:migrate only runs migrations in your current environment, probably development. Others environment like production and test ends without having those changes.
You can run
rake spec
That will prepare your testing db (drop and create using schema.rb) and run all tests.
As the other answer suggested, this:
rake db:test:prepare
Will also setup your testing db, but you have to run the rspec command after that, so, personally I prefer the first option.
try this out:
For rails version > 4.1+ this solution will work as the current scenario.
but in Rails 4.1+, rake db:test:prepare is deprecated.
try using
rake db:migrate RAILS_ENV=test (it will work for all version of rails)