Commit 8ebcd93c46458a0687e0b68e7aeb0386e09fff0c
1 parent
20dd43a2
Exists in
master
and in
79 other branches
Handle some cases in migrations to not crash in clean environments
Showing
3 changed files
with
73 additions
and
63 deletions
Show diff stats
db/migrate/20150720190133_change_blocks_mirror_option.rb
... | ... | @@ -4,26 +4,30 @@ class ChangeBlocksMirrorOption < ActiveRecord::Migration |
4 | 4 | institution = Community["institution"] |
5 | 5 | software = Community["software"] |
6 | 6 | |
7 | - boxTemplateInstitution = institution.boxes.where(:position => 2).first | |
7 | + if institution | |
8 | + boxTemplateInstitution = institution.boxes.where(:position => 2).first | |
8 | 9 | |
9 | - boxTemplateInstitution.blocks.each do |block| | |
10 | - block.mirror = true | |
11 | - print "." if block.save | |
10 | + boxTemplateInstitution.blocks.each do |block| | |
11 | + block.mirror = true | |
12 | + print "." if block.save | |
13 | + end | |
12 | 14 | end |
13 | 15 | |
14 | - boxTemplateSoftware = software.boxes.where(:position => 2).first | |
16 | + if software | |
17 | + boxTemplateSoftware = software.boxes.where(:position => 2).first | |
15 | 18 | |
16 | - boxTemplateSoftware.blocks.each do |block| | |
17 | - block.mirror = true | |
18 | - print "." if block.save | |
19 | + boxTemplateSoftware.blocks.each do |block| | |
20 | + block.mirror = true | |
21 | + print "." if block.save | |
22 | + end | |
19 | 23 | end |
20 | 24 | |
21 | 25 | blocks.each do |block| |
22 | 26 | if !(block.owner.class == Environment) && block.owner.organization? && !block.owner.enterprise? |
23 | - if block.owner.software? | |
27 | + if software && block.owner.software? | |
24 | 28 | software_block = boxTemplateSoftware.blocks.where(:title => block.title).first |
25 | 29 | block.mirror_block_id = software_block.id if software_block |
26 | - elsif block.owner.institution? | |
30 | + elsif institution && block.owner.institution? | |
27 | 31 | institution_block = boxTemplateInstitution.blocks.where(:title => block.title).first |
28 | 32 | block.mirror_block_id = institution_block.id if institution_block |
29 | 33 | end | ... | ... |
db/migrate/20150910121056_create_siorg_institutions.rb
... | ... | @@ -5,56 +5,60 @@ class CreateSiorgInstitutions < ActiveRecord::Migration |
5 | 5 | def up |
6 | 6 | governmental_power = GovernmentalPower.where("name ILIKE ?", "Executivo").first |
7 | 7 | governmental_sphere = GovernmentalSphere.where("name ILIKE ?", "Federal").first |
8 | + env = Environment.default | |
8 | 9 | |
9 | - CSV.foreach("plugins/spb_migrations/files/orgaos_siorg.csv", :headers => true) do |row| | |
10 | - template = Community["institution"] | |
10 | + if env && governmental_power && governmental_sphere | |
11 | + CSV.foreach("plugins/spb_migrations/files/orgaos_siorg.csv", :headers => true) do |row| | |
12 | + template = Community["institution"] | |
11 | 13 | |
12 | - community = Community.where("identifier ILIKE ?", row["Nome"].to_slug).first | |
13 | - unless community | |
14 | - institution = Institution.where("acronym ILIKE ?", row["Sigla"]).first | |
15 | - community = institution.community if institution | |
16 | - end | |
17 | - | |
18 | - community = Community.new unless community | |
14 | + community = Community.where("identifier ILIKE ?", row["Nome"].to_slug).first | |
15 | + unless community | |
16 | + institution = Institution.where("acronym ILIKE ?", row["Sigla"]).first | |
17 | + community = institution.community if institution | |
18 | + end | |
19 | 19 | |
20 | - community.name = row["Nome"].rstrip | |
21 | - community.country = row["Pais"] | |
22 | - community.state = row["Estado"] | |
23 | - community.city = row["Cidade"] | |
24 | - community.template = template if template | |
20 | + community = Community.new unless community | |
25 | 21 | |
26 | - community.save | |
22 | + community.environment = env if community.environment.blank? | |
23 | + community.name = row["Nome"].rstrip | |
24 | + community.country = row["Pais"] | |
25 | + community.state = row["Estado"] | |
26 | + community.city = row["Cidade"] | |
27 | + community.template = template if template | |
27 | 28 | |
28 | - juridical_nature = JuridicalNature.where("name ILIKE ? OR name ILIKE ?", "#{I18n.transliterate(row['Natureza Jurídica'].rstrip)}", "#{row['Natureza Jurídica'].rstrip}").first | |
29 | + community.save! | |
29 | 30 | |
30 | - juridical_nature = JuridicalNature.create!(name: row['Natureza Jurídica'].rstrip) unless juridical_nature | |
31 | + juridical_nature = JuridicalNature.where("name ILIKE ? OR name ILIKE ?", "#{I18n.transliterate(row['Natureza Jurídica'].rstrip)}", "#{row['Natureza Jurídica'].rstrip}").first | |
31 | 32 | |
33 | + juridical_nature = JuridicalNature.create!(name: row['Natureza Jurídica'].rstrip) unless juridical_nature | |
32 | 34 | |
33 | - institution = Hash.new | |
34 | 35 | |
35 | - institution[:name] = row["Nome"] | |
36 | - institution[:siorg_code] = row["Código do SIORG"] | |
37 | - institution[:acronym] = row["Sigla"] | |
38 | - institution[:governmental_sphere] = governmental_sphere | |
39 | - institution[:governmental_power] = governmental_power | |
40 | - institution[:juridical_nature] = juridical_nature | |
41 | - institution[:sisp] = (row["SISP"] == "Sim") | |
42 | - institution[:cnpj] = row["CNPJ"] | |
43 | - institution[:community] = community | |
36 | + institution = Hash.new | |
44 | 37 | |
45 | - if community.institution | |
46 | - community.institution.update_attributes(institution) | |
47 | - else | |
38 | + institution[:name] = row["Nome"] | |
39 | + institution[:siorg_code] = row["Código do SIORG"] | |
40 | + institution[:acronym] = row["Sigla"] | |
41 | + institution[:governmental_sphere] = governmental_sphere | |
42 | + institution[:governmental_power] = governmental_power | |
43 | + institution[:juridical_nature] = juridical_nature | |
44 | + institution[:sisp] = (row["SISP"] == "Sim") | |
45 | + institution[:cnpj] = row["CNPJ"] | |
48 | 46 | institution[:community] = community |
49 | - community.institution = PublicInstitution.create!(institution) | |
50 | - end | |
51 | 47 | |
52 | - if community.save | |
53 | - print "." | |
54 | - else | |
55 | - print "F" | |
56 | - end | |
48 | + if community.institution | |
49 | + community.institution.update_attributes(institution) | |
50 | + else | |
51 | + institution[:community] = community | |
52 | + community.institution = PublicInstitution.create!(institution) | |
53 | + end | |
54 | + | |
55 | + if community.save | |
56 | + print "." | |
57 | + else | |
58 | + print "F" | |
59 | + end | |
57 | 60 | |
61 | + end | |
58 | 62 | end |
59 | 63 | puts "" |
60 | 64 | end | ... | ... |
db/migrate/20150910133925_add_community_block_in_place_of_profile_image_block.rb
... | ... | @@ -2,26 +2,28 @@ class AddCommunityBlockInPlaceOfProfileImageBlock < ActiveRecord::Migration |
2 | 2 | def up |
3 | 3 | software_template = Community['software'] |
4 | 4 | |
5 | - software_area_two = software_template.boxes.find_by_position 2 | |
5 | + if software_template | |
6 | + software_area_two = software_template.boxes.find_by_position 2 | |
6 | 7 | |
7 | - community_block_template = CommunityBlock.new :mirror => true, :move_modes => "none", :edit_modes => "none" | |
8 | - community_block_template.settings[:fixed] = true | |
9 | - community_block_template.display = "except_home_page" | |
10 | - community_block_template.save! | |
11 | - print "." | |
12 | - | |
13 | - software_area_two.blocks << community_block_template | |
14 | - software_area_two.save! | |
15 | - print "." | |
16 | - | |
17 | - profile_image_block = software_area_two.blocks.find_by_type("ProfileImageBlock") | |
18 | - if !profile_image_block.nil? | |
19 | - community_block_template.position = profile_image_block.position | |
8 | + community_block_template = CommunityBlock.new :mirror => true, :move_modes => "none", :edit_modes => "none" | |
9 | + community_block_template.settings[:fixed] = true | |
10 | + community_block_template.display = "except_home_page" | |
20 | 11 | community_block_template.save! |
21 | 12 | print "." |
22 | 13 | |
23 | - profile_image_block.destroy | |
14 | + software_area_two.blocks << community_block_template | |
15 | + software_area_two.save! | |
24 | 16 | print "." |
17 | + | |
18 | + profile_image_block = software_area_two.blocks.find_by_type("ProfileImageBlock") | |
19 | + if !profile_image_block.nil? | |
20 | + community_block_template.position = profile_image_block.position | |
21 | + community_block_template.save! | |
22 | + print "." | |
23 | + | |
24 | + profile_image_block.destroy | |
25 | + print "." | |
26 | + end | |
25 | 27 | end |
26 | 28 | |
27 | 29 | Community.joins(:software_info).each do |software_community| |
... | ... | @@ -31,7 +33,7 @@ class AddCommunityBlockInPlaceOfProfileImageBlock < ActiveRecord::Migration |
31 | 33 | community_block = CommunityBlock.new :mirror => true, :move_modes => "none", :edit_modes => "none" |
32 | 34 | community_block.settings[:fixed] = true |
33 | 35 | community_block.display = "except_home_page" |
34 | - community_block.mirror_block_id = community_block_template.id | |
36 | + community_block.mirror_block_id = community_block_template.id if community_block_template | |
35 | 37 | community_block.save! |
36 | 38 | print "." |
37 | 39 | ... | ... |