Giter Site home page Giter Site logo

charlpcronje / audit-log-for-mariadb-mysql- Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 40 KB

This stored procedure is designed to automatically create database triggers for INSERT, UPDATE, and DELETE operations across all tables in a specified MariaDB database for the purpose of an audit log

PHP 10.97% Python 28.37% Ruby 7.69% Java 16.98% C# 18.83% JavaScript 17.17%

audit-log-for-mariadb-mysql-'s Introduction

Audit Log for MariaDB & MySQL Databases

This stored procedure is designed to automatically create database triggers for INSERT, UPDATE, and DELETE operations across all tables in a specified MariaDB database. These triggers log changes to an audit_logs table, capturing detailed information about data modifications in JSON format. The procedure ensures that each table has its corresponding triggers without duplications, making it safe to rerun whenever new tables are added.

Files in Repo

How It Works

Procedure Logic

  1. Database Iteration: The procedure iterates over all tables in the specified database.
  2. Existence Checks: For each table, it checks if specific triggers (INSERT, UPDATE, DELETE) already exist.
  3. Conditional Trigger Creation: If a trigger does not exist, it is created. Each trigger logs modifications to the audit_logs table, including:
    • dtime: The date and time of the log entry.
    • table_name: The name of the table where the change occurred.
    • action: The type of action (INSERT, UPDATE, DELETE).
    • json_log: A JSON object containing all column values involved in the transaction.
    • created_at: The timestamp when the log entry was created.

Technical Details

  • Dynamic SQL: The procedure uses dynamic SQL for trigger creation to accommodate various table structures dynamically.
  • Safety Checks: It includes checks to avoid creating duplicate triggers, allowing the procedure to be run multiple times safely.
  • JSON Data Handling: The JSON object is dynamically constructed based on the table's columns, ensuring comprehensive logging.

Setup Instructions

  1. Modify the Database Name: Replace 'YourDatabaseName' in the script with the actual name of your database.
  2. Execute the Procedure: Run the stored procedure script in your MariaDB environment to deploy it.
  3. Run the Procedure: Execute the procedure using CALL SetupAuditTriggers(); to create the triggers.

Potential Upgrades

  • Enhanced Error Handling: Incorporate detailed error handling to manage issues that might arise during trigger creation, such as permission problems or SQL syntax errors.
  • Performance Optimization: Implement batching or asynchronous processing to reduce the load on the database when multiple triggers are being created simultaneously.
  • Customizable Logging Details: Add parameters to the procedure allowing customization of which data points are logged, or handling different data types more gracefully.
  • Security Features: Include options for masking or encrypting sensitive data within the logs.

Recommended Storage Engine for the Log Table

  • InnoDB: Recommended for the audit_logs table due to its support for ACID transactions, row-level locking, and crash recovery. InnoDB’s performance and reliability make it suitable for handling frequent inserts generated by the triggers.

Conclusion

This automated trigger creation procedure facilitates comprehensive auditing across all database tables. It is ideal for maintaining a detailed change log, aiding in debugging, monitoring, and compliance with regulatory requirements.

audit-log-for-mariadb-mysql-'s People

Contributors

charlpcronje avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.