From b6bd97eea2ddeba2b0ea2380da29fba98b8a3208 Mon Sep 17 00:00:00 2001
From: Mark Michelson <mmichelson@digium.com>
Date: Mon, 20 Jun 2016 13:21:52 -0500
Subject: [PATCH] Fix Alembic upgrades.

A non-existent constraint was being referenced in the upgrade script.
This patch corrects the problem by removing the reference.

In addition, the head of the alembic branch referred to a non-existent
revision. This has been fixed by referring to the proper revision.

This patch fixes another realtime problem as well. Our Alembic scripts
store booleans as yes or no values. However, Sorcery tries to insert
"true" or "false" instead. This patch introduces a new boolean type that
translates to "yes" or "no" instead.

ASTERISK-26128 #close

Change-Id: I51574736a881189de695a824883a18d66a52dcef
---
 ...184_res_pjsip_add_contact_via_addr_and_.py |  2 +-
 ...b01a191a46_pjsip_add_contact_reg_server.py |  2 --
 include/asterisk/config_options.h             | 23 +++++++++++++++++++
 main/config_options.c                         |  5 ++++
 main/sorcery.c                                |  7 ++++++
 res/res_pjsip/location.c                      |  2 +-
 6 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/contrib/ast-db-manage/config/versions/6d8c104e6184_res_pjsip_add_contact_via_addr_and_.py b/contrib/ast-db-manage/config/versions/6d8c104e6184_res_pjsip_add_contact_via_addr_and_.py
index 893d9d82e7..77242ac4b3 100644
--- a/contrib/ast-db-manage/config/versions/6d8c104e6184_res_pjsip_add_contact_via_addr_and_.py
+++ b/contrib/ast-db-manage/config/versions/6d8c104e6184_res_pjsip_add_contact_via_addr_and_.py
@@ -8,7 +8,7 @@ Create Date: 2016-05-19 15:51:33.410852
 
 # revision identifiers, used by Alembic.
 revision = 'a845e4d8ade8'
-down_revision = 'bca7113d796f'
+down_revision = 'd7e3c73eb2bf'
 
 from alembic import op
 import sqlalchemy as sa
diff --git a/contrib/ast-db-manage/config/versions/81b01a191a46_pjsip_add_contact_reg_server.py b/contrib/ast-db-manage/config/versions/81b01a191a46_pjsip_add_contact_reg_server.py
index 0318b9a4fc..0919370ba6 100644
--- a/contrib/ast-db-manage/config/versions/81b01a191a46_pjsip_add_contact_reg_server.py
+++ b/contrib/ast-db-manage/config/versions/81b01a191a46_pjsip_add_contact_reg_server.py
@@ -16,10 +16,8 @@ import sqlalchemy as sa
 
 def upgrade():
     op.add_column('ps_contacts', sa.Column('reg_server', sa.String(20)))
-    op.drop_constraint('id', 'ps_contacts', type_='unique')
     op.create_unique_constraint('ps_contacts_uq', 'ps_contacts', ['id','reg_server'])
 
 def downgrade():
     op.drop_constraint('ps_contacts_uq', 'ps_contacts', type_='unique')
     op.drop_column('ps_contacts', 'reg_server')
-    op.create_unique_constraint(None, 'ps_contacts', 'id')
diff --git a/include/asterisk/config_options.h b/include/asterisk/config_options.h
index 30c0421763..f2a457eb57 100644
--- a/include/asterisk/config_options.h
+++ b/include/asterisk/config_options.h
@@ -445,6 +445,29 @@ enum aco_option_type {
 	 * {endcode}
 	 */
 	OPT_UINT_T,
+
+	/*! \brief Type for default option handler for bools (ast_true/ast_false)
+	 * \note aco_option_register flags:
+	 *   non-zero : process via ast_true
+	 *   0        : process via ast_false
+	 * aco_option_register varargs:
+	 *   FLDSET macro with the field of type int. It is important to note that the field
+	 *   cannot be a bitfield. If bitfields are required, they must be set via a custom handler.
+	 *
+	 * This is exactly the same as OPT_BOOL_T. The only difference is that when
+	 * translated to a string, OPT_BOOL_T becomes "true" or "false"; OPT_YESNO_T becomes
+	 * "yes" or "no".
+	 *
+	 * Example:
+	 * {code}
+	 * struct test_item {
+	 *     int enabled;
+	 * };
+	 * aco_option_register(&cfg_info, "enabled", ACO_EXACT, my_types, "no", OPT_YESNO_T, 1, FLDSET(struct test_item, enabled));
+	 * {endcode}
+	 */
+	OPT_YESNO_T,
+
 };
 
 /*! \brief A callback function for handling a particular option
diff --git a/main/config_options.c b/main/config_options.c
index c58dfdd868..2d2300893d 100644
--- a/main/config_options.c
+++ b/main/config_options.c
@@ -97,6 +97,7 @@ static char *aco_option_type_string[] = {
 	"IP Address",		/* OPT_SOCKADDR_T, */
 	"String",			/* OPT_STRINGFIELD_T, */
 	"Unsigned Integer",	/* OPT_UINT_T, */
+	"Boolean",			/* OPT_YESNO_T, */
 };
 
 void *aco_pending_config(struct aco_info *info)
@@ -139,6 +140,10 @@ static aco_option_handler ast_config_option_default_handler(enum aco_option_type
 	switch(type) {
 	case OPT_ACL_T: return acl_handler_fn;
 	case OPT_BOOL_T: return bool_handler_fn;
+	/* Reading from config files, BOOL and YESNO are handled exactly the
+	 * same. Their difference is in how they are rendered to users
+	 */
+	case OPT_YESNO_T: return bool_handler_fn;
 	case OPT_BOOLFLAG_T: return boolflag_handler_fn;
 	case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
 	case OPT_CODEC_T: return codec_handler_fn;
diff --git a/main/sorcery.c b/main/sorcery.c
index 6a7b54bbf3..a739f5eb8c 100644
--- a/main/sorcery.c
+++ b/main/sorcery.c
@@ -293,6 +293,12 @@ static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
 	return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
 }
 
+static int yesno_handler_fn(const void *obj, const intptr_t *args, char **buf)
+{
+	unsigned int *field = (unsigned int *)(obj + args[0]);
+	return !(*buf = ast_strdup(*field ? "yes" : "no")) ? -1 : 0;
+}
+
 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
 {
 	struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
@@ -316,6 +322,7 @@ static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type
 {
 	switch(type) {
 	case OPT_BOOL_T: return bool_handler_fn;
+	case OPT_YESNO_T: return yesno_handler_fn;
 	case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
 	case OPT_CODEC_T: return codec_handler_fn;
 	case OPT_DOUBLE_T: return double_handler_fn;
diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c
index 8f8c030c35..4f8f56fca0 100644
--- a/res/res_pjsip/location.c
+++ b/res/res_pjsip/location.c
@@ -1133,7 +1133,7 @@ int ast_sip_initialize_sorcery_location(void)
 	ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T,
 		PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400);
 	ast_sorcery_object_field_register(sorcery, "contact", "qualify_timeout", "3.0", OPT_DOUBLE_T, 0, FLDSET(struct ast_sip_contact, qualify_timeout));
-	ast_sorcery_object_field_register(sorcery, "contact", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_contact, authenticate_qualify));
+	ast_sorcery_object_field_register(sorcery, "contact", "authenticate_qualify", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_contact, authenticate_qualify));
 	ast_sorcery_object_field_register(sorcery, "contact", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, outbound_proxy));
 	ast_sorcery_object_field_register(sorcery, "contact", "user_agent", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, user_agent));
 	ast_sorcery_object_field_register(sorcery, "contact", "reg_server", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, reg_server));
-- 
GitLab