aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/db/migrate/README.txt
blob: a618f27923e522303c1cf70dc45dbbd79dd8e6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl
  + sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql :
    - add "INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('<THE MIGRATION ID>')"
* Update the migration id defined in the Java class org.sonar.core.persistence.DatabaseTest
* If a table is added or removed, then update the list org.sonar.core.persistence.DatabaseTest#TABLES



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