Sha256: fbc1c70b1cd3bc96655f57df934a79ea60f871e503980048cf9056e5ed30dbd4

Contents?: true

Size: 1018 Bytes

Versions: 5

Compression:

Stored size: 1018 Bytes

Contents

CREATE OR REPLACE FUNCTION write_message(
  _id varchar,
  _stream_name varchar,
  _type varchar,
  _data jsonb,
  _metadata jsonb DEFAULT NULL,
  _expected_version int DEFAULT NULL
)
RETURNS int
AS $$
DECLARE
  message_id uuid;
  stream_version int;
  position int;
  category varchar;
BEGIN
  message_id = uuid(_id);

  stream_version := stream_version(_stream_name);

  if stream_version is null then
    stream_version := -1;
  end if;

  if _expected_version is not null then
    if _expected_version != stream_version then
      raise exception 'Wrong expected version: % (Stream: %, Stream Version: %)', _expected_version, _stream_name, stream_version;
    end if;
  end if;

  position := stream_version + 1;

  insert into "messages"
    (
      "id",
      "stream_name",
      "position",
      "type",
      "data",
      "metadata"
    )
  values
    (
      message_id,
      _stream_name,
      position,
      _type,
      _data,
      _metadata
    )
  ;

  return position;
END;
$$ LANGUAGE plpgsql;

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
evt-message_store-postgres-0.3.0.3 database/functions/write-message.sql
evt-message_store-postgres-0.3.0.2 database/functions/write-message.sql
evt-message_store-postgres-0.3.0.1 database/functions/write-message.sql
evt-message_store-postgres-0.3.0.0 database/functions/write-message.sql
evt-message_store-postgres-0.2.0.0 database/functions/write-message.sql