
Post migration hooks
Reported by jonuts | June 23rd, 2009 @ 12:47 PM | in 0.10.0
Hey
I was looking for a good/easy/convenient way to populate tables with default data. Until now I have been using rake tasks, which I have assumed is the only way to do it. I came up with a solution to this problem, but I first was wondering if this really is a 'problem', or if this behavior belongs in a plugin instead, or if it exists already and I have missed it..
Here is an example of what I am talking about:
class Foo
include DataMapper::Resource
property :id, Serial
property :name, String
post_migration do
create :name => "Hello there"
end
end
Comments and changes to this ticket
-
Dan Kubb (dkubb) July 3rd, 2009 @ 02:58 AM
- State changed from new to not-applicable
I don't believe seed data belongs in the model. Typically I just follow the same convention defined in rails and make a "db:seed" task, and have that populate my database as needed. Of course you're free to write a plugin that does this, but I don't think it belongs in dm-core or dm-migrations.
-
jonuts July 3rd, 2009 @ 08:39 AM
That's sort of what I figured.
If anyone else is interested, I set this up as a plugin http://github.com/jonuts/dm-seed_data
-
Dan Kubb (dkubb) July 3rd, 2009 @ 12:20 PM
Actually, one thing that might be worth doing is integrating a rake db:seed task into merb_datamapper and rails_datamapper.
The style that Rails uses, which is what I tend to use in project, is just make a db:seed rake task that does load(Rails.root / 'db' / 'seeds.rb') and have it be responsible for creating the seed data.
I've found that the majority of the time I want to seed data from several models, with associations between the resources. Having the concens for one model in another's seed_data block seems like it could get more confusing as the seed data grows. I think it's better to group it all in one place, and allow the programmer to do whatever they like in the script.
-
jonuts July 5th, 2009 @ 12:59 AM
That's a good idea and quite a bit more useful. I'll take a look into it today.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »