evt-message_store-postgres 0.1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/database/clear-events-table.sh +40 -0
  3. data/database/extensions.sql +1 -0
  4. data/database/functions/category.sql +10 -0
  5. data/database/functions/stream-version.sql +13 -0
  6. data/database/functions/write-event.sql +55 -0
  7. data/database/indexes/events-category-global-position.sql +1 -0
  8. data/database/indexes/events-category.sql +1 -0
  9. data/database/indexes/events-id.sql +1 -0
  10. data/database/indexes/events-stream-name-position-uniq.sql +1 -0
  11. data/database/indexes/events-stream-name.sql +1 -0
  12. data/database/install.sh +103 -0
  13. data/database/list-events.sh +52 -0
  14. data/database/table/events-table.sql +19 -0
  15. data/database/test/write-event-expected-version.sql +1 -0
  16. data/database/test/write-event.sql +1 -0
  17. data/database/uninstall.sh +60 -0
  18. data/lib/message_store/postgres.rb +24 -0
  19. data/lib/message_store/postgres/controls.rb +6 -0
  20. data/lib/message_store/postgres/controls/category.rb +34 -0
  21. data/lib/message_store/postgres/controls/message_data.rb +7 -0
  22. data/lib/message_store/postgres/controls/put.rb +26 -0
  23. data/lib/message_store/postgres/controls/stream_name.rb +22 -0
  24. data/lib/message_store/postgres/get.rb +91 -0
  25. data/lib/message_store/postgres/get/last.rb +111 -0
  26. data/lib/message_store/postgres/get/last/select_statement.rb +47 -0
  27. data/lib/message_store/postgres/get/select_statement.rb +88 -0
  28. data/lib/message_store/postgres/log.rb +11 -0
  29. data/lib/message_store/postgres/put.rb +145 -0
  30. data/lib/message_store/postgres/read.rb +12 -0
  31. data/lib/message_store/postgres/read/iterator.rb +17 -0
  32. data/lib/message_store/postgres/session.rb +128 -0
  33. data/lib/message_store/postgres/settings.rb +30 -0
  34. data/lib/message_store/postgres/stream_name.rb +52 -0
  35. data/lib/message_store/postgres/write.rb +46 -0
  36. data/scripts/evt-pg-create-db +7 -0
  37. data/scripts/evt-pg-delete-db +7 -0
  38. data/scripts/evt-pg-list-events +7 -0
  39. data/scripts/evt-pg-recreate-db +11 -0
  40. data/scripts/scripts_init.rb +12 -0
  41. metadata +156 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f4b2f2927cc0e063949a067f30ac982f1418e4a
4
+ data.tar.gz: 0e9fb2772b23237a597fc14aa1ff0fde0a8567a3
5
+ SHA512:
6
+ metadata.gz: ad29845afd4883df20ea3ed878d6338ff8cf94f892fa9ae8bd59e8d635b745ddfcc4d31ca6255683850d85f5d19fe87f47e2c0dc8f68802c07903f74c8c343cd
7
+ data.tar.gz: 3631c15fa6864977c03e3e48d1f395a17c1e201c8319247c70984d3f72694b83c882ae121a9e40074766d799b4d1be197326d49ee204c891099adfd216b05496
@@ -0,0 +1,40 @@
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=event_source
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=events
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;"
@@ -0,0 +1 @@
1
+ CREATE EXTENSION IF NOT EXISTS "pgcrypto";
@@ -0,0 +1,10 @@
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;
@@ -0,0 +1,13 @@
1
+ CREATE OR REPLACE FUNCTION stream_version(
2
+ _stream_name varchar
3
+ )
4
+ RETURNS int
5
+ AS $$
6
+ DECLARE
7
+ stream_version int;
8
+ BEGIN
9
+ select max(position) into stream_version from events where stream_name = _stream_name;
10
+
11
+ return stream_version;
12
+ END;
13
+ $$ LANGUAGE plpgsql;
@@ -0,0 +1,55 @@
1
+ CREATE OR REPLACE FUNCTION write_event(
2
+ _id varchar,
3
+ _stream_name varchar,
4
+ _type varchar,
5
+ _data jsonb,
6
+ _metadata jsonb DEFAULT NULL,
7
+ _expected_version int DEFAULT NULL
8
+ )
9
+ RETURNS int
10
+ AS $$
11
+ DECLARE
12
+ event_id uuid;
13
+ stream_version int;
14
+ position int;
15
+ category varchar;
16
+ BEGIN
17
+ event_id = uuid(_id);
18
+
19
+ stream_version := stream_version(_stream_name);
20
+
21
+ if stream_version is null then
22
+ stream_version := -1;
23
+ end if;
24
+
25
+ if _expected_version is not null then
26
+ if _expected_version != stream_version then
27
+ raise exception 'Wrong expected version: % (Stream: %, Stream Version: %)', _expected_version, _stream_name, stream_version;
28
+ end if;
29
+ end if;
30
+
31
+ position := stream_version + 1;
32
+
33
+ insert into "events"
34
+ (
35
+ "id",
36
+ "stream_name",
37
+ "position",
38
+ "type",
39
+ "data",
40
+ "metadata"
41
+ )
42
+ values
43
+ (
44
+ event_id,
45
+ _stream_name,
46
+ position,
47
+ _type,
48
+ _data,
49
+ _metadata
50
+ )
51
+ ;
52
+
53
+ return position;
54
+ END;
55
+ $$ LANGUAGE plpgsql;
@@ -0,0 +1 @@
1
+ CREATE INDEX CONCURRENTLY "events_category_global_position_idx" ON "public"."events" USING btree(category(stream_name) COLLATE "default" "pg_catalog"."text_ops" ASC NULLS LAST, "global_position" "pg_catalog"."int8_ops" ASC NULLS LAST);
@@ -0,0 +1 @@
1
+ CREATE INDEX CONCURRENTLY "events_category_idx" ON "public"."events" USING btree(category(stream_name) COLLATE "default" "pg_catalog"."text_ops" ASC NULLS LAST);
@@ -0,0 +1 @@
1
+ CREATE INDEX CONCURRENTLY "events_id_idx" ON "public"."events" USING btree(id ASC NULLS LAST);
@@ -0,0 +1 @@
1
+ CREATE UNIQUE INDEX CONCURRENTLY "events_stream_name_position_uniq_idx" ON "public"."events" USING btree(stream_name COLLATE "default" "pg_catalog"."text_ops" ASC NULLS LAST, "position" "pg_catalog"."int4_ops" ASC NULLS LAST);
@@ -0,0 +1 @@
1
+ CREATE INDEX CONCURRENTLY "events_stream_name_idx" ON "public"."events" USING btree(stream_name COLLATE "default" "pg_catalog"."text_ops" ASC NULLS LAST);
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ echo
6
+ echo "Installing Database"
7
+ echo "= = ="
8
+ echo
9
+
10
+ default_name=event_source
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 -tAc "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 events table..."
65
+ base=$(script_dir)
66
+ psql $database -f $base/table/events-table.sql
67
+ echo
68
+ }
69
+
70
+ function create-functions {
71
+ base=$(script_dir)
72
+ echo "Creating functions..."
73
+ echo "category function"
74
+ psql $database -f $base/functions/category.sql
75
+ echo "stream_version function"
76
+ psql $database -f $base/functions/stream-version.sql
77
+ echo "write_sql function"
78
+ psql $database -f $base/functions/write-event.sql
79
+ echo
80
+ }
81
+
82
+ function create-indexes {
83
+ base=$(script_dir)
84
+ echo "Creating indexes..."
85
+ echo "events_id_idx"
86
+ psql $database -f $base/indexes/events-id.sql
87
+ echo "events_category_global_position_idx"
88
+ psql $database -f $base/indexes/events-category-global-position.sql
89
+ echo "events_category_idx"
90
+ psql $database -f $base/indexes/events-category.sql
91
+ echo "events_stream_name_idx"
92
+ psql $database -f $base/indexes/events-stream-name.sql
93
+ echo "events_stream_name_position_uniq_idx"
94
+ psql $database -f $base/indexes/events-stream-name-position-uniq.sql
95
+ echo
96
+ }
97
+
98
+ create-user
99
+ create-database
100
+ create-extensions
101
+ create-table
102
+ create-functions
103
+ create-indexes
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ echo
6
+ echo "Listing Events"
7
+ echo "= = ="
8
+ echo
9
+
10
+ default_name=event_source
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=events
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 -c "SELECT * FROM $table"
50
+ else
51
+ psql $database -c "SELECT * FROM $table WHERE stream_name = '$stream_name'"
52
+ fi
@@ -0,0 +1,19 @@
1
+ -- ----------------------------
2
+ -- Table structure for events
3
+ -- ----------------------------
4
+ CREATE TABLE "public"."events" (
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" int4 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 events
18
+ -- ----------------------------
19
+ ALTER TABLE "public"."events" ADD PRIMARY KEY ("global_position") NOT DEFERRABLE INITIALLY IMMEDIATE;
@@ -0,0 +1 @@
1
+ SELECT write_event('someStream-123', 'SomeType', '{"someField":"some value"}', '{"someMetadataField":"some metadata value"}', 1);
@@ -0,0 +1 @@
1
+ SELECT write_event('someStream-123', 'SomeType', '{"someField":"some value"}', '{"someMetadataField":"some metadata value"}');
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ echo
6
+ echo "Uninstalling Database"
7
+ echo "= = ="
8
+ echo
9
+
10
+ default_name=event_source
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 -tAc "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 -tAc "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
@@ -0,0 +1,24 @@
1
+ require 'pg'
2
+
3
+ require 'message_store'
4
+
5
+ require 'log'
6
+ require 'telemetry'
7
+ require 'settings'; Settings.activate
8
+
9
+ require 'message_store/postgres/log'
10
+
11
+ require 'message_store/postgres/settings'
12
+ require 'message_store/postgres/session'
13
+
14
+ require 'message_store/postgres/stream_name'
15
+
16
+ require 'message_store/postgres/put'
17
+ require 'message_store/postgres/write'
18
+
19
+ require 'message_store/postgres/get/select_statement'
20
+ require 'message_store/postgres/get'
21
+ require 'message_store/postgres/get/last/select_statement'
22
+ require 'message_store/postgres/get/last'
23
+ require 'message_store/postgres/read/iterator'
24
+ require 'message_store/postgres/read'
@@ -0,0 +1,6 @@
1
+ require 'message_store/controls'
2
+
3
+ require 'message_store/postgres/controls/category'
4
+ require 'message_store/postgres/controls/stream_name'
5
+ require 'message_store/postgres/controls/message_data'
6
+ require 'message_store/postgres/controls/put'
@@ -0,0 +1,34 @@
1
+ # module MessageStore
2
+ # module Postgres
3
+ # module Controls
4
+ # Category = MessageStore::Controls::Category
5
+ # end
6
+ # end
7
+ # end
8
+
9
+
10
+ module MessageStore
11
+ module Postgres
12
+ module Controls
13
+ module Category
14
+ def self.example(category: nil, randomize_category: nil)
15
+ if randomize_category.nil?
16
+ if !category.nil?
17
+ randomize_category = false
18
+ end
19
+ end
20
+
21
+ randomize_category = true if randomize_category.nil?
22
+
23
+ category ||= 'test'
24
+
25
+ if randomize_category
26
+ category = "#{category}#{SecureRandom.hex(16)}XX"
27
+ end
28
+
29
+ category
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end