evt-message_store-postgres 0.9.0.0 → 0.10.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2fe7030703e9d162fe2ffe3007ef76f4fffd8a834dda1e95118f2ce813e735e
4
- data.tar.gz: 00c25603ad17a4faa1e7b09e23a0e002049dae8931a8e34e789e7a2f3a4d48bb
3
+ metadata.gz: 1713903f546fd100c97c8431062fc70cd7e77d76b17b4a47c61e6222d327014c
4
+ data.tar.gz: 3752aa65bb75968be8dcc0f79ea18d38751eaebe92c9c36231cc40f096ee465a
5
5
  SHA512:
6
- metadata.gz: cdd24c589e7c7fae1d55d0d89df6753936a8e2a7da8f2d8af4f4e352c03c4aaefc5ae7bdb2f2c4aeb989035f8fa6009867bb141f1dabe194f7fb3a80ccaae16d
7
- data.tar.gz: 31faa413a341b5556c15bc0a895a57ebcc2f22a2368ab81feda00d08ef1158f728a387e7dba7b761e88052ac0cdfed5e1ed7777a5562485c1ba938178d67e478
6
+ metadata.gz: 164bde3d9e587fb2384501e630d1d3020ef5d9dc5174d748e455f27b9955b1c455c83799c02677b038932fc94a09b5526077f521425328f9f06a73c956f2c42b
7
+ data.tar.gz: acc2b7385136a1d1e9d401588eab91e8caa8df02bc44cfe388407c1fd902862e49c970fa36d4b061295ee98ced1261997b0af8840edde9c0fd499b6bcf958892
@@ -16,9 +16,7 @@ require 'message_store/postgres/stream_name'
16
16
  require 'message_store/postgres/put'
17
17
  require 'message_store/postgres/write'
18
18
 
19
- require 'message_store/postgres/get/select_statement'
20
19
  require 'message_store/postgres/get'
21
- require 'message_store/postgres/get/last/select_statement'
22
20
  require 'message_store/postgres/get/last'
23
21
  require 'message_store/postgres/read/iterator'
24
22
  require 'message_store/postgres/read'
@@ -10,15 +10,16 @@ module MessageStore
10
10
 
11
11
  message ||= MessageData::Write.example
12
12
 
13
+ position = nil
13
14
  instances.times do
14
- MessageStore::Postgres::Put.(message, stream_name)
15
+ position = MessageStore::Postgres::Put.(message, stream_name)
15
16
 
16
17
  unless message_specified
17
18
  message.id = MessageData::Write.id
18
19
  end
19
20
  end
20
21
 
21
- stream_name
22
+ [stream_name, position]
22
23
  end
23
24
  end
24
25
  end
@@ -3,10 +3,14 @@ module MessageStore
3
3
  class Get
4
4
  include MessageStore::Get
5
5
 
6
- initializer :batch_size, :condition
7
-
8
6
  dependency :session, Session
9
7
 
8
+ initializer na(:batch_size), :condition
9
+
10
+ def batch_size
11
+ @batch_size ||= Defaults.batch_size
12
+ end
13
+
10
14
  def self.build(batch_size: nil, session: nil, condition: nil)
11
15
  new(batch_size, condition).tap do |instance|
12
16
  instance.configure(session: session)
@@ -31,32 +35,63 @@ module MessageStore
31
35
  def call(stream_name, position: nil)
32
36
  logger.trace { "Getting message data (Position: #{position.inspect}, Stream Name: #{stream_name}, Batch Size: #{batch_size.inspect})" }
33
37
 
34
- records = get_records(stream_name, position)
38
+ position ||= Defaults.position
39
+
40
+ result = get_result(stream_name, position)
35
41
 
36
- messages = convert(records)
42
+ message_data = convert(result)
37
43
 
38
- logger.info { "Finished getting message data (Count: #{messages.length}, Position: #{position.inspect}, Stream Name: #{stream_name}, Batch Size: #{batch_size.inspect})" }
39
- logger.info(tags: [:data, :message_data]) { messages.pretty_inspect }
44
+ logger.info { "Finished getting message data (Count: #{message_data.length}, Position: #{position.inspect}, Stream Name: #{stream_name}, Batch Size: #{batch_size.inspect})" }
45
+ logger.info(tags: [:data, :message_data]) { message_data.pretty_inspect }
40
46
 
41
- messages
47
+ message_data
42
48
  end
43
49
 
44
- def get_records(stream_name, position)
45
- logger.trace { "Getting records (Stream: #{stream_name}, Position: #{position.inspect}, Batch Size: #{batch_size.inspect}, Condition: #{condition || '(none)'})" }
50
+ def get_result(stream_name, position)
51
+ logger.trace { "Getting result (Stream: #{stream_name}, Position: #{position.inspect}, Batch Size: #{batch_size.inspect}, Condition: #{condition || '(none)'})" }
52
+
53
+ sql_command = self.class.sql_command(stream_name, position, batch_size, condition)
46
54
 
47
- select_statement = SelectStatement.build(stream_name, position: position, batch_size: batch_size, condition: condition)
55
+ cond = self.class.constrain_condition(condition)
48
56
 
49
- records = session.execute(select_statement.sql)
57
+ params = [
58
+ stream_name,
59
+ position,
60
+ batch_size,
61
+ cond
62
+ ]
50
63
 
51
- logger.debug { "Finished getting records (Count: #{records.ntuples}, Stream: #{stream_name}, Position: #{position.inspect}, Batch Size: #{batch_size.inspect}, Condition: #{condition || '(none)'})" }
64
+ result = session.execute(sql_command, params)
52
65
 
53
- records
66
+ logger.debug { "Finished getting result (Count: #{result.ntuples}, Stream: #{stream_name}, Position: #{position.inspect}, Batch Size: #{batch_size.inspect}, Condition: #{condition || '(none)'})" }
67
+
68
+ result
54
69
  end
55
70
 
56
- def convert(records)
57
- logger.trace { "Converting records to message data (Records Count: #{records.ntuples})" }
71
+ def self.constrain_condition(condition)
72
+ return nil if condition.nil?
73
+
74
+ "(#{condition})"
75
+ end
58
76
 
59
- messages = records.map do |record|
77
+ def self.sql_command(stream_name, position, batch_size, condition)
78
+ parameters = '$1::varchar, $2::bigint, $3::bigint, $4::varchar'
79
+
80
+ if category_stream?(stream_name)
81
+ return "SELECT * FROM get_category_messages(#{parameters});"
82
+ else
83
+ return "SELECT * FROM get_stream_messages(#{parameters});"
84
+ end
85
+ end
86
+
87
+ def self.category_stream?(stream_name)
88
+ StreamName.category?(stream_name)
89
+ end
90
+
91
+ def convert(result)
92
+ logger.trace { "Converting result to message data (Result Count: #{result.ntuples})" }
93
+
94
+ message_data = result.map do |record|
60
95
  record['data'] = Deserialize.data(record['data'])
61
96
  record['metadata'] = Deserialize.metadata(record['metadata'])
62
97
  record['time'] = Time.utc_coerced(record['time'])
@@ -64,9 +99,9 @@ module MessageStore
64
99
  MessageData::Read.build record
65
100
  end
66
101
 
67
- logger.debug { "Converted records to message data (Message Data Count: #{messages.length})" }
102
+ logger.debug { "Converted result to message data (Message Data Count: #{message_data.length})" }
68
103
 
69
- messages
104
+ message_data
70
105
  end
71
106
 
72
107
  module Deserialize
@@ -86,6 +121,16 @@ module MessageStore
86
121
  Clock::UTC.coerce(local_time)
87
122
  end
88
123
  end
124
+
125
+ module Defaults
126
+ def self.position
127
+ 0
128
+ end
129
+
130
+ def self.batch_size
131
+ 1000
132
+ end
133
+ end
89
134
  end
90
135
  end
91
136
  end
@@ -13,11 +13,11 @@ module MessageStore
13
13
  def call(stream_name)
14
14
  logger.trace { "Getting last message data (Stream Name: #{stream_name})" }
15
15
 
16
- record = get_record(stream_name)
16
+ result = get_result(stream_name)
17
17
 
18
- return nil if record.nil?
18
+ return nil if result.nil?
19
19
 
20
- message_data = convert(record)
20
+ message_data = convert(result[0])
21
21
 
22
22
  logger.info { "Finished getting message data (Stream Name: #{stream_name})" }
23
23
  logger.info(tags: [:data, :message_data]) { message_data.pretty_inspect }
@@ -25,18 +25,28 @@ module MessageStore
25
25
  message_data
26
26
  end
27
27
 
28
- def get_record(stream_name)
28
+ def get_result(stream_name)
29
29
  logger.trace { "Getting last record (Stream: #{stream_name})" }
30
30
 
31
- select_statement = SelectStatement.build(stream_name)
31
+ sql_command = self.class.sql_command(stream_name)
32
32
 
33
- records = session.execute(select_statement.sql)
33
+ params = [
34
+ stream_name
35
+ ]
34
36
 
35
- logger.debug { "Finished getting record (Stream: #{stream_name})" }
37
+ result = session.execute(sql_command, params)
36
38
 
37
- return nil if records.ntuples == 0
39
+ logger.debug { "Finished getting result (Count: #{result.ntuples}, Stream: #{stream_name}" }
38
40
 
39
- records[0]
41
+ return nil if result.ntuples == 0
42
+
43
+ result
44
+ end
45
+
46
+ def self.sql_command(stream_name)
47
+ parameters = '$1::varchar'
48
+
49
+ "SELECT * FROM get_last_message(#{parameters});"
40
50
  end
41
51
 
42
52
  def convert(record)
@@ -57,6 +57,7 @@ module MessageStore
57
57
  return id, type, data, metadata
58
58
  end
59
59
 
60
+ ## TODO Rename to put_messages
60
61
  def insert_message(id, stream_name, type, data, metadata, expected_version)
61
62
  serialized_data = serialized_data(data)
62
63
  serialized_metadata = serialized_metadata(metadata)
@@ -33,7 +33,7 @@ module MessageStore
33
33
 
34
34
  if connected?
35
35
  logger.debug { "Already connected. A new connection will not be built." }
36
- return
36
+ return connection
37
37
  end
38
38
 
39
39
  logger.debug { "Not connected. A new connection will be built." }
@@ -80,9 +80,9 @@ module MessageStore
80
80
  settings
81
81
  end
82
82
 
83
- def execute(statement, params=nil)
84
- logger.trace { "Executing statement" }
85
- logger.trace(tag: :data) { statement }
83
+ def execute(sql_command, params=nil)
84
+ logger.trace { "Executing SQL command" }
85
+ logger.trace(tag: :data) { sql_command }
86
86
  logger.trace(tag: :data) { params.pretty_inspect }
87
87
 
88
88
  unless connected?
@@ -90,12 +90,12 @@ module MessageStore
90
90
  end
91
91
 
92
92
  if params.nil?
93
- connection.exec(statement).tap do
94
- logger.debug { "Executed statement" }
93
+ connection.exec(sql_command).tap do
94
+ logger.debug { "Executed SQL command (no params)" }
95
95
  end
96
96
  else
97
- connection.exec_params(statement, params).tap do
98
- logger.debug { "Executed statement with params" }
97
+ connection.exec_params(sql_command, params).tap do
98
+ logger.debug { "Executed SQL command with params" }
99
99
  end
100
100
  end
101
101
  end
@@ -117,7 +117,7 @@ module MessageStore
117
117
  s = settings.dup
118
118
 
119
119
  if s.has_key?(:password)
120
- s[:password] = '(hidden)'
120
+ s[:password] = '*' * 8
121
121
  end
122
122
 
123
123
  s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt-message_store-postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.0
4
+ version: 0.10.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Eventide Project
8
8
  autorequire:
9
9
  bindir: scripts
10
10
  cert_chain: []
11
- date: 2018-06-05 00:00:00.000000000 Z
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: evt-message_store
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: evt-message_store-postgres-database
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: evt-log
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -110,31 +124,10 @@ dependencies:
110
124
  version: '0'
111
125
  description: " "
112
126
  email: opensource@eventide-project.org
113
- executables:
114
- - evt-pg-print-messages
115
- - evt-pg-delete-db
116
- - evt-pg-create-db
117
- - evt-pg-recreate-db
127
+ executables: []
118
128
  extensions: []
119
129
  extra_rdoc_files: []
120
130
  files:
121
- - database/clear-events-table.sh
122
- - database/extensions.sql
123
- - database/functions/category.sql
124
- - database/functions/get-messages.sql
125
- - database/functions/hash-64.sql
126
- - database/functions/stream-version.sql
127
- - database/functions/write-message.sql
128
- - database/indexes/messages-category-global-position.sql
129
- - database/indexes/messages-id.sql
130
- - database/indexes/messages-stream-name-position-uniq.sql
131
- - database/install.sh
132
- - database/print-messages.sh
133
- - database/table/messages-table.sql
134
- - database/test/hash-64.sql
135
- - database/test/write-message-expected-version.sql
136
- - database/test/write-message.sql
137
- - database/uninstall.sh
138
131
  - lib/message_store/postgres.rb
139
132
  - lib/message_store/postgres/controls.rb
140
133
  - lib/message_store/postgres/controls/category.rb
@@ -144,8 +137,6 @@ files:
144
137
  - lib/message_store/postgres/controls/stream_name.rb
145
138
  - lib/message_store/postgres/get.rb
146
139
  - lib/message_store/postgres/get/last.rb
147
- - lib/message_store/postgres/get/last/select_statement.rb
148
- - lib/message_store/postgres/get/select_statement.rb
149
140
  - lib/message_store/postgres/log.rb
150
141
  - lib/message_store/postgres/put.rb
151
142
  - lib/message_store/postgres/read.rb
@@ -154,10 +145,6 @@ files:
154
145
  - lib/message_store/postgres/settings.rb
155
146
  - lib/message_store/postgres/stream_name.rb
156
147
  - lib/message_store/postgres/write.rb
157
- - scripts/evt-pg-create-db
158
- - scripts/evt-pg-delete-db
159
- - scripts/evt-pg-print-messages
160
- - scripts/evt-pg-recreate-db
161
148
  homepage: https://github.com/eventide-project/message-store-postgres
162
149
  licenses:
163
150
  - MIT
@@ -1,40 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- set -e
4
-
5
- echo
6
- echo "Clearing Events Table"
7
- echo "= = ="
8
- echo
9
-
10
- default_name=message_store
11
-
12
- if [ -z ${DATABASE_USER+x} ]; then
13
- echo "(DATABASE_USER is not set)"
14
- user=$default_name
15
- else
16
- user=$DATABASE_USER
17
- fi
18
- echo "Database user is: $user"
19
-
20
- if [ -z ${DATABASE_NAME+x} ]; then
21
- echo "(DATABASE_NAME is not set)"
22
- database=$default_name
23
- else
24
- database=$DATABASE_NAME
25
- fi
26
- echo "Database name is: $database"
27
-
28
- default_table_name=messages
29
-
30
- if [ -z ${TABLE_NAME+x} ]; then
31
- echo "(TABLE_NAME is not set)"
32
- table=$default_table_name
33
- else
34
- table=$TABLE_NAME
35
- fi
36
- echo "Table name is: $table"
37
-
38
- echo
39
-
40
- psql $database -c "TRUNCATE $table RESTART IDENTITY;"
@@ -1 +0,0 @@
1
- CREATE EXTENSION IF NOT EXISTS "pgcrypto";
@@ -1,10 +0,0 @@
1
- CREATE OR REPLACE FUNCTION category(
2
- _stream_name varchar
3
- )
4
- RETURNS varchar
5
- AS $$
6
- BEGIN
7
- return split_part(_stream_name, '-', 1);
8
- END;
9
- $$ LANGUAGE plpgsql
10
- IMMUTABLE;
@@ -1,63 +0,0 @@
1
- CREATE OR REPLACE FUNCTION get_messages(
2
- _stream_name varchar,
3
- _positon bigint,
4
- _batch_size bigint
5
-
6
-
7
- _type varchar,
8
- _data jsonb,
9
- _metadata jsonb DEFAULT NULL,
10
- _expected_version bigint DEFAULT NULL
11
- )
12
- RETURNS bigint
13
- AS $$
14
- DECLARE
15
- message_id uuid;
16
- stream_version bigint;
17
- position bigint;
18
- category varchar;
19
- stream_name_hash bigint;
20
- BEGIN
21
- message_id = uuid(_id);
22
-
23
- stream_name_hash = hash_64(_stream_name);
24
- PERFORM pg_advisory_xact_lock(stream_name_hash);
25
-
26
- stream_version := stream_version(_stream_name);
27
-
28
- if stream_version is null then
29
- stream_version := -1;
30
- end if;
31
-
32
- if _expected_version is not null then
33
- if _expected_version != stream_version then
34
- raise exception 'Wrong expected version: % (Stream: %, Stream Version: %)', _expected_version, _stream_name, stream_version;
35
- end if;
36
- end if;
37
-
38
- position := stream_version + 1;
39
-
40
- insert into "messages"
41
- (
42
- "id",
43
- "stream_name",
44
- "position",
45
- "type",
46
- "data",
47
- "metadata"
48
- )
49
- values
50
- (
51
- message_id,
52
- _stream_name,
53
- position,
54
- _type,
55
- _data,
56
- _metadata
57
- )
58
- ;
59
-
60
- return position;
61
- END;
62
- $$ LANGUAGE plpgsql
63
- VOLATILE;
@@ -1,13 +0,0 @@
1
- CREATE OR REPLACE FUNCTION hash_64(
2
- _stream_name varchar
3
- )
4
- RETURNS bigint
5
- AS $$
6
- DECLARE
7
- hash bigint;
8
- BEGIN
9
- select left('x' || md5(_stream_name), 16)::bit(64)::bigint into hash;
10
- return hash;
11
- END;
12
- $$ LANGUAGE plpgsql
13
- IMMUTABLE;
@@ -1,14 +0,0 @@
1
- CREATE OR REPLACE FUNCTION stream_version(
2
- _stream_name varchar
3
- )
4
- RETURNS bigint
5
- AS $$
6
- DECLARE
7
- stream_version bigint;
8
- BEGIN
9
- select max(position) into stream_version from messages where stream_name = _stream_name;
10
-
11
- return stream_version;
12
- END;
13
- $$ LANGUAGE plpgsql
14
- VOLATILE;
@@ -1,60 +0,0 @@
1
- CREATE OR REPLACE FUNCTION write_message(
2
- _id varchar,
3
- _stream_name varchar,
4
- _type varchar,
5
- _data jsonb,
6
- _metadata jsonb DEFAULT NULL,
7
- _expected_version bigint DEFAULT NULL
8
- )
9
- RETURNS bigint
10
- AS $$
11
- DECLARE
12
- message_id uuid;
13
- stream_version bigint;
14
- position bigint;
15
- category varchar;
16
- stream_name_hash bigint;
17
- BEGIN
18
- message_id = uuid(_id);
19
-
20
- stream_name_hash = hash_64(_stream_name);
21
- PERFORM pg_advisory_xact_lock(stream_name_hash);
22
-
23
- stream_version := stream_version(_stream_name);
24
-
25
- if stream_version is null then
26
- stream_version := -1;
27
- end if;
28
-
29
- if _expected_version is not null then
30
- if _expected_version != stream_version then
31
- raise exception 'Wrong expected version: % (Stream: %, Stream Version: %)', _expected_version, _stream_name, stream_version;
32
- end if;
33
- end if;
34
-
35
- position := stream_version + 1;
36
-
37
- insert into "messages"
38
- (
39
- "id",
40
- "stream_name",
41
- "position",
42
- "type",
43
- "data",
44
- "metadata"
45
- )
46
- values
47
- (
48
- message_id,
49
- _stream_name,
50
- position,
51
- _type,
52
- _data,
53
- _metadata
54
- )
55
- ;
56
-
57
- return position;
58
- END;
59
- $$ LANGUAGE plpgsql
60
- VOLATILE;
@@ -1 +0,0 @@
1
- CREATE INDEX CONCURRENTLY "messages_category_global_position_idx" ON "public"."messages" USING btree(category(stream_name) COLLATE "default" "pg_catalog"."text_ops" ASC NULLS LAST, "global_position" "pg_catalog"."int8_ops" ASC NULLS LAST);
@@ -1 +0,0 @@
1
- CREATE INDEX CONCURRENTLY "messages_id_idx" ON "public"."messages" USING btree(id ASC NULLS LAST);
@@ -1 +0,0 @@
1
- CREATE UNIQUE INDEX CONCURRENTLY "messages_stream_name_position_uniq_idx" ON "public"."messages" USING btree(stream_name COLLATE "default" "pg_catalog"."text_ops" ASC NULLS LAST, "position" "pg_catalog"."int8_ops" ASC NULLS LAST);
data/database/install.sh DELETED
@@ -1,106 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- set -e
4
-
5
- echo
6
- echo "Installing Database"
7
- echo "= = ="
8
- echo
9
-
10
- default_name=message_store
11
-
12
- if [ -z ${DATABASE_USER+x} ]; then
13
- echo "(DATABASE_USER is not set. Default will be used.)"
14
- user=$default_name
15
- else
16
- user=$DATABASE_USER
17
- fi
18
-
19
- if [ -z ${DATABASE_NAME+x} ]; then
20
- echo "(DATABASE_NAME is not set. Default will be used.)"
21
- database=$default_name
22
- else
23
- database=$DATABASE_NAME
24
- fi
25
-
26
- echo
27
-
28
- function create-user {
29
- echo "Database user is: $user"
30
-
31
- user_exists=`psql postgres -qtAXc "SELECT 1 FROM pg_roles WHERE rolname='$user'"`
32
-
33
- if [ "$user_exists" = "1" ]; then
34
- echo "Database user \"$user\" was previously created. Not creating again."
35
- else
36
- echo "Database user \"$user\" has not yet been created"
37
- echo "Creating database user \"$user\"..."
38
- createuser -s $user
39
- fi
40
-
41
- echo
42
- }
43
-
44
- function create-database {
45
- echo "Database name is: $database"
46
- echo "Creating database \"$database\"..."
47
- createdb $database
48
- echo
49
- }
50
-
51
- function script_dir {
52
- val="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
53
- echo "$val"
54
- }
55
-
56
- function create-extensions {
57
- echo "Creating extensions..."
58
- base=$(script_dir)
59
- psql $database -f $base/extensions.sql
60
- echo
61
- }
62
-
63
- function create-table {
64
- echo "Creating messages table..."
65
- base=$(script_dir)
66
- psql $database -f $base/table/messages-table.sql
67
- echo
68
- }
69
-
70
- function create-functions {
71
- base=$(script_dir)
72
- echo "Creating functions..."
73
-
74
- echo "hash_64 function"
75
- psql $database -f $base/functions/hash-64.sql
76
-
77
- echo "category function"
78
- psql $database -f $base/functions/category.sql
79
-
80
- echo "stream_version function"
81
- psql $database -f $base/functions/stream-version.sql
82
-
83
- echo "write_sql function"
84
- psql $database -f $base/functions/write-message.sql
85
-
86
- echo
87
- }
88
-
89
- function create-indexes {
90
- base=$(script_dir)
91
- echo "Creating indexes..."
92
- echo "messages_id_idx"
93
- psql $database -f $base/indexes/messages-id.sql
94
- echo "messages_category_global_position_idx"
95
- psql $database -f $base/indexes/messages-category-global-position.sql
96
- echo "messages_stream_name_position_uniq_idx"
97
- psql $database -f $base/indexes/messages-stream-name-position-uniq.sql
98
- echo
99
- }
100
-
101
- create-user
102
- create-database
103
- create-extensions
104
- create-table
105
- create-functions
106
- create-indexes
@@ -1,52 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- set -e
4
-
5
- echo
6
- echo "Printing Messages"
7
- echo "= = ="
8
- echo
9
-
10
- default_name=message_store
11
-
12
- if [ -z ${DATABASE_USER+x} ]; then
13
- echo "(DATABASE_USER is not set)"
14
- user=$default_name
15
- else
16
- user=$DATABASE_USER
17
- fi
18
- echo "Database user is: $user"
19
-
20
- if [ -z ${DATABASE_NAME+x} ]; then
21
- echo "(DATABASE_NAME is not set)"
22
- database=$default_name
23
- else
24
- database=$DATABASE_NAME
25
- fi
26
- echo "Database name is: $database"
27
-
28
- default_table_name=messages
29
-
30
- if [ -z ${TABLE_NAME+x} ]; then
31
- echo "(TABLE_NAME is not set)"
32
- table=$default_table_name
33
- else
34
- table=$TABLE_NAME
35
- fi
36
- echo "Table name is: $table"
37
-
38
- if [ -z ${STREAM_NAME+x} ]; then
39
- echo "(STREAM_NAME is not set)"
40
- stream_name=''
41
- else
42
- stream_name=$STREAM_NAME
43
- echo "Stream name is: $STREAM_NAME"
44
- fi
45
-
46
- echo
47
-
48
- if [ -z $stream_name ]; then
49
- psql $database -x -c "SELECT * FROM $table"
50
- else
51
- psql $database -x -c "SELECT * FROM $table WHERE stream_name = '$stream_name'"
52
- fi
@@ -1,19 +0,0 @@
1
- -- ----------------------------
2
- -- Table structure for messages
3
- -- ----------------------------
4
- CREATE TABLE "public"."messages" (
5
- "id" UUID NOT NULL DEFAULT gen_random_uuid(),
6
- "stream_name" varchar(255) NOT NULL COLLATE "default",
7
- "type" varchar(255) NOT NULL COLLATE "default",
8
- "position" bigint NOT NULL,
9
- "global_position" bigserial NOT NULL ,
10
- "data" jsonb,
11
- "metadata" jsonb,
12
- "time" TIMESTAMP WITHOUT TIME ZONE DEFAULT (now() AT TIME ZONE 'utc') NOT NULL
13
- )
14
- WITH (OIDS=FALSE);
15
-
16
- -- ----------------------------
17
- -- Primary key structure for table messages
18
- -- ----------------------------
19
- ALTER TABLE "public"."messages" ADD PRIMARY KEY ("global_position") NOT DEFERRABLE INITIALLY IMMEDIATE;
@@ -1 +0,0 @@
1
- SELECT hash_64('someStream-123');
@@ -1 +0,0 @@
1
- SELECT write_message(gen_random_uuid()::varchar, 'testWriteIsolation'::varchar, 'SomeType'::varchar, '{"attribute": "79a59e513f9182d5754abdf9b90e2179"}'::jsonb, '{"metaAttribute": "6b3ae03d713a51a445290f3fc4b5223a"}'::jsonb, 1::bigint);
@@ -1 +0,0 @@
1
- SELECT write_message(gen_random_uuid()::varchar, 'testWriteIsolation'::varchar, 'SomeType'::varchar, '{"attribute": "79a59e513f9182d5754abdf9b90e2179"}'::jsonb, '{"metaAttribute": "6b3ae03d713a51a445290f3fc4b5223a"}'::jsonb);
@@ -1,60 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- set -e
4
-
5
- echo
6
- echo "Uninstalling Database"
7
- echo "= = ="
8
- echo
9
-
10
- default_name=message_store
11
-
12
- if [ -z ${DATABASE_USER+x} ]; then
13
- echo "(DATABASE_USER is not set. Default will be used.)"
14
- user=$default_name
15
- else
16
- user=$DATABASE_USER
17
- fi
18
-
19
- if [ -z ${DATABASE_NAME+x} ]; then
20
- echo "(DATABASE_NAME is not set. Default will be used.)"
21
- database=$default_name
22
- else
23
- database=$DATABASE_NAME
24
- fi
25
-
26
- echo
27
-
28
- function delete-user {
29
- echo "Database user is: $user"
30
-
31
- user_exists=`psql postgres -qtAXc "SELECT 1 FROM pg_roles WHERE rolname='$user'"`
32
-
33
- if [ "$user_exists" = "1" ]; then
34
- echo "Deleting database user \"$user\"..."
35
- dropuser $user
36
- else
37
- echo "Database user \"$user\" does not exist. Not deleting."
38
- fi
39
-
40
- echo
41
- }
42
-
43
- function delete-database {
44
- echo "Database name is: $database"
45
-
46
- database_exists=`psql postgres -qtAXc "SELECT 1 FROM pg_database WHERE datname='$database'"`
47
-
48
- if [ "$database_exists" = "1" ]; then
49
- echo "Deleting database \"$database\"..."
50
- dropdb $database
51
- else
52
- echo "Database \"$database\" does not exist. Not deleting."
53
- fi
54
-
55
- echo
56
- }
57
-
58
-
59
- delete-database
60
- delete-user
@@ -1,47 +0,0 @@
1
- module MessageStore
2
- module Postgres
3
- class Get
4
- class Last
5
- class SelectStatement
6
- include Log::Dependency
7
-
8
- initializer :stream_name
9
-
10
- def self.build(stream_name)
11
- new(stream_name)
12
- end
13
-
14
- def sql
15
- logger.trace(tag: :sql) { "Composing select statement (Stream: #{stream_name})" }
16
-
17
- statement = <<-SQL
18
- SELECT
19
- id::varchar,
20
- stream_name::varchar,
21
- position::bigint,
22
- type::varchar,
23
- global_position::bigint,
24
- data::varchar,
25
- metadata::varchar,
26
- time::timestamp
27
- FROM
28
- messages
29
- WHERE
30
- stream_name = '#{stream_name}'
31
- ORDER BY
32
- position DESC
33
- LIMIT
34
- 1
35
- ;
36
- SQL
37
-
38
- logger.debug(tag: :sql) { "Composed select statement (Stream: #{stream_name})" }
39
- logger.debug(tags: [:data, :sql]) { "Statement: #{statement}" }
40
-
41
- statement
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,102 +0,0 @@
1
- module MessageStore
2
- module Postgres
3
- class Get
4
- class SelectStatement
5
- include Log::Dependency
6
-
7
- initializer :stream_name, na(:position), na(:batch_size), :condition
8
-
9
- def position
10
- @position ||= Defaults.position
11
- end
12
-
13
- def batch_size
14
- @batch_size ||= Defaults.batch_size
15
- end
16
-
17
- def stream_type_list
18
- @stream_type_list ||= StreamName.get_type_list(stream_name)
19
- end
20
-
21
- def category_stream?
22
- is_category_stream ||= StreamName.category?(stream_name)
23
- end
24
-
25
- def self.build(stream_name, position: nil, batch_size: nil, condition: nil)
26
- new(stream_name, position, batch_size, condition)
27
- end
28
-
29
- def sql
30
- logger.trace(tag: :sql) { "Composing select statement (Stream: #{stream_name}, Category: #{category_stream?}, Types: #{stream_type_list.inspect}, Position: #{position}, Batch Size: #{batch_size}, Condition: #{condition || '(none)'})" }
31
-
32
- formatted_where_clause = where_clause.each_line.to_a.join(" ")
33
-
34
- statement = <<~SQL
35
- SELECT
36
- id::varchar,
37
- stream_name::varchar,
38
- position::bigint,
39
- type::varchar,
40
- global_position::bigint,
41
- data::varchar,
42
- metadata::varchar,
43
- time::timestamp
44
- FROM
45
- messages
46
- WHERE
47
- #{formatted_where_clause}
48
- ORDER BY
49
- #{position_field} ASC
50
- LIMIT
51
- #{batch_size}
52
- ;
53
- SQL
54
-
55
- logger.debug(tag: :sql) { "Composed select statement (Stream: #{stream_name}, Category: #{category_stream?}, Types: #{stream_type_list.inspect}, Position: #{position}, Batch Size: #{batch_size}, Condition: #{condition || '(none)'})" }
56
- logger.debug(tags: [:data, :sql]) { "Statement: #{statement}" }
57
-
58
- statement
59
- end
60
-
61
- def where_clause
62
- clause = <<~SQL.chomp
63
- #{discriminator_field} = '#{stream_name}' AND
64
- #{position_field} >= #{position}
65
- SQL
66
-
67
- unless condition.nil?
68
- clause << " AND\n(#{condition})"
69
- end
70
-
71
- clause
72
- end
73
-
74
- def discriminator_field
75
- unless category_stream?
76
- 'stream_name'
77
- else
78
- 'category(stream_name)'
79
- end
80
- end
81
-
82
- def position_field
83
- unless category_stream?
84
- 'position'
85
- else
86
- 'global_position'
87
- end
88
- end
89
-
90
- module Defaults
91
- def self.position
92
- 0
93
- end
94
-
95
- def self.batch_size
96
- 1000
97
- end
98
- end
99
- end
100
- end
101
- end
102
- end
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- root = File.expand_path '../database', __dir__
4
- script_filename = 'install.sh'
5
- script_filepath = File.join root, script_filename
6
-
7
- system script_filepath
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- root = File.expand_path '../database', __dir__
4
- script_filename = 'uninstall.sh'
5
- script_filepath = File.join root, script_filename
6
-
7
- system script_filepath
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- root = File.expand_path '../database', __dir__
4
- script_filename = 'print-messages.sh'
5
- script_filepath = File.join root, script_filename
6
-
7
- system script_filepath
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- root = File.expand_path '../database', __dir__
4
-
5
- script_filename = 'uninstall.sh'
6
- script_filepath = File.join root, script_filename
7
- system script_filepath
8
-
9
- script_filename = 'install.sh'
10
- script_filepath = File.join root, script_filename
11
- system script_filepath