class UpdateIndexOnUsers < ActiveRecord::Migration
def change
sql = 'DROP INDEX index_users_on_email'
sql << ' ON users' if Rails.env == 'production' # Heroku pg
ActiveRecord::Base.connection.execute(sql)
end
end
I have to undo this migration. When I revert it using rake db:migrate VERSION=20150611173430,I get this error.
StandardError: An error has occurred, this and all later migrations canceled:
PG::SyntaxError: ERROR: syntax error at end of input
LINE 1: CREATE INDEX index_users_on_email
^
: CREATE INDEX index_users_on_email/Users/goda/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
Then I add a NEW migration file to try and reverse that.
class FixUpdateIndexOnUsers < ActiveRecord::Migration
def change
sql = 'CREATE INDEX index_users_on_email'
sql << ' ON users' if Rails.env == 'production' # Heroku pg
ActiveRecord::Base.connection.execute(sql)
end
end
BUT on heroku, heroku run rake db:migrate fails. Because it runs into that syntax error on the first migration that I cannot remove. What should be done?
EDIT: Fixed syntax, still throwing an error.
PG::SyntaxError: ERROR: syntax error at or near "ON"
LINE 1: DROP INDEX index_users_on_email ON users;
You're using a change method rather than an up, which you should only do for the subset of migrations that rails can reverse automatically.
You should change the change method to up, and add a down method that can reverse the effects of the up method.
Related
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
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!
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.
I'd like to have a way to produce the actual sql (ie: if I pasted into a mysql console, it would work) that will be generated by a rake db:migrate without actually updating the target database.
rake db:migrate:status does a good job of showing which migrations are pending for a given database, but I've yet to find a way to get the actual SQL produced.
Any ideas?
Very interesting question! I found this way:
Assume your migration placed in file db/migrate/20160102210050_create_items.rb and called CreateItems
Go to Rails console and load migration file:
rails c
require './db/migrate/20160102210050_create_items'
Open transaction, run migration and rollback transaction before commit :)
ActiveRecord::Base.connection.transaction do
CreateItems.new.migrate :up
raise ActiveRecord::Rollback
end
If you want to check SQL on rollback, just call CreateItems.new.migrate :down on step 3. SQL will be executed and tested on database, but not committed - so you can verify your migration without affects.
This can be done by monkey-patching the database adapter. This example works for MySQL.
Create a rake task for "fake db:migrate":
desc "Prints all SQL to be executed during pending migrations"
task :fake_db_migrate => :environment do
module ActiveRecord
module ConnectionAdapters
class AbstractMysqlAdapter < AbstractAdapter
alias_method :real_execute, :execute
def execute(sql, name = nil)
if sql =~ /^SHOW/ || sql =~ /^SELECT.*FROM.*schema_migrations/ || sql =~ /^SELECT.*information_schema/m
real_execute(sql, name)
else
puts sql
end
end
end
end
end
Rake::Task["db:migrate"].invoke
end
The rake task monkey-patches the execute method in the connection adapter so that SQL is printed instead of being executed, before actually running the migrations. However, we still have to execute some of the internal SQLs that are used by the db:migrate task to get the database schema and to find out which migrations are pending. That's what the real_execute call does.
Test
Suppose now that and we have a pending migration in db/migrate/20160211212415_create_some_table.rb:
class CreateSomeTable < ActiveRecord::Migration
def change
create_table :some_table do |t|
t.string :string_column, null: false, default: 'ok'
t.timestamps
end
end
end
$ rake db:migrate:status
...
down 20160211212415 Create some table
Now, let's run our fake migrations task:
$ rake fake_db_migrate
== 20160211212415 CreateSomeTable: migrating ==================================
-- create_table(:some_table)
CREATE TABLE `some_table` (`id` int(11) auto_increment PRIMARY KEY, `string_column` varchar(255) DEFAULT 'ok' NOT NULL, `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
-> 0.0009s
== 20160211212415 CreateSomeTable: migrated (0.0010s) =========================
BEGIN
INSERT INTO `schema_migrations` (`version`) VALUES ('20160211212415')
COMMIT
The migrations status has not been changed, i.e. the migration is still pending:
$ rake db:migrate:status
...
down 20160211212415 Create some table
Tested on rails 4.2.3 with the mysql2 gem.
A slightly more low-level function, which can be used for your purpose:
# Get SQL query of a migration expression without executing it.
#
# #example
# schema_statement_to_sql { create_table(:tomatoes) }
# # => "CREATE TABLE \"tomatoes\" (\"id\" serial primary key) "
def schema_statement_to_sql(&block)
raise ArgumentError, 'No block given' unless block_given?
connection = ActiveRecord::Base.connection
original_execute = connection.method(:execute)
sql_to_return = ''
capturing_execute = proc { |sql| sql_to_return = sql }
connection.define_singleton_method(:execute, &capturing_execute)
begin
connection.instance_eval(&block)
ensure
connection.define_singleton_method(:execute, &original_execute)
end
sql_to_return
end
You can do
rake db:migrate --dry-run --trace
and rake will test the task. Then use one of the methods listed to get the SQL that would be run.
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.