From 296525a65b7e6baa12f1941baf93b1b326a2405b Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Sat, 14 Apr 2018 14:50:23 -0300
Subject: [PATCH] Bug 20568: (QA follow-up) Get rid of the id column

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 Koha/Schema/Result/ApiKey.pm                  | 44 +++++--------------
 .../atomicupdate/bug_20568_api_keys.perl      |  6 +--
 installer/data/mysql/kohastructure.sql        |  8 ++--
 members/apikeys.pl                            |  6 +--
 opac/opac-apikeys.pl                          |  6 +--
 5 files changed, 23 insertions(+), 47 deletions(-)

diff --git a/Koha/Schema/Result/ApiKey.pm b/Koha/Schema/Result/ApiKey.pm
index 3ff057007e..f8e091fbb7 100644
--- a/Koha/Schema/Result/ApiKey.pm
+++ b/Koha/Schema/Result/ApiKey.pm
@@ -23,18 +23,6 @@ __PACKAGE__->table("api_keys");
 
 =head1 ACCESSORS
 
-=head2 id
-
-  data_type: 'integer'
-  is_auto_increment: 1
-  is_nullable: 0
-
-=head2 patron_id
-
-  data_type: 'integer'
-  is_foreign_key: 1
-  is_nullable: 0
-
 =head2 client_id
 
   data_type: 'varchar'
@@ -53,6 +41,12 @@ __PACKAGE__->table("api_keys");
   is_nullable: 0
   size: 255
 
+=head2 patron_id
+
+  data_type: 'integer'
+  is_foreign_key: 1
+  is_nullable: 0
+
 =head2 active
 
   data_type: 'tinyint'
@@ -62,16 +56,14 @@ __PACKAGE__->table("api_keys");
 =cut
 
 __PACKAGE__->add_columns(
-  "id",
-  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
-  "patron_id",
-  { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
   "client_id",
   { data_type => "varchar", is_nullable => 0, size => 191 },
   "secret",
   { data_type => "varchar", is_nullable => 0, size => 191 },
   "description",
   { data_type => "varchar", is_nullable => 0, size => 255 },
+  "patron_id",
+  { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
   "active",
   { data_type => "tinyint", default_value => 1, is_nullable => 0 },
 );
@@ -80,28 +72,16 @@ __PACKAGE__->add_columns(
 
 =over 4
 
-=item * L</id>
+=item * L</client_id>
 
 =back
 
 =cut
 
-__PACKAGE__->set_primary_key("id");
+__PACKAGE__->set_primary_key("client_id");
 
 =head1 UNIQUE CONSTRAINTS
 
-=head2 C<client_id>
-
-=over 4
-
-=item * L</client_id>
-
-=back
-
-=cut
-
-__PACKAGE__->add_unique_constraint("client_id", ["client_id"]);
-
 =head2 C<secret>
 
 =over 4
@@ -132,8 +112,8 @@ __PACKAGE__->belongs_to(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-04-14 00:56:23
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:b7AUAgl2SClXJ2lzPVV0FA
+# Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-04-14 14:48:10
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:qnu4QSACpOSQaZgd52ozmw
 
 __PACKAGE__->add_columns(
     '+active' => { is_boolean => 1 }
diff --git a/installer/data/mysql/atomicupdate/bug_20568_api_keys.perl b/installer/data/mysql/atomicupdate/bug_20568_api_keys.perl
index 8cc0bf1f05..21e1884702 100644
--- a/installer/data/mysql/atomicupdate/bug_20568_api_keys.perl
+++ b/installer/data/mysql/atomicupdate/bug_20568_api_keys.perl
@@ -4,14 +4,12 @@ if(CheckVersion($DBversion)) {
     if (!TableExists('api_keys')) {
         $dbh->do(q{
             CREATE TABLE `api_keys` (
-                `id`          INT(11) NOT NULL AUTO_INCREMENT,
-                `patron_id`   INT(11) NOT NULL,
                 `client_id`   VARCHAR(191) NOT NULL,
                 `secret`      VARCHAR(191) NOT NULL,
                 `description` VARCHAR(255) NOT NULL,
+                `patron_id`   INT(11) NOT NULL,
                 `active`      TINYINT(1) DEFAULT 1 NOT NULL,
-                PRIMARY KEY (`id`),
-                UNIQUE KEY `client_id` (`client_id`),
+                PRIMARY KEY `client_id` (`client_id`),
                 UNIQUE KEY `secret` (`secret`),
                 KEY `patron_id` (`patron_id`),
                 CONSTRAINT `api_keys_fk_patron_id`
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index b2d0c29986..5bde575b5b 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -1719,16 +1719,14 @@ CREATE TABLE borrower_sync (
 
 DROP TABLE IF EXISTS `api_keys`;
 CREATE TABLE `api_keys` (
-    `id`          INT(11) NOT NULL AUTO_INCREMENT, -- API key internal identifier
-    `patron_id`   INT(11) NOT NULL,                -- Foreign key to the borrowers table
     `client_id`   VARCHAR(191) NOT NULL,           -- API client ID
     `secret`      VARCHAR(191) NOT NULL,           -- API client secret used for API authentication
     `description` VARCHAR(255) NOT NULL,           -- API client description
+    `patron_id`   INT(11) NOT NULL,                -- Foreign key to the borrowers table
     `active`      TINYINT(1) DEFAULT 1 NOT NULL,   -- 0 means this API key is revoked
-    PRIMARY KEY (`id`),
-    KEY `patron_id` (`patron_id`),
-    UNIQUE KEY `client_id` (`client_id`),
+    PRIMARY KEY `client_id` (`client_id`),
     UNIQUE KEY `secret` (`secret`),
+    KEY `patron_id` (`patron_id`),
     CONSTRAINT `api_keys_fk_patron_id`
       FOREIGN KEY (`patron_id`)
       REFERENCES `borrowers` (`borrowernumber`)
diff --git a/members/apikeys.pl b/members/apikeys.pl
index 975ca86e5b..fda98ed984 100755
--- a/members/apikeys.pl
+++ b/members/apikeys.pl
@@ -68,7 +68,7 @@ if ($op) {
 
     if ( $op eq 'delete' ) {
         my $api_key_id = $cgi->param('key');
-        my $key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $api_key_id });
+        my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
         if ($key) {
             $key->delete;
         }
@@ -78,7 +78,7 @@ if ($op) {
 
     if ( $op eq 'revoke' ) {
         my $api_key_id = $cgi->param('key');
-        my $key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $api_key_id });
+        my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
         if ($key) {
             $key->active(0);
             $key->store;
@@ -89,7 +89,7 @@ if ($op) {
 
     if ( $op eq 'activate' ) {
         my $api_key_id = $cgi->param('key');
-        my $key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $api_key_id });
+        my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
         if ($key) {
             $key->active(1);
             $key->store;
diff --git a/opac/opac-apikeys.pl b/opac/opac-apikeys.pl
index 66167e8d38..a571c16ffd 100755
--- a/opac/opac-apikeys.pl
+++ b/opac/opac-apikeys.pl
@@ -63,7 +63,7 @@ if ($op) {
 
     if ($op eq 'delete') {
         my $key_id  = $cgi->param('key');
-        my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $key_id });
+        my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $key_id });
         if ($api_key) {
             $api_key->delete;
         }
@@ -73,7 +73,7 @@ if ($op) {
 
     if ($op eq 'revoke') {
         my $key_id  = $cgi->param('key');
-        my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $key_id });
+        my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $key_id });
         if ($api_key) {
             $api_key->active(0);
             $api_key->store;
@@ -84,7 +84,7 @@ if ($op) {
 
     if ($op eq 'activate') {
         my $key_id  = $cgi->param('key');
-        my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $key_id });
+        my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $key_id });
         if ($api_key) {
             $api_key->active(1);
             $api_key->store;
-- 
2.17.0