Trigger Generation

sir.trigger_generation.generate(trigger_filename, function_filename, broker_id, entities)[source]

Generates SQL queries that create and remove triggers for the MusicBrainz database.

Generation works in the following way:

  1. Determine which tables need to have triggers on them:
    • Entity tables themselves
    • Tables in every path of entity’s fields
  2. Generate triggers (for inserts, updates, and deletions) for each table (model in mbdata):

    2.1. Get a list of PKs 2.2. Write triggers that would send messages into appropriate RabbitMQ queues (“search.index”

    queue for INSERT and UPDATE queries, “search.delete” for DELETE queries):

    <table name>, PKs{<PK row name>, <PK value>}

  3. Write generated triggers into SQL scripts to be run on the MusicBrainz database

Since table might have multiple primary keys, we need to explicitly specify their row names and values.

sir.trigger_generation.generate_func(args)[source]

This is the entry point for this trigger_generation module. This function gets called from main().

sir.trigger_generation.get_trigger_tables(entities)[source]

Determines which tables need to have triggers set on them.

Returns a dictionary of table names (key) with a dictionary (value) that provides additional information about a table:

  • list of primary keys for each table.
  • whether it’s an entity table

:param [str] entities Which entity types to index if not all.

Write an SQL “footer” into a file.

Adds a statement to commit a transaction. Should be written at the end of each SQL script that wrote a header (see write_header function).

Parameters:f (file) – File to write the footer into.
sir.trigger_generation.write_header(f)[source]

Write an SQL “header” into a file.

Adds a note about editing, sets command line options, and begins a transaction. Should be written at the beginning of each SQL script.

Parameters:f (file) – File to write the header into.
sir.trigger_generation.write_triggers(trigger_file, function_file, model, is_direct, has_gid, **generator_args)[source]
Parameters:
  • file trigger_file (str) – File where triggers will be written.
  • file function_file (str) – File where functions will be written.
  • model – A declarative class.
  • is_direct (bool) – Whether this is an entity table or not.
sir.trigger_generation.write_triggers_to_file(generators, trigger_file, function_file, **generator_args)[source]

Write SQL for creation of triggers (for deletion, insertion, and updates) and associated functions into files.

Parameters:
  • generators (list) – A set of generator classes (based on``TriggerGenerator``) to use for creating SQL statements.
  • trigger_file (file) – File into which commands for creating triggers will be written.
  • function_file (file) – File into which commands for creating trigger functions will be written.
class sir.trigger_generation.sql_generator.TriggerGenerator(table_name, pk_columns, fk_columns, broker_id=1, **kwargs)[source]

Bases: object

Base generator class for triggers and corresponding function that would go into the MusicBrainz database.

Parameters:
  • table_name (str) – The table on which to generate the trigger.
  • pk_columns – List of primary key column names for a table that this trigger is being generated for.
  • broker_id (int) – ID of the AMQP broker row in a database.
op = None

The operation (INSERT, UPDATE, or DELETE)

trigger()[source]

The CREATE TRIGGER statement for this trigger.

Return type:str
function()[source]

The CREATE FUNCTION statement for this trigger.

https://www.postgresql.org/docs/9.0/static/plpgsql-structure.html

We use https://github.com/omniti-labs/pg_amqp to publish messages to an AMQP broker.

Return type:str
trigger_name

The name of this trigger and its function.

Return type:str
class sir.trigger_generation.sql_generator.InsertTriggerGenerator(table_name, pk_columns, fk_columns, broker_id=1, **kwargs)[source]

Bases: sir.trigger_generation.sql_generator.TriggerGenerator

A trigger generator for INSERT operations.

Parameters:
  • table_name (str) – The table on which to generate the trigger.
  • pk_columns – List of primary key column names for a table that this trigger is being generated for.
  • broker_id (int) – ID of the AMQP broker row in a database.
class sir.trigger_generation.sql_generator.UpdateTriggerGenerator(**gen_args)[source]

Bases: sir.trigger_generation.sql_generator.TriggerGenerator

A trigger generator for UPDATE operations.

trigger()[source]

The CREATE TRIGGER statement for this trigger.

Return type:str
class sir.trigger_generation.sql_generator.DeleteTriggerGenerator(table_name, pk_columns, fk_columns, broker_id=1, **kwargs)[source]

Bases: sir.trigger_generation.sql_generator.TriggerGenerator

A trigger generator for DELETE operations.

Parameters:
  • table_name (str) – The table on which to generate the trigger.
  • pk_columns – List of primary key column names for a table that this trigger is being generated for.
  • broker_id (int) – ID of the AMQP broker row in a database.
class sir.trigger_generation.sql_generator.GIDDeleteTriggerGenerator(*args, **kwargs)[source]

Bases: sir.trigger_generation.sql_generator.DeleteTriggerGenerator

This trigger generator produces DELETE statements that selects just gid row and ignores primary keys.

It should be used for entity tables themselves (in “direct” triggers) for tables like “artist”, “release_group”, “recording”, and the rest.

class sir.trigger_generation.sql_generator.ReferencedDeleteTriggerGenerator(table_name, pk_columns, fk_columns, broker_id=1, **kwargs)[source]

Bases: sir.trigger_generation.sql_generator.DeleteTriggerGenerator

A trigger generator for DELETE operations for tables which are referenced in SearchEntity tables. Delete operations in such tables cause the main SearchEntity tables to be updated.

Parameters:
  • table_name (str) – The table on which to generate the trigger.
  • pk_columns – List of primary key column names for a table that this trigger is being generated for.
  • broker_id (int) – ID of the AMQP broker row in a database.