Skip to content

Loading…

Rails 3 1 - Make db/seeds idempotent #61

Merged
merged 2 commits into from

3 participants

@jasherai

I also ran foul of a recent support request where the db:seed rake task recreated the contact pages from the refinery-inquiries engine.

This change checks to see if the relevant pages already exist and creates them if not.

@jasherai jasherai closed this
@jasherai jasherai reopened this
@jasherai

Closed cos of typos.

All working now. Sorry!

@parndt parndt commented on an outdated diff
db/seeds.rb
((7 lines not shown))
- :menu_match => "^/(inquiries|contact).*$",
- :deletable => false,
- :position => (page_position += 1)
- })
- contact_us_page.parts.create({
- :title => "Body",
- :body => "<p>Get in touch with us. Just use the form below and we'll get back to you as soon as we can.</p>",
- :position => 0
- })
- contact_us_page.parts.create({
- :title => "Side Body",
- :body => "",
- :position => 1
- })
- contact_us_page_position = -1
+ unless Refinery::Page.by_title("Contact").any?
@parndt Refinery member
parndt added a note

Can you use the link_url attribute instead?

Refinery::Page.where(:link_url => '/contact').any?

Same with the thank you page :-)

Sure. I'll change that in a bit.

As a note, I based this on the code in refinery-pages . Would you like an update for that too?

@parndt Refinery member
parndt added a note

No, thanks, the about page doesn't have any special link_url whereas these did

ok I see. My update also changed the query for the Privacy Policy page, which also doesn't have a link_url attribute. Should that be changed back to query on the title?

Thanks for the walkthrough, still working my way through your code :)

@parndt Refinery member
parndt added a note

Yes, @ugisozols was too eager merging this. The current code won't actually be idempotent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ugisozols ugisozols merged commit d75ef9d into refinery:rails-3-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 18, 2012
  1. @jasherai
Commits on Feb 21, 2012
  1. @jasherai
Showing with 48 additions and 42 deletions.
  1. +48 −42 db/seeds.rb
View
90 db/seeds.rb
@@ -8,48 +8,54 @@
if defined?(::Refinery::Page)
page_position = (::Refinery::Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)
- contact_us_page = ::Refinery::Page.create({
- :title => "Contact",
- :link_url => "/contact",
- :menu_match => "^/(inquiries|contact).*$",
- :deletable => false,
- :position => (page_position += 1)
- })
- contact_us_page.parts.create({
- :title => "Body",
- :body => "<p>Get in touch with us. Just use the form below and we'll get back to you as soon as we can.</p>",
- :position => 0
- })
- contact_us_page.parts.create({
- :title => "Side Body",
- :body => "",
- :position => 1
- })
- contact_us_page_position = -1
+ unless Refinery::Page.where(:link_url => '/contact').any?
+ contact_us_page = ::Refinery::Page.create({
+ :title => "Contact",
+ :link_url => "/contact",
+ :menu_match => "^/(inquiries|contact).*$",
+ :deletable => false,
+ :position => (page_position += 1)
+ })
+ contact_us_page.parts.create({
+ :title => "Body",
+ :body => "<p>Get in touch with us. Just use the form below and we'll get back to you as soon as we can.</p>",
+ :position => 0
+ })
+ contact_us_page.parts.create({
+ :title => "Side Body",
+ :body => "",
+ :position => 1
+ })
+ contact_us_page_position = -1
+ end
- thank_you_page = contact_us_page.children.create({
- :title => "Thank You",
- :link_url => "/contact/thank_you",
- :menu_match => "^/(inquiries|contact)/thank_you$",
- :show_in_menu => false,
- :deletable => false,
- :position => (contact_us_page_position += 1)
- })
- thank_you_page.parts.create({
- :title => "Body",
- :body => "<p>We've received your inquiry and will get back to you with a response shortly.</p><p><a href='/'>Return to the home page</a></p>",
- :position => 0
- })
+ unless Refinery::Page.where(:link_url => '/contact/thank_you').any?
+ thank_you_page = contact_us_page.children.create({
+ :title => "Thank You",
+ :link_url => "/contact/thank_you",
+ :menu_match => "^/(inquiries|contact)/thank_you$",
+ :show_in_menu => false,
+ :deletable => false,
+ :position => (contact_us_page_position += 1)
+ })
+ thank_you_page.parts.create({
+ :title => "Body",
+ :body => "<p>We've received your inquiry and will get back to you with a response shortly.</p><p><a href='/'>Return to the home page</a></p>",
+ :position => 0
+ })
+ end
- privacy_policy_page = contact_us_page.children.create({
- :title => "Privacy Policy",
- :deletable => true,
- :show_in_menu => false,
- :position => (contact_us_page_position += 1)
- })
- privacy_policy_page.parts.create({
- :title => "Body",
- :body => "<p>We respect your privacy. We do not market, rent or sell our email list to any outside parties.</p><p>We need your e-mail address so that we can ensure that the people using our forms are bona fide. It also allows us to send you e-mail newsletters and other communications, if you opt-in. Your postal address is required in order to send you information and pricing, if you request it.</p><p>Please call us at 123 456 7890 if you have any questions or concerns.</p>",
- :position => 0
- })
+ unless Refinery::Page.where(:link_url => '/privacy-policy').any?
+ privacy_policy_page = contact_us_page.children.create({
+ :title => "Privacy Policy",
+ :deletable => true,
+ :show_in_menu => false,
+ :position => (contact_us_page_position += 1)
+ })
+ privacy_policy_page.parts.create({
+ :title => "Body",
+ :body => "<p>We respect your privacy. We do not market, rent or sell our email list to any outside parties.</p><p>We need your e-mail address so that we can ensure that the people using our forms are bona fide. It also allows us to send you e-mail newsletters and other communications, if you opt-in. Your postal address is required in order to send you information and pricing, if you request it.</p><p>Please call us at 123 456 7890 if you have any questions or concerns.</p>",
+ :position => 0
+ })
+ end
end
Something went wrong with that request. Please try again.