Changeset: c3a5b1262d0e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=c3a5b1262d0e
Modified Files:
        sql/backends/monet5/sql_upgrades.c
Branch: Apr2019
Log Message:

Merged


diffs (truncated from 888 to 300 lines):

diff --git a/sql/backends/monet5/sql_upgrades.c 
b/sql/backends/monet5/sql_upgrades.c
--- a/sql/backends/monet5/sql_upgrades.c
+++ b/sql/backends/monet5/sql_upgrades.c
@@ -29,11 +29,10 @@
  * functions, args) when internal types and/or functions have changed
  * (i.e. the ones in sql_types.c) */
 static str
-sql_fix_system_tables(Client c, mvc *sql)
+sql_fix_system_tables(Client c, mvc *sql, const char *prev_schema)
 {
        size_t bufsize = 1000000, pos = 0;
        char *buf = GDKmalloc(bufsize), *err = NULL;
-       char *schema = stack_get_string(sql, "current_schema");
        node *n;
        sql_schema *s;
 
@@ -174,8 +173,7 @@ sql_fix_system_tables(Client c, mvc *sql
                }
        }
 
-       if (schema)
-               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", 
prev_schema);
 
        assert(pos < bufsize);
        printf("Running database upgrade commands:\n%s\n", buf);
@@ -186,20 +184,19 @@ sql_fix_system_tables(Client c, mvc *sql
 
 #ifdef HAVE_HGE
 static str
-sql_update_hugeint(Client c, mvc *sql)
+sql_update_hugeint(Client c, mvc *sql, const char *prev_schema, bool 
*systabfixed)
 {
        size_t bufsize = 8192, pos = 0;
        char *buf, *err;
-       char *schema;
 
-       if ((err = sql_fix_system_tables(c, sql)) != NULL)
+       if (!*systabfixed &&
+           (err = sql_fix_system_tables(c, sql, prev_schema)) != NULL)
                return err;
+       *systabfixed = true;
 
        if ((buf = GDKmalloc(bufsize)) == NULL)
                throw(SQL, "sql_update_hugeint", SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
 
-       schema = stack_get_string(sql, "current_schema");
-
        pos += snprintf(buf + pos, bufsize - pos, "set schema \"sys\";\n");
 
        /* 80_udf_hge.sql */
@@ -250,9 +247,7 @@ sql_update_hugeint(Client c, mvc *sql)
                        "update sys.functions set system = true where name in 
('fuse', 'generate_series', 'stddev_samp', 'stddev_pop', 'var_samp', 'var_pop', 
'median', 'quantile', 'corr') and schema_id = (select id from sys.schemas where 
name = 'sys');\n"
                        "update sys.functions set system = true where name = 
'filter' and schema_id = (select id from sys.schemas where name = 'json');\n");
 
-       if (schema)
-               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-       pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", 
prev_schema);
        assert(pos < bufsize);
 
        printf("Running database upgrade commands:\n%s\n", buf);
@@ -263,12 +258,10 @@ sql_update_hugeint(Client c, mvc *sql)
 #endif
 
 static str
-sql_update_geom(Client c, mvc *sql, int olddb)
+sql_update_geom(Client c, mvc *sql, int olddb, const char *prev_schema)
 {
        size_t bufsize, pos = 0;
-       char *buf, *err = NULL;
-       char *geomupgrade;
-       char *schema = stack_get_string(sql, "current_schema");
+       char *buf, *err = NULL, *geomupgrade;
        geomsqlfix_fptr fixfunc;
        node *n;
        sql_schema *s = mvc_bind_schema(sql, "sys");
@@ -297,12 +290,12 @@ sql_update_geom(Client c, mvc *sql, int 
                    (strcmp(t->base.name, "mbr") == 0 ||
                     strcmp(t->base.name, "wkb") == 0 ||
                     strcmp(t->base.name, "wkba") == 0))
-                       pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.types values (%d, '%s', '%s', %u, %u, %d, %d, %d);\n", t->base.id, 
t->base.name, t->sqlname, t->digits, t->scale, t->radix, t->eclass, t->s ? 
t->s->base.id : s->base.id);
+                       pos += snprintf(buf + pos, bufsize - pos, "insert into 
sys.types values (%d, '%s', '%s', %u, %u, %d, %d, %d);\n",
+                                                       t->base.id, 
t->base.name, t->sqlname, t->digits, t->scale, t->radix, t->eclass,
+                                                       t->s ? t->s->base.id : 
s->base.id);
        }
 
-       if (schema)
-               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-       pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", 
prev_schema);
 
        assert(pos < bufsize);
        printf("Running database upgrade commands:\n%s\n", buf);
@@ -312,11 +305,10 @@ sql_update_geom(Client c, mvc *sql, int 
 }
 
 static str
-sql_update_jul2017(Client c, mvc *sql)
+sql_update_jul2017(Client c, const char *prev_schema)
 {
        size_t bufsize = 10000, pos = 0;
        char *buf = GDKmalloc(bufsize), *err = NULL;
-       char *schema = stack_get_string(sql, "current_schema");
        char *q1 = "select id from sys.functions where name = 'shpload' and 
schema_id = (select id from sys.schemas where name = 'sys');\n";
        res_table *output;
        BAT *b;
@@ -416,9 +408,7 @@ sql_update_jul2017(Client c, mvc *sql)
        }
        res_tables_destroy(output);
 
-       if (schema)
-               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-       pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", 
prev_schema);
 
        assert(pos < bufsize);
        printf("Running database upgrade commands:\n%s\n", buf);
@@ -463,7 +453,6 @@ sql_update_jul2017_sp2(Client c)
                                "GRANT EXECUTE ON FUNCTION sys.environment() TO 
PUBLIC;\n"
                                "GRANT SELECT ON sys.environment TO PUBLIC;\n"
                                );
-                       pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
                        assert(pos < bufsize);
                        printf("Running database upgrade commands:\n%s\n", buf);
                        err = SQLstatementIntern(c, &buf, "update", true, 
false, NULL);
@@ -477,7 +466,7 @@ sql_update_jul2017_sp2(Client c)
 }
 
 static str
-sql_update_jul2017_sp3(Client c, mvc *sql)
+sql_update_jul2017_sp3(Client c, mvc *sql, const char *prev_schema, bool 
*systabfixed)
 {
        char *err = NULL;
        sql_schema *sys;
@@ -492,10 +481,11 @@ sql_update_jul2017_sp3(Client c, mvc *sq
        tab = find_sql_table(sys, "functions");
        col = find_sql_column(tab, "name");
        rid = table_funcs.column_find_row(sql->session->tr, col, 
"sys_update_schemas", NULL);
-       if (is_oid_nil(rid)) {
-               err = sql_fix_system_tables(c, sql);
+       if (is_oid_nil(rid) && !*systabfixed) {
+               err = sql_fix_system_tables(c, sql, prev_schema);
                if (err != NULL)
                        return err;
+               *systabfixed = true;
        }
        /* if there is no value "system_update_schemas" in
         * sys.triggers.name, we need to add the triggers */
@@ -503,7 +493,6 @@ sql_update_jul2017_sp3(Client c, mvc *sq
        col = find_sql_column(tab, "name");
        rid = table_funcs.column_find_row(sql->session->tr, col, 
"system_update_schemas", NULL);
        if (is_oid_nil(rid)) {
-               char *schema = stack_get_string(sql, "current_schema");
                size_t bufsize = 1024, pos = 0;
                char *buf = GDKmalloc(bufsize);
                if (buf == NULL)
@@ -514,9 +503,7 @@ sql_update_jul2017_sp3(Client c, mvc *sq
                        "set schema \"sys\";\n"
                        "create trigger system_update_schemas after update on 
sys.schemas for each statement call sys_update_schemas();\n"
                        "create trigger system_update_tables after update on 
sys._tables for each statement call sys_update_tables();\n");
-               if (schema)
-                       pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-               pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", prev_schema);
                assert(pos < bufsize);
                printf("Running database upgrade commands:\n%s\n", buf);
                err = SQLstatementIntern(c, &buf, "update", true, false, NULL);
@@ -526,11 +513,10 @@ sql_update_jul2017_sp3(Client c, mvc *sq
 }
 
 static str
-sql_update_mar2018_geom(Client c, mvc *sql, sql_table *t)
+sql_update_mar2018_geom(Client c, sql_table *t, const char *prev_schema)
 {
        size_t bufsize = 10000, pos = 0;
        char *buf = GDKmalloc(bufsize), *err = NULL;
-       char *schema = stack_get_string(sql, "current_schema");
 
        if (buf == NULL)
                throw(SQL, "sql_update_mar2018_geom", SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
@@ -553,9 +539,7 @@ sql_update_mar2018_geom(Client c, mvc *s
                        "GRANT SELECT ON sys.geometry_columns TO PUBLIC;\n"
                        "update sys._tables set system = true where name = 
'geometry_columns' and schema_id in (select id from schemas where name = 
'sys');\n");
 
-       if (schema)
-               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-       pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", 
prev_schema);
 
        assert(pos < bufsize);
        printf("Running database upgrade commands:\n%s\n", buf);
@@ -565,11 +549,10 @@ sql_update_mar2018_geom(Client c, mvc *s
 }
 
 static str
-sql_update_mar2018(Client c, mvc *sql)
+sql_update_mar2018(Client c, mvc *sql, const char *prev_schema, bool 
*systabfixed)
 {
        size_t bufsize = 30000, pos = 0;
        char *buf, *err;
-       char *schema;
        sql_schema *s;
        sql_table *t;
        res_table *output;
@@ -581,19 +564,19 @@ sql_update_mar2018(Client c, mvc *sql)
                return err;
        b = BATdescriptor(output->cols[0].b);
        if (b) {
-               if (BATcount(b) == 0) {
+               if (BATcount(b) == 0 && !*systabfixed) {
                        /* if there is no value "quarter" in
                         * sys.functions.name, we need to update the
                         * sys.functions table */
-                       err = sql_fix_system_tables(c, sql);
+                       err = sql_fix_system_tables(c, sql, prev_schema);
                        if (err != NULL)
                                return err;
+                       *systabfixed = true;
                }
                BBPunfix(b->batCacheid);
        }
        res_tables_destroy(output);
 
-       schema = stack_get_string(sql, "current_schema");
        buf = GDKmalloc(bufsize);
        if (buf == NULL)
                throw(SQL, "sql_update_mar2018", SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
@@ -1024,22 +1007,18 @@ sql_update_mar2018(Client c, mvc *sql)
                        "AND schema_id = (SELECT id FROM sys.schemas WHERE name 
= 'sys');\n"
                );
 
-       if (schema)
-               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-       pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", 
prev_schema);
 
        assert(pos < bufsize);
        printf("Running database upgrade commands:\n%s\n", buf);
        err = SQLstatementIntern(c, &buf, "update", true, false, NULL);
        if (err == MAL_SUCCEED) {
-               schema = stack_get_string(sql, "current_schema");
                pos = snprintf(buf, bufsize, "set schema \"sys\";\n"
                               "ALTER TABLE sys.keywords SET READ ONLY;\n"
                               "ALTER TABLE sys.function_types SET READ ONLY;\n"
                               "ALTER TABLE sys.function_languages SET READ 
ONLY;\n");
-               if (schema)
-                       pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-               pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", prev_schema);
+               assert(pos < bufsize);
                printf("Running database upgrade commands:\n%s\n", buf);
                err = SQLstatementIntern(c, &buf, "update", true, false, NULL);
        }
@@ -1049,14 +1028,11 @@ sql_update_mar2018(Client c, mvc *sql)
 
 #ifdef HAVE_NETCDF
 static str
-sql_update_mar2018_netcdf(Client c, mvc *sql)
+sql_update_mar2018_netcdf(Client c, const char *prev_schema)
 {
        size_t bufsize = 1000, pos = 0;
-       char *buf, *err;
-       char *schema;
+       char *buf = GDKmalloc(bufsize), *err;
 
-       schema = stack_get_string(sql, "current_schema");
-       buf = GDKmalloc(bufsize);
        if (buf == NULL)
                throw(SQL, "sql_update_mar2018_netcdf", SQLSTATE(HY001) 
MAL_MALLOC_FAIL);
 
@@ -1072,9 +1048,7 @@ sql_update_mar2018_netcdf(Client c, mvc 
                        "grant execute on procedure 
sys.netcdf_attach(varchar(256)) to public;\n"
                        "grant execute on procedure 
sys.netcdf_importvar(integer, varchar(256)) to public;\n");
 
-       if (schema)
-               pos += snprintf(buf + pos, bufsize - pos, "set schema 
\"%s\";\n", schema);
-       pos += snprintf(buf + pos, bufsize - pos, "commit;\n");
+       pos += snprintf(buf + pos, bufsize - pos, "set schema \"%s\";\n", 
prev_schema);
 
        assert(pos < bufsize);
        printf("Running database upgrade commands:\n%s\n", buf);
@@ -1086,17 +1060,15 @@ sql_update_mar2018_netcdf(Client c, mvc 
 
 #ifdef HAVE_SAMTOOLS
 static str
-sql_update_mar2018_samtools(Client c, mvc *sql)
+sql_update_mar2018_samtools(Client c, mvc *sql, const char *prev_schema)
 {
        size_t bufsize = 2000, pos = 0;
        char *buf, *err;
-       char *schema;
        sql_schema *s = mvc_bind_schema(sql, "bam");
 
        if (s == NULL)
                return MAL_SUCCEED;
 
-       schema = stack_get_string(sql, "current_schema");
        buf = GDKmalloc(bufsize);
        if (buf == NULL)
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to