From 692204e3810d1dc874c27472b7ff98b1e409f150 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Thu, 15 Jan 2015 18:34:51 +0900 Subject: [PATCH] Consolidate duplicated schema check code --- dbutils.c | 36 +++++++++++++++++++++++++++++++ dbutils.h | 1 + repmgr.c | 63 +++++++++++++------------------------------------------ 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/dbutils.c b/dbutils.c index 7b9e8306..0bb16796 100644 --- a/dbutils.c +++ b/dbutils.c @@ -80,6 +80,42 @@ establish_db_connection_by_params(const char *keywords[], const char *values[], return conn; } + +bool +check_cluster_schema(PGconn *conn) +{ + PGresult *res; + char sqlquery[QUERY_STR_LEN]; + + sqlquery_snprintf(sqlquery, + "SELECT 1 FROM pg_namespace WHERE nspname = '%s'", + get_repmgr_schema()); + + log_debug(_("check_cluster_schema(): %s\n"), sqlquery); + res = PQexec(conn, sqlquery); + if (PQresultStatus(res) != PGRES_TUPLES_OK) + { + log_err(_("check_cluster_schema(): unable to check cluster schema: %s\n"), + PQerrorMessage(conn)); + PQclear(res); + return false; + } + + if (PQntuples(res) == 0) + { + /* schema doesn't exist */ + log_notice(_("check_cluster_schema(): schema '%s' doesn't exist.\n"), get_repmgr_schema()); + PQclear(res); + + return false; + } + + PQclear(res); + + return true; +} + + int is_standby(PGconn *conn) { diff --git a/dbutils.h b/dbutils.h index e8399774..fb572ae8 100644 --- a/dbutils.h +++ b/dbutils.h @@ -27,6 +27,7 @@ PGconn *establish_db_connection(const char *conninfo, PGconn *establish_db_connection_by_params(const char *keywords[], const char *values[], const bool exit_on_error); +bool check_cluster_schema(PGconn *conn); int is_standby(PGconn *conn); int is_witness(PGconn *conn,char *cluster, int node_id); bool is_pgup(PGconn *conn, int timeout); diff --git a/repmgr.c b/repmgr.c index c33dad23..a233b6d8 100644 --- a/repmgr.c +++ b/repmgr.c @@ -571,36 +571,18 @@ do_master_register(void) exit(ERR_BAD_CONFIG); } - /* Check if there is a schema for this cluster */ - sqlquery_snprintf(sqlquery, - "SELECT 1 FROM pg_namespace " - "WHERE nspname = '%s' ", - get_repmgr_schema()); - log_debug(_("master register: %s\n"), sqlquery); - res = PQexec(conn, sqlquery); - if (PQresultStatus(res) != PGRES_TUPLES_OK) + schema_exists = check_cluster_schema(conn); + + /* If schema exists and force option not selected, raise an error */ + if(schema_exists && !runtime_options.force) { - log_err(_("Can't get info about schemas: %s\n"), PQerrorMessage(conn)); - PQclear(res); + log_notice(_("Schema '%s' already exists.\n"), get_repmgr_schema()); PQfinish(conn); exit(ERR_BAD_CONFIG); } - if (PQntuples(res) > 0) /* schema exists */ - { - if (!runtime_options.force) /* and we are not forcing so error */ - { - log_notice(_("Schema %s already exists.\n"), get_repmgr_schema()); - PQclear(res); - PQfinish(conn); - exit(ERR_BAD_CONFIG); - } - schema_exists = true; - } - PQclear(res); - - if (!schema_exists) + if(!schema_exists) { log_info(_("master register: creating database objects inside the %s schema\n"), get_repmgr_schema()); @@ -706,30 +688,13 @@ do_standby_register(void) } /* Check if there is a schema for this cluster */ - sqlquery_snprintf(sqlquery, - "SELECT 1 FROM pg_namespace WHERE nspname = '%s'", - get_repmgr_schema()); - log_debug(_("standby register: %s\n"), sqlquery); - res = PQexec(conn, sqlquery); - if (PQresultStatus(res) != PGRES_TUPLES_OK) - { - /* XXX error message does not match query */ - log_err(_("Can't get info about tablespaces: %s\n"), - PQerrorMessage(conn)); - PQclear(res); - PQfinish(conn); - exit(ERR_BAD_CONFIG); - } - - if (PQntuples(res) == 0) + if (check_cluster_schema(conn) == false) { /* schema doesn't exist */ - log_err(_("Schema %s doesn't exist.\n"), get_repmgr_schema()); - PQclear(res); + log_err(_("Schema '%s' doesn't exist.\n"), get_repmgr_schema()); PQfinish(conn); exit(ERR_BAD_CONFIG); } - PQclear(res); /* check if there is a master in this cluster */ log_info(_("%s connecting to master database\n"), progname); @@ -2075,12 +2040,12 @@ create_schema(PGconn *conn) /* ... the tables */ sqlquery_snprintf(sqlquery, "CREATE TABLE %s.repl_nodes ( " - " id INTEGER PRIMARY KEY, " - " cluster TEXT NOT NULL, " - " name TEXT NOT NULL, " - " conninfo TEXT NOT NULL, " - " priority INTEGER NOT NULL, " - " witness BOOLEAN NOT NULL DEFAULT FALSE) ", + " id INTEGER PRIMARY KEY, " + " cluster TEXT NOT NULL, " + " name TEXT NOT NULL, " + " conninfo TEXT NOT NULL, " + " priority INTEGER NOT NULL, " + " witness BOOLEAN NOT NULL DEFAULT FALSE) ", get_repmgr_schema_quoted(conn)); log_debug(_("master register: %s\n"), sqlquery);