message-db 0.0.0 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/database/VERSION.txt +1 -0
  3. data/database/benchmark.sh +62 -0
  4. data/database/benchmark_get.sql +24 -0
  5. data/database/benchmark_write.sql +24 -0
  6. data/database/clear-messages.sh +26 -0
  7. data/database/extensions/pgcrypto.sql +1 -0
  8. data/database/functions/acquire-lock.sql +24 -0
  9. data/database/functions/cardinal-id.sql +18 -0
  10. data/database/functions/category.sql +10 -0
  11. data/database/functions/get-category-messages.sql +129 -0
  12. data/database/functions/get-last-stream-message.sql +37 -0
  13. data/database/functions/get-stream-messages.sql +71 -0
  14. data/database/functions/hash-64.sql +13 -0
  15. data/database/functions/id.sql +18 -0
  16. data/database/functions/is-category.sql +14 -0
  17. data/database/functions/message-store-version.sql +8 -0
  18. data/database/functions/stream-version.sql +19 -0
  19. data/database/functions/write-message.sql +73 -0
  20. data/database/indexes/messages-category.sql +7 -0
  21. data/database/indexes/messages-id.sql +5 -0
  22. data/database/indexes/messages-stream.sql +6 -0
  23. data/database/install-functions.sh +79 -0
  24. data/database/install-indexes.sh +33 -0
  25. data/database/install-privileges.sh +39 -0
  26. data/database/install-views.sh +42 -0
  27. data/database/install.sh +102 -0
  28. data/database/print-category-type-summary.sh +44 -0
  29. data/database/print-message-store-version.sh +32 -0
  30. data/database/print-messages.sh +44 -0
  31. data/database/print-stream-summary.sh +44 -0
  32. data/database/print-stream-type-summary.sh +44 -0
  33. data/database/print-type-category-summary.sh +44 -0
  34. data/database/print-type-stream-summary.sh +44 -0
  35. data/database/print-type-summary.sh +44 -0
  36. data/database/privileges/functions.sql +15 -0
  37. data/database/privileges/schema.sql +1 -0
  38. data/database/privileges/sequence.sql +1 -0
  39. data/database/privileges/table.sql +1 -0
  40. data/database/privileges/views.sql +6 -0
  41. data/database/roles/message-store.sql +7 -0
  42. data/database/schema/message-store.sql +1 -0
  43. data/database/tables/messages.sql +12 -0
  44. data/database/types/message.sql +15 -0
  45. data/database/uninstall.sh +54 -0
  46. data/database/update.sh +171 -0
  47. data/database/views/category-type-summary.sql +32 -0
  48. data/database/views/stream-summary.sql +28 -0
  49. data/database/views/stream-type-summary.sql +32 -0
  50. data/database/views/type-category-summary.sql +32 -0
  51. data/database/views/type-stream-summary.sql +32 -0
  52. data/database/views/type-summary.sql +28 -0
  53. data/database/write-test-message.sh +63 -0
  54. data/scripts/mdb-clear-messages +7 -0
  55. data/scripts/mdb-create-db +7 -0
  56. data/scripts/mdb-delete-db +7 -0
  57. data/scripts/mdb-install-functions +7 -0
  58. data/scripts/mdb-install-indexes +7 -0
  59. data/scripts/mdb-install-privileges +7 -0
  60. data/scripts/mdb-install-views +7 -0
  61. data/scripts/mdb-open-database-scripts-dir +5 -0
  62. data/scripts/mdb-print-category-type-summary +7 -0
  63. data/scripts/mdb-print-message-store-version +7 -0
  64. data/scripts/mdb-print-messages +7 -0
  65. data/scripts/mdb-print-stream-summary +7 -0
  66. data/scripts/mdb-print-stream-type-summary +7 -0
  67. data/scripts/mdb-print-type-category-summary +7 -0
  68. data/scripts/mdb-print-type-stream-summary +7 -0
  69. data/scripts/mdb-print-type-summary +7 -0
  70. data/scripts/mdb-recreate-db +11 -0
  71. data/scripts/mdb-update-db +7 -0
  72. data/scripts/mdb-write-test-message +7 -0
  73. metadata +95 -6
  74. data/MIT-License.txt +0 -20
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ function script_dir {
6
+ val="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7
+ echo "$val"
8
+ }
9
+
10
+ base=$(script_dir)
11
+
12
+ echo
13
+ echo "Updating Database"
14
+ echo "Version: $(cat $base/VERSION.txt)"
15
+ echo "= = ="
16
+
17
+ if [ -z ${DATABASE_NAME+x} ]; then
18
+ echo "(DATABASE_NAME is not set. Default will be used.)"
19
+ database=message_store
20
+ export DATABASE_NAME=$database
21
+ else
22
+ database=$DATABASE_NAME
23
+ fi
24
+ echo
25
+
26
+ if [ -z ${PGOPTIONS+x} ]; then
27
+ export PGOPTIONS='-c client_min_messages=warning'
28
+ fi
29
+
30
+ function delete-extensions {
31
+ echo "» pgcrypto extension"
32
+ psql $database -q -c "DROP EXTENSION IF EXISTS pgcrypto";
33
+ }
34
+
35
+ function delete-indexes {
36
+ echo "» messages_id_uniq_idx index"
37
+ psql $database -q -c "DROP INDEX IF EXISTS messages_id_uniq_idx CASCADE";
38
+
39
+ echo "» messages_stream_name_position_uniq_idx index"
40
+ psql $database -q -c "DROP INDEX IF EXISTS messages_stream_name_position_uniq_idx";
41
+
42
+ echo "» messages_category_global_position_idx index"
43
+ psql $database -q -c "DROP INDEX IF EXISTS messages_category_global_position_idx";
44
+ }
45
+
46
+ function delete-views {
47
+ echo "» stream_summary view"
48
+ psql $database -q -c "DROP VIEW IF EXISTS stream_summary CASCADE";
49
+
50
+ echo "» type_summary view"
51
+ psql $database -q -c "DROP VIEW IF EXISTS type_summary CASCADE";
52
+
53
+ echo "» stream_type_summary view"
54
+ psql $database -q -c "DROP VIEW IF EXISTS stream_type_summary CASCADE";
55
+
56
+ echo "» type_stream_summary view"
57
+ psql $database -q -c "DROP VIEW IF EXISTS type_stream_summary CASCADE";
58
+
59
+ echo "» category_type_summary view"
60
+ psql $database -q -c "DROP VIEW IF EXISTS category_type_summary CASCADE";
61
+
62
+ echo "» type_category_summary view"
63
+ psql $database -q -c "DROP VIEW IF EXISTS type_category_summary CASCADE";
64
+ }
65
+
66
+ function delete-functions {
67
+ echo "» hash_64 function"
68
+ psql $database -q -c "DROP FUNCTION IF EXISTS hash_64 CASCADE";
69
+
70
+ echo "» category function"
71
+ psql $database -q -c "DROP FUNCTION IF EXISTS category CASCADE";
72
+
73
+ echo "» stream_version function"
74
+ psql $database -q -c "DROP FUNCTION IF EXISTS stream_version CASCADE";
75
+
76
+ echo "» write_message function"
77
+ psql $database -q -c "DROP FUNCTION IF EXISTS write_message CASCADE";
78
+
79
+ echo "» get_stream_messages function"
80
+ psql $database -q -c "DROP FUNCTION IF EXISTS get_stream_messages CASCADE";
81
+
82
+ echo "» get_category_messages function"
83
+ psql $database -q -c "DROP FUNCTION IF EXISTS get_category_messages CASCADE";
84
+
85
+ echo "» get_last_message function"
86
+ psql $database -q -c "DROP FUNCTION IF EXISTS get_last_message CASCADE";
87
+ }
88
+
89
+ function delete-extensions {
90
+ echo "» pgcrypto extension"
91
+ psql $database -q -c "DROP EXTENSION IF EXISTS pgcrypto CASCADE";
92
+ }
93
+
94
+ function create-schema {
95
+ echo "» message_store schema"
96
+ psql $database -q -f $base/schema/message-store.sql
97
+ }
98
+
99
+ function add-table-to-schema {
100
+ echo "» messages table"
101
+ psql $database -q -c "ALTER TABLE messages SET SCHEMA message_store";
102
+ }
103
+
104
+ function create-extensions {
105
+ base=$(script_dir)
106
+
107
+ echo "» pgcrypto extension"
108
+ psql $database -q -f $base/extensions/pgcrypto.sql
109
+ }
110
+
111
+ function set-default-value {
112
+ echo "» id column"
113
+ psql $database -q -c "ALTER TABLE message_store.messages ALTER COLUMN id SET DEFAULT gen_random_uuid()";
114
+ }
115
+
116
+ echo "Deleting Views"
117
+ echo "- - -"
118
+ delete-views
119
+ echo
120
+
121
+ echo "Deleting Indexes"
122
+ echo "- - -"
123
+ delete-indexes
124
+ echo
125
+
126
+ echo "Deleting Functions"
127
+ echo "- - -"
128
+ delete-functions
129
+ echo
130
+
131
+ echo "Deleting Extensions"
132
+ echo "- - -"
133
+ delete-extensions
134
+ echo
135
+
136
+ echo "Creating Schema"
137
+ echo "- - -"
138
+ create-schema
139
+ echo
140
+
141
+ echo "Creating Extensions"
142
+ echo "- - -"
143
+ create-extensions
144
+ echo
145
+
146
+ echo "Adding Table to Schema"
147
+ echo "- - -"
148
+ add-table-to-schema
149
+ echo
150
+
151
+ echo "Set Default Value for ID Column"
152
+ echo "- - -"
153
+ set-default-value
154
+ echo
155
+
156
+ # Install functions
157
+ source $base/install-functions.sh
158
+
159
+ # Install indexes
160
+ source $base/install-indexes.sh
161
+
162
+ # Install views
163
+ source $base/install-views.sh
164
+
165
+ # Install privileges
166
+ source $base/install-privileges.sh
167
+
168
+ echo "= = ="
169
+ echo "Done Updating Database"
170
+ echo "Version: $(cat $base/VERSION.txt)"
171
+ echo
@@ -0,0 +1,32 @@
1
+ CREATE OR REPLACE VIEW message_store.category_type_summary AS
2
+ WITH
3
+ type_count AS (
4
+ SELECT
5
+ message_store.category(stream_name) AS category,
6
+ type,
7
+ COUNT(id) AS message_count
8
+ FROM
9
+ message_store.messages
10
+ GROUP BY
11
+ category,
12
+ type
13
+ ),
14
+
15
+ total_count AS (
16
+ SELECT
17
+ COUNT(id)::decimal AS total_count
18
+ FROM
19
+ message_store.messages
20
+ )
21
+
22
+ SELECT
23
+ category,
24
+ type,
25
+ message_count,
26
+ ROUND((message_count / total_count)::decimal * 100, 2) AS percent
27
+ FROM
28
+ type_count,
29
+ total_count
30
+ ORDER BY
31
+ category,
32
+ type;
@@ -0,0 +1,28 @@
1
+ CREATE OR REPLACE VIEW message_store.stream_summary AS
2
+ WITH
3
+ stream_count AS (
4
+ SELECT
5
+ stream_name,
6
+ COUNT(id) AS message_count
7
+ FROM
8
+ message_store.messages
9
+ GROUP BY
10
+ stream_name
11
+ ),
12
+
13
+ total_count AS (
14
+ SELECT
15
+ COUNT(id)::decimal AS total_count
16
+ FROM
17
+ message_store.messages
18
+ )
19
+
20
+ SELECT
21
+ stream_name,
22
+ message_count,
23
+ ROUND((message_count / total_count)::decimal * 100, 2) AS percent
24
+ FROM
25
+ stream_count,
26
+ total_count
27
+ ORDER BY
28
+ stream_name;
@@ -0,0 +1,32 @@
1
+ CREATE OR REPLACE VIEW message_store.stream_type_summary AS
2
+ WITH
3
+ type_count AS (
4
+ SELECT
5
+ stream_name,
6
+ type,
7
+ COUNT(id) AS message_count
8
+ FROM
9
+ message_store.messages
10
+ GROUP BY
11
+ stream_name,
12
+ type
13
+ ),
14
+
15
+ total_count AS (
16
+ SELECT
17
+ COUNT(id)::decimal AS total_count
18
+ FROM
19
+ message_store.messages
20
+ )
21
+
22
+ SELECT
23
+ stream_name,
24
+ type,
25
+ message_count,
26
+ ROUND((message_count / total_count)::decimal * 100, 2) AS percent
27
+ FROM
28
+ type_count,
29
+ total_count
30
+ ORDER BY
31
+ stream_name,
32
+ type;
@@ -0,0 +1,32 @@
1
+ CREATE OR REPLACE VIEW message_store.type_category_summary AS
2
+ WITH
3
+ type_count AS (
4
+ SELECT
5
+ type,
6
+ message_store.category(stream_name) AS category,
7
+ COUNT(id) AS message_count
8
+ FROM
9
+ message_store.messages
10
+ GROUP BY
11
+ type,
12
+ category
13
+ ),
14
+
15
+ total_count AS (
16
+ SELECT
17
+ COUNT(id)::decimal AS total_count
18
+ FROM
19
+ message_store.messages
20
+ )
21
+
22
+ SELECT
23
+ type,
24
+ category,
25
+ message_count,
26
+ ROUND((message_count / total_count)::decimal * 100, 2) AS percent
27
+ FROM
28
+ type_count,
29
+ total_count
30
+ ORDER BY
31
+ type,
32
+ category;
@@ -0,0 +1,32 @@
1
+ CREATE OR REPLACE VIEW message_store.type_stream_summary AS
2
+ WITH
3
+ type_count AS (
4
+ SELECT
5
+ type,
6
+ stream_name,
7
+ COUNT(id) AS message_count
8
+ FROM
9
+ message_store.messages
10
+ GROUP BY
11
+ type,
12
+ stream_name
13
+ ),
14
+
15
+ total_count AS (
16
+ SELECT
17
+ COUNT(id)::decimal AS total_count
18
+ FROM
19
+ message_store.messages
20
+ )
21
+
22
+ SELECT
23
+ type,
24
+ stream_name,
25
+ message_count,
26
+ ROUND((message_count / total_count)::decimal * 100, 2) AS percent
27
+ FROM
28
+ type_count,
29
+ total_count
30
+ ORDER BY
31
+ type,
32
+ stream_name;
@@ -0,0 +1,28 @@
1
+ CREATE OR REPLACE VIEW message_store.type_summary AS
2
+ WITH
3
+ type_count AS (
4
+ SELECT
5
+ type,
6
+ COUNT(id) AS message_count
7
+ FROM
8
+ message_store.messages
9
+ GROUP BY
10
+ type
11
+ ),
12
+
13
+ total_count AS (
14
+ SELECT
15
+ COUNT(id)::decimal AS total_count
16
+ FROM
17
+ message_store.messages
18
+ )
19
+
20
+ SELECT
21
+ type,
22
+ message_count,
23
+ ROUND((message_count / total_count)::decimal * 100, 2) AS percent
24
+ FROM
25
+ type_count,
26
+ total_count
27
+ ORDER BY
28
+ type;
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -u
4
+
5
+ instances=1
6
+ if [ ! -z ${INSTANCES+x} ]; then
7
+ instances=$INSTANCES
8
+ fi
9
+
10
+ uuid=$(echo $(uuidgen) | tr '[:upper:]' '[:lower:]')
11
+ stream_name="testStream-$uuid"
12
+ if [ ! -z ${STREAM_NAME+x} ]; then
13
+ stream_name=$STREAM_NAME
14
+ fi
15
+
16
+ title="Writing $instances Messages to Stream $stream_name"
17
+ if [ -z ${METADATA+x} ]; then
18
+ metadata="'{\"metaAttribute\": \"some meta value\"}'"
19
+ else
20
+ metadata="$METADATA"
21
+ title="$title with Metadata $metadata"
22
+ fi
23
+
24
+ metadata="$metadata::jsonb"
25
+
26
+ echo
27
+ echo $title
28
+ echo "= = ="
29
+ echo
30
+
31
+ default_name=message_store
32
+
33
+ if [ -z ${DATABASE_USER+x} ]; then
34
+ echo "(DATABASE_USER is not set)"
35
+ user=$default_name
36
+ else
37
+ user=$DATABASE_USER
38
+ fi
39
+ echo "Database user is: $user"
40
+
41
+ if [ -z ${DATABASE_NAME+x} ]; then
42
+ echo "(DATABASE_NAME is not set)"
43
+ database=$default_name
44
+ else
45
+ database=$DATABASE_NAME
46
+ fi
47
+ echo "Database name is: $database"
48
+ echo
49
+
50
+
51
+ for (( i=1; i<=instances; i++ )); do
52
+ uuid=$(echo $(uuidgen) | tr '[:upper:]' '[:lower:]')
53
+
54
+ echo "Instance: $i, Message ID: $uuid"
55
+
56
+ psql $database -U $user -c "SELECT write_message('$uuid'::varchar, '$stream_name'::varchar, 'SomeType'::varchar, '{\"attribute\": \"some value\"}'::jsonb, $metadata);" > /dev/null
57
+ done
58
+
59
+
60
+ echo
61
+ psql $database -U $user -P pager=off -x -c "SELECT * FROM messages WHERE stream_name = '$stream_name';"
62
+
63
+ echo
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ root = File.expand_path '../database', __dir__
4
+ script_filename = 'clear-messages.sh'
5
+ script_filepath = File.join root, script_filename
6
+
7
+ system script_filepath
@@ -0,0 +1,7 @@
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