rails test database cloning dev database - ruby-on-rails

I'm new to Ruby on Rails and following Michael Hartls Ruby on Rails Tutorial. When I run bundle rake exec test, I am getting this error:
ERROR["test_should_get_new", SessionsControllerTest, 2.331314]
test_should_get_new#SessionsControllerTest (2.33s)
ActiveRecord::StatementInvalid:
ActiveRecord::StatementInvalid: Could not find table 'users'
and once similar for all of my tests. I see that my test database: test.sqlite3 is empty. My development database development.sqlite3 does have the users table in it.
I tried running the following to attempt cloning the users database to the test database, but nothing is working:
rake db:migrate:reset
rake db:migrate db:test:prepare
rake db:test:clone
rake db:test:prepare
rake db:test:load
but it's still empty. Any suggestions please?

I think you might be able to get it with:
RAILS_ENV=test rake db:migrate
Give it a try, I've had this problem before myself. I believe that
rake db:test:prepare
is deprecated. What version of ruby and rails are you using?

I figured out a fix, although I'm still not sure why there was a problem to begin with. What I did was I took the pure Sql CREATE TABLE used to create the development table and executed it in the users table. Now everything is working fine.

Related

Rails Project automatically deleted after rake db:reset

I was working on Nitrous.io on a Rails Project.
I have created a new branch and then after some coding , I reset the database with
rake db:reset.
After this action my Projects folders were automatically deleted.
Someone could help me understand what is wrong on my command?
db:reset will do exactly as described - it will "reset" the database. So db:reset is the same as executing the following commands: db:drop (which will drop/delete all entries in the database) + db:setup
I somehow doubt it that rake db:reset deletes your .rb files. Must be something else. Maybe you messed up with git?
rake db:reset
rake db:create
rake db:migrate
works just fine, normally.

Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue [unable to proceed]

I appear to have a circular issue in regards to Ruby on Rails migration procedure. I am following the introduction article and I have reached the point when I need to create my first table.
I have ran the following,
[tims#web2 working_ror]# rails generate model Homepage first_name:string last_name:string email:string message:text
invoke active_record
create db/migrate/20131119203948_create_homepages.rb
create app/models/homepage.rb
invoke test_unit
createtest /models/homepage_test.rb
createtest /fixtures/homepages.yml
I then proceeded with the migration,
[tims#web2 working_ror]# rake db:migrate
== CreateHomepages: migrating ================================================
-- create_table(:homepages)
-> 0.0493s
== CreateHomepages: migrated (0.0494s) =======================================
, however, when I run my application I see the following message,
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
but, IF I run the above,
[tims#web2 working_ror]# rake db:migrate RAILS_ENV=development
[tims#web2 working_ror]#
and the message continues ...
I have spent considerable amount of time researching forums in-which the closest I could find was to drop and re-build everything, which have done the following.
rake db:drop
rake db:create
rake db:migrate
and the results are the same.
You need to do
bundle exec rake test:prepare
or
bundle exec rake db:test:prepare
and then
bundle exec rake db:migrate
before running the specs
Cheers
cited from : Why am I asked to run 'rake db:migrate RAILS_ENV=test'?
you can do
bundle exec rake test:prepare
In Rails 4.1+, they deprecated db:test:prepare
You can now just use:
ActiveRecord::Migration.maintain_test_schema!
If you need to do it manually
rake db:schema:load RAILS_ENV=test
and then
bundle exec rake db:migrate
try
In RAILS_ROOT/config/environments/development.rb Set the following setting to false:
config.active_record.migration_error = false#:page_load
One weird trick that you can use when your migrations are screwed (file deleted, manually renamed, etc.)
Fire up your favourite DB admin tool (eg. PGAdmin3) and browse to the database in question.
Look for a table called schema_migrations and browse its content. It should have a single column called version. This field is used by Rails to check whether migrations are up to date.
Make sure that your migration timestamps corresponds with the data in this column. If you have deleted an older migration, delete the corresponding timestamp.
Check to make sure that table doesn't already exist:
type - rails dbconsole
type - .tables (check to see if there was an error during the rake db:migrate that has the table name like -- create_table(:test) rake aborted!)
If you see the table name after running the .tables in the console type - drop table TABLENAME;
Then .quit to go back to the branch and run the rake db:migrate command again.
this was what i did:
rails db:environment:set RAILS_ENV=test
If you need to do it manually
rake db:schema:load RAILS_ENV=test
and then
bundle exec rake db:migrate
Thanks to Ahmed Ali....... your comment was helpful.

What does rake db:test:prepare actually do?

I am following the rails tutorial videos and I can't figure out what the db:test:prepare command actually does. Can someone provide an explanation?
The rake db:migrate above runs any pending migrations on the
development environment and updates db/schema.rb. The rake
db:test:load recreates the test database from the current
db/schema.rb. On subsequent attempts, it is a good idea to first run
db:test:prepare, as it first checks for pending migrations and warns
you appropriately.
-- http://guides.rubyonrails.org/testing.html
Basically it handles cloning the database so you don't have to run the migrations against test to update the test database.
Specifically, rake db:test:prepare will do the following:
Check for pending migrations and,
load the test schema
That is, it will look your db/schema.rb file to determine if any migrations that exist in your project that have not been run. Assuming there are no outstanding migrations, it will then empty the database and reload it based on the contents of the db/schema.rb file.
rake db:test:prepare is a good solution for PG issues like this.
“PG::UndefinedTable: ERROR: relation does not exist” with a correct Rails naming and convention" where I couldn't just execute rake db:migrate RAILS_ENV=production
When, for example you can't create test database for a bug discussed here: "PG undefinedtable error relation users does not exist"
All arround this error
"PG::UndefinedTable: ERROR: relation xxxxx does not exist”

Recreate database+tables (it dosn't want to work with rake commands) ROR 3

I'm newbie with ror and I just managed to ruin the databse... I tried to recreate with this 2 methodes bu none works:
first:
rake db:reset
rake db:migrate
second:
rake db:drop
rake db:create
rake db:migrate
But none work... I'm out of ideas what to do, please help...
My migrate folder is empty, if I migrate nothing happens, no table created notice.
Try using:
rake db:migrate:reset
examine your database.yml file, it might default to SQLITE which is basically a text file.
Make sure you are using the correct database with the correct name.

Rake db:migrate updates schema.rb, but not Mysql table. Any ideas why? [duplicate]

I have two instances of my app: one for development, one for production. My development database is called snip_development and my production database is called snip.
I've been doing migrations all along in my development environment and it's been going just fine. I recently created a production instance of my app but rake db:migrate doesn't seem to have any effect. After I run rake db:migrate and log into my database server, I can see that snip_development has all the tables I expect it to but snip doesn't have any tables at all.
I suspect the problem is that rake db:migrate is running on snip_development instead of snip and that's why I'm not seeing anything happen.
How do I get my migrations to work on my production database?
Sometimes I forget about Google. The answer is this:
rake db:migrate RAILS_ENV=production
For me the answer above not works. I have to add bundle exec to make it works.
bundle exec rails db:migrate RAILS_ENV=production

Resources