Devise token auth preventing migration - ruby-on-rails

My rails app is using Devise. I added devise_token_auth so I can link the app to an android app.
In the routes
namespace :api do
scope :v1 do
#mount_devise_token_auth_for 'User', at: 'auth'
end
end
in the initializer
# enable_standard_devise_support = false #for working with Devise
(in addition the other code that was in the file)
I got this error
:~/workspace (master) $ rake db:migrate
-- [](4.2)
-- [](4.2)
rake aborted!
NoMethodError: undefined method `[]' for #<ActiveRecord::Migration:0x00000002c3c470>
Solution, I deleted modified the migration file
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration[4.2]
to
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration
I stopped getting the error but now I got this
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" IN ........
Please help, how can I get this to work?

Try the following steps :
rake db:drop
rake db:create
rake db:migrate

Related

Obstacle with Ruby Rails Authentication

I am a brand new Ruby on Rails User
When I enter localhost:3000 ,
I get an error reading
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=development
You have 2 pending migrations:
20221119205559_add_devise_to_users.rb
20221119211811_drop_users_table.rb
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
end
def load_schema_if_pending!
In a nutshell, it was working before but I attempted to create a a basic authentication page(which was also working) , but for some reason when I clicked sign up I received an error also.
Thank you for any tips on how to fix this!
I have tried to
rake db:drop
rake db:create
rake db:migrate
Editing the migrate file with:
edit your migration file
class DropUsersTable < ActiveRecord::Migration
def change
drop_table :users
end
end
Then doing rake db:migrate
Also, I have run :
rails generate devise:install
rails generate devise User
bundle exec rake db:migrate

Rails 4 Migration Error (Can't rake db:migrate) SQLite3::SQLException: duplicate column name

I've been trying to get the Paperclip gem working. The problem that I was initially running into was that pictures were getting uploaded but not displaying. I then messed around with the database by doing a rake db:rollback to try and fix the error. Now I can't rake db:migrate again because of this error
SQLite3::SQLException: duplicate column name: image_file_name: ALTER TABLE "posts" ADD "image_file_name" varchar
I've personally went into the migration folder and deleted the file to try and generate a migration again. I've been trying to do rails generate paperclip post image and it does create a migration file, but I'm unable to rake db:migrate.
Any suggestions?
Thanks!
Deleting a migration file doesn't really rollback the change it made in your database. Your best bet is to:
Don't delete the migration but comment out the content of the migration class, and run rake db:migrate,
Let's say I have this as my migration file
class AddEmailSentToNeeds < ActiveRecord::Migration
def change
add_column :needs, :email_sent, :boolean ,default: false
end
end
Just comment out the method but leave the class, so it would be:
class AddEmailSentToNeeds < ActiveRecord::Migration
# def change
# add_column :needs, :email_sent, :boolean ,default: false
# end
end
This is just a hacky way to tell rails to skip this migration.
OR
start from the start so go do, rake db:drop, rake db:create, and rake db:migrate

How to remove the cancelled migration from the db folder

I want to remove a migration from my application.
I have a migration file 20141105030942_removedate_fromexpense.rb
the class file for the migrations is
class RemovedateFromexpense < ActiveRecord::Migration
def change
remove_column :expenses, :date, :date
end
end
When I give this command:
rake db:migrate:down VERSION=20141105030942
I get the following error:
== 20141105030942 RemovedateFromexpense: reverting ============================
-- add_column(:expenses, :date, :date)
rake aborted!
StandardError: An error has occurred, this migration was canceled:
SQLite3::SQLException: duplicate column name: date: ALTER TABLE "expenses" ADD "date" date/home/sumyvps/.rvm/gems/ruby-1.9.3-p545#railstutorial_rails_4_0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
db:migrate:status for migration file is as below
up 20141105030942 Removedate fromexpense
Has anyone an idea why this is happening?
You do not need to specify the column type in your migration file. Just the name of the table and the column is enough to remove the column from the table.
Edit your migration file to:
class RemovedateFromexpense < ActiveRecord::Migration
def change
remove_column :expenses, :date
end
end
And then run:
rake db:migrate
This should do the work.
A common task is to rollback the last migration. For example, if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run:
rake db:rollback
This will rollback the latest migration, either by reverting the change method or by running the down method. If you need to undo several migrations you can provide a STEP parameter:
rake db:rollback STEP=3
will revert the last 3 migrations.
First Run:
rails generate migration RemoveDateFromExpense date:date
then rake db:migrate
Hope this help!

Execute method in Rails 4.1.1 migration not working

When I try to run migration for this file:
class AddIndexToUsernameDowncaseForUsers < ActiveRecord::Migration
def up
    execute 'CREATE INDEX index_users_on_username_downcase ON users (lower(username));'
end
def down
remove_index :users, name: :index_users_on_username_downcase
end
end
I get this error:
== 20140521043803 AddIndexToUsernameDowncaseForUsers: migrating ===============
--     execute("CREATE INDEX index_users_on_username_downcase ON groups lower(username);")
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `    execute' for #<AddIndexToUsernameDowncaseForUsers:0x007fea8a9b9c50>/Users/mydir/db/migrate/20140521043803_add_index_to_username_downcase_for_users.rb:3:in `up'
NoMethodError: undefined method `    execute' for #<AddIndexToUsernameDowncaseForUsers:0x007fea8a9b9c50>
/Users/mydir/db/migrate/20140521043803_add_index_to_username_downcase_for_users.rb:3:in `up'
I'm really confused as I've never had issues running migrations before. It looks like the helpers aren't getting included or something. Pretty stumped, but my guess is this is a facepalm-level issue by me.
Rails 4.1.1, Ruby 2.1.1, Postgres 9.3
There are 4 unprintable characters before the word execute - it shows both when it echoes the statement during the migration and also within the quotes in the error: ' execute'.
So it's actually looking for a method called ....execute where .... are those characters.

heroku migration fails

In heroku I ran rake db:migrate and got the following error
== AlterBodyForDocuments: migrating ==========================================
-- change_column(:documents, :body, :mediumtext)
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: type "mediumtext" does not exist
: ALTER TABLE "documents" ALTER COLUMN "body" TYPE mediumtext
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
this is my AlterBodyForDocuments migration:
class AlterBodyForDocuments < ActiveRecord::Migration
def change
change_column :documents, :body, :mediumtext
end
end
I think mediumtext is a mysql thing
heroku uses postgresql
use :text instead

Resources