Commit 8ebcd93c46458a0687e0b68e7aeb0386e09fff0c

Authored by Arthur Esposte
1 parent 20dd43a2
Exists in master and in 79 other branches add_sisp_to_chef, add_super_archives_plugin, api_for_colab, automates_core_packing, backup_not_prod, changes_in_buttons_on_content_panel, colab_automated_login, colab_spb_plugin_recipe, colab_widgets_settings, design_validation, dev_env_minimal, disable_email_dev, fix_breadcrumbs_position, fix_categories_software_link, fix_edit_institution, fix_edit_software_with_another_license, fix_get_license_info, fix_gitlab_assets_permission, fix_list_style_inside_article, fix_list_style_on_folder_elements, fix_members_pagination, fix_merge_request_url, fix_models_translations, fix_no_license, fix_software_api, fix_software_block_migration, fix_software_communities_translations, fix_software_communities_unit_test, fix_style_create_institution_admin_panel, fix_superarchives_imports, fix_sym_links_noosfero, focus_search_field_theme, gov-user-refactoring, gov-user-refactoring-rails4, header_fix, institution_modal_on_rating, kalibro-conf-refactoring, kalibro-processor-package, lxc_settings, margin_fix, mezuro_cookbook, prezento, refactor_download_block, refactor_software_communities, refactor_software_for_sisp, register_page, release-process, release-process-v2, remove-unused-images, remove_broken_theme, remove_secondary_email_from_user, remove_sisp_buttons, removing_super_archives_email, review_message, scope2method, signals_user_noosfero, sisp_catalog_header, sisp_colab_config, sisp_dev, sisp_dev_master, sisp_simple_version, software_as_organization, software_catalog_style_fix, software_communities_html_refactor, software_infos_api, spb_minimal_env, spb_to_rails4, spec_refactor, stable-4.1, stable-4.2, stable-4.x, temp_soft_comm_refactoring, theme_header, theme_javascript_refactory, thread_dropdown, thread_page, update_search_by_categories, update_software_api, update_softwares_boxes

Handle some cases in migrations to not crash in clean environments

db/migrate/20150720190133_change_blocks_mirror_option.rb
@@ -4,26 +4,30 @@ class ChangeBlocksMirrorOption < ActiveRecord::Migration @@ -4,26 +4,30 @@ class ChangeBlocksMirrorOption < ActiveRecord::Migration
4 institution = Community["institution"] 4 institution = Community["institution"]
5 software = Community["software"] 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 end 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 end 23 end
20 24
21 blocks.each do |block| 25 blocks.each do |block|
22 if !(block.owner.class == Environment) && block.owner.organization? && !block.owner.enterprise? 26 if !(block.owner.class == Environment) && block.owner.organization? && !block.owner.enterprise?
23 - if block.owner.software? 27 + if software && block.owner.software?
24 software_block = boxTemplateSoftware.blocks.where(:title => block.title).first 28 software_block = boxTemplateSoftware.blocks.where(:title => block.title).first
25 block.mirror_block_id = software_block.id if software_block 29 block.mirror_block_id = software_block.id if software_block
26 - elsif block.owner.institution? 30 + elsif institution && block.owner.institution?
27 institution_block = boxTemplateInstitution.blocks.where(:title => block.title).first 31 institution_block = boxTemplateInstitution.blocks.where(:title => block.title).first
28 block.mirror_block_id = institution_block.id if institution_block 32 block.mirror_block_id = institution_block.id if institution_block
29 end 33 end
db/migrate/20150910121056_create_siorg_institutions.rb
@@ -5,56 +5,60 @@ class CreateSiorgInstitutions < ActiveRecord::Migration @@ -5,56 +5,60 @@ class CreateSiorgInstitutions < ActiveRecord::Migration
5 def up 5 def up
6 governmental_power = GovernmentalPower.where("name ILIKE ?", "Executivo").first 6 governmental_power = GovernmentalPower.where("name ILIKE ?", "Executivo").first
7 governmental_sphere = GovernmentalSphere.where("name ILIKE ?", "Federal").first 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 institution[:community] = community 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 end 62 end
59 puts "" 63 puts ""
60 end 64 end
db/migrate/20150910133925_add_community_block_in_place_of_profile_image_block.rb
@@ -2,26 +2,28 @@ class AddCommunityBlockInPlaceOfProfileImageBlock < ActiveRecord::Migration @@ -2,26 +2,28 @@ class AddCommunityBlockInPlaceOfProfileImageBlock < ActiveRecord::Migration
2 def up 2 def up
3 software_template = Community['software'] 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 community_block_template.save! 11 community_block_template.save!
21 print "." 12 print "."
22 13
23 - profile_image_block.destroy 14 + software_area_two.blocks << community_block_template
  15 + software_area_two.save!
24 print "." 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 end 27 end
26 28
27 Community.joins(:software_info).each do |software_community| 29 Community.joins(:software_info).each do |software_community|
@@ -31,7 +33,7 @@ class AddCommunityBlockInPlaceOfProfileImageBlock &lt; ActiveRecord::Migration @@ -31,7 +33,7 @@ class AddCommunityBlockInPlaceOfProfileImageBlock &lt; ActiveRecord::Migration
31 community_block = CommunityBlock.new :mirror => true, :move_modes => "none", :edit_modes => "none" 33 community_block = CommunityBlock.new :mirror => true, :move_modes => "none", :edit_modes => "none"
32 community_block.settings[:fixed] = true 34 community_block.settings[:fixed] = true
33 community_block.display = "except_home_page" 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 community_block.save! 37 community_block.save!
36 print "." 38 print "."
37 39