diff --git a/Anvil/Tools/Account.pm b/Anvil/Tools/Account.pm index 575af275..8dc564c3 100755 --- a/Anvil/Tools/Account.pm +++ b/Anvil/Tools/Account.pm @@ -428,7 +428,7 @@ UPDATE sessions SET session_salt = '', - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE session_user_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($user_uuid)." "; if ($host_uuid ne "all") diff --git a/Anvil/Tools/Alert.pm b/Anvil/Tools/Alert.pm index 6968c1ad..03e3c5df 100755 --- a/Anvil/Tools/Alert.pm +++ b/Anvil/Tools/Alert.pm @@ -83,7 +83,7 @@ If there is a problem, C<< !!error!! >> is returned. Parameters; -=head3 change_date (optional) +=head3 modified_date (optional) By default, this is set to C<< sys::database::timestamp >>. If you want to force a different timestamp, you can do so with this parameter. @@ -115,13 +115,13 @@ sub check_alert_sent my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $anvil = $self->parent; - my $change_date = defined $parameter->{change_date} ? $parameter->{change_date} : $anvil->data->{sys}{database}{timestamp}; + my $modified_date = defined $parameter->{modified_date} ? $parameter->{modified_date} : $anvil->data->{sys}{database}{timestamp}; my $name = defined $parameter->{name} ? $parameter->{name} : ""; my $record_locator = defined $parameter->{record_locator} ? $parameter->{record_locator} : ""; my $set_by = defined $parameter->{set_by} ? $parameter->{set_by} : ""; my $type = defined $parameter->{type} ? $parameter->{type} : ""; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - change_date => $change_date, + modified_date => $modified_date, name => $name, record_locator => $record_locator, set_by => $set_by, @@ -129,7 +129,7 @@ sub check_alert_sent }}); # Do we have a timestamp? - if (not $change_date) + if (not $modified_date) { # Nope $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0093"}); @@ -224,7 +224,7 @@ WHERE set_by => $set_by, record_locator => $record_locator, name => $name, - change_date => $change_date, + modified_date => $modified_date, }}); return("!!error!!"); } @@ -245,7 +245,7 @@ INSERT INTO alert_set_by, alert_record_locator, alert_name, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->Get->uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{host_uuid}).", @@ -423,7 +423,7 @@ INSERT INTO alert_message_variables, alert_sort, alert_header, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->Get->uuid()).", ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{host_uuid}).", diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index ffd98169..d33d9c43 100755 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -9,7 +9,6 @@ use DBI; use Scalar::Util qw(weaken isweak); use Data::Dumper; use Time::HiRes qw(gettimeofday tv_interval); -use SQL::Parser; our $VERSION = "3.0.0"; my $THIS_FILE = "Database.pm"; @@ -619,7 +618,7 @@ This is the SQL schema file that will be used to initialize the database, if the =head3 tables (optional) -This is an optional array reference of of tables to specifically check when connecting to databases. Each entry is treated as a table name, and that table's most recent C<< change_date >> time stamp will be read. If a column name in the table ends in C<< _host_uuid >>, then the check and resync will be restricted to entries in that column matching the current host's C<< sys::host_uuid >>. If the table does not have a corresponding table in the C<< history >> schema, then only the public table will be synced. +This is an optional array reference of of tables to specifically check when connecting to databases. Each entry is treated as a table name, and that table's most recent C<< modified_date >> time stamp will be read. If a column name in the table ends in C<< _host_uuid >>, then the check and resync will be restricted to entries in that column matching the current host's C<< sys::host_uuid >>. If the table does not have a corresponding table in the C<< history >> schema, then only the public table will be synced. Note; The array order is used to allow you to ensure tables with primary keys are synchronyzed before tables with foreign keys. As such, please be aware of the order the table hash references are put into the array reference. @@ -1059,7 +1058,7 @@ sub connect set_by => $THIS_FILE, record_locator => $uuid, name => "connect_to_db", - change_date => $anvil->data->{sys}{database}{timestamp}, + modified_date => $anvil->data->{sys}{database}{timestamp}, }); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { set => $set }}); @@ -1120,7 +1119,7 @@ sub connect set_by => $THIS_FILE, record_locator => $uuid, name => "connect_to_db", - change_date => $anvil->data->{sys}{database}{timestamp}, + modified_date => $anvil->data->{sys}{database}{timestamp}, }); if ($cleared) { @@ -1236,7 +1235,7 @@ Each anonymous hash is structured as: host_uuid => $host_uuid, host_name => $host_name, host_type => $host_type, - change_date => $change_date, + modified_date => $modified_date, It also sets the variables C<< sys::hosts::by_uuid:: = >> and C<< sys::hosts::by_name:: = >> per host read, for quick reference. @@ -1254,7 +1253,7 @@ SELECT host_uuid, host_name, host_type, - change_date + modified_date FROM hosts ;"; @@ -1272,18 +1271,18 @@ FROM my $host_uuid = $row->[0]; my $host_name = $row->[1]; my $host_type = $row->[2]; - my $change_date = $row->[3]; + my $modified_date = $row->[3]; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_uuid => $host_uuid, host_name => $host_name, host_type => $host_type, - change_date => $change_date, + modified_date => $modified_date, }}); push @{$return}, { host_uuid => $host_uuid, host_name => $host_name, host_type => $host_type, - change_date => $change_date, + modified_date => $modified_date, }; # Record the host_uuid in a hash so that the name can be easily retrieved. @@ -1339,7 +1338,7 @@ SELECT job_title, job_description, job_status, - change_date + modified_date FROM jobs WHERE @@ -1366,7 +1365,7 @@ WHERE my $job_title = $row->[8]; my $job_description = $row->[9]; my $job_status = $row->[10]; - my $change_date = $row->[11]; + my $modified_date = $row->[11]; my $started_seconds_ago = $job_picked_up_at ? (time - $job_picked_up_at) : 0; my $updated_seconds_ago = $job_updated ? (time - $job_updated) : 0; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { @@ -1381,7 +1380,7 @@ WHERE job_title => $job_title, job_description => $job_description, job_status => $job_status, - change_date => $change_date, + modified_date => $modified_date, started_seconds_ago => $started_seconds_ago, updated_seconds_ago => $updated_seconds_ago, }}); @@ -1405,7 +1404,7 @@ WHERE job_title => $job_title, job_description => $job_description, job_status => $job_status, - change_date => $change_date, + modified_date => $modified_date, }; } @@ -1724,7 +1723,7 @@ INSERT INTO bridge_name, bridge_id, bridge_stp_enabled, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($bridge_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($bridge_host_uuid).", @@ -1793,7 +1792,7 @@ SET bridge_name = ".$anvil->data->{sys}{database}{use_handle}->quote($bridge_name).", bridge_id = ".$anvil->data->{sys}{database}{use_handle}->quote($bridge_id).", bridge_stp_enabled = ".$anvil->data->{sys}{database}{use_handle}->quote($bridge_stp_enabled).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE bridge_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($bridge_uuid)." "; @@ -2008,7 +2007,7 @@ INSERT INTO bond_down_delay, bond_mac_address, bond_operational, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($bond_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($bond_host_uuid).", @@ -2125,7 +2124,7 @@ SET bond_down_delay = ".$anvil->data->{sys}{database}{use_handle}->quote($bond_down_delay).", bond_mac_address = ".$anvil->data->{sys}{database}{use_handle}->quote($bond_mac_address).", bond_operational = ".$anvil->data->{sys}{database}{use_handle}->quote($bond_operational).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE bond_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($bond_uuid)." "; @@ -2246,7 +2245,7 @@ INSERT INTO host_uuid, host_name, host_type, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($host_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($host_name).", @@ -2266,7 +2265,7 @@ UPDATE SET host_name = ".$anvil->data->{sys}{database}{use_handle}->quote($host_name).", host_type = ".$anvil->data->{sys}{database}{use_handle}->quote($host_type).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE host_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($host_uuid)." ;"; @@ -2466,7 +2465,7 @@ INSERT INTO ip_address_gateway, ip_address_default_gateway, ip_address_dns, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($ip_address_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($ip_address_host_uuid).", @@ -2559,7 +2558,7 @@ SET ip_address_gateway = ".$anvil->data->{sys}{database}{use_handle}->quote($ip_address_gateway).", ip_address_default_gateway = ".$anvil->data->{sys}{database}{use_handle}->quote($ip_address_default_gateway).", ip_address_dns = ".$anvil->data->{sys}{database}{use_handle}->quote($ip_address_dns).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE ip_address_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($ip_address_uuid)." "; @@ -2832,7 +2831,7 @@ INSERT INTO job_title, job_description, job_status, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($job_host_uuid).", @@ -2925,7 +2924,7 @@ UPDATE jobs SET job_progress = ".$anvil->data->{sys}{database}{use_handle}->quote($job_progress).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid)." "; @@ -2937,7 +2936,7 @@ UPDATE SET job_progress = ".$anvil->data->{sys}{database}{use_handle}->quote($job_progress).", job_status = ".$anvil->data->{sys}{database}{use_handle}->quote($job_status).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid)." "; @@ -2976,7 +2975,7 @@ SET job_title = ".$anvil->data->{sys}{database}{use_handle}->quote($job_title).", job_description = ".$anvil->data->{sys}{database}{use_handle}->quote($job_description).", job_status = ".$anvil->data->{sys}{database}{use_handle}->quote($job_status).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid)." "; @@ -3238,7 +3237,7 @@ SET network_interface_medium = ".$anvil->data->{sys}{database}{use_handle}->quote($network_interface_medium).", network_interface_mtu = ".$anvil->data->{sys}{database}{use_handle}->quote($network_interface_mtu).", network_interface_speed = ".$anvil->data->{sys}{database}{use_handle}->quote($network_interface_speed).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE network_interface_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($network_interface_uuid)." ;"; @@ -3270,7 +3269,7 @@ INSERT INTO network_interface_medium, network_interface_mtu, network_interface_speed, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($network_interface_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($network_interface_bond_uuid).", @@ -3445,7 +3444,7 @@ SET session_user_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($session_user_uuid).", session_salt = ".$anvil->data->{sys}{database}{use_handle}->quote($session_salt).", session_user_agent = ".$anvil->data->{sys}{database}{use_handle}->quote($session_user_agent).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE session_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($session_uuid)." "; @@ -3468,7 +3467,7 @@ INSERT INTO session_user_uuid, session_salt, session_user_agent, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($session_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($session_host_uuid).", @@ -3625,7 +3624,7 @@ INSERT INTO state_name, state_host_uuid, state_note, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($state_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($state_name).", @@ -3688,7 +3687,7 @@ SET state_name = ".$anvil->data->{sys}{database}{use_handle}->quote($state_name).", state_host_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($state_host_uuid).", state_note = ".$anvil->data->{sys}{database}{use_handle}->quote($state_note).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE state_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($state_uuid)." "; @@ -3919,7 +3918,7 @@ INSERT INTO user_is_admin, user_is_experienced, user_is_trusted, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($user_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($user_name).", @@ -4019,7 +4018,7 @@ SET user_is_admin = ".$anvil->data->{sys}{database}{use_handle}->quote($user_is_admin).", user_is_experienced = ".$anvil->data->{sys}{database}{use_handle}->quote($user_is_experienced).", user_is_trusted = ".$anvil->data->{sys}{database}{use_handle}->quote($user_is_trusted).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE user_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($user_uuid)." "; @@ -4209,7 +4208,7 @@ INSERT INTO variable_section, variable_source_uuid, variable_source_table, - change_date + modified_date ) VALUES ( ".$anvil->data->{sys}{database}{use_handle}->quote($variable_uuid).", ".$anvil->data->{sys}{database}{use_handle}->quote($variable_name).", @@ -4280,7 +4279,7 @@ UPDATE variables SET variable_value = ".$anvil->data->{sys}{database}{use_handle}->quote($variable_value).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE variable_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($variable_uuid); if (($variable_source_uuid ne "") && ($variable_source_table ne "")) @@ -4360,7 +4359,7 @@ SET variable_default = ".$anvil->data->{sys}{database}{use_handle}->quote($variable_default).", variable_description = ".$anvil->data->{sys}{database}{use_handle}->quote($variable_description).", variable_section = ".$anvil->data->{sys}{database}{use_handle}->quote($variable_section).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE variable_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($variable_uuid)." "; @@ -4490,11 +4489,11 @@ sub locking # If I have been asked to check, we will return the variable_uuid if a lock is set. if ($check) { - my ($lock_value, $variable_uuid, $change_date) = $anvil->Database->read_variable({variable_name => $variable_name}); + my ($lock_value, $variable_uuid, $modified_date) = $anvil->Database->read_variable({variable_name => $variable_name}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { lock_value => $lock_value, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); return($lock_value); @@ -4505,11 +4504,11 @@ sub locking { # We check to see if there is a lock before we clear it. This way we don't log that we # released a lock unless we really released a lock. - my ($lock_value, $variable_uuid, $change_date) = $anvil->Database->read_variable({variable_name => $variable_name}); + my ($lock_value, $variable_uuid, $modified_date) = $anvil->Database->read_variable({variable_name => $variable_name}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { lock_value => $lock_value, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); if ($lock_value) @@ -4571,12 +4570,12 @@ sub locking $waiting = 0; # See if we had a lock. - my ($lock_value, $variable_uuid, $change_date) = $anvil->Database->read_variable({variable_name => $variable_name}); + my ($lock_value, $variable_uuid, $modified_date) = $anvil->Database->read_variable({variable_name => $variable_name}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { waiting => $waiting, lock_value => $lock_value, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); if ($lock_value =~ /^(.*?)::(.*?)::(\d+)/) { @@ -4893,7 +4892,7 @@ sub read_variable SELECT variable_value, variable_uuid, - round(extract(epoch from change_date)) AS mtime + round(extract(epoch from modified_date)) AS mtime FROM variables WHERE "; @@ -4920,7 +4919,7 @@ AND $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0124", variables => { query => $query }}); my $variable_value = ""; - my $change_date = ""; + my $modified_date = ""; my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $THIS_FILE, line => __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { @@ -4931,20 +4930,20 @@ AND { $variable_value = $row->[0]; $variable_uuid = $row->[1]; - $change_date = $row->[2]; + $modified_date = $row->[2]; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { variable_value => $variable_value, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); } $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { variable_value => $variable_value, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); - return($variable_value, $variable_uuid, $change_date); + return($variable_value, $variable_uuid, $modified_date); } =head2 resync_databases @@ -4967,9 +4966,6 @@ sub resync_databases return(0); } - ### TODO: Multiple updates to a column with the same change_date don't resync properly. Use - ### history_id? It has to become a UUID, as histpry_id will conflict across DBs given it simply - ### increments per-DB currently ### NOTE: Don't sort this array, we need to resync in the order that the user passed the tables to us ### to avoid trouble with primary/foreign keys. # We're going to use the array of tables assembles by _find_behind_databases() stored in @@ -5054,21 +5050,15 @@ sub resync_databases $anvil->data->{db_resync}{$uuid}{public}{sql} = []; $anvil->data->{db_resync}{$uuid}{history}{sql} = []; - # Read in the data, change_date first as we'll need that for all entries we record. + # Read in the data, modified_date first as we'll need that for all entries we record. + my $query = "SELECT modified_date AT time zone 'UTC', $uuid_column, "; my $read_columns = []; - my $query = "SELECT change_date AT time zone 'UTC', $uuid_column, "; - push @{$read_columns}, "change_date"; - if ($schema eq "history") - { - $query = "SELECT change_date AT time zone 'UTC', history_id, $uuid_column, "; - push @{$read_columns}, "history_id"; - } + push @{$read_columns}, "modified_date"; push @{$read_columns}, $uuid_column; foreach my $column_name (sort {$a cmp $b} keys %{$anvil->data->{sys}{database}{table}{$table}{column}}) { # We'll skip the host column as we'll use it in the conditional. - next if $column_name eq "change_date"; - next if $column_name eq "history_id"; + next if $column_name eq "modified_date"; next if $column_name eq $host_column; next if $column_name eq $uuid_column; $query .= $column_name.", "; @@ -5085,11 +5075,7 @@ sub resync_databases { $query .= " WHERE ".$host_column." = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{host_uuid}); } - $query .= " ORDER BY change_date DESC;"; - if ($schema eq "history") - { - $query .= " ORDER BY change_date DESC, history_id ASC;"; - } + $query .= " ORDER BY modified_date DESC;"; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0074", variables => { uuid => $uuid, query => $query }}); my $results = $anvil->Database->query({uuid => $uuid, query => $query, source => $THIS_FILE, line => __LINE__}); @@ -5104,7 +5090,7 @@ sub resync_databases foreach my $row (@{$results}) { $row_number++; - my $change_date = ""; + my $modified_date = ""; my $row_uuid = ""; for (my $column_number = 0; $column_number < @{$read_columns}; $column_number++) { @@ -5112,7 +5098,7 @@ sub resync_databases my $column_value = defined $row->[$column_number] ? $row->[$column_number] : "NULL"; my $not_null = $anvil->data->{sys}{database}{table}{$table}{column}{$column_name}{not_null}; my $data_type = $anvil->data->{sys}{database}{table}{$table}{column}{$column_name}{data_type}; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + $anvil->Log->variables({source => 2, line => __LINE__, level => $debug, list => { "s1:id" => $uuid, "s2:row_number" => $row_number, "s3:column_number" => $column_number, @@ -5127,11 +5113,11 @@ sub resync_databases $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { column_value => $column_value }}); } - # The change_date should be the first row. - if ($column_name eq "change_date") + # The modified_date should be the first row. + if ($column_name eq "modified_date") { - $change_date = $column_value; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { change_date => $change_date }}); + $modified_date = $column_value; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { modified_date => $modified_date }}); next; } @@ -5153,26 +5139,26 @@ sub resync_databases } # TODO: Remove these or make them proper errors - die $THIS_FILE." ".__LINE__."; This row's change_date wasn't the first column returned in query: [$query]\n" if not $change_date; + die $THIS_FILE." ".__LINE__."; This row's modified_date wasn't the first column returned in query: [$query]\n" if not $modified_date; die $THIS_FILE." ".__LINE__."; This row's UUID column: [$uuid_column] wasn't the second column returned in query: [$query]\n" if not $row_uuid; - # Record this in the unified and db hashes. - $anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}{$column_name} = $column_value; - $anvil->data->{db_data}{$uuid}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}{$column_name} = $column_value; + # Record this in the unified and local hashes. # This table isn't restricted to given hosts. + $anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}{$column_name} = $column_value; + $anvil->data->{db_data}{$uuid}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}{$column_name} = $column_value; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - "db_data::unified::${table}::change_date::${change_date}::${uuid_column}::${row_uuid}::${column_name}" => $anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}{$column_name}, - "db_data::${uuid}::${table}::change_date::${change_date}::${uuid_column}::${row_uuid}::${column_name}" => $anvil->data->{db_data}{$uuid}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}{$column_name}, + "db_data::unified::${table}::modified_date::${modified_date}::${uuid_column}::${row_uuid}::${column_name}" => $anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}{$column_name}, + "db_data::${uuid}::${table}::modified_date::${modified_date}::${uuid_column}::${row_uuid}::${column_name}" => $anvil->data->{db_data}{$uuid}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}{$column_name}, }}); } } } # Now all the data is read in, we can see what might be missing from each DB. - foreach my $change_date (sort {$b cmp $a} keys %{$anvil->data->{db_data}{unified}{$table}{change_date}}) + foreach my $modified_date (sort {$b cmp $a} keys %{$anvil->data->{db_data}{unified}{$table}{modified_date}}) { - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { change_date => $change_date }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { modified_date => $modified_date }}); - foreach my $row_uuid (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}}) + foreach my $row_uuid (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}}) { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { row_uuid => $row_uuid }}); @@ -5207,15 +5193,15 @@ sub resync_databases { # It exists, but does it exist at this time stamp? $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - "db_data::${uuid}::${table}::change_date::${change_date}::${uuid_column}::${row_uuid}" => $anvil->data->{db_data}{$uuid}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}, + "db_data::${uuid}::${table}::modified_date::${modified_date}::${uuid_column}::${row_uuid}" => $anvil->data->{db_data}{$uuid}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}, }}); - if (not $anvil->data->{db_data}{$uuid}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}) + if (not $anvil->data->{db_data}{$uuid}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}) { # No, so UPDATE it. We'll build the query now... my $query = "UPDATE public.$table SET "; - foreach my $column_name (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}}) + foreach my $column_name (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}}) { - my $column_value = $anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}{$column_name}); + my $column_value = $anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}{$column_name}); $column_value = "NULL" if not defined $column_value; $column_value =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { @@ -5225,7 +5211,7 @@ sub resync_databases $query .= "$column_name = ".$column_value.", "; } - $query .= "change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($change_date)."::timestamp AT TIME ZONE 'UTC' WHERE $uuid_column = ".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).";"; + $query .= "modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($modified_date)."::timestamp AT TIME ZONE 'UTC' WHERE $uuid_column = ".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).";"; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0074", variables => { uuid => $uuid, query => $query }}); # Now record the query in the array @@ -5240,9 +5226,9 @@ sub resync_databases # they're in the same order. my $columns = ""; my $values = ""; - foreach my $column_name (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}}) + foreach my $column_name (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}}) { - my $column_value = $anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}{$column_name}); + my $column_value = $anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}{$column_name}); $column_value =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { column_name => $column_name, @@ -5256,11 +5242,11 @@ sub resync_databases 'values' => $values, }}); - my $query = "INSERT INTO public.$table (".$uuid_column.", ".$columns."change_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($change_date)."::timestamp AT TIME ZONE 'UTC');"; + my $query = "INSERT INTO public.$table (".$uuid_column.", ".$columns."modified_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($modified_date)."::timestamp AT TIME ZONE 'UTC');"; if ($host_column) { # Add the host column. - $query = "INSERT INTO public.$table ($host_column, $uuid_column, ".$columns."change_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{host_uuid}).", ".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($change_date)."::timestamp AT TIME ZONE 'UTC');"; + $query = "INSERT INTO public.$table ($host_column, $uuid_column, ".$columns."modified_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{host_uuid}).", ".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($modified_date)."::timestamp AT TIME ZONE 'UTC');"; } $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0074", variables => { uuid => $uuid, query => $query }}); @@ -5278,9 +5264,9 @@ sub resync_databases # question of whether the entry for the current # timestamp exists in the history schema. $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - "db_data::${uuid}::${table}::change_date::${change_date}::${uuid_column}::${row_uuid}" => $anvil->data->{db_data}{$uuid}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}, + "db_data::${uuid}::${table}::modified_date::${modified_date}::${uuid_column}::${row_uuid}" => $anvil->data->{db_data}{$uuid}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}, }}); - if (not $anvil->data->{db_data}{$uuid}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}) + if (not $anvil->data->{db_data}{$uuid}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}) { # It hasn't been seen, so INSERT it. We need # to build entries for the column names and @@ -5288,9 +5274,9 @@ sub resync_databases # they're in the same order. my $columns = ""; my $values = ""; - foreach my $column_name (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}}) + foreach my $column_name (sort {$a cmp $b} keys %{$anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}}) { - my $column_value = $anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{db_data}{unified}{$table}{change_date}{$change_date}{$uuid_column}{$row_uuid}{$column_name}); + my $column_value = $anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{db_data}{unified}{$table}{modified_date}{$modified_date}{$uuid_column}{$row_uuid}{$column_name}); $column_value =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { column_name => $column_name, @@ -5304,11 +5290,11 @@ sub resync_databases 'values' => $values, }}); - my $query = "INSERT INTO history.$table (".$uuid_column.", ".$columns."change_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($change_date)."::timestamp AT TIME ZONE 'UTC');"; + my $query = "INSERT INTO history.$table (".$uuid_column.", ".$columns."modified_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($modified_date)."::timestamp AT TIME ZONE 'UTC');"; if ($host_column) { # Add the host column. - $query = "INSERT INTO history.$table ($host_column, $uuid_column, ".$columns."change_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{host_uuid}).", ".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($change_date)."::timestamp AT TIME ZONE 'UTC');"; + $query = "INSERT INTO history.$table ($host_column, $uuid_column, ".$columns."modified_date) VALUES (".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{host_uuid}).", ".$anvil->data->{sys}{database}{use_handle}->quote($row_uuid).", ".$values.$anvil->data->{sys}{database}{use_handle}->quote($modified_date)."::timestamp AT TIME ZONE 'UTC');"; } $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0074", variables => { uuid => $uuid, query => $query }}); @@ -5318,7 +5304,7 @@ sub resync_databases } # if seen } # foreach $id } # foreach $row_uuid - } # foreach $change_date ... + } # foreach $modified_date ... # Free up memory by deleting the DB data from the main hash. delete $anvil->data->{db_data}; @@ -5462,7 +5448,6 @@ sub write $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { i => $i, 'next' => $next }}); foreach my $this_query (@{$query}) { - $anvil->Database->_split_query({query => $query}); push @{$query_set}, $this_query; $i++; @@ -5518,9 +5503,11 @@ sub write uuid => $uuid, count => $count, }}); - - # More than one query, so start a transaction block. - $anvil->data->{cache}{database_handle}{$uuid}->begin_work; + if ($count) + { + # More than one query, so start a transaction block. + $anvil->data->{cache}{database_handle}{$uuid}->begin_work; + } foreach my $query (@{$query_set}) { @@ -5546,8 +5533,12 @@ sub write }}); } - # Commit the changes. - $anvil->data->{cache}{database_handle}{$uuid}->commit(); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { count => $count }}); + if ($count) + { + # Commit the changes. + $anvil->data->{cache}{database_handle}{$uuid}->commit(); + } } $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { count => $count }}); @@ -5649,7 +5640,7 @@ sub _archive_table # There is enough data to trigger an archive, so lets get started with a list of columns in this # table. - $query = "SELECT column_name FROM information_schema.columns WHERE table_schema = 'history' AND table_name = ".$anvil->data->{sys}{database}{use_handle}->quote($table)." AND column_name != 'history_id' AND column_name != 'change_date';"; + $query = "SELECT column_name FROM information_schema.columns WHERE table_schema = 'history' AND table_name = ".$anvil->data->{sys}{database}{use_handle}->quote($table)." AND column_name != 'history_id' AND column_name != 'modified_date';"; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0124", variables => { query => $query }}); my $columns = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); @@ -5713,7 +5704,7 @@ sub _find_behind_databases } # Now, look through the core tables, plus any tables the user might have passed, for differing - # 'change_date' entries, or no entries in one DB with entries in the other (as can happen with a + # 'modified_date' entries, or no entries in one DB with entries in the other (as can happen with a # newly setup db). $anvil->data->{sys}{database}{check_tables} = []; @@ -5725,7 +5716,7 @@ sub _find_behind_databases # resync methods. push @{$anvil->data->{sys}{database}{check_tables}}, $table; - # Preset all tables to have an initial 'change_date' and 'row_count' of 0. + # Preset all tables to have an initial 'modified_date' and 'row_count' of 0. $anvil->data->{sys}{database}{table}{$table}{last_updated} = 0; $anvil->data->{sys}{database}{table}{$table}{row_count} = 0; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { @@ -5782,7 +5773,7 @@ sub _find_behind_databases my $schema = $count ? "history" : "public"; $query = " SELECT - round(extract(epoch from change_date)) + round(extract(epoch from modified_date)) FROM ".$schema.".".$table." "; if ($host_column) @@ -5793,7 +5784,7 @@ WHERE } $query .= " ORDER BY - change_date DESC + modified_date DESC ;"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { uuid => $uuid, @@ -5811,7 +5802,7 @@ ORDER BY my $last_updated = $results->[0]->[0]; $last_updated = 0 if not defined $last_updated; - # Record this table's last change_date for later comparison. We'll also + # Record this table's last modified_date for later comparison. We'll also # record the schema and host column, if found, to save looking the same thing # up later if we do need a resync. $anvil->data->{sys}{database}{table}{$table}{id}{$uuid}{last_updated} = $last_updated; @@ -5880,6 +5871,7 @@ ORDER BY } last if $anvil->data->{sys}{database}{resync_needed}; } + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::database::resync_needed" => $anvil->data->{sys}{database}{resync_needed}, }}); @@ -5938,161 +5930,6 @@ sub _mark_database_as_behind return(0); } -=head2 _split_query - -This method takes a query and examines it to see if a copy is needed for the history schema. - -It will return two variables; The original query and, if applicable, the query needed to write to the history schema. A UUID will be generated as needed for the 'change_uuid' columns. - -Parameters; - -=head3 query (required) - -This is the query to process. - -=cut -sub _split_query -{ - my $self = shift; - my $parameter = shift; - my $anvil = $self->parent; - my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->_test_access()" }}); - - my $query = $parameter->{query} ? $parameter->{query} : ""; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - - # Flatten the query to make it easier to parse - my $public_query = ""; - my $history_query = ""; - - # Find out if we're doing an INSERT or UPDATE, which schema we're writing to, and if we have or need - # to inject the change_uuid. - my $parser = SQL::Parser->new(); - my $schema = "public"; - my $table = ""; - my $command = ""; - my $change_date = ""; - my $change_uuid = ""; - my $success = $parser->parse($query); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { success => $success }}); - if ($success) - { - #print Dumper $parser->structure; - $table = $parser->structure->{org_table_names}->[0]; - $command = $parser->structure->{command}; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - table => $table, - command => $command, - }}); - if ($table =~ /^(.*?)\.(.*)$/) - { - $table = $1; - $schema = $2; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - table => $table, - schema => $schema, - }}); - } - - for (my $i = 0; $i < @{$parser->structure->{column_defs}}; $i++) - { - my $column_name = $parser->structure->{column_defs}->[$i]->{value}; - my $column_value = $parser->structure->{'values'}->[0]->[$i]->{value}; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - column_name => $column_name, - column_value => $column_value, - }}); - if ($column_name eq "change_date") - { - $change_date = $column_value; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { change_date => $change_date }}); - } - elsif ($column_name eq "change_uuid") - { - $change_uuid = $column_value; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { change_uuid => $change_uuid }}); - } - } - } - else - { - print "Failed to parse: [".$query."]\n"; - } - - # Inject the $change_uuid, if needed. - my $new_query = ""; - if ($change_uuid) - { - # Just strait copy the query to new_query - $new_query = $query; - } - else - { - my $column_end_seen = 0; - $change_uuid = $anvil->Get->uuid({debug => $debug}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { change_uuid => $change_uuid }}); - - foreach my $line (split/\n/, $query) - { - if ((not $column_end_seen) && ($command eq "INSERT") && ($line =~ /\)/)) - { - $column_end_seen = 1; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ">> line" => $line }}); - $line =~ s/\)/, change_uuid = '$change_uuid')/; - $line =~ s/ ,/,/gs; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "<< line" => $line }}); - } - elsif ((not $column_end_seen) && ($command eq "UPDATE") && ($line =~ /WHERE/i)) - { - $column_end_seen = 1; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ">> line" => $line }}); - $line =~ s/WHERE/, change_uuid = '$change_uuid' WHERE/gs; - $line =~ s/ ,/,/gs; - $line =~ s/ WHERE $/ \nWHERE /; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "<< line" => $line }}); - } - $new_query .= $line."\n"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_query => $new_query }}); - } - $new_query =~ s/\n$//gs; - - if ($new_query =~ /\n, change_uuid/gs) - { - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { ">> new_query" => $new_query }}); - $new_query =~ s/\n, change_uuid/, \n change_uuid/gs; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "<< new_query" => $new_query }}); - } - - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { new_query => $new_query }}); - } - - if ($schema eq "history") - { - # Nothing more to do. - $history_query = $new_query; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { history_query => $history_query }}); - } - else - { - # Create a copy and prepend 'history.' to the table name - $public_query = $new_query; - $history_query = $new_query; - $history_query =~ s/ $table/ history.$table/gs; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - public_query => $public_query, - history_query => $history_query, - }}); - } - - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - query => $query, - public_query => $public_query, - history_query => $history_query, - }}); - return($public_query, $history_query); -} - =head2 _test_access This method takes a database UUID and tests the connection to it using the DBD 'ping' method. If it fails, open references to the database are removed or replaced, then an attempt to reconnect is made. diff --git a/Anvil/Tools/Job.pm b/Anvil/Tools/Job.pm index 47e5ae67..a65369c1 100755 --- a/Anvil/Tools/Job.pm +++ b/Anvil/Tools/Job.pm @@ -110,7 +110,7 @@ UPDATE jobs SET job_picked_up_by = '0', - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid)." "; @@ -367,7 +367,7 @@ SET job_updated = ".$anvil->data->{sys}{database}{use_handle}->quote(time).", job_progress = ".$anvil->data->{sys}{database}{use_handle}->quote($progress).", job_status = ".$anvil->data->{sys}{database}{use_handle}->quote($job_status).", - change_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." + modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid)." "; diff --git a/Anvil/Tools/System.pm b/Anvil/Tools/System.pm index b455ffbf..053a23d5 100755 --- a/Anvil/Tools/System.pm +++ b/Anvil/Tools/System.pm @@ -477,7 +477,7 @@ sub check_if_configured my $anvil = $self->parent; my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; - my ($configured, $variable_uuid, $change_date) = $anvil->Database->read_variable({ + my ($configured, $variable_uuid, $modified_date) = $anvil->Database->read_variable({ variable_name => "system::configured", variable_source_uuid => $anvil->Get->host_uuid, variable_source_table => "hosts", @@ -485,7 +485,7 @@ sub check_if_configured $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { configured => $configured, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { configured => $configured }}); @@ -1062,7 +1062,7 @@ sub maintenance_mode } } - my ($maintenance_mode, $variable_uuid, $change_date) = $anvil->Database->read_variable({ + my ($maintenance_mode, $variable_uuid, $modified_date) = $anvil->Database->read_variable({ debug => $debug, variable_name => "maintenance_mode", variable_source_table => "hosts", @@ -1072,7 +1072,7 @@ sub maintenance_mode debug => $debug, maintenance_mode => $maintenance_mode, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); if ($maintenance_mode eq "") @@ -1885,7 +1885,7 @@ sub reboot_needed } } - my ($reboot_needed, $variable_uuid, $change_date) = $anvil->Database->read_variable({ + my ($reboot_needed, $variable_uuid, $modified_date) = $anvil->Database->read_variable({ debug => $debug, variable_name => "reboot::needed", variable_source_table => "hosts", @@ -1894,7 +1894,7 @@ sub reboot_needed $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { reboot_needed => $reboot_needed, variable_uuid => $variable_uuid, - change_date => $change_date, + modified_date => $modified_date, }}); if ($reboot_needed eq "") diff --git a/cgi-bin/striker b/cgi-bin/striker index 9dffaebf..2fdc243c 100755 --- a/cgi-bin/striker +++ b/cgi-bin/striker @@ -985,8 +985,8 @@ sub check_availability my $query = " SELECT job_progress, - change_date, - extract(epoch from change_date) + modified_date, + extract(epoch from modified_date) FROM jobs WHERE diff --git a/rpm/SPECS/anvil.spec b/rpm/SPECS/anvil.spec index d9c8dc17..021544ef 100644 --- a/rpm/SPECS/anvil.spec +++ b/rpm/SPECS/anvil.spec @@ -43,7 +43,6 @@ Requires: perl-Log-Journald Requires: perl-Net-SSH2 Requires: perl-NetAddr-IP Requires: perl-Proc-Simple -Requires: perl-SQL-Statement Requires: perl-Sys-Syslog Requires: perl-Time-HiRes Requires: perl-UUID-Tiny @@ -396,7 +395,7 @@ firewall-cmd --add-service=postgresql --permanent %changelog * Madison Kelly 3.0-17 -- Added 'perl-UUID-Tiny' and 'perl-SQL-Statement' to core dependencies. +- Added 'perl-UUID-Tiny' to core dependencies. * Fri Sep 14 2018 Madison Kelly 3.0-16 - Added htop as a -core dependency. diff --git a/share/anvil.sql b/share/anvil.sql index e7a446f0..298f0c32 100644 --- a/share/anvil.sql +++ b/share/anvil.sql @@ -1,6 +1,6 @@ -- This is the core database schema for the Anvil! Intelligent Availability platform. -- --- It expects PostgreSQL v. 10.x+ +-- It expects PostgreSQL v. 9.1+ -- -- Table construction rules; -- @@ -9,7 +9,7 @@ -- plural, the '_uuid' and can use the singular form of the table. For example, the table 'hosts' can -- use 'host_uuid'. -- --- All tables must hast a 'change_date timestamp with time zone not null' column. This is used to track +-- All tables must hast a 'modified_date timestamp with time zone not null' column. This is used to track -- changes through time in the history schema and used to groups changes when resync'ing. -- -- Tables can optionally have a '*_host_uuid uuid not null' colum. If this is found, when resync'ing the @@ -44,46 +44,45 @@ $$; CREATE TABLE hosts ( host_uuid uuid not null primary key, -- This is the single most important record in Anvil!. Everything links back to here. host_name text not null, - host_type text not null, -- Either 'node', 'dashboard' or 'dr'. - change_date timestamp with time zone not null, - change_uuid uuid not null + host_type text not null, -- Either 'node' or 'dashboard'. + modified_date timestamp with time zone not null ); ALTER TABLE hosts OWNER TO #!variable!user!#; CREATE TABLE history.hosts ( + history_id bigserial, host_uuid uuid not null, host_name text not null, host_type text not null, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.hosts OWNER TO #!variable!user!#; --- CREATE FUNCTION history_hosts() RETURNS trigger --- AS $$ --- DECLARE --- history_hosts RECORD; --- BEGIN --- SELECT INTO history_hosts * FROM hosts WHERE host_uuid = new.host_uuid; --- INSERT INTO history.hosts --- (host_uuid, --- host_name, --- host_type, --- change_date) --- VALUES --- (history_hosts.host_uuid, --- history_hosts.host_name, --- history_hosts.host_type, --- history_hosts.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_hosts() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_hosts --- AFTER INSERT OR UPDATE ON hosts --- FOR EACH ROW EXECUTE PROCEDURE history_hosts(); +CREATE FUNCTION history_hosts() RETURNS trigger +AS $$ +DECLARE + history_hosts RECORD; +BEGIN + SELECT INTO history_hosts * FROM hosts WHERE host_uuid = new.host_uuid; + INSERT INTO history.hosts + (host_uuid, + host_name, + host_type, + modified_date) + VALUES + (history_hosts.host_uuid, + history_hosts.host_name, + history_hosts.host_type, + history_hosts.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_hosts() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_hosts + AFTER INSERT OR UPDATE ON hosts + FOR EACH ROW EXECUTE PROCEDURE history_hosts(); -- This stores information about users. @@ -99,12 +98,12 @@ CREATE TABLE users ( user_is_admin integer not null default 0, -- If 1, all aspects of the program are available to the user. user_is_experienced integer not null default 0, -- If 1, user is allowed to delete a server, alter disk size, alter hardware and do other potentially risky things. They will also get fewer confirmation dialogues. user_is_trusted integer not null default 0, -- If 1, user is allowed to do things that would cause interruptions, like force-reset and gracefully stop servers, withdraw nodes, and stop the Anvil! entirely. - change_date timestamp with time zone not null, - change_uuid uuid not null, + modified_date timestamp with time zone not null ); ALTER TABLE users OWNER TO #!variable!user!#; CREATE TABLE history.users ( + history_id bigserial, user_uuid uuid, user_name text, user_password_hash text, @@ -115,50 +114,49 @@ CREATE TABLE history.users ( user_is_admin integer, user_is_experienced integer, user_is_trusted integer, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.users OWNER TO #!variable!user!#; --- CREATE FUNCTION history_users() RETURNS trigger --- AS $$ --- DECLARE --- history_users RECORD; --- BEGIN --- SELECT INTO history_users * FROM users WHERE user_uuid = new.user_uuid; --- INSERT INTO history.users --- (user_uuid, --- user_name, --- user_password_hash, --- user_salt, --- user_algorithm, --- user_hash_count, --- user_language, --- user_is_admin, --- user_is_experienced, --- user_is_trusted, --- change_date) --- VALUES --- (history_users.user_uuid, --- history_users.user_name, --- history_users.user_password_hash, --- history_users.user_salt, --- history_users.user_algorithm, --- history_users.user_hash_count, --- history_users.user_language, --- history_users.user_is_admin, --- history_users.user_is_experienced, --- history_users.user_is_trusted, --- history_users.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_users() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_users --- AFTER INSERT OR UPDATE ON users --- FOR EACH ROW EXECUTE PROCEDURE history_users(); +CREATE FUNCTION history_users() RETURNS trigger +AS $$ +DECLARE + history_users RECORD; +BEGIN + SELECT INTO history_users * FROM users WHERE user_uuid = new.user_uuid; + INSERT INTO history.users + (user_uuid, + user_name, + user_password_hash, + user_salt, + user_algorithm, + user_hash_count, + user_language, + user_is_admin, + user_is_experienced, + user_is_trusted, + modified_date) + VALUES + (history_users.user_uuid, + history_users.user_name, + history_users.user_password_hash, + history_users.user_salt, + history_users.user_algorithm, + history_users.user_hash_count, + history_users.user_language, + history_users.user_is_admin, + history_users.user_is_experienced, + history_users.user_is_trusted, + history_users.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_users() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_users + AFTER INSERT OR UPDATE ON users + FOR EACH ROW EXECUTE PROCEDURE history_users(); -- This stores special variables for a given host that programs may want to record. @@ -167,48 +165,47 @@ CREATE TABLE host_variable ( host_variable_host_uuid uuid not null, host_variable_name text not null, host_variable_value text not null, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE host_variable OWNER TO #!variable!user!#; CREATE TABLE history.host_variable ( + history_id bigserial, host_variable_uuid uuid, host_variable_host_uuid uuid, host_variable_name text, host_variable_value text, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.host_variable OWNER TO #!variable!user!#; --- CREATE FUNCTION history_host_variable() RETURNS trigger --- AS $$ --- DECLARE --- history_host_variable RECORD; --- BEGIN --- SELECT INTO history_host_variable * FROM host_variable WHERE host_uuid = new.host_uuid; --- INSERT INTO history.host_variable --- (host_variable_uuid, --- host_variable_host_uuid, --- host_variable_name, --- host_variable_value, --- change_date) --- VALUES --- (history_host_variable.host_variable_uuid, --- history_host_variable.host_variable_host_uuid, --- history_host_variable.host_variable_name, --- history_host_variable.host_variable_value, --- history_host_variable.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_host_variable() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_host_variable --- AFTER INSERT OR UPDATE ON host_variable --- FOR EACH ROW EXECUTE PROCEDURE history_host_variable(); +CREATE FUNCTION history_host_variable() RETURNS trigger +AS $$ +DECLARE + history_host_variable RECORD; +BEGIN + SELECT INTO history_host_variable * FROM host_variable WHERE host_uuid = new.host_uuid; + INSERT INTO history.host_variable + (host_variable_uuid, + host_variable_host_uuid, + host_variable_name, + host_variable_value, + modified_date) + VALUES + (history_host_variable.host_variable_uuid, + history_host_variable.host_variable_host_uuid, + history_host_variable.host_variable_name, + history_host_variable.host_variable_value, + history_host_variable.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_host_variable() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_host_variable + AFTER INSERT OR UPDATE ON host_variable + FOR EACH ROW EXECUTE PROCEDURE history_host_variable(); -- This stores user session information on a per-dashboard basis. @@ -216,10 +213,9 @@ CREATE TABLE sessions ( session_uuid uuid not null primary key, -- This is the single most important record in Anvil!. Everything links back to here. session_host_uuid uuid not null, -- This is the host uuid for this session. session_user_uuid uuid not null, -- This is the user uuid for the user logging in. - session_salt text not null, -- This is used when generating a session hash for a session when they log in. + session_salt text not null, -- This is used when generating a session hash for a session when they log in. session_user_agent text, - change_date timestamp with time zone not null, - change_uuid uuid not null, + modified_date timestamp with time zone not null, FOREIGN KEY(session_host_uuid) REFERENCES hosts(host_uuid), FOREIGN KEY(session_user_uuid) REFERENCES users(user_uuid) @@ -227,45 +223,45 @@ CREATE TABLE sessions ( ALTER TABLE sessions OWNER TO #!variable!user!#; CREATE TABLE history.sessions ( + history_id bigserial, session_uuid uuid, session_host_uuid uuid, session_user_uuid uuid, - session_salt text, + session_salt text, session_user_agent text, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.sessions OWNER TO #!variable!user!#; --- CREATE FUNCTION history_sessions() RETURNS trigger --- AS $$ --- DECLARE --- history_sessions RECORD; --- BEGIN --- SELECT INTO history_sessions * FROM sessions WHERE session_uuid = new.session_uuid; --- INSERT INTO history.sessions --- (session_uuid, --- session_host_uuid, --- session_user_uuid, --- session_salt, --- session_user_agent, --- change_date) --- VALUES --- (history_sessions.session_uuid, --- history_sessions.session_host_uuid, --- history_sessions.session_user_uuid, --- history_sessions.session_salt, --- history_sessions.session_user_agent, --- history_sessions.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_sessions() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_sessions --- AFTER INSERT OR UPDATE ON sessions --- FOR EACH ROW EXECUTE PROCEDURE history_sessions(); +CREATE FUNCTION history_sessions() RETURNS trigger +AS $$ +DECLARE + history_sessions RECORD; +BEGIN + SELECT INTO history_sessions * FROM sessions WHERE session_uuid = new.session_uuid; + INSERT INTO history.sessions + (session_uuid, + session_host_uuid, + session_user_uuid, + session_salt, + session_user_agent, + modified_date) + VALUES + (history_sessions.session_uuid, + history_sessions.session_host_uuid, + history_sessions.session_user_uuid, + history_sessions.session_salt, + history_sessions.session_user_agent, + history_sessions.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_sessions() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_sessions + AFTER INSERT OR UPDATE ON sessions + FOR EACH ROW EXECUTE PROCEDURE history_sessions(); -- This stores alerts coming in from various sources @@ -280,14 +276,14 @@ CREATE TABLE alerts ( alert_message_variables text not null, -- List of variables to substitute into the message key. Format is 'var1=val1 #!# var2 #!# val2 #!# ... #!# varN=valN'. alert_sort text not null, -- The alerts will sort on this column. It allows for an optional sorting of the messages in the alert. alert_header integer not null default 1, -- This can be set to have the alert be printed with only the contents of the string, no headers. - change_date timestamp with time zone not null, - change_uuid uuid not null, + modified_date timestamp with time zone not null, FOREIGN KEY(alert_host_uuid) REFERENCES hosts(host_uuid) ); ALTER TABLE alerts OWNER TO #!variable!user!#; CREATE TABLE history.alerts ( + history_id bigserial, alert_uuid uuid, alert_host_uuid uuid, alert_set_by text, @@ -298,50 +294,49 @@ CREATE TABLE history.alerts ( alert_message_variables text, alert_sort text, alert_header integer, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.alerts OWNER TO #!variable!user!#; --- CREATE FUNCTION history_alerts() RETURNS trigger --- AS $$ --- DECLARE --- history_alerts RECORD; --- BEGIN --- SELECT INTO history_alerts * FROM alerts WHERE alert_uuid = new.alert_uuid; --- INSERT INTO history.alerts --- (alert_uuid, --- alert_host_uuid, --- alert_set_by, --- alert_level, --- alert_title_key, --- alert_title_variables, --- alert_message_key, --- alert_message_variables, --- alert_sort, --- alert_header, --- change_date) --- VALUES --- (history_alerts.alert_uuid, --- history_alerts.alert_host_uuid, --- history_alerts.alert_set_by, --- history_alerts.alert_level, --- history_alerts.alert_title_key, --- history_alerts.alert_title_variables, --- history_alerts.alert_message_key, --- history_alerts.alert_message_variables, --- history_alerts.alert_sort, --- history_alerts.alert_header, --- history_alerts.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_alerts() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_alerts --- AFTER INSERT OR UPDATE ON alerts --- FOR EACH ROW EXECUTE PROCEDURE history_alerts(); +CREATE FUNCTION history_alerts() RETURNS trigger +AS $$ +DECLARE + history_alerts RECORD; +BEGIN + SELECT INTO history_alerts * FROM alerts WHERE alert_uuid = new.alert_uuid; + INSERT INTO history.alerts + (alert_uuid, + alert_host_uuid, + alert_set_by, + alert_level, + alert_title_key, + alert_title_variables, + alert_message_key, + alert_message_variables, + alert_sort, + alert_header, + modified_date) + VALUES + (history_alerts.alert_uuid, + history_alerts.alert_host_uuid, + history_alerts.alert_set_by, + history_alerts.alert_level, + history_alerts.alert_title_key, + history_alerts.alert_title_variables, + history_alerts.alert_message_key, + history_alerts.alert_message_variables, + history_alerts.alert_sort, + history_alerts.alert_header, + history_alerts.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_alerts() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_alerts + AFTER INSERT OR UPDATE ON alerts + FOR EACH ROW EXECUTE PROCEDURE history_alerts(); -- This holds user-configurable variable. These values override defaults but NOT configuration files. @@ -354,12 +349,12 @@ CREATE TABLE variables ( variable_section text not null, -- This is a free-form field that is used when displaying the various entries to a user. This allows for the various variables to be grouped into sections. variable_source_uuid text not null, -- Optional; Marks the variable as belonging to a specific X_uuid, where 'X' is a table name set in 'variable_source_table' variable_source_table text not null, -- Optional; Marks the database table corresponding to the 'variable_source_uuid' value. - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE variables OWNER TO #!variable!user!#; CREATE TABLE history.variables ( + history_id bigserial, variable_uuid uuid, variable_name text, variable_value text, @@ -368,46 +363,45 @@ CREATE TABLE history.variables ( variable_section text, variable_source_uuid text, variable_source_table text, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.variables OWNER TO #!variable!user!#; --- CREATE FUNCTION history_variables() RETURNS trigger --- AS $$ --- DECLARE --- history_variables RECORD; --- BEGIN --- SELECT INTO history_variables * FROM variables WHERE variable_uuid = new.variable_uuid; --- INSERT INTO history.variables --- (variable_uuid, --- variable_name, --- variable_value, --- variable_default, --- variable_description, --- variable_section, --- variable_source_uuid, --- variable_source_table, --- change_date) --- VALUES --- (history_variables.variable_uuid, --- history_variables.variable_name, --- history_variables.variable_value, --- history_variables.variable_default, --- history_variables.variable_description, --- history_variables.variable_section, --- history_variables.variable_source_uuid, --- history_variables.variable_source_table, --- history_variables.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_variables() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_variables --- AFTER INSERT OR UPDATE ON variables --- FOR EACH ROW EXECUTE PROCEDURE history_variables(); +CREATE FUNCTION history_variables() RETURNS trigger +AS $$ +DECLARE + history_variables RECORD; +BEGIN + SELECT INTO history_variables * FROM variables WHERE variable_uuid = new.variable_uuid; + INSERT INTO history.variables + (variable_uuid, + variable_name, + variable_value, + variable_default, + variable_description, + variable_section, + variable_source_uuid, + variable_source_table, + modified_date) + VALUES + (history_variables.variable_uuid, + history_variables.variable_name, + history_variables.variable_value, + history_variables.variable_default, + history_variables.variable_description, + history_variables.variable_section, + history_variables.variable_source_uuid, + history_variables.variable_source_table, + history_variables.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_variables() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_variables + AFTER INSERT OR UPDATE ON variables + FOR EACH ROW EXECUTE PROCEDURE history_variables(); -- This holds jobs to be run. @@ -424,14 +418,14 @@ CREATE TABLE jobs ( job_title text not null, -- This is a word key for the title of this job job_description text not null, -- This is a word key that describes this job. job_status text, -- This is a field used to report the status of the job. It is expected to be 'key,!!var1!foo!!,...,!!varN!bar!!' format, one per line. If the last line is 'failed', the job will be understood to have failed. - change_date timestamp with time zone not null, - change_uuid uuid not null, + modified_date timestamp with time zone not null, FOREIGN KEY(job_host_uuid) REFERENCES hosts(host_uuid) ); ALTER TABLE jobs OWNER TO #!variable!user!#; CREATE TABLE history.jobs ( + history_id bigserial, job_uuid uuid, job_host_uuid uuid, job_command text, @@ -444,54 +438,53 @@ CREATE TABLE history.jobs ( job_title text, job_description text, job_status text, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.jobs OWNER TO #!variable!user!#; --- CREATE FUNCTION history_jobs() RETURNS trigger --- AS $$ --- DECLARE --- history_jobs RECORD; --- BEGIN --- SELECT INTO history_jobs * FROM jobs WHERE job_uuid = new.job_uuid; --- INSERT INTO history.jobs --- (job_uuid, --- job_host_uuid, --- job_command, --- job_data, --- job_picked_up_by, --- job_picked_up_at, --- job_updated, --- job_name, --- job_progress, --- job_title, --- job_description, --- job_status, --- change_date) --- VALUES --- (history_jobs.job_uuid, --- history_jobs.job_host_uuid, --- history_jobs.job_command, --- history_jobs.job_data, --- history_jobs.job_picked_up_by, --- history_jobs.job_picked_up_at, --- history_jobs.job_updated, --- history_jobs.job_name, --- history_jobs.job_progress, --- history_jobs.job_title, --- history_jobs.job_description, --- history_jobs.job_status, --- history_jobs.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_jobs() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_jobs --- AFTER INSERT OR UPDATE ON jobs --- FOR EACH ROW EXECUTE PROCEDURE history_jobs(); +CREATE FUNCTION history_jobs() RETURNS trigger +AS $$ +DECLARE + history_jobs RECORD; +BEGIN + SELECT INTO history_jobs * FROM jobs WHERE job_uuid = new.job_uuid; + INSERT INTO history.jobs + (job_uuid, + job_host_uuid, + job_command, + job_data, + job_picked_up_by, + job_picked_up_at, + job_updated, + job_name, + job_progress, + job_title, + job_description, + job_status, + modified_date) + VALUES + (history_jobs.job_uuid, + history_jobs.job_host_uuid, + history_jobs.job_command, + history_jobs.job_data, + history_jobs.job_picked_up_by, + history_jobs.job_picked_up_at, + history_jobs.job_updated, + history_jobs.job_name, + history_jobs.job_progress, + history_jobs.job_title, + history_jobs.job_description, + history_jobs.job_status, + history_jobs.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_jobs() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_jobs + AFTER INSERT OR UPDATE ON jobs + FOR EACH ROW EXECUTE PROCEDURE history_jobs(); -- NOTE: network_interfaces, network_bonds and network_bridges are all used by scan-network (which doesn't -- exist yet). @@ -511,12 +504,12 @@ CREATE TABLE network_interfaces ( network_interface_medium text not null, -- This is 'tp' (twisted pair), 'fiber' or whatever they invent in the future. network_interface_bond_uuid uuid, -- If this iface is in a bond, this will contain the 'bonds -> bond_uuid' that it is slaved to. network_interface_bridge_uuid uuid, -- If this iface is attached to a bridge, this will contain the 'bridgess -> bridge_uuid' that it is connected to. - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE network_interfaces OWNER TO #!variable!user!#; CREATE TABLE history.network_interfaces ( + history_id bigserial, network_interface_uuid uuid not null, network_interface_host_uuid uuid, network_interface_mac_address text, @@ -529,54 +522,53 @@ CREATE TABLE history.network_interfaces ( network_interface_medium text, network_interface_bond_uuid uuid, network_interface_bridge_uuid uuid, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.network_interfaces OWNER TO #!variable!user!#; --- CREATE FUNCTION history_network_interfaces() RETURNS trigger --- AS $$ --- DECLARE --- history_network_interfaces RECORD; --- BEGIN --- SELECT INTO history_network_interfaces * FROM network_interfaces WHERE network_interface_uuid = new.network_interface_uuid; --- INSERT INTO history.network_interfaces --- (network_interface_uuid, --- network_interface_host_uuid, --- network_interface_mac_address, --- network_interface_name, --- network_interface_speed, --- network_interface_mtu, --- network_interface_link_state, --- network_interface_operational, --- network_interface_duplex, --- network_interface_medium, --- network_interface_bond_uuid, --- network_interface_bridge_uuid, --- change_date) --- VALUES --- (history_network_interfaces.network_interface_uuid, --- history_network_interfaces.network_interface_host_uuid, --- history_network_interfaces.network_interface_mac_address, --- history_network_interfaces.network_interface_name, --- history_network_interfaces.network_interface_speed, --- history_network_interfaces.network_interface_mtu, --- history_network_interfaces.network_interface_link_state, --- history_network_interfaces.network_interface_operational, --- history_network_interfaces.network_interface_duplex, --- history_network_interfaces.network_interface_medium, --- history_network_interfaces.network_interface_bond_uuid, --- history_network_interfaces.network_interface_bridge_uuid, --- history_network_interfaces.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_network_interfaces() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_network_interfaces --- AFTER INSERT OR UPDATE ON network_interfaces --- FOR EACH ROW EXECUTE PROCEDURE history_network_interfaces(); +CREATE FUNCTION history_network_interfaces() RETURNS trigger +AS $$ +DECLARE + history_network_interfaces RECORD; +BEGIN + SELECT INTO history_network_interfaces * FROM network_interfaces WHERE network_interface_uuid = new.network_interface_uuid; + INSERT INTO history.network_interfaces + (network_interface_uuid, + network_interface_host_uuid, + network_interface_mac_address, + network_interface_name, + network_interface_speed, + network_interface_mtu, + network_interface_link_state, + network_interface_operational, + network_interface_duplex, + network_interface_medium, + network_interface_bond_uuid, + network_interface_bridge_uuid, + modified_date) + VALUES + (history_network_interfaces.network_interface_uuid, + history_network_interfaces.network_interface_host_uuid, + history_network_interfaces.network_interface_mac_address, + history_network_interfaces.network_interface_name, + history_network_interfaces.network_interface_speed, + history_network_interfaces.network_interface_mtu, + history_network_interfaces.network_interface_link_state, + history_network_interfaces.network_interface_operational, + history_network_interfaces.network_interface_duplex, + history_network_interfaces.network_interface_medium, + history_network_interfaces.network_interface_bond_uuid, + history_network_interfaces.network_interface_bridge_uuid, + history_network_interfaces.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_network_interfaces() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_network_interfaces + AFTER INSERT OR UPDATE ON network_interfaces + FOR EACH ROW EXECUTE PROCEDURE history_network_interfaces(); -- This stores information about network bonds (mode=1) on a hosts. @@ -594,14 +586,14 @@ CREATE TABLE bonds ( bond_down_delay bigint not null, bond_mac_address text not null, bond_operational text not null, -- This is 'up', 'down' or 'unknown' - change_date timestamp with time zone not null, - change_uuid uuid not null, + modified_date timestamp with time zone not null, FOREIGN KEY(bond_host_uuid) REFERENCES hosts(host_uuid) ); ALTER TABLE bonds OWNER TO #!variable!user!#; CREATE TABLE history.bonds ( + history_id bigserial, bond_uuid uuid, bond_host_uuid uuid, bond_name text, @@ -615,56 +607,55 @@ CREATE TABLE history.bonds ( bond_down_delay bigint, bond_mac_address text, bond_operational text, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.bonds OWNER TO #!variable!user!#; --- CREATE FUNCTION history_bonds() RETURNS trigger --- AS $$ --- DECLARE --- history_bonds RECORD; --- BEGIN --- SELECT INTO history_bonds * FROM bonds WHERE bond_uuid = new.bond_uuid; --- INSERT INTO history.bonds --- (bond_uuid, --- bond_host_uuid, --- bond_name, --- bond_mode, --- bond_mtu, --- bond_primary_slave, --- bond_primary_reselect, --- bond_active_slave, --- bond_mii_polling_interval, --- bond_up_delay, --- bond_down_delay, --- bond_mac_address, --- bond_operational, --- change_date) --- VALUES --- (history_bonds.bond_uuid, --- history_bonds.bond_host_uuid, --- history_bonds.bond_name, --- history_bonds.bond_mode, --- history_bonds.bond_mtu, --- history_bonds.bond_primary_slave, --- history_bonds.bond_primary_reselect, --- history_bonds.bond_active_slave, --- history_bonds.bond_mii_polling_interval, --- history_bonds.bond_up_delay, --- history_bonds.bond_down_delay, --- history_bonds.bond_mac_address, --- history_bonds.bond_operational, --- history_bonds.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_bonds() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_bonds --- AFTER INSERT OR UPDATE ON bonds --- FOR EACH ROW EXECUTE PROCEDURE history_bonds(); +CREATE FUNCTION history_bonds() RETURNS trigger +AS $$ +DECLARE + history_bonds RECORD; +BEGIN + SELECT INTO history_bonds * FROM bonds WHERE bond_uuid = new.bond_uuid; + INSERT INTO history.bonds + (bond_uuid, + bond_host_uuid, + bond_name, + bond_mode, + bond_mtu, + bond_primary_slave, + bond_primary_reselect, + bond_active_slave, + bond_mii_polling_interval, + bond_up_delay, + bond_down_delay, + bond_mac_address, + bond_operational, + modified_date) + VALUES + (history_bonds.bond_uuid, + history_bonds.bond_host_uuid, + history_bonds.bond_name, + history_bonds.bond_mode, + history_bonds.bond_mtu, + history_bonds.bond_primary_slave, + history_bonds.bond_primary_reselect, + history_bonds.bond_active_slave, + history_bonds.bond_mii_polling_interval, + history_bonds.bond_up_delay, + history_bonds.bond_down_delay, + history_bonds.bond_mac_address, + history_bonds.bond_operational, + history_bonds.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_bonds() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_bonds + AFTER INSERT OR UPDATE ON bonds + FOR EACH ROW EXECUTE PROCEDURE history_bonds(); -- This stores information about network bridges. @@ -674,55 +665,54 @@ CREATE TABLE bridges ( bridge_name text not null, bridge_id text not null, bridge_stp_enabled text not null, - change_date timestamp with time zone not null, - change_uuid uuid not null, + modified_date timestamp with time zone not null, FOREIGN KEY(bridge_host_uuid) REFERENCES hosts(host_uuid) ); ALTER TABLE bridges OWNER TO #!variable!user!#; CREATE TABLE history.bridges ( + history_id bigserial, bridge_uuid uuid, bridge_host_uuid uuid, bridge_name text, bridge_id text, bridge_stp_enabled text, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.bridges OWNER TO #!variable!user!#; --- CREATE FUNCTION history_bridges() RETURNS trigger --- AS $$ --- DECLARE --- history_bridges RECORD; --- BEGIN --- SELECT INTO history_bridges * FROM bridges WHERE bridge_uuid = new.bridge_uuid; --- INSERT INTO history.bridges --- (bridge_uuid, --- bridge_host_uuid, --- bridge_name, --- bridge_name, --- bridge_id, --- bridge_stp_enabled, --- change_date) --- VALUES --- (history_bridges.bridge_uuid, --- history_bridges.bridge_host_uuid, --- history_bridges.bridge_name, --- history_bridges.bridge_name, --- history_bridges.bridge_id, --- history_bridges.bridge_stp_enabled, --- history_bridges.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_bridges() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_bridges --- AFTER INSERT OR UPDATE ON bridges --- FOR EACH ROW EXECUTE PROCEDURE history_bridges(); +CREATE FUNCTION history_bridges() RETURNS trigger +AS $$ +DECLARE + history_bridges RECORD; +BEGIN + SELECT INTO history_bridges * FROM bridges WHERE bridge_uuid = new.bridge_uuid; + INSERT INTO history.bridges + (bridge_uuid, + bridge_host_uuid, + bridge_name, + bridge_name, + bridge_id, + bridge_stp_enabled, + modified_date) + VALUES + (history_bridges.bridge_uuid, + history_bridges.bridge_host_uuid, + history_bridges.bridge_name, + history_bridges.bridge_name, + history_bridges.bridge_id, + history_bridges.bridge_stp_enabled, + history_bridges.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_bridges() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_bridges + AFTER INSERT OR UPDATE ON bridges + FOR EACH ROW EXECUTE PROCEDURE history_bridges(); -- This stores information about network ip addresss. @@ -736,14 +726,14 @@ CREATE TABLE ip_addresses ( ip_address_gateway text not null, -- If set, this is the gateway IP for this subnet ip_address_default_gateway integer not null default 0, -- If true, the gateway will be the default for the host. ip_address_dns text not null, -- If set, this is a comma-separated list of DNS IPs to use (in the order given) - change_date timestamp with time zone not null, - change_uuid uuid not null, + modified_date timestamp with time zone not null, FOREIGN KEY(ip_address_host_uuid) REFERENCES hosts(host_uuid) ); ALTER TABLE ip_addresses OWNER TO #!variable!user!#; CREATE TABLE history.ip_addresses ( + history_id bigserial, ip_address_uuid uuid, ip_address_host_uuid uuid, ip_address_on_type text, @@ -753,48 +743,47 @@ CREATE TABLE history.ip_addresses ( ip_address_gateway text, ip_address_default_gateway integer, ip_address_dns text, - change_date timestamp with time zone not null, - change_uuid uuid not null + modified_date timestamp with time zone not null ); ALTER TABLE history.ip_addresses OWNER TO #!variable!user!#; --- CREATE FUNCTION history_ip_addresses() RETURNS trigger --- AS $$ --- DECLARE --- history_ip_addresses RECORD; --- BEGIN --- SELECT INTO history_ip_addresses * FROM ip_addresses WHERE ip_address_uuid = new.ip_address_uuid; --- INSERT INTO history.ip_addresses --- (ip_address_uuid, --- ip_address_host_uuid, --- ip_address_on_type, --- ip_address_on_uuid, --- ip_address_address, --- ip_address_subnet_mask, --- ip_address_gateway, --- ip_address_default_gateway, --- ip_address_dns, --- change_date) --- VALUES --- (history_ip_addresses.ip_address_uuid, --- history_ip_addresses.ip_address_host_uuid, --- history_ip_addresses.ip_address_on_type, --- history_ip_addresses.ip_address_on_uuid, --- history_ip_addresses.ip_address_address, --- history_ip_addresses.ip_address_subnet_mask, --- history_ip_addresses.ip_address_gateway, --- history_ip_addresses.ip_address_default_gateway, --- history_ip_addresses.ip_address_dns, --- history_ip_addresses.change_date); --- RETURN NULL; --- END; --- $$ --- LANGUAGE plpgsql; --- ALTER FUNCTION history_ip_addresses() OWNER TO #!variable!user!#; --- --- CREATE TRIGGER trigger_ip_addresses --- AFTER INSERT OR UPDATE ON ip_addresses --- FOR EACH ROW EXECUTE PROCEDURE history_ip_addresses(); +CREATE FUNCTION history_ip_addresses() RETURNS trigger +AS $$ +DECLARE + history_ip_addresses RECORD; +BEGIN + SELECT INTO history_ip_addresses * FROM ip_addresses WHERE ip_address_uuid = new.ip_address_uuid; + INSERT INTO history.ip_addresses + (ip_address_uuid, + ip_address_host_uuid, + ip_address_on_type, + ip_address_on_uuid, + ip_address_address, + ip_address_subnet_mask, + ip_address_gateway, + ip_address_default_gateway, + ip_address_dns, + modified_date) + VALUES + (history_ip_addresses.ip_address_uuid, + history_ip_addresses.ip_address_host_uuid, + history_ip_addresses.ip_address_on_type, + history_ip_addresses.ip_address_on_uuid, + history_ip_addresses.ip_address_address, + history_ip_addresses.ip_address_subnet_mask, + history_ip_addresses.ip_address_gateway, + history_ip_addresses.ip_address_default_gateway, + history_ip_addresses.ip_address_dns, + history_ip_addresses.modified_date); + RETURN NULL; +END; +$$ +LANGUAGE plpgsql; +ALTER FUNCTION history_ip_addresses() OWNER TO #!variable!user!#; + +CREATE TRIGGER trigger_ip_addresses + AFTER INSERT OR UPDATE ON ip_addresses + FOR EACH ROW EXECUTE PROCEDURE history_ip_addresses(); -- ------------------------------------------------------------------------------------------------------- -- @@ -803,11 +792,11 @@ ALTER TABLE history.ip_addresses OWNER TO #!variable!user!#; -- This table records the last time a scan ran. It's sole purpose is to make sure at least one table's --- 'change_date' changes per run, so that database resyncs can be triggered reliably. +-- 'modified_date' changes per run, so that database resyncs can be triggered reliably. CREATE TABLE updated ( updated_host_uuid uuid not null, updated_by text not null, -- The name of the agent (or "ScanCore' itself) that updated. - change_date timestamp with time zone not null, + modified_date timestamp with time zone not null, FOREIGN KEY(updated_host_uuid) REFERENCES hosts(host_uuid) ); @@ -824,7 +813,7 @@ CREATE TABLE alert_sent ( alert_set_by text not null, -- name of the program that set this alert alert_record_locator text not null, -- String used by the agent to identify the source of the alert (ie: UPS serial number) alert_name text not null, -- A free-form name used by the caller to identify this alert. - change_date timestamp with time zone not null, + modified_date timestamp with time zone not null, FOREIGN KEY(alert_sent_host_uuid) REFERENCES hosts(host_uuid) ); @@ -837,7 +826,7 @@ CREATE TABLE states ( state_name text not null, -- This is the name of the state (ie: 'migration', etc) state_host_uuid uuid not null, -- The UUID of the machine that the state relates to. In migrations, this is the UUID of the target state_note text not null, -- This is a free-form note section that the application setting the state can use for extra information (like the name of the server being migrated) - change_date timestamp with time zone not null default now(), + modified_date timestamp with time zone not null, FOREIGN KEY(state_host_uuid) REFERENCES hosts(host_uuid) ); diff --git a/share/words.xml b/share/words.xml index cf23ba48..9075e9a2 100644 --- a/share/words.xml +++ b/share/words.xml @@ -233,7 +233,7 @@ The database connection error was: Log->secure' is not set.]]>Failed to connect to any database. - check_alert_sent() was called but the 'change_date' parameter was not passed and/or 'sys::database::timestamp' is not set. Did the program fail to connect to any databases?]]> + check_alert_sent() was called but the 'modified_date' parameter was not passed and/or 'sys::database::timestamp' is not set. Did the program fail to connect to any databases?]]>[ Error ] - Failed to start the Postgres server. Please check the system logs for details.The database user: [#!variable!user!#] was created with UUID: [#!variable!id!#].[ Error ] - Failed to add the database user: [#!variable!user!#]! Unable to proceed. @@ -245,7 +245,7 @@ The database connection error was: - Set By: ....... [#!variable!set_by!#] - Record Locator: [#!variable!record_locator!#] - Name: ......... [#!variable!name!#] -- Timestamp: .... [#!variable!change_date!#] +- Timestamp: .... [#!variable!modified_date!#] [ Error ] - There is no Anvil! database user set for the local machine. Please check: [#!data!path::config::anvil.conf!#]'s DB entry: [#!variable!uuid!#].Database user: [#!variable!user!#] password has been set/updated. diff --git a/tools/anvil-daemon b/tools/anvil-daemon index 3b018add..93c9716e 100755 --- a/tools/anvil-daemon +++ b/tools/anvil-daemon @@ -213,7 +213,7 @@ sub boot_time_tasks my $query = " SELECT variable_value, - (SELECT extract(epoch from now()) - extract(epoch from change_date)) AS changed_seconds_ago + (SELECT extract(epoch from now()) - extract(epoch from modified_date)) AS changed_seconds_ago FROM variables WHERE diff --git a/tools/anvil-update-states b/tools/anvil-update-states index c0f853de..774ceeaa 100755 --- a/tools/anvil-update-states +++ b/tools/anvil-update-states @@ -553,7 +553,7 @@ WHERE AND network_interface_operational != 'DELETED' ORDER BY - change_date DESC + modified_date DESC ;"; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0124", variables => { query => $query }}); $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__});