mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-27 17:06:29 +00:00
Consolidate duplicated code
This commit is contained in:
32
dbutils.c
32
dbutils.c
@@ -977,6 +977,7 @@ create_node_record(PGconn *conn, char *action, int node, char *type, int upstrea
|
|||||||
{
|
{
|
||||||
log_warning(_("Cannot insert node details, %s\n"),
|
log_warning(_("Cannot insert node details, %s\n"),
|
||||||
PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
|
PQclear(res);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -984,3 +985,34 @@ create_node_record(PGconn *conn, char *action, int node, char *type, int upstrea
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
delete_node_record(PGconn *conn, int node, char *action)
|
||||||
|
{
|
||||||
|
char sqlquery[QUERY_STR_LEN];
|
||||||
|
PGresult *res;
|
||||||
|
|
||||||
|
sqlquery_snprintf(sqlquery,
|
||||||
|
"DELETE FROM %s.repl_nodes "
|
||||||
|
" WHERE id = %d",
|
||||||
|
get_repmgr_schema_quoted(conn),
|
||||||
|
node);
|
||||||
|
if(action != NULL)
|
||||||
|
{
|
||||||
|
log_debug(_("%s: %s\n"), action, sqlquery);
|
||||||
|
}
|
||||||
|
|
||||||
|
res = PQexec(conn, sqlquery);
|
||||||
|
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||||
|
{
|
||||||
|
log_warning(_("Cannot delete node details, %s\n"),
|
||||||
|
PQerrorMessage(conn));
|
||||||
|
PQclear(res);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
PQclear(res);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,5 +60,8 @@ bool stop_backup(PGconn *conn, char *last_wal_segment);
|
|||||||
bool set_config_bool(PGconn *conn, const char *config_param, bool state);
|
bool set_config_bool(PGconn *conn, const char *config_param, bool state);
|
||||||
bool copy_configuration(PGconn *masterconn, PGconn *witnessconn, char *cluster_name);
|
bool copy_configuration(PGconn *masterconn, PGconn *witnessconn, char *cluster_name);
|
||||||
bool create_node_record(PGconn *conn, char *action, int node, char *type, int upstream_node, char *cluster_name, char *node_name, char *conninfo, int priority, char *slot_name);
|
bool create_node_record(PGconn *conn, char *action, int node, char *type, int upstream_node, char *cluster_name, char *node_name, char *conninfo, int priority, char *slot_name);
|
||||||
|
bool delete_node_record(PGconn *conn, int node, char *action);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
57
repmgr.c
57
repmgr.c
@@ -670,8 +670,6 @@ static void
|
|||||||
do_master_register(void)
|
do_master_register(void)
|
||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
PGresult *res;
|
|
||||||
char sqlquery[QUERY_STR_LEN];
|
|
||||||
|
|
||||||
bool schema_exists = false;
|
bool schema_exists = false;
|
||||||
int ret;
|
int ret;
|
||||||
@@ -721,26 +719,6 @@ do_master_register(void)
|
|||||||
{
|
{
|
||||||
PGconn *master_conn;
|
PGconn *master_conn;
|
||||||
|
|
||||||
if (runtime_options.force)
|
|
||||||
{
|
|
||||||
sqlquery_snprintf(sqlquery,
|
|
||||||
"DELETE FROM %s.repl_nodes "
|
|
||||||
" WHERE id = %d ",
|
|
||||||
get_repmgr_schema_quoted(conn),
|
|
||||||
options.node);
|
|
||||||
log_debug(_("master register: %s\n"), sqlquery);
|
|
||||||
|
|
||||||
res = PQexec(conn, sqlquery);
|
|
||||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
||||||
{
|
|
||||||
log_warning(_("Cannot delete node details, %s\n"),
|
|
||||||
PQerrorMessage(conn));
|
|
||||||
PQfinish(conn);
|
|
||||||
exit(ERR_BAD_CONFIG);
|
|
||||||
}
|
|
||||||
PQclear(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Ensure there isn't any other master already registered */
|
/* Ensure there isn't any other master already registered */
|
||||||
master_conn = get_master_connection(conn,
|
master_conn = get_master_connection(conn,
|
||||||
options.cluster_name, NULL, NULL);
|
options.cluster_name, NULL, NULL);
|
||||||
@@ -751,6 +729,20 @@ do_master_register(void)
|
|||||||
options.cluster_name);
|
options.cluster_name);
|
||||||
exit(ERR_BAD_CONFIG);
|
exit(ERR_BAD_CONFIG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (runtime_options.force)
|
||||||
|
{
|
||||||
|
bool node_record_deleted = delete_node_record(conn,
|
||||||
|
options.node,
|
||||||
|
"master register");
|
||||||
|
|
||||||
|
if (node_record_deleted == false)
|
||||||
|
{
|
||||||
|
PQfinish(master_conn);
|
||||||
|
PQfinish(conn);
|
||||||
|
exit(ERR_BAD_CONFIG);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Now register the master */
|
/* Now register the master */
|
||||||
@@ -783,9 +775,6 @@ do_standby_register(void)
|
|||||||
PGconn *master_conn;
|
PGconn *master_conn;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
PGresult *res;
|
|
||||||
char sqlquery[QUERY_STR_LEN];
|
|
||||||
|
|
||||||
char master_version[MAXVERSIONSTR];
|
char master_version[MAXVERSIONSTR];
|
||||||
int master_version_num = 0;
|
int master_version_num = 0;
|
||||||
|
|
||||||
@@ -794,8 +783,6 @@ do_standby_register(void)
|
|||||||
|
|
||||||
bool node_record_created;
|
bool node_record_created;
|
||||||
|
|
||||||
/* XXX: A lot of copied code from do_master_register! Refactor */
|
|
||||||
|
|
||||||
log_info(_("%s connecting to standby database\n"), progname);
|
log_info(_("%s connecting to standby database\n"), progname);
|
||||||
conn = establish_db_connection(options.conninfo, true);
|
conn = establish_db_connection(options.conninfo, true);
|
||||||
|
|
||||||
@@ -856,24 +843,16 @@ do_standby_register(void)
|
|||||||
log_info(_("%s registering the standby\n"), progname);
|
log_info(_("%s registering the standby\n"), progname);
|
||||||
if (runtime_options.force)
|
if (runtime_options.force)
|
||||||
{
|
{
|
||||||
sqlquery_snprintf(sqlquery,
|
bool node_record_deleted = delete_node_record(master_conn,
|
||||||
"DELETE FROM %s.repl_nodes "
|
options.node,
|
||||||
" WHERE id = %d",
|
"standby register");
|
||||||
get_repmgr_schema_quoted(master_conn),
|
|
||||||
options.node);
|
|
||||||
|
|
||||||
log_debug(_("standby register: %s\n"), sqlquery);
|
if (node_record_deleted == false)
|
||||||
|
|
||||||
res = PQexec(master_conn, sqlquery);
|
|
||||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
||||||
{
|
{
|
||||||
log_err(_("Cannot delete node details, %s\n"),
|
|
||||||
PQerrorMessage(master_conn));
|
|
||||||
PQfinish(master_conn);
|
PQfinish(master_conn);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
exit(ERR_BAD_CONFIG);
|
exit(ERR_BAD_CONFIG);
|
||||||
}
|
}
|
||||||
PQclear(res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
node_record_created = create_node_record(master_conn,
|
node_record_created = create_node_record(master_conn,
|
||||||
|
|||||||
Reference in New Issue
Block a user