Closed wildtayne closed 6 years ago
Thanks @srrollo for getting this change started. I will share my observations over the next few comments, probably organized by file.
The following observations are about addDDLMonitors.sql
:
SELECT
is not requiredupdateDDLActivity
should be renamed to logDDLActivity
CASE
expression, but that is not importantAND
such that it is left-aligned with the first part of the predicate. Better yet, move up the end-of-line comment and place the entire predicate on one lineVOLATILE
is default: no need to specifyClassDB
instead of classdb
Also, I recommend adding functions enableDDLActivityLogging()
and disableDDLActivityLogging()
to respectively enable and disable the triggers. Instructors and DB managers should be able to execute these functions.
Thanks @smurthys for the review. The commits up to 43b20e1 address the issues raised. I added the enableDDLActivityLogging()
and disableDDLActivityLogging()
functions as well. One thing to note about them is that they remain owned by the creating user, and not ClassDB
. This is because only superusers may CREATE
/DROP
event triggers, and ClassDB
is not a superuser. However, we are asserting at the beginning of the script that the user running it is.
Quick thoughts on the updated addDDLMonitors.sql
:
ALTER EVENT TRIGGER
.
CREATE EVENT TRIGGER
is still required.CREATE EVENT TRIGGER
should be guarded by a block to create the trigger only if it does not exist (because there is no IF NOT EXISTS
option)7109499 addresses the points brought up in this thread. Currently, it is assuming that ClassDB.isTriggerDefined()
will be available, but this can easily be changed to match whatever we end up implementing.
I've also added the files disableServerLogging.sql
and addConnectionLogging.psql
based on today's discussion.
On function isTriggerDefined
, please see my recent comment on another PR.
The latest commits add a unit test for the DDL event triggers, and a significant improvement to importLog()
.
startDate
parameter for importLog
will now always be used as the start date for log importing unless it is null.ConnectionActivity
, and converting it to the server timezone. This should be accurate in almost all circumstances, since the dates on log files should match up with the server's time zone.@srrollo 0955c84 actually "Added terrible unit test" ? 😕
Yeah, I was pretty frustrated with the unit test last night 😩. The latest update is much better. The two unit tests are now somewhat comprehensive:
testAddDDLMonitors.sql
creates several ClassDB roles of each type, and uses SET SESSION AUTHORIZATION
to execute DDL statements as each
Additionally, it creates a non-ClassDB user, and executes DDL statements using it
The DDL activity log is checked for the correct number of entries, correct object names, and correct operation names. Additionally, the presence of each ClassDB user, and absence of the non-ClassDB user is checked
Similarly, testAddLogMgmt.psql
creates several ClassDB roles, and one non-ClassDB role
Using psql's \c
command, the connection is switched to each user, causing a log entry
This requires the user to manually enter the password for each user (annoying), but each user has a password of ' ' (less annoying)
The test checks if the number of new connections in the log table matches the expected number
I am having one more issue with testAddLogMgmt.psql
- I can't get my non-ClassDB user to login to the database (I get password authentication failed
). The code to create the user is this:
CREATE USER conNonClassDB ENCRYPTED PASSWORD ' ';
EXECUTE FORMAT('GRANT CONNECT ON DATABASE %I TO conNonClassDB', CURRENT_DATABASE());
The latest commits also remove isTriggerDefined()
, as per our discussion. I've removed the creation of the non-ClassDB user in the test for now, as it is not strictly necessary, and can be addressed later (after M2 if necessary).
Quickly: Functions enableDDLActivityLogging
and disableDDLActivityLogging
are missing owner assignment.
FYI: I have a meeting and other work related to a different project starting now to EOD.
enableDDLActivityLogging
and disableDDLActivityLogging
must be owned by a superuser, in this case the user executing addDDLMonitors.sql
, since all event trigger DDL operations can only be performed by superusers.
I've added some extra comments to better document this requirement.
Thanks @srrollo for the reminder about event triggers needing super user.
FYI, I'm reviewing the files right now. EDIT: I'll share my observations over multiple comments.
Here are some observations:
addConnectionLogging.psql
:
enableServerLogging.sql
, I feel this script should not include enableServerLogging.sql
.
enableServerLogging.sql
is excluded from this script, the script itself will not be needed. enableServerLogging.sql
.psql
extensionEDIT: Same observations in disableServerLogging.sql
as for enableServerLogging.sql
. Plus:
enableServerLogging.sql
apply even hereaddLogMgmt.sql
is really streamlined. Excellent work @srrollo.
Some observations:
postgresLog
should be temporary, especially because that table is truncated at the end of function importLog
. Should probably not make this change right now, but rather add an issue and address in the next release.ClassDB.ChangeTimeZone
should be called on the SELECT MAX(...)...
SELECT
query until it is really required, because table ConnectionActivity
can get rather large over a period of time. For example, L99 could probably be written as an IF-ELSE
block. Alternatively, lastConDate
in the COALESCE
expression could be replaced with the SELECT
query, but that won't be very readable.connection_from
(I assume indicates a "machine") and application_name
to ConnectionActivity? Should probably not make this change right now, but rather add an issue and address in the next releasepick up only connection-related entries
ORDER BY
is unclear.ClassDB
role as well.
For now I am unable to review the test scripts due to other urgent work. I think @afig's review on that suffices.
Thanks @smurthys for the thorough review. I've made the following changes:
addConnectionLogging.psql
:
enableServerLogging.sql/disableServerLogging.sql
addDDLMonitors.sql
DO
block around the CREATE EVENT TRIGGER
statements, since it is no longer neededaddLogMgmt.sql
importLog()
:
lastConTimestampUTC
and lastConDateLocal
ClassDB.ConnectionActivity
in the INSERT
queryLastConTimestampUTC
will always be needed in the INSERT
query, I've elected to always compute it instead of only computing it if no startDate
is suppliedAll files:
EXECUTE
on all superuser-owned functions to ClassDB
I've added changeTimeZone()
to addHelpers.sql
in this PR. Originally, it was going to be added in #152, however it is now required by importLog()
, and it looks like this PR will be merged before #152.
All good changes @srrollo:
In addDDLMonitors.sql
:
IF ClassDB.isUser(SESSION_USER::ClassDB.IDNameDomain)
In addLogMgmt.sql
:
SELECT
query that feed into the INSERT
query.In *ServerLogging.psql
, file name is incorrect on L1 in both files.
On file names:
addDDLMonitors.sql
should be renamed to something like addDDLActivityLogging.sql
because there will be a separate DDL trigger (in progress) to monitor schema drop etc. as part of RoleBase mgmt. addLogMgmt.sql
should be renamed to addConnectionActivityLogging.sql
.disableServerLogging.psql
should be renamed to disableConnectionLogging.psql
because the script only disables connection logging. In solidarity, and to avoid confusion, the enable counterpart should probably also be renamed similarly though that script enables logging as well (however, the scripts intent is to enable connection logging) Also, perhaps the function importLog
should be renamed to importConnectionActivity
.
Should addLogMgmt.sql
maybe be named addConnectionActivityMgmt.sql
instead? I feel like addConnectionActivityLogging.sql
doesn't convey the intent of the script, since it only adds the management functions, and doesn't directly influence the logging system.
I'm also not sure about taking the minimum of startDate
and lastConDateLocal
. startDate
is intended to override the date check if users want to skip days. I think the documentation also states that except for the first run, you should generally just use importConnectionLog()
.
I just pushed the commits addressing the recent comments.
All changes look good @srrollo. I have only one observation on addHelpers.sql
: It is not necessary to revoke permission from anyone to execute function changeTimeZone
. Just granting ownership to ClassDB
suffices.
Thanks @afig and @smurthys for the reviews. I've removed the unnecessary REVOKE
and fixed the errors in the comments.
This PR updates the DDL monitor and log import functions to support the new
DDLActivity
andConnectionActivity
tables. I have not tested this functionality yet, but I am creating the PR so others can begin to review it.Basically, both functions now perform
INSERT
statements instead ofUPDATE
statements, which has greatly simplified their code.