diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-02-15 11:40:59 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-02-15 11:40:59 +0100 |
commit | 364850c6ee27e86a6a788a6d7bf5c889ca7bf55a (patch) | |
tree | 9271d85e08df9302f8237cc324888fd2aaf22501 /sonar-server/src | |
parent | 024166d58faf575747198a84e47b30e1e8c9d311 (diff) | |
download | sonarqube-364850c6ee27e86a6a788a6d7bf5c889ca7bf55a.tar.gz sonarqube-364850c6ee27e86a6a788a6d7bf5c889ca7bf55a.zip |
Improve management of indexes and faux models in database migrations
Diffstat (limited to 'sonar-server/src')
24 files changed, 112 insertions, 37 deletions
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/118_add_snapshots_qualifier_index.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/118_add_snapshots_qualifier_index.rb index 1ee67cf96f1..aab8eeffda4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/118_add_snapshots_qualifier_index.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/118_add_snapshots_qualifier_index.rb @@ -24,7 +24,11 @@ class AddSnapshotsQualifierIndex < ActiveRecord::Migration def self.up - add_index :snapshots, :qualifier, :name => 'snapshots_qualifier' + begin + add_index :snapshots, :qualifier, :name => 'snapshots_qualifier' + rescue + # already exists + end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/119_add_measures_sid_metric_index.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/119_add_measures_sid_metric_index.rb index 6387ea36c7d..cf8d6907908 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/119_add_measures_sid_metric_index.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/119_add_measures_sid_metric_index.rb @@ -24,7 +24,11 @@ class AddMeasuresSidMetricIndex < ActiveRecord::Migration def self.up - add_index :project_measures, [:snapshot_id, :metric_id], :name => 'measures_sid_metric' + begin + add_index :project_measures, [:snapshot_id, :metric_id], :name => 'measures_sid_metric' + rescue + # already exists + end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/167_add_index_to_measure_data_snapshot.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/167_add_index_to_measure_data_snapshot.rb index 0c858571c9c..fc83ebdfcc5 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/167_add_index_to_measure_data_snapshot.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/167_add_index_to_measure_data_snapshot.rb @@ -24,7 +24,11 @@ class AddIndexToMeasureDataSnapshot < ActiveRecord::Migration def self.up - add_index :measure_data, :snapshot_id, :name => 'm_data_sid' + begin + add_index :measure_data, :snapshot_id, :name => 'm_data_sid' + rescue + # already exists + end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/190_add_permanent_id_and_switched_off_to_rule_failures.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/190_add_permanent_id_and_switched_off_to_rule_failures.rb index 69ce668446a..be6f9ff3dd1 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/190_add_permanent_id_and_switched_off_to_rule_failures.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/190_add_permanent_id_and_switched_off_to_rule_failures.rb @@ -26,9 +26,7 @@ class AddPermanentIdAndSwitchedOffToRuleFailures < ActiveRecord::Migration def self.up add_column 'rule_failures', 'permanent_id', :integer, :null => true add_column 'rule_failures', 'switched_off', :boolean, :null => true - add_index 'rule_failures', 'permanent_id', :name => 'rf_permanent_id' - RuleFailure.reset_column_information end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/200_add_index_to_characteristic_property.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/200_add_index_to_characteristic_property.rb index 6f45c9d9f71..e2868bd88e7 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/200_add_index_to_characteristic_property.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/200_add_index_to_characteristic_property.rb @@ -24,7 +24,11 @@ class AddIndexToCharacteristicProperty < ActiveRecord::Migration def self.up - add_index :characteristic_properties, :characteristic_id, :name => 'characteristic_properties_cid' + begin + add_index :characteristic_properties, :characteristic_id, :name => 'characteristic_properties_cid' + rescue + # already exists + end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/201_change_false_positive_on_reviews.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/201_change_false_positive_on_reviews.rb index 3bfae85db62..5e3a4b686f0 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/201_change_false_positive_on_reviews.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/201_change_false_positive_on_reviews.rb @@ -23,14 +23,16 @@ # class ChangeFalsePositiveOnReviews < ActiveRecord::Migration + class Review < ActiveRecord::Base + end + def self.up add_column 'reviews', 'resolution', :string, :limit => 200, :null => true - Review.reset_column_information + Review.reset_column_information Review.update_all("status='RESOLVED', resolution='FALSE-POSITIVE'", "review_type='FALSE_POSITIVE'") remove_column 'reviews', 'review_type' - Review.reset_column_information end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/212_move_async_measures.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/212_move_async_measures.rb index 17de99f74d6..dc720113b3d 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/212_move_async_measures.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/212_move_async_measures.rb @@ -30,8 +30,10 @@ class MoveAsyncMeasures < ActiveRecord::Migration end def self.up + ProjectMeasure.reset_column_information deprecated_measures=ProjectMeasure.find_by_sql("select p1.* from project_measures p1 where p1.snapshot_id is null and p1.measure_date is not null and not exists(select id from project_measures p2 where p2.project_id=p1.project_id and p2.metric_id=p1.metric_id and p2.measure_date is not null and p2.measure_date>p1.measure_date)") + ManualMeasure.reset_column_information say_with_time "Moving #{deprecated_measures.size} measures" do deprecated_measures.each do |dm| if dm.project_id diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/214_add_index_on_manual_measures.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/214_add_index_on_manual_measures.rb index c30fdd264b3..22dc78fa472 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/214_add_index_on_manual_measures.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/214_add_index_on_manual_measures.rb @@ -24,7 +24,11 @@ class AddIndexOnManualMeasures < ActiveRecord::Migration def self.up - add_index('manual_measures', 'resource_id', :name => 'manual_measures_resource_id') + begin + add_index 'manual_measures', 'resource_id', :name => 'manual_measures_resource_id' + rescue + # already exists + end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/216_set_nullable_rule_name.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/216_set_nullable_rule_name.rb index f1d7ed79184..0bd4490e049 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/216_set_nullable_rule_name.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/216_set_nullable_rule_name.rb @@ -23,15 +23,17 @@ # class SetNullableRuleName < ActiveRecord::Migration + class Rule < ActiveRecord::Base + end + def self.up add_column(:rules, :temp_name, :string, :limit => 200, :null => true) - Rule.reset_column_information + Rule.reset_column_information Rule.update_all('temp_name=name') remove_column(:rules, :name) rename_column(:rules, :temp_name, :name) - Rule.reset_column_information end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/220_update_events_table.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/220_update_events_table.rb index cc4d1e1b78b..5b9d623dcf3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/220_update_events_table.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/220_update_events_table.rb @@ -27,8 +27,6 @@ class UpdateEventsTable < ActiveRecord::Migration remove_column :events, :data change_column :events, :name, :string, :limit => 400, :null => true change_column :events, :description, :string, :limit => 4000, :null => true - - Event.reset_column_information end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/221_attach_events_to_snapshots.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/221_attach_events_to_snapshots.rb index f0926a1a573..ff5f9dcb645 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/221_attach_events_to_snapshots.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/221_attach_events_to_snapshots.rb @@ -22,9 +22,15 @@ # Sonar 2.12 # class AttachEventsToSnapshots < ActiveRecord::Migration - + + class Event < ActiveRecord::Base + end + + def self.up logger = RAILS_DEFAULT_LOGGER + Event.reset_column_information + Event.find(:all, :conditions => "snapshot_id IS NULL").each do |event| begin next_snapshot = Snapshot.find(:first, :conditions => ["created_at >= ? AND project_id = ?", event.event_date, event.resource_id], :order => :created_at) diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/231_refactor_rule_measures.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/231_refactor_rule_measures.rb index 26a31dac2c9..191953072a4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/231_refactor_rule_measures.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/231_refactor_rule_measures.rb @@ -24,7 +24,16 @@ # class RefactorRuleMeasures < ActiveRecord::Migration + class ProjectMeasure < ActiveRecord::Base + end + + class Metric < ActiveRecord::Base + end + def self.up + Metric.reset_column_information + ProjectMeasure.reset_column_information + replace('violations', 0, 'info_violations') replace('violations', 1, 'minor_violations') replace('violations', 2, 'major_violations') diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/232_add_rule_columns_to_reviews.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/232_add_rule_columns_to_reviews.rb index 6eb0114da10..1dc5d46040b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/232_add_rule_columns_to_reviews.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/232_add_rule_columns_to_reviews.rb @@ -26,7 +26,6 @@ class AddRuleColumnsToReviews < ActiveRecord::Migration def self.up add_column 'reviews', 'rule_id', :integer, :null => true add_column 'reviews', 'manual_violation', :boolean, :null => true - Review.reset_column_information end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/235_create_loaded_templates.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/235_create_loaded_templates.rb index b4da00171a7..69c6dd45ca4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/235_create_loaded_templates.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/235_create_loaded_templates.rb @@ -23,6 +23,12 @@ # class CreateLoadedTemplates < ActiveRecord::Migration + class Dashboard < ActiveRecord::Base + end + + class LoadedTemplate < ActiveRecord::Base + end + def self.up create_table 'loaded_templates' do |t| t.column 'kee', :string, :null => true, :limit => 200 @@ -30,8 +36,9 @@ class CreateLoadedTemplates < ActiveRecord::Migration end # if this is a migration, then the default dashboard already exists in the DB so it should not be loaded again - default_dashboard = Dashboard.find(:first, :conditions => {:name => 'Dashboard', :user_id => nil}) - if default_dashboard + Dashboard.reset_column_information + if Dashboard.exists?(:name => 'Dashboard', :user_id => nil) + LoadedTemplate.reset_column_information LoadedTemplate.create({:template_type => 'DASHBOARD', :kee => 'Dashboard'}) end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/236_delete_value_type_from_widget_properties.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/236_delete_value_type_from_widget_properties.rb index 64361abfea5..fc6b91ed8c7 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/236_delete_value_type_from_widget_properties.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/236_delete_value_type_from_widget_properties.rb @@ -25,7 +25,6 @@ class DeleteValueTypeFromWidgetProperties < ActiveRecord::Migration def self.up remove_column('widget_properties', 'value_type') - WidgetProperty.reset_column_information() end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/239_delete_duplications_id.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/239_delete_duplications_id.rb index b7510faa0ae..47b2bd7ff42 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/239_delete_duplications_id.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/239_delete_duplications_id.rb @@ -24,9 +24,7 @@ class DeleteDuplicationsId < ActiveRecord::Migration def self.up - begin - remove_column('duplications_index', 'id') - end + remove_column('duplications_index', 'id') end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/240_delete_resource_orphans.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/240_delete_resource_orphans.rb index 5c6d92a1bd4..42a4e89300a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/240_delete_resource_orphans.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/240_delete_resource_orphans.rb @@ -26,7 +26,11 @@ # class DeleteResourceOrphans < ActiveRecord::Migration + class Project < ActiveRecord::Base + end + def self.up + Project.reset_column_information ids=Project.find_by_sql(["select id from projects p where qualifier<>'LIB' and not exists (select * from snapshots s where s.project_id = p.id and s.islast=?)", true]) say_with_time "Delete #{ids.size} resources" do # partition ids because of the Oracle limitation on IN statements diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/252_add_rule_and_active_rule_notes.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/252_add_rule_and_active_rule_notes.rb index db934a2d24f..75ab8d5c467 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/252_add_rule_and_active_rule_notes.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/252_add_rule_and_active_rule_notes.rb @@ -30,8 +30,7 @@ class AddRuleAndActiveRuleNotes < ActiveRecord::Migration t.column :user_login, :string, :null => true, :limit => 40 t.column :data, :binary, :null => true end - add_index :active_rule_notes, :active_rule_id - + add_index :active_rule_notes, :active_rule_id, :name => 'active_rule_notes_active_rule_id' create_table :rule_notes do |t| t.timestamps @@ -39,7 +38,7 @@ class AddRuleAndActiveRuleNotes < ActiveRecord::Migration t.column :user_login, :string, :null => true, :limit => 40 t.column :data, :binary, :null => true end - add_index :rule_notes, :rule_id + add_index :rule_notes, :rule_id, :name => 'rule_notes_rule_id' end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/254_add_metrics_delete_historical_data.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/254_add_metrics_delete_historical_data.rb index 41d8f665889..7794feae48f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/254_add_metrics_delete_historical_data.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/254_add_metrics_delete_historical_data.rb @@ -25,7 +25,6 @@ class AddMetricsDeleteHistoricalData < ActiveRecord::Migration def self.up add_column 'metrics', 'delete_historical_data', :boolean, :null => true - Metric.reset_column_information end end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/255_rename_dbcleaner_properties.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/255_rename_dbcleaner_properties.rb index c1b60665f4a..95338bd38f8 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/255_rename_dbcleaner_properties.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/255_rename_dbcleaner_properties.rb @@ -28,6 +28,7 @@ class RenameDbcleanerProperties < ActiveRecord::Migration def self.up + Property.reset_column_information rename('sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByWeek', 'sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek') rename('sonar.dbcleaner.monthsBeforeKeepingOnlyOneSnapshotByMonth', 'sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByMonth') rename('sonar.dbcleaner.monthsBeforeDeletingAllSnapshots', 'sonar.dbcleaner.weeksBeforeDeletingAllSnapshots') diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/257_add_active_field_on_users.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/257_add_active_field_on_users.rb index ddcb1b06d41..2a7da8f1afd 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/257_add_active_field_on_users.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/257_add_active_field_on_users.rb @@ -23,10 +23,13 @@ # class AddActiveFieldOnUsers < ActiveRecord::Migration + class User < ActiveRecord::Base + end + def self.up add_column 'users', 'active', :boolean, :null => true, :default => true + User.reset_column_information - User.find(:all).each do |user| user.active = true user.save diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/258_add_index_on_review_resource_id.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/258_add_index_on_review_resource_id.rb index 40810ba2a7f..192777d5a63 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/258_add_index_on_review_resource_id.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/258_add_index_on_review_resource_id.rb @@ -25,7 +25,7 @@ class AddIndexOnReviewResourceId < ActiveRecord::Migration def self.up begin - add_index('reviews', 'resource_id', :name => 'reviews_rid') + add_index('reviews', 'resource_id', :name => 'reviews_resource_id') rescue # already exists end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/260_clean_reviews_with_deleted_users_or_resources.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/260_clean_reviews_with_deleted_users_or_resources.rb index b73a34f896b..68c5fc5b5dd 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/260_clean_reviews_with_deleted_users_or_resources.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/260_clean_reviews_with_deleted_users_or_resources.rb @@ -23,8 +23,14 @@ # class CleanReviewsWithDeletedUsersOrResources < ActiveRecord::Migration + class Review < ActiveRecord::Base + end + + class ReviewComment < ActiveRecord::Base + end + def self.up - + Review.reset_column_information Review.find(:all, :include => ['resource', 'assignee', 'user']).each do |review| if review.resource_id && !review.resource # For http://jira.codehaus.org/browse/SONAR-3223 @@ -45,6 +51,7 @@ class CleanReviewsWithDeletedUsersOrResources < ActiveRecord::Migration end # For http://jira.codehaus.org/browse/SONAR-3102 + ReviewComment.reset_column_information ReviewComment.find(:all, :include => 'user').each do |comment| comment.delete if comment.user_id && !comment.user end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt b/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt index a5a7e8fcd91..1c3a2d850bb 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt @@ -10,14 +10,36 @@ HOW TO ADD A MIGRATION -RECOMMANDATIONS +RECOMMENDATIONS -* Don't forget that index name limited to 30 characters in Oracle DB. * Prefer to add nullable columns to avoid problems during migration. -* When adding index, do not forget to name the index (so that it is possible later to delete it) - + Example: - add_index "action_plans", "project_id", :name => "ap_project_id" -* When modifying columns in a table, do not forget to reset the column information on the Ruby model if you use it - + Example: - add_column 'users', 'active', :boolean, :null => true, :default => true - User.reset_column_information + +* Always create an index with a name : add_index "action_plans", "project_id", :name => "action_plans_project_id" + Note that this name is limited to 30 characters because of Oracle constraint. + +* Silently ignore failures when adding an index that has already been created by users. It can occur when the index + is not created in the same migration script than the table. + + begin + add_index "action_plans", "project_id", :name => "action_plans_project_id" + rescue + # ignore + end + +* Use faux models when touching rows (SELECT/INSERT/UPDATE/DELETE). See http://guides.rubyonrails.org/migrations.html#using-models-in-your-migrations + for more details. + + class MyMigration < ActiveRecord::Migration + # This is the faux model. It only maps columns. No functional methods. + class Metric < ActiveRecord::Base + end + + def self.up + # it’s a good idea to call reset_column_information to refresh the ActiveRecord cache for the model prior to + # updating data in the database + Metric.reset_column_information + Metric.find(:all) do |m| + m.save + end + end + end
\ No newline at end of file |