rake db:migrate fail. No terminal error - ruby-on-rails

I am working on the onemonthrails course.
I need to execute a migration using the below rake command. The devise migration file looks to be setup correctly.
rake db:migrate
I am expecting to see some kind of confirmation like the following:
=======DeviseCreateUsers: Migrating=======
create table (:users)
-> 0.0145s
add_index (:users :index etc...
etc..
Instead, my terminal reverts straight back to the command line (with no errors) but nothing appears to have been done. For example:
Petes-Computer:example Pete$ rake db:migrate
Petes-Computer:example Pete$
The below error in my browser confirms it didn't work.
ActiveRecord::StatementInvalid in Devise::RegistrationsController#new
Could not find table 'users'
There are a couple of other posts on this but no luck resolving. I am very new to ruby/rails/rake; please could someone advise.

After our discussion, it turns out Pete's migration file didn't have an extension. rake db:migrate didn't pick up the migration because it wasn't an .rb file.

Related

How do I run a server on ruby on rails?

I want to link to localserver:3000 on ruby on rails but when I use bundle exec rails server, it just exits and doesn't generate the server.
➜ demo git:(master) ✗ bundle exec rails server
=> Booting Puma
=> Rails 5.2.0 application starting in development
=> Run `rails server -h` for more startup options
Exiting
This is my route.rb if it helps!
Rails.application.routes.draw do
root "pages#home"
end
I created a controller called pages and created a view called home. When I first generated the server, I already ended the session so I'm wondering how I can generate the server again so I can see my "home" page.
This is what happens when I try to get the server. Can anyone explain to me what could be wrong? Thank you!
Edit:
I created my controller by using
bundle exec rails g controller pages
Then I added home.html.erb under views.
I managed to connect to the server but this is the error I see on my page:
I ran the command but nothing happened. I copied and pasted
bin/rails db:migrate RAILS_ENV=development
and this was what I got:
I recommend you to check the status of all your migrations, maybe there is another migration pending. Use this command rake db:migrate:status to see all migrations status.
You can also reset your db and do the migrations all over again.
rake db:reset (which runs db:drop then db:setup) then run your migrations rake db:migrate.
Also, please check if any of your migrations are referencing on any other migration that is still not created!
P.S. Please add the complete error you are getting, so I can have a better idea what the problem is!
At this point, I think either of this should be the problem.
You cloned the files that was built on Mac to run on windows or Vice versa.
There are some queries you cannot pass to the database. Pending the type of database, you are using with your Rails application.
Crosscheck if your database settings are correct.
That's it, Try this.
1. For rails, 5+ do rails db:rollback
Bundle install (just to see if everything works well)
Run bin/rails db:migrate RAILS_ENV=development. if it still doesn't work, Look through the error report on your terminal, you should be able to see the things that are resisting the "migrate" command.
start your server with rails s --port=PORT_NUMBER. (e.g rails --port=4000)
You have a problem with the db/migrations/20180619074210_create_users.rb file. Delete it as the migration already failed. You do not need to undo it (rollback).
Also, I would highly recommend using command line to create a migration file until you are familiar with them. And if you have time, checkout Rails guides for Active Record Migrations.
Example:
1) Create a user table with name column (string) and age column (integer):
rails generate migration CreateUsers name:string age:integer
2) This will create a similar file with a different timestamp. In my machine I got db/migrate/20180626151529_create_users.rb.
3) If you open that file, you can examine the changes:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.integer :age
end
end
end
4) If all looks good, you need to run this migration file. That can be achieved by:
rake db:migrate

Pending Migration Error | Cannot Remove Dupe Migrations

I am trying to run a rails server and am coming across this error:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
The DB in question is currently empty so no worries of losing data. When I run the code:
bundle exec rake db:migrate
RAILS_ENV=development rake db:migrate
rake db:migrate
I am returned the error:
rake aborted!
ActiveRecord::DuplicateMigrationNameError:
Multiple migrations have the name CreatePosts
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
When running the server, this is returned to my terminal:
Started GET "/" for ::1 at 2015-09-22 11:30:34 -0400
ActiveRecord::PendingMigrationError (
Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=development
):
And finally, running rake db:migrate:status returns:
Schema migrations table does not exist yet.
What could possibly be going on? Any help is greatly appreciated!
Output of migrate:status after bin/rake command
Status Migration ID Migration Name
--------------------------------------------------
up 20150922142819 Devise create users
up 20150922143253 Create posts
down 20150922143414 Create posts
down 20150922145906 Acts as votable migration
down 20150922150209 Create comments
down 20150922151035 Acts as follower migration
You must create the database schema/structure before you run migrations. db:schema:load will do this for you:
bin/rake db:schema:load
As for the error "Multiple migrations have the name CreatePosts" - it's as it says. Look in the contents your db/migrate folder for two files that have the same name class CreatePosts within the files - they should be named differently, or the second one should be removed if they are duplicates.
It seems like you have more than one migration with the same name. This is not allowed. If your database is empty, no tables, the best plan is to rename the second one so they don't conflict any more.
Obviously the filenames are unique, but each migration file defines a class, and these classes must be unique as well, plus should match the filename.
Having 201509201949343_create_posts and 201509220293910_create_posts would be a typical conflict here.
You may want to investigate how this came to pass, as having your development schema diverge from the production one is generally a bad idea. Once you resolve the conflict locally, try and update the schema tracking table on production accordingly.

Rails pending migration in rake db:test:prepare

I've run rake db:migrate and all of my migrations ran. However, when I try to run rake db:test:prepare I get the error:
You have 1 pending migrations:
20130724211328 CreateGalleries
Run `rake db:migrate` to update your database then try again.
Then running rake db:migrate again gives the error:
PG::Error: ERROR: relation "galleries" already exists...
But in the console I can create and manipulate the Gallery model exactly as shown in the CreateGalleries migration. The table is not being created or even mentioned in any other migrations.
It seems the migration ran just fine but did not register. Any ideas how to fix this?
EDIT
I solved this with rake db:drop db:create db:migrate then rake db:test:prepare, but I'm happy to give the solution to anyone who can shed light on what caused the problem in the first place.
I suspect the migration for galleries hasn't been executed properly. If you're 100% sure everything is right in your table, you can bump up the migration version to the version of the galleries migration.
To do this, find the timestamp of your galleries migration (the 14 numbers in front of your migration file, in this case 20130724211328) and insert this as a new row into the table schema_migrations (which is done automatically by Rails after successfully executing a migration).
If the table is empty, you could also drop the table galleries and run rake db:migrate again. This way you can also make sure your migration doesn't trigger any errors.

rake db:rollback not working?

I'm writing my first Rails app. I've run a few rails generate model ... and rake db:migrate commands, but I now want to change my data model and so need to undo a few migrations.
The docs say I can undo a migration with rake db:rollback, but this isn't working. When I run this in the console the computer thinks for a few seconds but doesn't make any changes to db/migrate/ or db/migrate/schema.rb. No output is printed to the console.
Is this behavior correct? Shouldn't db:rollback be changing my schema? If so, can anyone think why it might not be working?
I'm on Rails v. 3.2.6.
EDIT
At the moment rake db:migrate:status gives
database: db/development.sqlite3
Status Migration ID Migration Name
--------------------------------------------------
up 20120617191211 Create irs
up 20120701154357 Create paths
up 20120701154421 Create nodes
up 20120702151447 ********** NO FILE **********
down 20120702155140 Create venues
down 20120703233833 Remove path from venues
Solution (see my comment): run
rake db:migrate:status
and correct problems you find there. In this case (per #MarkThomas' followup), you might want to check all files you need are in place.
This is what worked for me. Combine the steps given in this answer and comment by dB.
run rake db:migrate:status
If you have a ****NO FILE**** entry, just note the version number as noFileVersion. Note the version of the entry just above no file entry(stable_version).
created a "dummy" file with name noFileVersion_create_nothing.rb, and body
class CreateNothing < ActiveRecord::Migration[6.0]
def change
end
end
run rake db:migrate VERSION=stable_version
remove the noFileVersion_create_nothing.rb manually.
run rake db:migrate.
run rake db:migrate:status again to check if No file entry has disappeared.
The following worked for me:
rake db:migrate:down VERSION=20190304092208
Version number can be obtained by the following command:
rake db:migrate:status
This was the last version number to rollback one last migration
NOTE: Use the suggestions with caution, it is very dangerous for non-dev environments.
If
rake db:migrate:status
gives you a migration that says
up 20120702151447 ********** NO FILE **********
Then the best thing to do would be to do a (note that the following command will drop the database):
rake db:reset
to redo all migrations. If the last migration is the one missing, then schema.rb will have the last migration that rake db:migrate will look for:
ActiveRecord::Schema.define(:version => 20120702151447) do
Change that number to the last one in your migrate folder.

seems that rails is replay a migration

when I new a migration and run it,error occurred:
$ rake db:migrate
== CreateEReadings: migrating ================================================
-- create_table(:e_readings) rake aborted! An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "e_readings" already exists
while this e_readings is the last table I created using migration.
The migration file name is :20120508145115_create_e_readings.rb
and the version in db/schema.rb is :
:version => 20120508145115
Seems that rails forget that I already have run this migration and try to re-play it, so error occurred,but why is this happening and how can I solve this?
It seems like you may have run it before and it failed after creating the table for some reason. If you're sure it has already run, you can manually add a record to the "schema_migrations" table with the 20120508145115 as the version.
If this is just a dev environment and you don't mind blowing it away, you could also run rake db:reset and that would drop, create, load the schema and reseed it.
I think beerlington is right. Your migration probably failed the first time you ran it. In addition to his suggestions you could also try manually dropping the table from the database and re-running the migration to see what went wrong the first time
I agree with both Beerlington and Andy. If it's a development environment, try the following in the terminal:
rake db:drop
rake db:create
rake db:migrate
That will destroy your database, recreate it and run all your migrations.
The other thing you could try is to rollback using rake db:rollback until you see that this migration (or the one before it) has been rolled back, and then run rake db:migrate to run from that point till your latest point again. rake db:rollback rolls back one migration file at a time.
I'd go with the drop and recreate, though, just to make sure nothing funny has remained.
Hope this helps.

Resources