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
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
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
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
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
#
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
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
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
#
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
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
# 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)
#
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')
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
#
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
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
def self.up
remove_column('widget_properties', 'value_type')
- WidgetProperty.reset_column_information()
end
end
class DeleteDuplicationsId < ActiveRecord::Migration
def self.up
- begin
- remove_column('duplications_index', 'id')
- end
+ remove_column('duplications_index', 'id')
end
end
#
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
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
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
def self.up
add_column 'metrics', 'delete_historical_data', :boolean, :null => true
- Metric.reset_column_information
end
end
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')
#
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
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
#
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
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
-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