From b62e3e85e99579a4ef56e24331296122d28f68c7 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 9 Mar 2015 12:07:07 +0100 Subject: [PATCH] Reentrant db migrations when adding/removing columns or tables --- .../main/webapp/WEB-INF/config/environment.rb | 26 ++++++++++++++++--- .../db/migrate/540_create_activities_table.rb | 2 +- .../db/migrate/718_add_issue_long_dates.rb | 3 +++ .../db/migrate/720_rename_issue_long_dates.rb | 3 +++ ...21_remove_sort_field_from_issue_filters.rb | 3 +++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/server/sonar-web/src/main/webapp/WEB-INF/config/environment.rb b/server/sonar-web/src/main/webapp/WEB-INF/config/environment.rb index 62d3e809416..3e0fed42893 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/config/environment.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/config/environment.rb @@ -116,13 +116,31 @@ class ActiveRecord::Migration ActiveRecord::Base.configurations[ENV['RAILS_ENV']]['dialect'] end + def self.column_exists?(table_name, column_name) + columns(table_name).any?{ |c| c.name == column_name.to_s } + end + def self.add_index(table_name, column_name, options = {}) # ActiveRecord can generate index names longer than 30 characters, but that's # not supported by Oracle, the "Enterprise" database. # For this reason we force to set name of indexes. raise ArgumentError, 'Missing index name' unless options[:name] - super(table_name, column_name, options) + unless index_exists?(table_name, column_name, :name => options[:name]) + super(table_name, column_name, options) + end + end + + def self.remove_column(table_name, column_name) + if column_exists?(table_name, column_name) + super(table_name, column_name) + end + end + + def self.add_column(table_name, column_name, type, options = {}) + unless column_exists?(table_name, column_name) + super(table_name, column_name, type, options) + end end def self.add_varchar_index(table_name, column_name, options = {}) @@ -173,8 +191,10 @@ class ActiveRecord::Migration # Oracle constraint (see names of triggers and indices) raise ArgumentError, "Table name is too long: #{table_name}" if table_name.to_s.length>25 - super(table_name, options) - create_id_trigger(table_name) if dialect()=='oracle' && options[:id] != false + unless table_exists?(table_name) + super(table_name, options) + create_id_trigger(table_name) if dialect()=='oracle' && options[:id] != false + end end def drop_table(table_name, options = {}) diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/540_create_activities_table.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/540_create_activities_table.rb index 590dbac61b1..9402c05c765 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/540_create_activities_table.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/540_create_activities_table.rb @@ -24,7 +24,7 @@ # class CreateActivitiesTable < ActiveRecord::Migration def self.up - create_table 'activities'do |t| + create_table 'activities' do |t| t.column 'created_at', :datetime, :null => false t.column 'user_login', :string, :limit => 255 t.column 'data_field', :text diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/718_add_issue_long_dates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/718_add_issue_long_dates.rb index bf0288c7f12..8ccb228a7df 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/718_add_issue_long_dates.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/718_add_issue_long_dates.rb @@ -18,6 +18,9 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +# +# SonarQube 5.0 +# class AddIssueLongDates < ActiveRecord::Migration def self.up diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/720_rename_issue_long_dates.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/720_rename_issue_long_dates.rb index e81c5659a0b..1e2cce5f22d 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/720_rename_issue_long_dates.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/720_rename_issue_long_dates.rb @@ -18,6 +18,9 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +# +# SonarQube 5.0 +# class RenameIssueLongDates < ActiveRecord::Migration def self.up diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/721_remove_sort_field_from_issue_filters.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/721_remove_sort_field_from_issue_filters.rb index 8978b97225c..262b9fd2c2f 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/721_remove_sort_field_from_issue_filters.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/721_remove_sort_field_from_issue_filters.rb @@ -18,6 +18,9 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +# +# SonarQube 5.0 +# class RemoveSortFieldFromIssueFilters < ActiveRecord::Migration def self.up -- 2.39.5