]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1576667 - Add creation timestamp to the user_api_keys table to show when an api...
authordklawren <dklawren@users.noreply.github.com>
Thu, 29 Aug 2019 15:05:43 +0000 (11:05 -0400)
committerGitHub <noreply@github.com>
Thu, 29 Aug 2019 15:05:43 +0000 (11:05 -0400)
Bugzilla/DB/Schema.pm
Bugzilla/Install/DB.pm

index 4e9f193b00f9150fd4af67d567557e2bde7cfb6f..15ce3c33708371f23c7e8df95394465b25366bc1 100644 (file)
@@ -1788,6 +1788,7 @@ use constant ABSTRACT_SCHEMA => {
       api_key      => {TYPE => 'varchar(40)', NOTNULL => 1},
       description  => {TYPE => 'varchar(255)'},
       revoked      => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'},
+      creation_ts  => {TYPE => 'DATETIME', NOTNULL => 1},
       last_used    => {TYPE => 'DATETIME'},
       last_used_ip => {TYPE => 'varchar(40)'},
       app_id       => {TYPE => 'varchar(64)'},
index 6ff2d4d51cdae6ce9479aa0775f2aab363d60559..dea994f5cac4e770e9b0a8f018b63b98fd73ee1b 100644 (file)
@@ -802,6 +802,9 @@ sub update_table_definitions {
   $dbh->bz_add_column('bugs', 'filed_via',
     {TYPE => 'varchar(40)', NOTNULL => 1, DEFAULT => "'unknown'"});
 
+  # Bug 1576667 - dkl@mozilla.com
+  _populate_api_keys_creation_ts();
+
   ################################################################
   # New --TABLE-- changes should go *** A B O V E *** this point #
   ################################################################
@@ -4307,6 +4310,22 @@ sub _add_oauth2_jwt_support {
     {TYPE => 'INT4', NOTNULL => 1});
 }
 
+sub _populate_api_keys_creation_ts {
+  my $dbh = Bugzilla->dbh;
+
+  # Return if we have already made these changes
+  return if $dbh->bz_column_info('user_api_keys', 'creation_ts');
+
+  $dbh->bz_add_column('user_api_keys', 'creation_ts', {TYPE => 'DATETIME'});
+
+  # We do not have a way to tell when an API key was originally created
+  # so we use the last_used timestamp as the initial creation value.
+  $dbh->do('UPDATE user_api_keys SET creation_ts = COALESCE(last_used, LOCALTIMESTAMP(0))');
+
+  $dbh->bz_alter_column('user_api_keys', 'creation_ts',
+    {TYPE => 'DATETIME', NOTNULL => 1});
+}
+
 1;
 
 __END__