From cd76e180270c9de5dc636b9fa8bab0d61b476a03 Mon Sep 17 00:00:00 2001
From: Jon Bonilla <jbonilla@sipwise.com>
Date: Thu, 30 Aug 2012 10:00:29 +0000
Subject: [PATCH] Try to fix the lcr_rule key issue #1967

The ley that is tried to be created in 9868.up
has no group_id field so it won't be applied in
case you already have two peering groups with
same peering rules. In case you don't it will
apply but the resulting 2.6 version won't allow
you to have different peering groups with same
peering rules.

The solution is what follows: We edit the
existing db scripts to allow upgrades to work
well. This will make existing 2.6 systems and
new 2.6 systems to have different db scehmas.
And the existing ones will be still broken. A new
db rev 10860.up has been created to unify both
kind of systems (the ones which executed the
original revs and the ones which excuted the
edited ones) and it will also fix the existing
versions as it will be executed in existing 2.6
 systems via postinst script.

Why postinst script? We can't trust users to
execute update-db-schema as we don't know all
of them. It's better to handle this automatically.
This postinst addition is being handled by Mika
in another commit.
---
 db_scripts/diff/10180.down | 2 +-
 db_scripts/diff/10180.up   | 5 ++---
 db_scripts/diff/10860.down | 5 +++++
 db_scripts/diff/10860.up   | 5 +++++
 db_scripts/diff/9868.down  | 2 +-
 db_scripts/diff/9868.up    | 4 +++-
 6 files changed, 17 insertions(+), 6 deletions(-)
 create mode 100644 db_scripts/diff/10860.down
 create mode 100644 db_scripts/diff/10860.up

diff --git a/db_scripts/diff/10180.down b/db_scripts/diff/10180.down
index 9c3af535..ef5228fb 100644
--- a/db_scripts/diff/10180.down
+++ b/db_scripts/diff/10180.down
@@ -1,6 +1,6 @@
 use kamailio;
 
 ALTER TABLE lcr_rule DROP KEY `lcr_id_prefix_from_uri_idx`;
-ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`);
+#ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`);
 
 ALTER TABLE lcr_gw DROP `group_id`;
diff --git a/db_scripts/diff/10180.up b/db_scripts/diff/10180.up
index 078eec94..3be5b93e 100644
--- a/db_scripts/diff/10180.up
+++ b/db_scripts/diff/10180.up
@@ -1,6 +1,5 @@
 use kamailio;
 
-ALTER TABLE lcr_rule DROP KEY `lcr_id_prefix_from_uri_idx`;
-ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`, `request_uri`);
+#ALTER TABLE lcr_rule DROP KEY `lcr_id_prefix_from_uri_idx`;
+ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`, `request_uri`, `group_id`);
 
-ALTER TABLE lcr_gw ADD `group_id` int(11) unsigned NOT NULL AFTER defunct;
diff --git a/db_scripts/diff/10860.down b/db_scripts/diff/10860.down
new file mode 100644
index 00000000..d6b5989c
--- /dev/null
+++ b/db_scripts/diff/10860.down
@@ -0,0 +1,5 @@
+use kamailio;
+
+ALTER TABLE lcr_rule DROP KEY `lcr_id_prefix_from_uri_idx`;
+ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`, `request_uri`, `group_id`);
+
diff --git a/db_scripts/diff/10860.up b/db_scripts/diff/10860.up
new file mode 100644
index 00000000..d6b5989c
--- /dev/null
+++ b/db_scripts/diff/10860.up
@@ -0,0 +1,5 @@
+use kamailio;
+
+ALTER TABLE lcr_rule DROP KEY `lcr_id_prefix_from_uri_idx`;
+ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`, `request_uri`, `group_id`);
+
diff --git a/db_scripts/diff/9868.down b/db_scripts/diff/9868.down
index 028884e1..262b0fc7 100644
--- a/db_scripts/diff/9868.down
+++ b/db_scripts/diff/9868.down
@@ -4,5 +4,5 @@ ALTER TABLE dispatcher DROP column `attrs`;
 ALTER TABLE lcr_gw CHANGE column `gw_name` `gw_name` varchar(128) NOT NULL;
 ALTER TABLE lcr_gw ADD column `group_id` int(11) unsigned NOT NULL AFTER defunct;
 ALTER TABLE lcr_rule ADD KEY `lcr_id_idx` (`lcr_id`);
-ALTER TABLE lcr_rule DROP KEY `lcr_id_prefix_from_uri_idx`;
+#ALTER TABLE lcr_rule DROP KEY `lcr_id_prefix_from_uri_idx`;
 
diff --git a/db_scripts/diff/9868.up b/db_scripts/diff/9868.up
index 7c1df4ab..b5de9baf 100644
--- a/db_scripts/diff/9868.up
+++ b/db_scripts/diff/9868.up
@@ -7,5 +7,7 @@ ALTER TABLE lcr_gw CHANGE column `gw_name` `gw_name` varchar(128) DEFAULT NULL;
 ALTER TABLE lcr_gw DROP column `group_id`;
 
 ALTER TABLE lcr_rule DROP KEY lcr_id_idx;
-ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`);
+
+#Edited because it breaks upgrade not being really unique between peering groups
+#ALTER TABLE lcr_rule ADD UNIQUE KEY `lcr_id_prefix_from_uri_idx` (`lcr_id`,`prefix`,`from_uri`);