diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-13 12:20:34 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-14 12:11:53 +0100 |
commit | 610a9a839737166711a29cbb1fdb7911474c5b40 (patch) | |
tree | f9cb60518fd3c4aaf5d8a1d6233a55c0ace06000 /server/sonar-web/src/main | |
parent | e2cff9277df4ccaae9098a13d36c52ef70b259fc (diff) | |
download | sonarqube-610a9a839737166711a29cbb1fdb7911474c5b40.tar.gz sonarqube-610a9a839737166711a29cbb1fdb7911474c5b40.zip |
SONAR-8445 move SQ 5.6 create schema migrations out of Ruby
and start some cleaning of migration related code in Ruby
Diffstat (limited to 'server/sonar-web/src/main')
5 files changed, 1 insertions, 260 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 3c375e4607b..00f28b34ec3 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 @@ -100,164 +100,9 @@ Rails::Initializer.run do |config| # Prevent appearance of ANSI style escape sequences in logs config.active_record.colorize_logging = false - # Use SQL instead of Active Record's schema dumper when creating the test database. - # This is necessary if your schema can't be completely dumped by the schema dumper, - # like if you have constraints or database-specific column types - # config.active_record.schema_format = :sql - - # Activate observers that should always be running - # Please note that observers generated using script/generate observer need to have an _observer suffix - # config.active_record.observers = :cacher, :garbage_collector, :forum_observer end -class ActiveRecord::Migration - def self.dialect - 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] - - 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 = {}) - if dialect()=='mysql' && !options[:length] - # Index of varchar column is limited to 767 bytes on mysql (<= 255 UTF-8 characters) - # See http://jira.sonarsource.com/browse/SONAR-4137 and - # http://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html - options[:length]=255 - end - add_index table_name, column_name, options - end - - def self.execute_java_migration(classname) - Java::OrgSonarServerUi::JRubyFacade.getInstance().databaseMigrator().executeMigration(classname) - end - - def self.alter_to_big_primary_key(tablename) - case dialect() - when "postgre" - execute "ALTER TABLE #{tablename} ALTER COLUMN id TYPE bigint" - when "mysql" - execute "ALTER TABLE #{tablename} CHANGE id id BIGINT AUTO_INCREMENT" - when "h2" - # not needed? - when "oracle" - # do nothing, oracle integer are big enough - when "sqlserver" - constraint=select_one "select name from sysobjects where parent_obj = (select id from sysobjects where name = '#{tablename}')" - execute "ALTER TABLE #{tablename} DROP CONSTRAINT #{constraint["name"]}" - execute "ALTER TABLE #{tablename} ALTER COLUMN id bigint" - execute "ALTER TABLE #{tablename} ADD PRIMARY KEY(id)" - end - end - - def self.alter_to_big_integer(tablename, columnname, indexname=nil) - case dialect() - when "sqlserver" - execute "DROP INDEX #{indexname} on #{tablename}" if indexname - change_column(tablename, columnname, :big_integer, :null => true) - execute "CREATE INDEX #{indexname} on #{tablename}(#{columnname})" if indexname - else - change_column(tablename, columnname, :big_integer, :null => true) - end - end - - def self.add_primary_key(tablename, columnname) - if dialect()=="mysql" - execute "ALTER TABLE `#{tablename}` ADD PRIMARY KEY (`#{columnname}`)" - else - execute "ALTER TABLE #{tablename} ADD CONSTRAINT pk_#{tablename} PRIMARY KEY (#{columnname})" - end - end - - # SONAR-4178 - def self.create_table(table_name, options = {}) - # 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 - end - - def drop_table(table_name, options = {}) - super(table_name, options) - drop_id_trigger(table_name) if dialect()=='oracle' - end - - def self.rename_table(old_table_name, new_table_name, options = {}) - drop_id_trigger(old_table_name) if dialect()=='oracle' && options[:id] != false - super(old_table_name, new_table_name) - create_id_trigger(new_table_name) if dialect()=='oracle' && options[:id] != false - end - - def self.create_id_trigger(table) - execute_ddl("create trigger for table #{table}", - - %{CREATE OR REPLACE TRIGGER #{table}_idt - BEFORE INSERT ON #{table} - FOR EACH ROW - BEGIN - IF :new.id IS null THEN - SELECT #{table}_seq.nextval INTO :new.id FROM dual; - END IF; - END;}) - end - - def self.drop_id_trigger(table) - drop_trigger("#{table}_idt") - end - - def self.drop_trigger(trigger_name) - execute_ddl("drop trigger #{trigger_name}", "DROP TRIGGER #{trigger_name}") - end - - def self.write(text="") - # See migration.rb, the method write directly calls "puts" - Java::OrgSlf4j::LoggerFactory::getLogger('DbMigration').info(text) if verbose - end - - def self.drop_index_quietly(table, index) - begin - remove_index table, :name => index - rescue - #ignore - end - end - - - private - - def self.execute_ddl(message, ddl) - say_with_time(message) do - ActiveRecord::Base.connection.execute(ddl) - end - end -end - # patch for SONAR-1182. GWT does not support ISO8601 dates that end with 'Z' # http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/i18n/client/DateTimeFormat.html module ActiveSupport diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/001_create_initial_schema.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/001_create_initial_schema.rb deleted file mode 100644 index 049022bd018..00000000000 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/001_create_initial_schema.rb +++ /dev/null @@ -1,24 +0,0 @@ - # - # SonarQube, open source software quality management tool. - # Copyright (C) 2008-2016 SonarSource - # mailto:contact AT sonarsource DOT com - # - # SonarQube is free software; you can redistribute it and/or - # modify it under the terms of the GNU Lesser General Public - # License as published by the Free Software Foundation; either - # version 3 of the License, or (at your option) any later version. - # - # SonarQube is distributed in the hope that it will be useful, - # but WITHOUT ANY WARRANTY; without even the implied warranty of - # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - # Lesser General Public License for more details. - # - # You should have received a copy of the GNU Lesser General Public License - # along with this program; if not, write to the Free Software Foundation, - # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - # -class CreateInitialSchema < ActiveRecord::Migration - def self.up - execute_java_migration('org.sonar.db.version.v56.CreateInitialSchema') - end -end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/002_populate_initial_schema.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/002_populate_initial_schema.rb deleted file mode 100644 index 3687c57fe3d..00000000000 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/002_populate_initial_schema.rb +++ /dev/null @@ -1,24 +0,0 @@ - # - # SonarQube, open source software quality management tool. - # Copyright (C) 2008-2016 SonarSource - # mailto:contact AT sonarsource DOT com - # - # SonarQube is free software; you can redistribute it and/or - # modify it under the terms of the GNU Lesser General Public - # License as published by the Free Software Foundation; either - # version 3 of the License, or (at your option) any later version. - # - # SonarQube is distributed in the hope that it will be useful, - # but WITHOUT ANY WARRANTY; without even the implied warranty of - # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - # Lesser General Public License for more details. - # - # You should have received a copy of the GNU Lesser General Public License - # along with this program; if not, write to the Free Software Foundation, - # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - # -class PopulateInitialSchema < ActiveRecord::Migration - def self.up - execute_java_migration('org.sonar.db.version.v56.PopulateInitialSchema') - end -end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/README.txt b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/README.txt deleted file mode 100644 index 3cd43b6e984..00000000000 --- a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/README.txt +++ /dev/null @@ -1,55 +0,0 @@ -HOW TO ADD A MIGRATION - -* Jump some versions when adding the first Ruby on Rails migration of a new sonar version. For example if sonar 2.10 is 193, then sonar 2.11 should start at 200. -* Complete the DDL files for H2 : - + sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl - + sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql : - - add "INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('<THE MIGRATION ID>')" -* Update the migration id defined in org.sonar.db.version.DatabaseVersion -* If a table is added or removed, then edit org.sonar.db.version.DatabaseVersion#TABLES -* Changes in bulk must be handled using Java migrations based on org.sonar.db.version.MassUpdate : - + Create the class for the Java migration in package org.sonar.db.version.vXYZ, where XYZ is the version of SQ without dots - + Add the class to org.sonar.db.version.MigrationStepModule - + Create a Ruby migration which calls execute_java_migration('org.sonar.db.version.vXYZ.MyMigration') - + Simple, "one to one" migrations that only need to be split by 1000 can rely on class org.sonar.db.version.BaseDataChange - - -RECOMMENDATIONS - -* Prefer to add nullable columns to avoid problems during migration, EXCEPT for booleans. For booleans: - - * columns must be NON-nullable but default value (false) must NOT be set in database. It allows to fully define the model programmatically. - * column names must be chosen so that the default value is actually false. - * E.g.: rule_failures.switched_off - -* 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. Note that associations must not be used. - IMPORTANT : do not use faux models for user models (User, Group, UserRole, GroupRole) because of required associations and password encryption. - - - 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 diff --git a/server/sonar-web/src/main/webapp/WEB-INF/lib/database_version.rb b/server/sonar-web/src/main/webapp/WEB-INF/lib/database_version.rb index 52f032d0441..9656163835a 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/lib/database_version.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/lib/database_version.rb @@ -53,7 +53,7 @@ class DatabaseVersion def self.uptodate? unless $uptodate - $uptodate = (current_version>=target_version) + $uptodate = Java::OrgSonarServerUi::JRubyFacade.getInstance().isDbUptodate() end $uptodate end @@ -63,7 +63,6 @@ class DatabaseVersion end def self.upgrade_and_start - ActiveRecord::Migrator.migrate(migrations_path) Java::OrgSonarServerPlatform::Platform.getInstance().upgradeDb() Java::OrgSonarServerPlatform::Platform.getInstance().doStart() load_java_web_services |