From: dklawren Date: Thu, 29 Aug 2019 15:05:43 +0000 (-0400) Subject: Bug 1576667 - Add creation timestamp to the user_api_keys table to show when an api... X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ef0cc65ba7386d9ff0a160ec475e334591f99035;p=thirdparty%2Fbugzilla.git Bug 1576667 - Add creation timestamp to the user_api_keys table to show when an api key was first generated (Schema Only) --- diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 4e9f193b0..15ce3c337 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -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)'}, diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 6ff2d4d51..dea994f5c 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -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__