From deaae7dd7224fe83df80067c9cdcae0e3583844e Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 15 Nov 2012 23:37:09 +0000 Subject: [PATCH] Added version_sql() function and consistency check of sql version --- bin/pg_repack.c | 18 ++++++++++++++++-- doc/pg_repack.rst | 7 +++++++ lib/Makefile | 7 ++++--- lib/pg_repack.sql.in | 4 ++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/bin/pg_repack.c b/bin/pg_repack.c index afcb030..98d3aa5 100644 --- a/bin/pg_repack.c +++ b/bin/pg_repack.c @@ -240,15 +240,18 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t reconnect(ERROR); /* Query the extension version. Exit if no match */ - res = execute_elevel("select repack.version()", 0, NULL, DEBUG2); + res = execute_elevel("select repack.version(), repack.version_sql()", + 0, NULL, DEBUG2); if (PQresultStatus(res) == PGRES_TUPLES_OK) { const char *libver; char buf[64]; /* the string is something like "pg_repack 1.1.7" */ - libver = getstr(res, 0, 0); snprintf(buf, sizeof(buf), "%s %s", PROGRAM_NAME, PROGRAM_VERSION); + + /* check the version of the C library */ + libver = getstr(res, 0, 0); if (0 != strcmp(buf, libver)) { if (errbuf) @@ -257,6 +260,17 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t buf, libver); goto cleanup; } + + /* check the version of the SQL extension */ + libver = getstr(res, 0, 1); + if (0 != strcmp(buf, libver)) + { + if (errbuf) + snprintf(errbuf, errsize, + "extension '%s' required, found extension '%s'", + buf, libver); + goto cleanup; + } } else { diff --git a/doc/pg_repack.rst b/doc/pg_repack.rst index 6682f3c..3adb41e 100644 --- a/doc/pg_repack.rst +++ b/doc/pg_repack.rst @@ -290,6 +290,13 @@ ERROR: program 'pg_repack V1' does not match database library 'pg_repack V2' database; if they are what expected you may need to repeat pg_repack installation. +ERROR: extension 'pg_repack V1' required, found extension 'pg_repack V2' + The SQL extension found in the database does not match the version + required by the pg_repack program. + + You should drop the extension from the database and reload it as described + in the installation_ section. + ERROR: relation "table" has no primary key The target table doesn't have PRIMARY KEY. diff --git a/lib/Makefile b/lib/Makefile index 045cc0a..7b9a92b 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -30,7 +30,7 @@ DATA_built = pg_repack.sql DATA = uninstall_pg_repack.sql endif -USE_PGXS = 1 # use pgxs if not in contrib directory +USE_PGXS = 1 PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) @@ -40,11 +40,12 @@ LIBS := $(filter-out -lxslt, $(LIBS)) pg_repack.sql: pg_repack.sql.in echo "BEGIN;\n" > $@; \ - sed 's,MODULE_PATHNAME,$$libdir/$(MODULE_big),g' $< >> $@; \ + sed 's,MODULE_PATHNAME,$$libdir/$(MODULE_big),g' $< \ + | sed 's,REPACK_VERSION,$(REPACK_VERSION),g' >> $@; \ echo "\nCOMMIT;" >> $@; pg_repack--$(REPACK_VERSION).sql: pg_repack.sql.in - cat $< > $@; + sed 's,REPACK_VERSION,$(REPACK_VERSION),g' $< > $@; pg_repack.control: pg_repack.control.in sed 's,REPACK_VERSION,$(REPACK_VERSION),g' $< > $@ diff --git a/lib/pg_repack.sql.in b/lib/pg_repack.sql.in index 52514f2..3f78d19 100644 --- a/lib/pg_repack.sql.in +++ b/lib/pg_repack.sql.in @@ -12,6 +12,10 @@ CREATE FUNCTION repack.version() RETURNS text AS 'MODULE_PATHNAME', 'repack_version' LANGUAGE C IMMUTABLE STRICT; +CREATE FUNCTION repack.version_sql() RETURNS text AS +$$SELECT 'pg_repack REPACK_VERSION'::text$$ +LANGUAGE SQL IMMUTABLE STRICT; + CREATE AGGREGATE repack.array_accum ( sfunc = array_append, basetype = anyelement,