diff --git a/lib/generators/pg_audit_log/templates/lib/tasks/pg_audit_log.rake b/lib/generators/pg_audit_log/templates/lib/tasks/pg_audit_log.rake index f022ad0..276a115 100644 --- a/lib/generators/pg_audit_log/templates/lib/tasks/pg_audit_log.rake +++ b/lib/generators/pg_audit_log/templates/lib/tasks/pg_audit_log.rake @@ -7,7 +7,7 @@ namespace :pg_audit_log do end puts "Installing audit_changes() function..." - PgAuditLog::Function.install + PgAuditLog::Function.install(ENV['PG_AUDIT_LOG_SCHEMA'].presence) puts "Installing all audit log triggers... " PgAuditLog::Triggers.install diff --git a/lib/pg_audit_log/function.rb b/lib/pg_audit_log/function.rb index f96a29c..30c741f 100644 --- a/lib/pg_audit_log/function.rb +++ b/lib/pg_audit_log/function.rb @@ -43,7 +43,8 @@ def user_unique_name_temporary_function(username) end end - def install + def install(only_audit_schema=nil) + schema_restriction = only_audit_schema ? "AND table_schema = '#{only_audit_schema}'" : "" execute <<-SQL CREATE OR REPLACE PROCEDURAL LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION #{name}() RETURNS trigger @@ -75,7 +76,7 @@ def install INTO primary_key_column USING TG_RELNAME; primary_key_value := NULL; - FOR col IN SELECT * FROM information_schema.columns WHERE table_name = TG_RELNAME LOOP + FOR col IN SELECT * FROM information_schema.columns WHERE table_name = TG_RELNAME #{schema_restriction} LOOP new_value := NULL; old_value := NULL; column_name := col.column_name;