ruined database through 'rake db:setup'? (RoR) - ruby-on-rails

Yesterday, i was tryin' to setup up a fulltext search-engine named "Elasticsearch" and i followed the instructions on Railscasts.com (http://railscasts.com/episodes/306-elasticsearch-part-1?view=comments&_=1345840910957) to integrate this search-engine onto my website.
I followed the tasks to the point where to call 'rake db:setup' and it "recreated" my whole database (didn't wanted that) and now nothing works. It seems like Rails recreated the db from the schema.rb, but it only creates the tables and the indexes and not the associated columns with it.
http://img14.imageshack.us/img14/1535/bildschirmfoto20120825u.png
Im on _development db.
Running rake db:setup looked like this:
doanything_development already exists
doanything_test already exists
-- create_table("admin_notes", {:force=>true})
NOTICE: CREATE TABLE will create implicit sequence "admin_notes_id_seq" for serial column "admin_notes.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "admin_notes_pkey" for table "admin_notes"
-> 0.1088s
-- add_index("admin_notes", ["admin_user_type", "admin_user_id"], {:name=>"index_admin_notes_on_admin_user_type_and_admin_user_id"})
-> 0.0064s
-- add_index("admin_notes", ["resource_type", "resource_id"], {:name=>"index_admin_notes_on_resource_type_and_resource_id"})
-> 0.0049s ...<br /><br />
Schema.rb looks like: " create_table "users", :force => true do |t|<br />
t.string "name"
t.string "email"
t.datetime "created_at",:null => false
t.datetime "updated_at",:null => false ... end <br />
And it created on my pgdb table "Sequences" columns like "users_id_seq", which i hadn't before. Running 'rake db:migrate' doesn't load any of the existing migrationfiles: e.x.
class AddSaltToUsers < ActiveRecord::Migration <br/>
def change <br/>
add_column :users, :salt, :string <br/>
end
end
versions are : ruby 1.9.3p194, Rails 3.2.6 and PostgreSQL 9.1.4
If someone could figure out what just happenend and how i can get my site working again.

If you ran rake db:migrate once. The migration for AddSaltToUsers may have already ran. If so, there are a few ways to overcome this.
Either create a new migrations file, or manually edit schema_migrations, by removing the version number from that table and then re-running rake db:migrate. I would highly not recommend doing the latter, as it can screw up your migrations all together if you don't know what you are doing.
Hope that helps.

Related

Rails migration stopped, even though table does not exist

Rails 6
environment: local development on Mac OS
DB server: MySQL
I deleted all the tables from the DB, and the only tables left in the DB are:
schema_migrations
ar_internal_metadata
I made sure that schema_migrations has no data in it and looked into ar_internal_metadata, and that table has a single row in it, with the following values:
key: environment, value: development
I have several migrations, the most recent one, is devise_create_users.rb.
I am trying to run:
rake db:migrate
But I am getting the error message:
=> rake db:migrate
== 20200317184535 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'users' already exists
/Users/dev/rails/myapp/db/migrate/20200317184535_devise_create_users.rb:5:in `change'
Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'users' already exists
/Users/dev/rails/myapp/db/migrate/20200317184535_devise_create_users.rb:5:in `change'
Caused by:
Mysql2::Error: Table 'users' already exists
/Users/dev/rails/myapp/db/migrate/20200317184535_devise_create_users.rb:5:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Process finished with exit code 1
class DeviseCreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :reset_password_token
t.datetime :reset_password_sent_at
t.datetime :remember_created_at
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
When I check the DB after this, I still don't see a users table, and the schema_migrations table is still empty. In addition, the DeviseCreateUsers migration is the most recent on, so why is it running first.
Any ideas?
Edit:
Based on a comment to the question, I looked at my database.yml file:
default: &default
host: localhost
database: <%= ENV['RAILS_DB_NAME'] %>
username: <%= ENV['RAILS_DB_USER'] %>
password: <%= ENV['RAILS_DB_PWD'] %>
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
socket: /tmp/mysql.sock
development:
<<: *default
I made that change last night, and I forgot that my local ENV settings, were for a different project, so Rails was picking up the settings for that project, and was indeed correct, in that the users table is already there. The fix was for me to create project specific ENV settings, for my local development environment
When things like this happen it is needed to check the database.yml. As in most cases it happens because of some faulty configuration.
Good thing that just mentioning the database.yml in the comment section of the question helped to find the problem.
Using rails console to make sure there is not any users table, try create a new instance of User User.new. If the object was instanciated, your User table is hidden in somewhere. It is not a problem, see all atributtes previously generated and just migrate new ones. I can see in your devise_create_user migration that you can just replace it with something like addColumnstoUser.rb. This will just add the apropriated columns necessary to let Devise runing well.
Choosing this way, inside your migration:
add_column :table_name, :fieldname, :type, in your case, using just one column as an example, it must be:
def change
add_column :users, :reset_password_token, :string
end.
If you are using version controll, such as git, the migration might not know where you can rollback or go ahead. All the migration and rollbacks must stay on the same branch.
For example:
1) Adding a new branch to handle user migration: git checkout -b generate-user-model, generating user model rails generate model User name:string email:string
and running the migration rails db:migrate will create the users table and will update scheema.rb (check scheema file). Adding it to version controll git add . and `git commit -m "Migrating user model"
2) Moving to a new branch git checkout -b comments-controller, generating a new controller rails generate model Comment. And then migrating it rails db:migrate.
This second migration only teachs scheema.rb how to go ahead and how to rollback on this specifc migration. This last git branch doesn't know nothing about how to rollback the User model, unless you merge a branch inside another git merge generate-user-model.
After many migrations has been used, it's usefull keep using migrations to add or delete tables.
Hence, If I had a table called users and I wanted to set it to handle Devise, I just need to generate a new migration with the columns I'd needed.
Use https://sqlitebrowser.org/ to help you cheking your database tables and columns.

Error ActiveRecord::PendingMigrationError

I am following Michael Hartl's book and I am getting this error when I run the server:
I am getting ActiveRecord::PendingMigrationError when I run the server, this shows up:
Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development
Please I've been stuck in this error for so long.
When I type $ RAILS_ENV=development rake db:migrate I get this error:
== 20161209073230 AddActivationToUsers: migrating =============================
-- add_column(:users, :activation_digest, :string) rake aborted! StandardError: An error has occurred, this and all later migrations
canceled:
SQLite3::SQLException: duplicate column name: activation_digest: ALTER
TABLE "users" ADD "activation_digest" varchar
(required)>' Tasks: TOP => db:migrate (See full trace by running task
with --trace)
test/mailers/previews/user_mailer_preview.rb
UserMailerPreview < ActionMailer::Preview
# Preview this email at
http://localhost:3000/rails/mailers/user_mailer/account_activation
def account_activation
user = User.first
user.activation_token = User.new_token
UserMailer.account_activation(user) end
# Preview this email at
http://localhost:3000/rails/mailers/user_mailer/password_reset def
password_reset
UserMailer.password_reset end
end
Schema.rb:
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161123005710) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.string "activation_digest"
t.index ["email"], name: "index_users_on_email", unique: true end
end
Latest migration is
db/migrate/[timestamp]_add_activation_to_users.rb:
class AddActivationToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :activation_digest, :string
add_column :users, :activated, :boolean, default: falserao
add_column :users, :activated_at, :datetime
end
end
The correct command to apply unapplied migrations is RAILS_ENV=development rake db:migrate
This just means that you have a migration pending. When you create a new migration
railas g migration MigrationName
it means that it changes something in the database schema or the layout of your database. In order to commit that change, you have to run:
bin/rails db:migrate
This will look into your migration file and apply it to your databse. Once the migration is done, you can run server as usual again.
rails server
If you have more question, I recommend reading the migration documentation that Rails published:
http://guides.rubyonrails.org/active_record_migrations.html
SQLite3::SQLException: duplicate column name: activation_digest:
(The following is the SQL command that actually modifies the db):
ALTER TABLE "users" ADD "activation_digest"
The SQL command reads like plain English. Somehow one of your migrations is doing something a previous migration already did, namely adding the activation_digest column to your users table in the db. If you look in the directory db/migrate/, you will see all your migration files. If you open one of them up, you should sort of be able to tell what it is doing. So look at all your migration files and find the two migrations that both add the activation_digest column. If the two migration files are identical, then you need to delete one--but take the following steps before deleting a migration:
https://www.baserails.com/questions/i-messed-up-while-generating-my-migration-how-can-i-undo-it
Also check out the section Rolling Back in the Rails Guide:
http://edgeguides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations
If for some reason you don't have two identical migration files that both add the activation_digest column, e.g one of the migration files does something additionally, then you need to figure out what steps in the tutorial that you did wrong, then rollback to the last migration that you know is correct. Finally, follow the steps in the tutorial again for generating the subsequent migrations.
It seems your users table already have a column named activation_digest. Remove the add_column line where the column is added from the migration file and run the migration again.
I think this migration file should work:
class AddActivationToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :activated, :boolean, default: false
add_column :users, :activated_at, :datetime
end
end
Ok the answer is very simple!Just try migrating your DB to version=0 with command: rake db:migrate VERSION=0
and then run rake db:migrate

schema.rb doesnt include add_index method after migration using postgresql

So I was trying to create a database index on the email column of a Users model, but I must be doing something wrong, since after I do the migration I go and check on the schema.rb file to see if the add_index method is included and nothing shows up. I am using postgresql, by the way. So here is what I did...
I created the migration
rails generate migration add_index_to_users_email
After that, I edited and saved the db/migrate/20140911192804_add_index_to_users_email.rb file with the following code for indexing:
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
After that I ran on the console
bundle exec rake db:migrate
And when I went to check on the schema.rb file to see if the add_index method was included, I found that it wasnt there. Here is what my schema.rb looked like
ActiveRecord::Schema.define(version: 20140911192804) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I tried to run rake db:rollback to run again db:migrate and see if some "magic" occurred but I wasnt even able to rollback, getting this error message:
== AddIndexToUsersEmail: reverting ===========================================
-- remove_index(:users, {:unique=>true, :column=>:email})
←[1m←[35m (0.0ms)←[0m ROLLBACK
rake aborted!
An error has occurred, this and all later migrations canceled:
Index name 'index_users_on_email' on table 'users' does not existC:in `migrate'
Tasks: TOP => db:rollback
I'm pretty lost... something that i found interesting was that in the schema.rb file this line
ActiveRecord::Schema.define(version: 20140911192804) do
had the same timestamp as the migration file for the add_index db/migrate/20140911192804_add_index_to_users_email.rb
So there was some sort of update on the schema.rb file during the migration but not what I was expecting to occur.
I don't even know where to start :D so I hope someone a bit more experienced can give me a hint.
Thanks so much!
After hours of trial and error I could finally find a solution!
For some reason beyond my understanding, the migration didnt add the add_index method to my schema.rb, however, when I tried to rollback, it was looking for an index in the users table that didnt exist, so it kept aborting the rollback.
I assumed that the info about the index that it had to look for in the table was in the migration file. So I deleted the content of the migration file leaving it like this:
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
end
end
I was finally able to rollback.
Then I typed again the content inside the AddIndexToUsersEmail migration file
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
I ran again bundle exec rake db:migrate
and it worked! :D
Thanks a lot to everyone who took their time to read this issue!
I just had the same problem and found why !!!
You (we) used the migrate command before saving the migrate file !! So the migration was passed with nothing inside. Just a change method with no instructions.
And if after you try to rollback and the migration file have been saved since. The add_index instruction is in so it can't rollback.
I hope i am clear enough.

Ruby on Rails - error running server

am currently working on a rails project. When i tried to start rails server its throwing the following error:
=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters
/sqlite_adapter.rb:439:in `table_structure': Could not find table 'dbrick'
(ActiveRecord::StatementInvalid)
My table name is 'dbrick'. I also Tried to rake db:drop and rake db:mirgrate. While migrating its throwing the following error:
rake aborted!
Could not find table 'dbrick'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
This is my migrate file:
class CreateDbricks < ActiveRecord::Migration
def self.up
create_table :dbricks do |t|
t.text :description
t.string :video
t.string :video_html
t.string :image_id
t.string :option_id
t.boolean :choice
t.string :reach
t.integer :category_id
t.string :user_id
t.datetime :deleted_at
t.timestamps
end
end
def self.down
drop_table :dbricks
end
end
It will be so much help full if any one help me out of this.
Thanks in advance.
I would try :
rake db:schema:load
To load your schema ( to which I believe its finding the error against your DB ).
If that fails, I would manually find the migration that creates your dbrick, locate the name of the file and copy and paste the number in the filename to produce this :
rake db:migrate:down VERSION=123412341234 # <-- where the number is the number you pasted
Look for errors. Occasionally one thing exists already, or doesn't exist already and prevents the migration from running all the way, and consequentially that would be the source of your error. If it goes successfully then rake it back up :
rake db:migrate:up VERSION=123412341234 # <-- where the number is the number you pasted
If it doesn't go successfully, then you'll have to put on your miner's helmet, and get your hands dirty with :
rails dbconsole
Which will take you into your database and you'll have to manually delete whatever table/column is preventing the migration from occurring. Once that is fixed, exit out and rake db:migrate:up!
Have you migrated your database? rake db:migrate
If you have, drop your database (this deletes all data, so be careful - do it if you do not care about losing data in your db)
rake db:drop
This will clear out your database, and your schema. Then
rake db:migrate
This will re-migrate your schema.

Ruby On Rails Heroku db:migrate Aborted!

I've pushed my app to Heroku and now am trying to run '$ heroku rake db:migrate'. I get this error:
PGError: ERROR: relation "inventories" does not exist
: SELECT "inventories".* FROM "inventories"
On my local machine everything works great. The local is using SQLite 3. Also, previous versions of the app worked just fine -- the previous versions did include the inventories model. Now, I've read ( almost ) every post on stackoverflow and on the web about this issue, but I still cannot find a way around it. Does anybody have any advice on getting this to work?
Ruby 1.9.2
ROR 3
UPDATE..
Here is the source to the migration that creates the inventories table:
class CreateInventories < ActiveRecord::Migration
def self.up
create_table :inventories do |t|
t.decimal :initial_amount, :precision => 10, :scale => 2
t.decimal :remaining_amount, :precision => 10, :scale => 2
t.string :unit
t.decimal :cost, :precision => 10, :scale => 2
t.integer :type_id
t.integer :brand_id
t.integer :blend_id
t.integer :user_id
t.boolean :in
t.timestamps
end
end
def self.down
drop_table :inventories
end
end
Have you used the Inventory model in your migration? Maybe you have an error in your migration, for example you edited the migration file after you migrated your local database?
In any case, running rake --trace db:migrate should show you the whole error message, together with stack trace - you will find the problematic line of code.
UPDATE:
In your stack trace (link is in the comment) there is one suspicious line:
...0-d4e1268c8981/mnt/config/environment.rb:5
What code is there?
SOLUTION: I finally figured out the issue. I had this method in my user model:
def self.search( id )
#inventory = Inventory.where(:primary_user [id])
#cups = Cup.where(:user_id [id])
#inventory + #cups
end
For some reason, that caused errors. I updated it to use #user.inventories and #user.cups instead. Thanks everybody.
The error indicates that the table does not exist remotely. See this:
http://docs.heroku.com/database#common-issues-migrating-to-postgresql
I would expect a previous db:migrate to have created the table, unless database changes have occurred outside of the rake db task. I find that it's rather easy to get this out of sync. You can run specific migrations by specifying the migration method desired (up or down) and the timestamp of the migration to be executed. An example:
heroku rake db:migrate:up VERSION=20101108092153
This runs the migration generated on 11/8/2010 at 9:21:53 AM. You can browse your migrations in the db/ folder to find the migration that contains the missing table.

Resources