Ruby Rails: ActiveRecord::PendingMigrationError - ruby-on-rails

When trying to deploy my Rails app to Railway I'm getting the following error:
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=development
You have 1 pending migration:
20221102234102_create_contacts.rb
I'm able to get the app online by clicking a button that appears below the message, which says "Run Pending Migration". I would like to Find a method to auto run that migration every deploy.
I've tried everything listed in Getting: "Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue." after cloning and migrating the project
So I've run:
rm -f db/*.sqlite3
rake db:create
RAILS_ENV=development bundle exec rake db:migrate
rails s -e development
to no success.
The contents of the "20221102234102_create_contacts.rb" file are as follows:
class CreateContacts < ActiveRecord::Migration[7.0]
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comments
t.timestamps
end
end
end
I'm completely new to web development and was following a free bootcamps videos to get this far, but they are using Heroku and that was unsuccessful, and since Heroku will no longer be free I figured I should try Railway out instead.

Railway will use a Procfile to start your service, so an easy way to make sure your migrations are run is to use this.
In the root folder of your app, add a file called Procfile (no extension)
Here's a sample from one of my apps:
web: /bin/bash -l -c "rake db:migrate && bundle exec puma -C config/puma.rb"
This first runs rake db:migrate then it starts a puma server using the configurations I've defined in config/puma.rb
This command runs once after a successful build.

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

Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development

I am rather new to Rails, and I am attempting to run an app from my console. When I open up the site, however, I see this error message. Does anyone have any insight on what it means?
When I try running "rails db:migrate RAILS_ENV=development" I just get:
Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development
When I try running just "rails db:migrate" or "rake db:migrate" I get:
ArgumentError: wrong number of arguments (0 for 1)
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:600:in `migrate'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:573:in `check_pending!'
Thank you so much!
To resolve this issue, run: bin/rails rake db:migrate RAILS_ENV=development
And the issue was raised due to pending migrations, which are created migration files at /db/migrate directory by doing add/remove fields to the existing active record or relation(table) in database.
Migrations are stored as files in the db/migrate directory, one for each migration class. The name of the file is of the form YYYYMMDDHHMMSS_create_products.rb, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example 20080906120000_create_products.rb should define class CreateProducts and 20080906120001_add_details_to_products.rb should define AddDetailsToProducts. Rails uses this timestamp to determine which migration should be run and in what order, so if you're copying a migration from another application or generate a file yourself, be aware of its position in the order.
Example:
$bin/rails generate migration AddPartNumberToProducts
This will create an empty but appropriately named migration:
class AddPartNumberToProducts < ActiveRecord::Migration[5.0]
def change
end
end
If the migration name is of the form "AddXXXToYYY" or "RemoveXXXFromYYY" and is followed by a list of column names and types then a migration containing the appropriate add_column and remove_column statements will be created.
$ bin/rails generate migration AddPartNumberToProducts part_number:string
will generate
class AddPartNumberToProducts < ActiveRecord::Migration[5.0]
def change
add_column :products, :part_number, :string
end
end
Referred at: http://guides.rubyonrails.org/active_record_migrations.html
Goto your app folder and run below mention command:
bin/rails db:migrate RAILS_ENV=development
I ran commands
# wait until this completes successfully before continuing
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
and it solved the problem for me.

PG::Error: ERROR: relation does not exist (rails 3.1.3) on development vs production

I've blown away my database and am now having problems migrating back to a working database.
The error looks the same as other questions already posted, but I can't figure out how to solve it. What can I change to get my environment back? How did it get like this?
I've verified the database does indeed exist, but the tables are not being created by the migrations step.
The error looks like:
$ RAILS_ENV="development" bundle exec rake db:test:prepare
rake aborted!
PG::Error: ERROR: relation "documents" does not exist
LINE 4: WHERE a.attrelid = '"documents"'::regclass
^
Note: I get the same result attempting any of:
$ bundle exec spec
$ RAILS_ENV="development" bundle exec rake db:migrate
$ RAILS_ENV="development" bundle exec rake db:test:prepare
$ RAILS_ENV="development" bundle exec rake db:schema:load
THEORY ONE:
It may have to do with the way my migrations are written: I have a class Paragraph:
class Paragraph < ActiveRecord::Base
belongs_to :document
validates_presence_of :document, :document_index
validates_uniqueness_of :document_index, scope: [:document_id]
However, originally when developing this I added the model "document" LAST. Should I somehow change the order of the migrations?
THEORY TWO:
When I run any of these with "production" they work fine (except for my tests of course).
Therefore the config error is influenced in some way by the database configuration.
What say you?
Before migrations are run, the whole Rails environment is initialized. So probably there is code executed during initialization process that tries to use the missing relation.
Run the rake task with --trace option to track this code down.

Getting: "Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue." after cloning and migrating the project

I cloned my project. Bundled with "bundle install", then run "rake db:migrate". I am getting this error: (when I run the rails server and open my browser to localhost:3000) "Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue."
I checked all migrations one by one and all were executed without errors. Also no errors were shown after the execution of "rake db:migrate".
This is what I see when I execute "rake db:migrate:status"
I am on development environment. Please let me know if you need any other information.
I also tried "bundle exec rake db:migrate", and "bundle exec rake db:migrate:reset" as "burninggramma" suggested.
Any clues what causes the error?
Interesting. Did you run rake db:create? Assuming you are using sqlite3, do this:
rm -f db/*.sqlite3
rake db:create
RAILS_ENV=development bundle exec rake db:migrate
rails s -e development
Also, can you list the contents of your config/database.yml file?
Edit: Warning! Obviously, you will lose your existing data.
After running the migrate command, I still had the same error.
What worked for me was to just stop the rails server and start it again.
List your executed migrations with rake db:migrate:status and look if every migration was executed. You can try to cancel your migration with rake db:abort_if_pending_migrations and try to migrate again.
1. Maybe its default in ruby2/rails4 but have you tried: bundle exec rake db:migrate?
2. Another option would be resetting the whole database - use with CAUTION! resets all the data as well - bundle exec rake db:migrate:reset
+) I would just make sure that you are executing everything in the same development env:
RAILS_ENV=development bundle exec rake db:migrate:reset
RAILS_ENV=development bundle exec rails s
Running rake db:migrate RAILS_ENV=test did it for me
I had the same error in the browser, but upon closely looking at the error message, I noticed some how I had an extra white space in the migrate comment and post files. Once I removed it, it worked perfectly.
Open the database and click schema_migration table. The migrations will be listed as below.
Sort the version column and find the latest migration you want to go back. Delete or Insert a new one. Rails keeps all the migration history in this table, so you can adjust the migration history to you liking.
I got the same error working on the Learn Enough to Be Dangerous Rails tutorial. I'm using Git Bash terminals on a Windows 10 machine. The error showed up in the terminal where I'm running guard, after I tried to migrate my db using the command (in another terminal):
$ bundle exec rake db: migrate:
After trying the solution offered by #lewstherin, I still got the same error. I tried the command:
$ rails test
And got the explicit and helpful warning:
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
I ran the command:
$ bin/rails db:migrate RAILS_ENV=test
and now I'm working again.
For me i just had to migrate for the error:
rake db:migrate --trace
By setting false to config.active_record.migration_error in development.rb might make it workable but i wouldn't recommend it.
Here's what worked for me:
-gem install rails -v 4.1.0
Inside Gemfile:
-gem 'rails', '4.1.0'
(replace the newer/older with this)
Do bundle install and update
-bundle install
-bundle update
In your application.rb :
Remove/Comment - config.active_record.raise_in_transactional_callbacks = true
run bundle rake:
-bundle exec rake db:migrate
Refresh your page and the error should be gone.
Two reasons 'db:migrate:reset' did not work for me
1) loosing data
2) we moved from php to rails, so we had an existing DB and the migrations were written on top of it not from the scratch
So What I tried is to update the 'scheema_migrations'(mysql) table with the list of migrations(just version values) that I was really sure were already run on my db(development), this can be lil time consuming process but it works. I would not attempt this on production though.
I'm guessing the error is that you are creating a table that already exists, I had this problem before.
Step 1
look into the error when you rake dv:migrate
Step 2
go to the model where this table is created
Step 3
add drop_table :[YOUR TABLE] right before the create_table :[YOUR TABLE]
Step 2
run rake db:migrate
Step 3
remove the drop_table once the your migration is done
I had the same problem in genieacs and this code helped:
rake db:drop rake db:create rake db:schema:load RAILS_ENV=development
rake db:migrate rails s -e development
You can always run rake db:reset

rake db:migrate is not working

I'm working through the rails tutorial and have gotten stuck. Starting at Listing 8.16 I have made the following modifications to <timestamp>_add_remember_token_to_users.rb:
class AddRememberTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :remember_token, :string
add_index :users, :remember_token
end
end
The guide then says to update dev & test db as usual:
$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare
My User test for the *remember_token* is still failing so I took a look at the user table in dev and tests database with command line sqlite3. They look like this:
sqlite> .schema users
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" varchar(255),
"email" varchar(255),
"created_at" datetime NOT NULL,
"updated_at" datetime NOT NULL,
"password_digest" varchar(255));
CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email");
It seems like my migration has not been run yet but I do not know how to force it to run.
Try to rebuild your database structure(WARNING: all db-data will be lost):
rake db:drop:all
rake db:create:all
rake db:migrate
If you use Rails < 4.1, don't forget to prepare test database:
rake db:test:prepare
This is the easiest solution since you are working with tutorial. However in production or having important data in development you should take time to investigate the issue. In this case you most likely had created an empty migration, ran rake db:migrate, then added instructions to the migration, so you don't see a new field and further rake db:migrate does nothing. To resolve this issue you need to comment your change instructions, perform rake db:rollback, uncomment instructions and then rake db:migrate to apply instructions you missed.
I had the same issue as the initial question. $ bundle exec rake db:migrate wasn't adding remember_token to the .db and Latha Doddikadi's answer worked for me.
I did:
rake db:rollback
and then:
$ bundle exec rake db:migrate
which added the remember_token field to the database followed by:
bundle exec rspec spec/models/user_spec.rb
which passed.
Finished in 0.92841 seconds
21 examples, 0 failures
Roll back and then re run the migration it might work.
rake db:rollback
And after rolling back re run your migrations again.
I had a bizarre case of the last migration simply refusing to run. I'd created the migration file manually. So I deleted it and remade the migration using
rails g migration ...
and copied the code into the new file and it ran.
I didn't discover why manually creating that file didn't work; all I know is doing it the 'right/rails way' fixed the issue.
Leaving this here in case it helps someone else.

Resources