ActiveRecord migration on specific database connection can't rollback
up vote
0
down vote
favorite
TL;DR - Using an explicit database connection in a migration breaks reversibility.
I have a simple migration, adding a column to a specific database connection: (My app has several databases, for good reasons)
class AddFlavorToBikes < ActiveRecord::Migration
def change
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
end
This works great:
% be rake db:migrate
== 20181120215337 AddFlavorToBikes: migrating =================================
== 20181120215337 AddFlavorToBikes: migrated (0.0060s) ========================
However, it fails to rollback:
% be rake db:rollback
== 20181120215337 AddFlavorToBikes: reverting =================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "flavor" of relation "bikes" already exists
: ALTER TABLE "bikes" ADD "flavor" character varying/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:5:in `block in change'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `tap'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `change'
I don't understand this. The rollback of db.add_column should remove the column. So why am I getting an error that the field I'm trying to remove already exists? Of course it exists, that's why I'm trying to remove it.
I searched the Interwebs for a solution, or even anyone with the same problem, and did not find any leads.
I tried using an explicit variable instead of .tap, but got the same error:
class AddFlavorToBikes < ActiveRecord::Migration
def change
db = VehicleBase.connection
db.add_column :bikes, :flavor, :string
end
end
The closest I've been able to discern, ActiveRecord::Migration loses its ability to detect if it is migrating up or down on any but the default ActiveRecord::Base connection.
So, it tries to migrate add_column
UP, even though it is in a rollback and should be migrating it DOWN. Hence, it is trying to add the column a second time, instead of reversing the add_column into a remove_column.
This is on Rails 4.2.7 and Ruby 2.1.9
How can I make this migration reversible?
ruby-on-rails activerecord migration rollback
add a comment |
up vote
0
down vote
favorite
TL;DR - Using an explicit database connection in a migration breaks reversibility.
I have a simple migration, adding a column to a specific database connection: (My app has several databases, for good reasons)
class AddFlavorToBikes < ActiveRecord::Migration
def change
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
end
This works great:
% be rake db:migrate
== 20181120215337 AddFlavorToBikes: migrating =================================
== 20181120215337 AddFlavorToBikes: migrated (0.0060s) ========================
However, it fails to rollback:
% be rake db:rollback
== 20181120215337 AddFlavorToBikes: reverting =================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "flavor" of relation "bikes" already exists
: ALTER TABLE "bikes" ADD "flavor" character varying/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:5:in `block in change'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `tap'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `change'
I don't understand this. The rollback of db.add_column should remove the column. So why am I getting an error that the field I'm trying to remove already exists? Of course it exists, that's why I'm trying to remove it.
I searched the Interwebs for a solution, or even anyone with the same problem, and did not find any leads.
I tried using an explicit variable instead of .tap, but got the same error:
class AddFlavorToBikes < ActiveRecord::Migration
def change
db = VehicleBase.connection
db.add_column :bikes, :flavor, :string
end
end
The closest I've been able to discern, ActiveRecord::Migration loses its ability to detect if it is migrating up or down on any but the default ActiveRecord::Base connection.
So, it tries to migrate add_column
UP, even though it is in a rollback and should be migrating it DOWN. Hence, it is trying to add the column a second time, instead of reversing the add_column into a remove_column.
This is on Rails 4.2.7 and Ruby 2.1.9
How can I make this migration reversible?
ruby-on-rails activerecord migration rollback
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
TL;DR - Using an explicit database connection in a migration breaks reversibility.
I have a simple migration, adding a column to a specific database connection: (My app has several databases, for good reasons)
class AddFlavorToBikes < ActiveRecord::Migration
def change
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
end
This works great:
% be rake db:migrate
== 20181120215337 AddFlavorToBikes: migrating =================================
== 20181120215337 AddFlavorToBikes: migrated (0.0060s) ========================
However, it fails to rollback:
% be rake db:rollback
== 20181120215337 AddFlavorToBikes: reverting =================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "flavor" of relation "bikes" already exists
: ALTER TABLE "bikes" ADD "flavor" character varying/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:5:in `block in change'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `tap'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `change'
I don't understand this. The rollback of db.add_column should remove the column. So why am I getting an error that the field I'm trying to remove already exists? Of course it exists, that's why I'm trying to remove it.
I searched the Interwebs for a solution, or even anyone with the same problem, and did not find any leads.
I tried using an explicit variable instead of .tap, but got the same error:
class AddFlavorToBikes < ActiveRecord::Migration
def change
db = VehicleBase.connection
db.add_column :bikes, :flavor, :string
end
end
The closest I've been able to discern, ActiveRecord::Migration loses its ability to detect if it is migrating up or down on any but the default ActiveRecord::Base connection.
So, it tries to migrate add_column
UP, even though it is in a rollback and should be migrating it DOWN. Hence, it is trying to add the column a second time, instead of reversing the add_column into a remove_column.
This is on Rails 4.2.7 and Ruby 2.1.9
How can I make this migration reversible?
ruby-on-rails activerecord migration rollback
TL;DR - Using an explicit database connection in a migration breaks reversibility.
I have a simple migration, adding a column to a specific database connection: (My app has several databases, for good reasons)
class AddFlavorToBikes < ActiveRecord::Migration
def change
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
end
This works great:
% be rake db:migrate
== 20181120215337 AddFlavorToBikes: migrating =================================
== 20181120215337 AddFlavorToBikes: migrated (0.0060s) ========================
However, it fails to rollback:
% be rake db:rollback
== 20181120215337 AddFlavorToBikes: reverting =================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "flavor" of relation "bikes" already exists
: ALTER TABLE "bikes" ADD "flavor" character varying/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:5:in `block in change'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `tap'
/Users/david/rider-gate/db/migrate/20181120215337_add_flavor_to_bikes.rb:3:in `change'
I don't understand this. The rollback of db.add_column should remove the column. So why am I getting an error that the field I'm trying to remove already exists? Of course it exists, that's why I'm trying to remove it.
I searched the Interwebs for a solution, or even anyone with the same problem, and did not find any leads.
I tried using an explicit variable instead of .tap, but got the same error:
class AddFlavorToBikes < ActiveRecord::Migration
def change
db = VehicleBase.connection
db.add_column :bikes, :flavor, :string
end
end
The closest I've been able to discern, ActiveRecord::Migration loses its ability to detect if it is migrating up or down on any but the default ActiveRecord::Base connection.
So, it tries to migrate add_column
UP, even though it is in a rollback and should be migrating it DOWN. Hence, it is trying to add the column a second time, instead of reversing the add_column into a remove_column.
This is on Rails 4.2.7 and Ruby 2.1.9
How can I make this migration reversible?
ruby-on-rails activerecord migration rollback
ruby-on-rails activerecord migration rollback
asked 2 days ago
David Hempy
884925
884925
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
I found a reasonable solution by splitting change
into up
and down
methods:
class AddFlavorToBikes < ActiveRecord::Migration
def up
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
def down
VehicleBase.connection.tap do |db|
db.remove_column :bikes, :flavor
end
end
end
While not as graceful or DRY as a reversible migration, this allows db:migrate and db:rollback to work succesfully.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
I found a reasonable solution by splitting change
into up
and down
methods:
class AddFlavorToBikes < ActiveRecord::Migration
def up
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
def down
VehicleBase.connection.tap do |db|
db.remove_column :bikes, :flavor
end
end
end
While not as graceful or DRY as a reversible migration, this allows db:migrate and db:rollback to work succesfully.
add a comment |
up vote
2
down vote
I found a reasonable solution by splitting change
into up
and down
methods:
class AddFlavorToBikes < ActiveRecord::Migration
def up
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
def down
VehicleBase.connection.tap do |db|
db.remove_column :bikes, :flavor
end
end
end
While not as graceful or DRY as a reversible migration, this allows db:migrate and db:rollback to work succesfully.
add a comment |
up vote
2
down vote
up vote
2
down vote
I found a reasonable solution by splitting change
into up
and down
methods:
class AddFlavorToBikes < ActiveRecord::Migration
def up
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
def down
VehicleBase.connection.tap do |db|
db.remove_column :bikes, :flavor
end
end
end
While not as graceful or DRY as a reversible migration, this allows db:migrate and db:rollback to work succesfully.
I found a reasonable solution by splitting change
into up
and down
methods:
class AddFlavorToBikes < ActiveRecord::Migration
def up
VehicleBase.connection.tap do |db|
db.add_column :bikes, :flavor, :string
end
end
def down
VehicleBase.connection.tap do |db|
db.remove_column :bikes, :flavor
end
end
end
While not as graceful or DRY as a reversible migration, this allows db:migrate and db:rollback to work succesfully.
answered 2 days ago
David Hempy
884925
884925
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402734%2factiverecord-migration-on-specific-database-connection-cant-rollback%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown