]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4178 Override create_table to automatically add Oracle triggers
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 25 Jun 2013 15:39:02 +0000 (17:39 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 25 Jun 2013 15:39:46 +0000 (17:39 +0200)
sonar-server/src/main/webapp/WEB-INF/config/environment.rb

index a83470fee39e048940cd13d16c8a5052f1fa7d23..26a8b0053fb0cce17a9cdb2dd24d0fa170233155 100644 (file)
@@ -126,6 +126,42 @@ class ActiveRecord::Migration
         change_column(tablename, columnname, :big_integer, :null => true)
     end
   end
+  
+  # SONAR-4178
+  def self.create_table(table_name, options = {})
+    super(table_name, options)
+    case dialect()
+      when "oracle"
+        create_id_trigger(table_name) unless options[:id] == false
+      else
+        # Do nothing
+    end
+  end
+  
+  private
+  
+  def self.create_id_trigger(table)
+      execute_ddl("create trigger for table #{table}", 
+      
+      %{CREATE OR REPLACE TRIGGER #{table}_id_trg
+          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.execute_ddl(message, ddl)
+    begin
+      say_with_time(message) do
+        ActiveRecord::Base.connection.execute(ddl)
+      end
+    rescue
+      # already executed
+    end
+  end
 end
 
 # patch for SONAR-1182. GWT does not support ISO8601 dates that end with 'Z'