rewrite create_schema in plpgsql
authorSteve Hackbarth <stephenhackbarth@gmail.com>
Tue, 19 Aug 2014 14:34:20 +0000 (09:34 -0500)
committerSteve Hackbarth <stephenhackbarth@gmail.com>
Tue, 19 Aug 2014 14:34:20 +0000 (09:34 -0500)
lib/orm/source/manifest.js
lib/orm/source/xt/functions/create_schema.sql

index e8ef2d1..45ce9ec 100644 (file)
@@ -3,6 +3,9 @@
   "version": "1.3.9",
   "databaseScripts": [
     "create_plv8.sql",
+    "xt/functions/create_schema.sql",
+    "create_sys_schema.sql",
+    "create_xm_schema.sql",
     "xt/functions/add_inheritance.sql",
     "xt/functions/any_numeric.sql",
     "xt/functions/any_text.sql",
@@ -22,9 +25,6 @@
     "xt/functions/js_init.sql",
     "xt/functions/install_js.sql",
     "xt/functions/install_orm.sql",
-    "xt/functions/create_schema.sql",
-    "create_sys_schema.sql",
-    "create_xm_schema.sql",
     "xt/functions/is_date.sql",
     "xt/functions/not_any_numeric.sql",
     "xt/functions/not_any_text.sql",
index 586a69b..9134819 100644 (file)
@@ -1,9 +1,25 @@
-create or replace function xt.create_schema(schema_name text) returns boolean volatile as $$
+create or replace function xt.create_schema(sch text) returns boolean volatile as $$
+declare
+  count integer;
+  query text;
+begin
+
   /* Only create the schema if it hasn't been created already */
-  var res, sql = "select schema_name from information_schema.schemata where schema_name = $1",
-  res = plv8.execute(sql, [schema_name]);
-  if (!res.length) {
-    sql = "create schema %1$I; grant all on schema %1$I to group xtrole;"
-    plv8.execute(XT.format(sql, [schema_name]));
-  }
-$$ language plv8;
+  perform *
+  from information_schema.schemata
+  where schema_name = sch;
+
+  get diagnostics count = row_count;
+
+  if (count > 0) then
+    return false;
+  end if;
+
+  query = 'create schema ' || sch || ';' ||
+           'grant all on schema ' || sch || ' to group xtrole;';
+  execute query;
+
+  return true;
+
+end;
+$$ language 'plpgsql';