On Tuesday, July 03, 2012 10:12:45 AM Scott Seago wrote:
The migration which adds denormalized permissions has to iterate over
existing
permission records to create the DerivedPermission objects. Unfortunately,
now that the user groups code is in place, the rails permission model is
incompatible with what's in the database when the create_derived_permissions
migration is run. Normally, this sort of thing is resolved by overriding
the model classes in the migration to remove validations and other model
components that may be broken by future code changes.
In this case, we have more extensive needs at the model level, so sorting
through all of this is much more difficult. After chasing down phantom
migration failures over several hours, I decided to do the simpler thing here.
I've just moved the code that iterates over Permissions and creates Derived
Permissions
from the CreateDerivedPermissions migration to the final migration that
modifies these models for user groups. Now, if a user tries to run a set of
migrations on an existing database that includes both of these, the derived
permissions are simply created later. If a user has already successfully
run the CreateDerivedPermissions migration but not the newer one, then the
iteration over permissions to update derived permissions will be run
again, but this operation is safe to run more than once, as it compares
derived permissions in the database with what _should_ be in the database
for a given set of permissions -- so if you run it more than once, it just won't
make any db changes on subsequent runs.
to update derived permissions will
---
.../20120424151000_create_derived_permissions.rb | 7 -------
.../20120520151500_change_permission_user.rb | 7 +++++++
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/src/db/migrate/20120424151000_create_derived_permissions.rb
b/src/db/migrate/20120424151000_create_derived_permissions.rb
index 59c1863..1220fa4 100644
--- a/src/db/migrate/20120424151000_create_derived_permissions.rb
+++ b/src/db/migrate/20120424151000_create_derived_permissions.rb
@@ -29,13 +29,6 @@ class CreateDerivedPermissions < ActiveRecord::Migration
add_index :derived_permissions,
[:permission_object_id, :permission_object_type],
:name => 'index_derived_permissions_on_permission_object'
- # create derived permissions for existing records
- counter = 0
- total_perms = Permission.count
- Permission.all.each do |permission|
- puts "updating permision #{counter +=1} of #{total_perms}"
- permission.update_derived_permissions
- end
end
def self.down
diff --git a/src/db/migrate/20120520151500_change_permission_user.rb
b/src/db/migrate/20120520151500_change_permission_user.rb
index f71ee33..fe19be3 100644
--- a/src/db/migrate/20120520151500_change_permission_user.rb
+++ b/src/db/migrate/20120520151500_change_permission_user.rb
@@ -47,6 +47,13 @@ class ChangePermissionUser < ActiveRecord::Migration
Permission.reset_column_information
DerivedPermission.reset_column_information
+ # create derived permissions for existing records
+ counter = 0
+ total_perms = Permission.count
+ Permission.all.each do |permission|
+ puts "updating permision #{counter +=1} of #{total_perms}"
+ permission.update_derived_permissions
+ end
end
def self.down
ACK, works for me.
--
With regards
Petr Blaho