On 09/01/2011 08:48 PM, Matt Wagner wrote:
Also fixes the :down method that would have never worked. Oops.
Also adds a Pool.default convenience method to find default pool object.
---
src/app/models/pool.rb | 6 +++++-
...amed_suggested_deployable_to_catalog_entries.rb | 18 ++++++++++++++++--
2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/src/app/models/pool.rb b/src/app/models/pool.rb
index d29675d..412f8f5 100644
--- a/src/app/models/pool.rb
+++ b/src/app/models/pool.rb
@@ -1,4 +1,3 @@
-# == Schema Information
# Schema version: 20110603204130
#
# Table name: pools
@@ -97,4 +96,9 @@ class Pool< ActiveRecord::Base
:order => (order_field || 'name') +' '+ (order_dir ||
'asc'))
end
+ # Make this easier to find:
+ def self.default
+ MetadataObject.lookup("self_service_default_pool")
+ end
\o/
+
end
diff --git
a/src/db/migrate/20110816192727_renamed_suggested_deployable_to_catalog_entries.rb
b/src/db/migrate/20110816192727_renamed_suggested_deployable_to_catalog_entries.rb
index 8276e0b..aab588e 100644
--- a/src/db/migrate/20110816192727_renamed_suggested_deployable_to_catalog_entries.rb
+++ b/src/db/migrate/20110816192727_renamed_suggested_deployable_to_catalog_entries.rb
@@ -1,11 +1,25 @@
class RenamedSuggestedDeployableToCatalogEntries< ActiveRecord::Migration
+
def self.up
+ # We need a default catalog to successfully preserve existing data
+ catalog = Catalog.first || Catalog.create!(:pool => Pool.default, :name =>
'Default')
+
rename_table :suggested_deployables, :catalog_entries
- add_column :catalog_entries, :catalog_id, :integer, :null => false
+
+ # This should be :null => false, but this will fail if we had existing data,
+ # so do this in an absurdly roundabout manner instead:
+ add_column :catalog_entries, :catalog_id, :integer, :null => true
+ # Now, populate catalog_id for existing data:
+ CatalogEntry.all.each do |catalog_entry|
+ catalog_entry.catalog_id ||= catalog.id
+ catalog_entry.save!
+ end
+ # And now, with the column populated, change it to disallow nulls
+ change_column :catalog_entries, :catalog_id, :integer, :null => false
end
def self.down
rename_table :catalog_entries, :suggested_deployables
- drop_column :catalog_id
+ remove_column :suggested_deployables, :catalog_id
end
end
Generally, this looks good but I've had two issues.
First, the migration was failing for me anyway (I created some suggested
deployables to test this). The migration you fixed is run before the one
that adds the Catalogs table. So calls like `Catalog.first` don't work.
To test it further, I renamed my the migration files so that
`create_catalogs.rb`
is run before `renamed_suggested_deployable_to_catalog_entries.rb`.
That did the trick -- migrations passed.
But then I went to the UI for Catalog and Catalog Entries and both were
empty.
When I looked them up in `rails console`, everything seemed to be all right.
I'm pretty certain that this is because db/seeds.rb adds Catalog
permissions to the Admin role, but when you seed the database earlier
and then run migrations, the permissions are not added automatically.
And you can't re-run `rake db:seeds` to update it.
I'm not sure how important this is, but if this should be fixed, you can
look at my recent Hardware Profile Permissions patch. I had to solve a
similar problem (adding permissions through a migration) there.
Thomas
--
No trees were killed to send this message, but a large number of
electrons were terribly inconvenienced.