Notes posted by dani
RSS feed
0 thanks
Update the uniqueness field when it value dependent on another existent field without uniqueness restriction.
I’m using sub-transaction to update existent records on DB. I use this approach to update the uniqueness field when it value dependent on another existent field without uniqueness restriction.
Migration for uniqueness with existent dependent data in DB
class AddUniquenessBarToFoo < ActiveRecord::Migration class Foo < ActiveRecord::Base end def change add_column :foos, :bar, :string execute "ALTER TABLE foos ADD CONSTRAINT uk_foods_bar UNIQUE (bar)" Foo.reset_column_information Foo.all.each do |f| begin #try get unique value in a new sub-transaction Foo.transaction(requires_new: true) do f.update_attributes!(:bar => "some ops. with another non-unique existent field to set this") end rescue ActiveRecord::StatementInvalid #We can't reuse a crashed transaction. New one. Foo.transaction(requires_new: true) do #Alternative unique value, if another error exist it's another #migration problem and then raise new error. f.update_attributes!(:bar => "some operation to set this-#{f.id}") end end end change_column :foos, :bar, :string, :null => false end end
Be aware about performance that is transaction per record for big DB.

0 thanks
Migration for uniqueness with existent data in DB
I’m using sub-transaction to update existent records on DB. I use this approach to update the uniqueness field when it value dependent on another existent field without uniqueness restriction.
Migration for uniqueness with existent dependent data in DB
class AddUniquenessBarToFoo < ActiveRecord::Migration class Foo < ActiveRecord::Base end def change add_column :foos, :bar, :string execute "ALTER TABLE foos ADD CONSTRAINT uk_foods_bar UNIQUE (bar)" Foo.reset_column_information Foo.all.each do |f| begin #try get unique value in a new sub-transaction Foo.transaction(requires_new: true) do f.update_attributes!(:bar => "some ops. with another non-unique existent field to set this") end rescue ActiveRecord::StatementInvalid #We can't reuse a crashed transaction. New one. Foo.transaction(requires_new: true) do #Alternative unique value, if another error exist it's another #migration problem and then raise new error. f.update_attributes!(:bar => "some operation to set this-#{f.id}") end end end change_column :foos, :bar, :string, :null => false end end
Be aware about performance that is transaction per record for big DB.