How to remove the migration in my database? - ruby-on-rails

I have been trying to use devise to make an authentication system for a blog that I am programming.When I try to view the blog, it says (Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.), and when I run the command, it fails and renders this message:
Moussas-MacBook-Pro:theBlog moussasarr$ bin/rake db:migrate RAILS_ENV=development
== 20141031151735 SorceryCore: migrating ======================================
-- create_table(:authors)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "authors" already exists: CREATE TABLE "authors" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar(255) NOT NULL, "email" varchar(255) NOT NULL, "crypted_password" varchar(255) NOT NULL, "salt" varchar(255) NOT NULL, "created_at" datetime, "updated_at" datetime) /Users/moussasarr/.rvm/gems/ruby-2.0.0-p576#railstutorial_rails_4_0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
. I did migrate the authors table in the past but I had to restart from an earlier point as it was not working out. How can I delete the SorceryCore migration to make a new one ?
Here is the migration table:
class SorceryCore < ActiveRecord::Migration
def change
create_table :authors do |t|
t.string :username, :null => false
t.string :email, :null => false
t.string :crypted_password, :null => false
t.string :salt, :null => false
t.timestamps
end
add_index :authors, :email, unique: true
end
end
I really want to get this table out of the database and push in a new table.
Thanks a lot for your help ! This is the original problem that made me restart at an earlier level and made me lose so much time.

You can do db:migrate:down VERSION=your_migration_version to drop the table from the database, then delete the migration file and then run the migrations that devise needs for your authentication.
However, make sure that you don't need the authors table in any migrations until the devise one as it won't exist and can be a potentially big problem with your codebase and everything.

Related

Relation does not exist issue

I am running into an issue when migrating the db
class CreateBlogoTaggings < ActiveRecord::Migration
def change
taggings_table = "#{Blogo.table_name_prefix}taggings"
create_table(taggings_table) do |t|
t.integer :post_id, null: false
t.integer :tag_id , null: false
end
add_index taggings_table, :tag_id, unique: true
add_index taggings_table, :post_id, unique: true
if defined?(Foreigner)
tags_table = "#{Blogo.table_name_prefix}tags"
posts_table = "#{Blogo.table_name_prefix}posts"
add_foreign_key taggings_table, tags_table , column: :tag_id
add_foreign_key taggings_table, posts_table, column: :post_id
end
end
end
Migrating that gives me
== 20180215114117 CreateBlogoTaggings: migrating ==============================
-- create_table("blogo_taggings")
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "posts" does not exist
: CREATE TABLE "blogo_taggings" ("id" serial primary key, "post_id" integer NOT NULL, CONSTRAINT fk_blogo_taggings_post_id FOREIGN KEY ("tpost_id") REFERENCES "posts" ("id"))
I have even commented everything inside change below the create_table method and it still gives the same error.
Can you tell me why this is happening?
It is expecting a posts table probably backed by a Post model. Try editing the CreateBlogoTaggings migration file. Replace all occurrences of post_id with blogo_post_id and run the migration again.

Rails remove column that has no data type

In my Rails 5 application I've accidentally created a column with no data type. Database is SQLite.
create_table "students", force: :cascade do |t|
t.integer "club_id"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
t.integer "payment_plan_id"
t. "activity"
t.index ["club_id"], name: "index_students_on_club_id"
end
When I attempt rails db:rollback I get this error:
Rhys-MacBook-Pro:classmaster Rhys$ rails db:rollback == 20161011105423 AddActivitiesRefToStudents: reverting =======================
-- remove_column(:students, :activity, :reference)
rails aborted! StandardError: An error has occurred, this and all later migrations canceled: undefined method `to_sym' for nil:NilClass
I've tried running this migration:
class RemoveColumn < ActiveRecord::Migration
def up
execute "ALTER TABLE students DROP COLUMN activity";
end
end
But I think SQLite doesn't support dropping columns. Is there a fix for this?
I am assuming it is the activity column you want to drop based on it not having a type in that file, if it is a different column then just change the column name below.
What you want to do is drop a column from the table, which you absolutely can do. In the command line do something like the line below (or exactly like the line below) to create the migration to drop the column.
rails g migration drop_activity_column_from_students
Then open that file, the method inside should be this.
def change
remove_column :students, :activity
end
Then run
rake db:migrate
and you should see that one column get dropped from the table.
You can find a ton of information here about migrations, if that is something you want to learn more about.
I had the same problem, and after trying various migrations and running rails db:reset without success, I just deleted my schema.rb file and ran rails db:migrate, which rebuilt my schema from scratch without the column that didn't have a data type. Just make sure that none of your migrations contain the addition of the "activity" column, so you don't repeat the problem.
Try to change the type of the column by:
rails g migration FixActivityTypeStudents
Then on the migration add this part.
def change
change_column :students, : activity, :string
end

Reset Rails Database

I am in a world of mess. I had some issues with the mailboxer gem and the tables it created in my schema, so i commented the offending attributes and reset my database. Nothing changed when I ran db:migrate, so I dropped the table, created it anew and reran the migration. The schema has not updated to reflect the attributes which had been commented out. Any ideas as to why? I now have an error message when trying to rake db:migrate which aborts and says that:
PG::UndefinedTable: ERROR: relation "roles" does not exist : ALTER TABLE "roles" DROP "user_id"
My schema has a roles table which includes the following attributes:
create_table "roles", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
I have a migration in my file which is intended to remove the user_id:
class Removeuidfromrole < ActiveRecord::Migration
def change
remove_column :roles, :user_id, :integer
end
end
I have no idea why it is aborting with an error telling me to remove the user id, when I have a migration to do just that. Further, why doesn't the schema update to reflect the recreated db?

rails destroy scaffold leaves back table

I created a new scaffold using the command:
rails generate scaffold level
But then I destroyed it using the command
rails destroy scaffold level
And then again added it back using the command
rails generate scaffold level question:string answer:string prev_q:integer next_q:integer
But now when I try rake db:migrate then I get the following error
SQLite3::SQLException: table "levels" already exists: CREATE TABLE "levels" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "question" varchar(255), "answer" varchar(255), "prev_q" integer, "next_q" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
My migrate/create_level.rb is
class CreateLevels < ActiveRecord::Migration
def change
create_table :levels do |t|
t.string :question
t.string :answer
t.integer :prev_q
t.integer :next_q
t.timestamps
end
end
end
But my schema.rb is:
create_table "levels", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I want to know that how can I update the levels table in the schema. Also I would like to know that why doesn't the table get deleted when I destroy the scaffold. Do I need to run another command to do it?
Using destroy scaffold does not run the rollback to the migration. The correct way of doing it would have been
rake db:rollback
rails destroy scaffold level
now, as you don't have that other migration anymore, you cannot roll it back. You'll need to delete that table manually:
rails dbconsole
DROP TABLE levels;
What I had to do was:
First I followed the steps specified by #Alex Siri.
But after doing that using
rake db:migrate
Did not add the values question,answer etc to the table, so I did the following steps
I again deleted the scaffold using the command
rails destroy scaffold level
After that I again created the scaffold using the command:
rails generate scaffold level question:string answer:string prev_q:integer next_q:integer
Finally I ran migration using
rake db:migrate
This solved my problem. I wrote all the steps so that if anyone else has the same problem then they can easily solve it.

Heroku - PGError: ERROR: syntax error at or near "ENGINE"

I am trying to run
heroku rake db:migrate
to run my migrations on heroku and the first two migrations ran great but the third which looks like this
create_table :charities, :options => "ENGINE=MyISAM" do |t|
t.string :name, :null => false
t.string :title, :null => false
t.timestamps
end
add_index :charities, :name
add_index :charities, :title
Migrating to CreateCharitiesAndThemes (20091019140537)
== CreateCharitiesAndThemes: migrating =======================================
-- create_table(:charities, {:options=>"ENGINE=MyISAM"})
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: syntax error at or near "ENGINE"
LINE 1: ..., "created_at" timestamp, "updated_at" timestamp) ENGINE=MyI...
^
: CREATE TABLE "charities" ("id" serial primary key, "name" character varying(255) NOT NULL, "title" character varying(255) NOT NULL, "created_at" timestamp, "updated_at" timestamp) ENGINE=MyISAM
Heroku uses PostgreSQL, and MyISAM engine is MySQL-specific. I suggest you remove that part. Or, add checking on what database is used and make that optional.
Here's a link to how to check the database.

Resources