GithubHelp home page GithubHelp logo

macaroon's Introduction

Many tricky table configurations -- for example, logged tables and partitioned tables -- would seem to be amenable to mechanical derivation. The SQL standard provides for fairly rich introspection capabilities -- on par with any object oriented language -- so it stands to reason that we should be able to use metaprogramming to derive advanced table configurations mechanically.

The meta schema in meta.sql should be loaded before loading the others. To load all the schemas together, run \i macaroon.psql at the psql prompt.

Creating Tables for Auditing & Versioning

Imagine that your application has tables in the app namespace; and you'd like to log past row versions and metadata about changes to the state and events schemas, respectively. You can idempotently configure both audit and version stracking by SELECTing tables in the app namespace that are not already tracked and passing them to the setup functions:

SELECT tab,
       temporal.temporal(tab, 'state'),
       audit.audit(tab, 'events')
  FROM pg_tables,
       LATERAL (SELECT (schemaname||'.'||tablename)::regclass AS tab)
            AS casted_to_regclass
 WHERE schemaname = 'app'
   AND tab NOT IN (SELECT logged FROM temporal.logged
                    UNION
                   SELECT audited FROM audit.audited);

      tab      │    temporal     │      audit
───────────────┼─────────────────┼──────────────────
 app.user_infostate.user_infoevents.user_info
 app.telephonestate.telephoneevents.telephone
 app.cpustate.cpuevents.cpu
 ...           │ ...             │ ...

Using The Auditing & Versioning Tables

The audit and temporal tables for each table that is tracked can be joined on the txid column to see all the actions that took place during a particular transaction.

A Note About Migrations

The audit tables are indifferent to migrations -- they do not store any row data. The temporal tables have columns that contain whole rows from the logged tables, so migrations will transparently upgrade the temporal tables, too. This is good and bad. It's good because migrations won't break logging; it's bad because migrations are going to hit not only the present state but also all past states.

macaroon's People

Contributors

francoiscampbell avatar solidsnack 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.