Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename the date column of reports to creation_time #1520

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
- Update default log config [#1501](https://github.com/greenbone/gvmd/pull/1501)
- Change report timestamp filter and iterator columns [#1512](https://github.com/greenbone/gvmd/pull/1512)
- Rename the date column of reports to creation_time [#1520](https://github.com/greenbone/gvmd/pull/1520)

### Fixed
- Improve VT version handling for CVE & OVAL results [#1496](https://github.com/greenbone/gvmd/pull/1496)
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ include (CPack)

## Variables

set (GVMD_DATABASE_VERSION 244)
set (GVMD_DATABASE_VERSION 245)

set (GVMD_SCAP_DATABASE_VERSION 18)

Expand Down
32 changes: 32 additions & 0 deletions src/manage_migrators.c
Original file line number Diff line number Diff line change
Expand Up @@ -2714,6 +2714,37 @@ migrate_243_to_244 ()
return 0;
}

/**
* @brief Migrate the database from version 244 to version 245.
*
* @return 0 success, -1 error.
*/
int
migrate_244_to_245 ()
{
sql_begin_immediate ();

/* Ensure that the database is currently version 244. */

if (manage_db_version () != 244)
{
sql_rollback ();
return -1;
}

/* Update the database. */

sql ("ALTER TABLE reports RENAME COLUMN date TO creation_time;");

/* Set the database version to 245. */

set_db_version (245);

sql_commit ();

return 0;
}


#undef UPDATE_DASHBOARD_SETTINGS

Expand Down Expand Up @@ -2765,6 +2796,7 @@ static migrator_t database_migrators[] = {
{242, migrate_241_to_242},
{243, migrate_242_to_243},
{244, migrate_243_to_244},
{245, migrate_244_to_245},
/* End marker. */
{-1, NULL}};

Expand Down
86 changes: 49 additions & 37 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1114,42 +1114,54 @@ manage_create_sql_functions ()
" END;"
"$$ LANGUAGE SQL;");

sql ("CREATE OR REPLACE FUNCTION task_last_report (integer)"
" RETURNS integer AS $$"
/* Get the report from the most recently completed invocation of task. */
" SELECT id FROM reports WHERE task = $1 AND scan_run_status = %u"
" ORDER BY date DESC LIMIT 1;"
"$$ LANGUAGE SQL;",
TASK_STATUS_DONE);

sql ("CREATE OR REPLACE FUNCTION task_second_last_report (integer)"
" RETURNS integer AS $$"
/* Get report from second most recently completed invocation of task. */
" SELECT id FROM reports WHERE task = $1 AND scan_run_status = %u"
" ORDER BY date DESC LIMIT 1 OFFSET 1;"
"$$ LANGUAGE SQL;",
TASK_STATUS_DONE);

/* result_nvt column (in OVERRIDES_SQL) was added in version 189. */
if (current_db_version >= 189)
sql ("CREATE OR REPLACE FUNCTION task_severity (integer," // task
" integer," // overrides
" integer)" // min_qod
" RETURNS double precision AS $$"
/* Calculate the severity of a task. */
" SELECT CASE"
" WHEN (SELECT target = 0"
" FROM tasks WHERE id = $1)"
" THEN CAST (NULL AS double precision)"
" ELSE"
" (SELECT report_severity ((SELECT id FROM reports"
" WHERE task = $1"
" AND scan_run_status = %u"
" ORDER BY date DESC"
" LIMIT 1 OFFSET 0), $2, $3))"
" END;"
"$$ LANGUAGE SQL;",
TASK_STATUS_DONE);
/* column date in table reports was renamed to creation_time in version 245 */
if (current_db_version >= 245)
{
sql ("CREATE OR REPLACE FUNCTION task_last_report (integer)"
" RETURNS integer AS $$"
/* Get the report from the most recently completed invocation of task. */
" SELECT id FROM reports WHERE task = $1 AND scan_run_status = %u"
" ORDER BY creation_time DESC LIMIT 1;"
"$$ LANGUAGE SQL;",
TASK_STATUS_DONE);
}

/* column date in table reports was renamed to creation_time in version 245 */
if (current_db_version >= 245)
{
sql ("CREATE OR REPLACE FUNCTION task_second_last_report (integer)"
" RETURNS integer AS $$"
/* Get report from second most recently completed invocation of task. */
" SELECT id FROM reports WHERE task = $1 AND scan_run_status = %u"
" ORDER BY creation_time DESC LIMIT 1 OFFSET 1;"
"$$ LANGUAGE SQL;",
TASK_STATUS_DONE);
}

/* result_nvt column (in OVERRIDES_SQL) was added in version 189. */
/* if (current_db_version >= 189) */
/* column date in table reports was renamed to creation_time in version 245 */
if (current_db_version >= 245)
{
sql ("CREATE OR REPLACE FUNCTION task_severity (integer," // task
" integer," // overrides
" integer)" // min_qod
" RETURNS double precision AS $$"
/* Calculate the severity of a task. */
" SELECT CASE"
" WHEN (SELECT target = 0"
" FROM tasks WHERE id = $1)"
" THEN CAST (NULL AS double precision)"
" ELSE"
" (SELECT report_severity ((SELECT id FROM reports"
" WHERE task = $1"
" AND scan_run_status = %u"
" ORDER BY creation_time DESC"
" LIMIT 1 OFFSET 0), $2, $3))"
" END;"
"$$ LANGUAGE SQL;",
TASK_STATUS_DONE);
}

sql ("CREATE OR REPLACE FUNCTION task_trend (integer, integer, integer)"
" RETURNS text AS $$"
Expand Down Expand Up @@ -2302,7 +2314,7 @@ create_tables ()
" uuid text UNIQUE NOT NULL,"
" owner integer REFERENCES users (id) ON DELETE RESTRICT,"
" task integer REFERENCES tasks (id) ON DELETE RESTRICT,"
" date integer,"
" creation_time integer,"
" start_time integer,"
" end_time integer,"
" comment text,"
Expand Down
58 changes: 29 additions & 29 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -2961,7 +2961,7 @@ filter_clause (const char* type, const char* filter,
" FROM (SELECT report_progress (id) AS temp"
" FROM reports"
" WHERE task = tasks.id"
" ORDER BY date DESC LIMIT 1)"
" ORDER BY creation_time DESC LIMIT 1)"
" AS temp_sub)"
" END)"
" ASC");
Expand Down Expand Up @@ -3153,7 +3153,7 @@ filter_clause (const char* type, const char* filter,
" FROM (SELECT report_progress (id) AS temp"
" FROM reports"
" WHERE task = tasks.id"
" ORDER BY date DESC LIMIT 1)"
" ORDER BY creation_time DESC LIMIT 1)"
" AS temp_sub)"
" END)"
" DESC");
Expand Down Expand Up @@ -12050,7 +12050,7 @@ generate_report_filename (report_t report, report_format_t report_format,
report_id = report_uuid (report);

creation_time
= sql_string ("SELECT iso_time (date)"
= sql_string ("SELECT iso_time (creation_time)"
" FROM reports"
" WHERE id = %llu",
report);
Expand Down Expand Up @@ -14573,7 +14573,7 @@ append_to_task_string (task_t task, const char* field, const char* value)
"(SELECT uuid FROM reports WHERE task = tasks.id" \
/* TODO 1 == TASK_STATUS_DONE */ \
" AND scan_run_status = 1" \
" ORDER BY date ASC LIMIT 1)", \
" ORDER BY creation_time ASC LIMIT 1)", \
"first_report", \
KEYWORD_TYPE_STRING \
}, \
Expand All @@ -14582,7 +14582,7 @@ append_to_task_string (task_t task, const char* field, const char* value)
"(SELECT uuid FROM reports WHERE task = tasks.id" \
/* TODO 1 == TASK_STATUS_DONE */ \
" AND scan_run_status = 1" \
" ORDER BY date DESC LIMIT 1)", \
" ORDER BY creation_time DESC LIMIT 1)", \
"last_report", \
KEYWORD_TYPE_STRING \
}, \
Expand Down Expand Up @@ -14635,18 +14635,18 @@ append_to_task_string (task_t task, const char* field, const char* value)
KEYWORD_TYPE_INTEGER \
}, \
{ \
"(SELECT date FROM reports WHERE task = tasks.id" \
"(SELECT creation_time FROM reports WHERE task = tasks.id" \
/* TODO 1 == TASK_STATUS_DONE */ \
" AND scan_run_status = 1" \
" ORDER BY date ASC LIMIT 1)", \
" ORDER BY creation_time ASC LIMIT 1)", \
"first", \
KEYWORD_TYPE_INTEGER \
}, \
{ \
"(SELECT date FROM reports WHERE task = tasks.id" \
"(SELECT creation_time FROM reports WHERE task = tasks.id" \
/* TODO 1 == TASK_STATUS_DONE */ \
" AND scan_run_status = 1" \
" ORDER BY date DESC LIMIT 1)", \
" ORDER BY creation_time DESC LIMIT 1)", \
"last", \
KEYWORD_TYPE_INTEGER \
}, \
Expand Down Expand Up @@ -17838,9 +17838,9 @@ task_report_previous (task_t task, report_t report, report_t *previous)
"SELECT id FROM reports"
" WHERE task = %llu"
" AND scan_run_status = %u"
" AND date < (SELECT date FROM reports"
" WHERE id = %llu)"
" ORDER BY date DESC LIMIT 1;",
" AND creation_time < (SELECT creation_time FROM reports"
" WHERE id = %llu)"
" ORDER BY creation_time DESC LIMIT 1;",
task,
TASK_STATUS_DONE,
report))
Expand Down Expand Up @@ -17874,7 +17874,7 @@ task_last_report (task_t task, report_t *report)
switch (sql_int64 (report,
"SELECT id FROM reports WHERE task = %llu"
" AND scan_run_status = %u"
" ORDER BY date DESC LIMIT 1;",
" ORDER BY creation_time DESC LIMIT 1;",
task,
TASK_STATUS_DONE))
{
Expand Down Expand Up @@ -17906,7 +17906,7 @@ task_last_report_any_status (task_t task, report_t *report)
{
switch (sql_int64 (report,
"SELECT id FROM reports WHERE task = %llu"
" ORDER BY date DESC LIMIT 1;",
" ORDER BY creation_time DESC LIMIT 1;",
task))
{
case 0:
Expand Down Expand Up @@ -17938,7 +17938,7 @@ task_second_last_report (task_t task, report_t *report)
switch (sql_int64 (report,
"SELECT id FROM reports WHERE task = %llu"
" AND scan_run_status = %u"
" ORDER BY date DESC LIMIT 1 OFFSET 1;",
" ORDER BY creation_time DESC LIMIT 1 OFFSET 1;",
task,
TASK_STATUS_DONE))
{
Expand Down Expand Up @@ -17972,7 +17972,7 @@ task_last_resumable_report (task_t task, report_t *report)
"SELECT id FROM reports WHERE task = %llu"
" AND (scan_run_status = %u"
" OR scan_run_status = %u)"
" ORDER BY date DESC LIMIT 1;",
" ORDER BY creation_time DESC LIMIT 1;",
task,
TASK_STATUS_STOPPED,
TASK_STATUS_INTERRUPTED))
Expand Down Expand Up @@ -18004,7 +18004,7 @@ task_second_last_report_id (task_t task)
{
return sql_string ("SELECT uuid FROM reports WHERE task = %llu"
" AND scan_run_status = %u"
" ORDER BY date DESC LIMIT 1 OFFSET 1;",
" ORDER BY creation_time DESC LIMIT 1 OFFSET 1;",
task,
TASK_STATUS_DONE);
}
Expand Down Expand Up @@ -18470,7 +18470,7 @@ task_severity_double (task_t task, int overrides, int min_qod, int offset)
"SELECT id FROM reports"
" WHERE reports.task = %llu"
" AND reports.scan_run_status = %u"
" ORDER BY reports.date DESC"
" ORDER BY reports.creation_time DESC"
" LIMIT 1 OFFSET %d",
task, TASK_STATUS_DONE, offset);

Expand Down Expand Up @@ -20136,7 +20136,7 @@ report_clear_count_cache (report_t report,
report_t
make_report (task_t task, const char* uuid, task_status_t status)
{
sql ("INSERT into reports (uuid, owner, task, date, comment,"
sql ("INSERT into reports (uuid, owner, task, creation_time, comment,"
" scan_run_status, slave_progress)"
" VALUES ('%s',"
" (SELECT owner FROM tasks WHERE tasks.id = %llu),"
Expand Down Expand Up @@ -20952,11 +20952,11 @@ report_add_result (report_t report, result_t result)
* @brief Filter columns for report iterator.
*/
#define REPORT_ITERATOR_FILTER_COLUMNS \
{ ANON_GET_ITERATOR_FILTER_COLUMNS, "task_id", "name", "date", "status", \
"task", "severity", "false_positive", "log", "low", "medium", "high", \
"hosts", "result_hosts", "fp_per_host", "log_per_host", "low_per_host", \
"medium_per_host", "high_per_host", "duration", "duration_per_host", \
"start_time", "end_time", "scan_start", "scan_end", \
{ ANON_GET_ITERATOR_FILTER_COLUMNS, "task_id", "name", "creation_time", \
"date", "status", "task", "severity", "false_positive", "log", "low", \
"medium", "high", "hosts", "result_hosts", "fp_per_host", "log_per_host", \
"low_per_host", "medium_per_host", "high_per_host", "duration", \
"duration_per_host", "start_time", "end_time", "scan_start", "scan_end", \
NULL }

/**
Expand All @@ -20966,11 +20966,11 @@ report_add_result (report_t report, result_t result)
{ \
{ "id", NULL, KEYWORD_TYPE_INTEGER }, \
timopollmeier marked this conversation as resolved.
Show resolved Hide resolved
{ "uuid", NULL, KEYWORD_TYPE_STRING }, \
{ "iso_time (date)", "name", KEYWORD_TYPE_STRING }, \
{ "iso_time (creation_time)", "name", KEYWORD_TYPE_STRING }, \
{ "''", NULL, KEYWORD_TYPE_STRING }, \
{ "iso_time (date)", NULL, KEYWORD_TYPE_STRING }, \
{ "iso_time (creation_time)", NULL, KEYWORD_TYPE_STRING }, \
{ "iso_time (modification_time)", NULL, KEYWORD_TYPE_STRING }, \
{ "date", "created", KEYWORD_TYPE_INTEGER }, \
{ "creation_time", "created", KEYWORD_TYPE_INTEGER }, \
{ "modification_time", "modified", KEYWORD_TYPE_INTEGER }, \
{ "(SELECT name FROM users WHERE users.id = reports.owner)", \
"_owner", \
Expand All @@ -20992,7 +20992,7 @@ report_add_result (report_t report, result_t result)
"task_id", \
KEYWORD_TYPE_STRING \
}, \
{ "date", NULL, KEYWORD_TYPE_INTEGER }, \
{ "creation_time", "date", KEYWORD_TYPE_INTEGER }, \
{ "(SELECT name FROM tasks WHERE tasks.id = task)", "task" }, \
{ \
"report_severity (id, opts.override, opts.min_qod)", \
Expand Down Expand Up @@ -23654,7 +23654,7 @@ int
report_timestamp (const char* report_id, gchar** timestamp)
{
const char* stamp;
time_t time = sql_int ("SELECT date FROM reports where uuid = '%s';",
time_t time = sql_int ("SELECT creation_time FROM reports where uuid = '%s';",
report_id);
stamp = iso_time (&time);
if (stamp == NULL) return -1;
Expand Down