rails 6.0.0 postgresql 11.5
rails db:migrate runs successfully, but there is no required column in users table.
rails db:rollback also runs successfully even without that column.
I have only one database and config files have no mistakes.
Tried to reinstall rails, postgresql and use new database dump.
class AddCountryToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :country, :string
end
end
migration result:
== 20191105100235 AddCountryToUsers: migrating ============================
-- add_column(:users, :country, :string)
-> 0.0037s
== 20191105100235 AddCountryToUsers: migrated (0.0038s) ===================
rollback result:
== 20191105100235 AddCountryToUsers: reverting ============================
-- remove_column(:users, :country, :string)
-> 0.0027s
== 20191105100235 AddCountryToUsers: reverted (0.0028s) ===================
The most interesting thing is that manual execution of sql command creates column successfully...
Related
After changing my GuideIndustry model to ConnectIndustry through find and replace, I have been getting the error below on the release log preventing me from deploying my rails app onto heroku. Is there anyway to have rails override all the files heroku is using? My local copy of all the files and the schema are working just fine. I have tried using the heroku pg:reset and then heroku run rake db:migrate.
== 20190215035620 AddArticleIdToConnectIndustry: migrating ====================
-- add_column(:connect_industries, :article_id, :integer)
(2.4ms) ALTER TABLE "connect_industries" ADD "article_id" integer
(1.0ms) ROLLBACK
(1.2ms) SELECT pg_advisory_unlock(3329669382293610510)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "connect_industries" does not exist
Update:
Through find and replace and a manual file name change, i edited the create GuideIndustry table migration to be as follows:
class CreateConnectIndustries < ActiveRecord::Migration[5.1]
def change
create_table :connect_industries do |t|
t.integer :guide_id
t.integer :industry_id
t.timestamps
end
end
end
After pushing my master branch to heroku from git, I'm now trying to migrate my db file to heroku also but I came across this error
clydiscope$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.6472
Migrating to DeviseCreateUsers (20141203201816)
== 20141203201816 DeviseCreateUsers: migrating ================================
-- create_table(:users)
-> 0.3488s
-- add_index(:users, :email, {:unique=>true})
-> 0.0146s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0143s
== 20141203201816 DeviseCreateUsers: migrated (0.3782s) =======================
Migrating to AddNameToUsers (20141206140057)
== 20141206140057 AddNameToUsers: migrating ===================================
-- add_column(:users, :name, :string)
PG::DuplicateColumn: ERROR: column "name" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "name" character varying(255)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "name" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "name" character
/app/db/migrate/20141206140057_add_name_to_users.rb:3:in `up'
Apparently, there's a duplicate column that I wasn't aware of...
I was continuously migrating in the development phase and it seemed to work up to this point. How can I change ,my db now so that heroku accepts it?
Here's the code from the last line.
class AddNameToUsers < ActiveRecord::Migration
def up
add_column :users, :name, :string
end
def down
remove_column :users, :name
end
end
Probably just deleting the migration file 20141206140057_add_name_to_users.rb will solve the problem (of course, after committing and push to Heroku).
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!
I'm at the very beginning stages of learning rails using heroku as deployment tool. I ran into a bit of problem today, which is now fixed, but I was wondering if there's a proper/better way to do what I did.
My problem was as follows: I wrote a migration file that created a table with some indices (using add_index). The code would look like this:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :username
...
end
add_index :users, :username, :unique => true
end
def self.down
drop_table(:users)
remove_index :users, :username
end
end
heroku run rake db:migrate ran fine but heroku run rake db:rollback failed because (I assume) remove_index was trying to delete an index from a column that had already been erased.
So I then added a self.down method to my migration file (removing the indices before dropping the table). Afterwards, heroku run rake db:migrate didn't do anything, and heroku run rake db:rollback is stuck at the same error as before. Resetting the database or dropping the table didn't work either. I ended up removing the add_index lines in my migration before the rollback finally works.
... and unfortunately I no longer have any idea why db:rollback failed. The error message was 'index_users_on_username' on table 'users' does not exist', so my guess is that I did something stupid like modifying the database or modified the migration file before doing a rollback. Or could it be because I am mixing change and down method in the same migration file?
Anyway, my main question is, if a db:rollback fails, what then?
Some options in my head:
Fix the migration file until the rollback works
Fix the database manually until the rollback works
Fix the database manually and ignore the migration completely (dunno how to do this)
this)
???
I am trying to update some default values for new columns set in a migration. However I am getting a Postgres error whenever I try to do anything with the records of users table (except modify its structure). I am using Rails 3.0.7, ruby 1.9.2 and the pg gem version 0.11.0
Here is the migration:
def self.up
add_column :users, :state_machine, :string
add_column :users, :wizard_steps_completed, :integer, :default => "1"
add_column :users, :activated_at, :datetime
User.reset_column_information
User.all.each do |u|
u.update_attributes(:state_machine => "activated", :wizard_steps_completed => 3, :activated_at => u.created_at)
end
end
The columns are added with no problems. however the changes to existing records all fail with the following error:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== AddUserSignupInfo: migrating ==============================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: current transaction is aborted, commands ignored until end of transaction block
: SELECT COUNT(*)
FROM pg_tables
WHERE tablename = 'users'
If I attempt to update any orecord it seems to work, I can only make structural changes...
Any ideas?
Turn on postgres logging (Configured in /var/lib/pgsql/data/postgresql.conf and grep for "ERROR REPORTING AND LOGGING"). Or you might want to take the SQL and run it yourself to see what error happens. It could be a constraint thats failing because of your update.