lib/event_sourcery/postgres/schema.rb in event_sourcery-postgres-0.4.0 vs lib/event_sourcery/postgres/schema.rb in event_sourcery-postgres-0.5.0
- old
+ new
@@ -1,19 +1,30 @@
module EventSourcery
module Postgres
module Schema
module_function
+ # This will create the event store tables and functions
+ # (event, aggregates, tracker and create or update functions)
+ # for the given Postgres database.
+ # The default will be the one specified in the config.
+ #
+ # @param db the Postgres database to use
def create_event_store(db: EventSourcery::Postgres.config.event_store_database,
events_table_name: EventSourcery::Postgres.config.events_table_name,
aggregates_table_name: EventSourcery::Postgres.config.aggregates_table_name,
write_events_function_name: EventSourcery::Postgres.config.write_events_function_name)
create_events(db: db, table_name: events_table_name)
create_aggregates(db: db, table_name: aggregates_table_name)
create_or_update_functions(db: db, events_table_name: events_table_name, function_name: write_events_function_name, aggregates_table_name: aggregates_table_name)
end
+ # Create the events table. Needs the database and the table name.
+ # The defaults will be whats specified in config.
+ #
+ # @param db the Postgres database to use
+ # @param table_name the name of the events table
def create_events(db: EventSourcery::Postgres.config.event_store_database,
table_name: EventSourcery::Postgres.config.events_table_name)
db.run 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
db.create_table(table_name) do
primary_key :id, type: :Bignum
@@ -32,18 +43,31 @@
index :causation_id
index :created_at
end
end
+ # Create the aggregates table. Needs the database and the table name.
+ # The defaults will be whats specified in config.
+ #
+ # @param db the Postgres database to use
+ # @param table_name the name of the aggregates table
def create_aggregates(db: EventSourcery::Postgres.config.event_store_database,
table_name: EventSourcery::Postgres.config.aggregates_table_name)
db.create_table(table_name) do
primary_key :aggregate_id, :uuid
column :version, :bigint, default: 1
end
end
+ # Create the 'create or update' fucntions.
+ # Needs the database, table name, function name and aggregates table name.
+ # The defaults will be whats specified in config.
+ #
+ # @param db the Postgres database to use
+ # @param function_name the name of the write events function
+ # @param events_table_name the name of the events table
+ # @param aggregates_table_name the name of the aggregates table
def create_or_update_functions(db: EventSourcery::Postgres.config.event_store_database,
function_name: EventSourcery::Postgres.config.write_events_function_name,
events_table_name: EventSourcery::Postgres.config.events_table_name,
aggregates_table_name: EventSourcery::Postgres.config.aggregates_table_name)
db.run <<-SQL
@@ -136,9 +160,14 @@
end;
$$ language plpgsql;
SQL
end
+ # Create the projector tracker table. Needs the database and the table name.
+ # The defaults will be whats specified in config.
+ #
+ # @param db the Postgres database to use
+ # @param table_name the name of the aggregates table
def create_projector_tracker(db: EventSourcery::Postgres.config.projections_database,
table_name: EventSourcery::Postgres.config.tracker_table_name)
db.create_table(table_name) do
primary_key :id, type: :Bignum
column :name, 'varchar(255) not null'