Skip to content
This repository
Browse code

Merge pull request #6028 from sikachu/master-fix_plpgsql

Create plpgsql language if not available
  • Loading branch information...
commit e4c48fff379bef7492fc5158181395763ed843d1 2 parents 71cf6ef + 5156110
Aaron Patterson authored April 28, 2012
50  activerecord/test/schema/postgresql_specific_schema.rb
@@ -127,28 +127,36 @@
127 127
   );
128 128
 _SQL
129 129
 
130  
-  execute <<_SQL
131  
-  CREATE TABLE postgresql_partitioned_table_parent (
132  
-    id SERIAL PRIMARY KEY,
133  
-    number integer
134  
-  );
135  
-  CREATE TABLE postgresql_partitioned_table ( )
136  
-    INHERITS (postgresql_partitioned_table_parent);
137  
-
138  
-  CREATE OR REPLACE FUNCTION partitioned_insert_trigger()
139  
-  RETURNS TRIGGER AS $$
140  
-  BEGIN
141  
-    INSERT INTO postgresql_partitioned_table VALUES (NEW.*);
142  
-    RETURN NULL;
143  
-  END;
144  
-  $$
145  
-  LANGUAGE plpgsql;
146  
-
147  
-  CREATE TRIGGER insert_partitioning_trigger
148  
-    BEFORE INSERT ON postgresql_partitioned_table_parent
149  
-    FOR EACH ROW EXECUTE PROCEDURE partitioned_insert_trigger();
  130
+begin
  131
+    execute <<_SQL
  132
+    CREATE TABLE postgresql_partitioned_table_parent (
  133
+      id SERIAL PRIMARY KEY,
  134
+      number integer
  135
+    );
  136
+    CREATE TABLE postgresql_partitioned_table ( )
  137
+      INHERITS (postgresql_partitioned_table_parent);
  138
+
  139
+    CREATE OR REPLACE FUNCTION partitioned_insert_trigger()
  140
+    RETURNS TRIGGER AS $$
  141
+    BEGIN
  142
+      INSERT INTO postgresql_partitioned_table VALUES (NEW.*);
  143
+      RETURN NULL;
  144
+    END;
  145
+    $$
  146
+    LANGUAGE plpgsql;
  147
+
  148
+    CREATE TRIGGER insert_partitioning_trigger
  149
+      BEFORE INSERT ON postgresql_partitioned_table_parent
  150
+      FOR EACH ROW EXECUTE PROCEDURE partitioned_insert_trigger();
150 151
 _SQL
151  
-
  152
+rescue ActiveRecord::StatementInvalid => e
  153
+  if e.message =~ /language "plpgsql" does not exist/
  154
+    execute "CREATE LANGUAGE 'plpgsql';"
  155
+    retry
  156
+  else
  157
+    raise e
  158
+  end
  159
+end
152 160
 
153 161
   begin
154 162
     execute <<_SQL

0 notes on commit e4c48ff

Please sign in to comment.
Something went wrong with that request. Please try again.