evt-message_store-postgres-database 0.1.0.0
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.
- checksums.yaml +7 -0
- data/database/clear-events-table.sh +40 -0
- data/database/extensions.sql +1 -0
- data/database/functions/category.sql +10 -0
- data/database/functions/get-category-messages.sql +45 -0
- data/database/functions/get-last-message.sql +33 -0
- data/database/functions/get-stream-messages.sql +45 -0
- data/database/functions/hash-64.sql +13 -0
- data/database/functions/scratch.sql +68 -0
- data/database/functions/stream-version.sql +14 -0
- data/database/functions/write-message.sql +60 -0
- data/database/indexes/messages-category-global-position.sql +1 -0
- data/database/indexes/messages-id.sql +1 -0
- data/database/indexes/messages-stream-name-position-uniq.sql +1 -0
- data/database/install.sh +119 -0
- data/database/install_functions.sh +65 -0
- data/database/print-messages.sh +52 -0
- data/database/table/messages-table.sql +19 -0
- data/database/types/message.sql +15 -0
- data/database/uninstall.sh +60 -0
- data/scripts/evt-pg-create-db +7 -0
- data/scripts/evt-pg-delete-db +7 -0
- data/scripts/evt-pg-print-messages +7 -0
- data/scripts/evt-pg-recreate-db +11 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a0f4bb6cb696f88af5f25636de90de8a77dd47c1998c7b8a92a4a053debd2328
|
4
|
+
data.tar.gz: b5f19af7297e09cee6cc1a15d708756618ebf499be46435d2c0bf468540a810e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 59b9a5d456bfbb21649322559cbf9335478c1afc78012dac917985c5687f43dfaf32fb0a370771a1e05ca6c934258b43f51919ba9d8c045f5c4170aabb6e86f8
|
7
|
+
data.tar.gz: e026fca219bf6bfa0abc15d70f683f9a2c32c113d25b81e46fadbdc74674f72441c8aa56722a681939c0088957c61e3b7709a558c7abf869e9c7a4596b6d60da
|
@@ -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=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;"
|
@@ -0,0 +1 @@
|
|
1
|
+
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
@@ -0,0 +1,45 @@
|
|
1
|
+
CREATE OR REPLACE FUNCTION get_category_messages(
|
2
|
+
_stream_name varchar,
|
3
|
+
_position bigint DEFAULT 0,
|
4
|
+
_batch_size bigint DEFAULT 1000,
|
5
|
+
_condition varchar DEFAULT NULL
|
6
|
+
)
|
7
|
+
RETURNS SETOF message
|
8
|
+
AS $$
|
9
|
+
DECLARE
|
10
|
+
command text;
|
11
|
+
BEGIN
|
12
|
+
command := '
|
13
|
+
SELECT
|
14
|
+
id::varchar,
|
15
|
+
stream_name::varchar,
|
16
|
+
type::varchar,
|
17
|
+
position::bigint,
|
18
|
+
global_position::bigint,
|
19
|
+
data::varchar,
|
20
|
+
metadata::varchar,
|
21
|
+
time::timestamp
|
22
|
+
FROM
|
23
|
+
messages
|
24
|
+
WHERE
|
25
|
+
category(stream_name) = $1 AND
|
26
|
+
global_position >= $2';
|
27
|
+
|
28
|
+
if _condition is not null then
|
29
|
+
command := command || ' AND
|
30
|
+
%s';
|
31
|
+
command := format(command, _condition);
|
32
|
+
end if;
|
33
|
+
|
34
|
+
command := command || '
|
35
|
+
ORDER BY
|
36
|
+
global_position ASC
|
37
|
+
LIMIT
|
38
|
+
$3';
|
39
|
+
|
40
|
+
-- RAISE NOTICE '%', command;
|
41
|
+
|
42
|
+
RETURN QUERY EXECUTE command USING _stream_name, _position, _batch_size;
|
43
|
+
END;
|
44
|
+
$$ LANGUAGE plpgsql
|
45
|
+
VOLATILE;
|
@@ -0,0 +1,33 @@
|
|
1
|
+
CREATE OR REPLACE FUNCTION get_last_message(
|
2
|
+
_stream_name varchar
|
3
|
+
)
|
4
|
+
RETURNS SETOF message
|
5
|
+
AS $$
|
6
|
+
DECLARE
|
7
|
+
command text;
|
8
|
+
BEGIN
|
9
|
+
command := '
|
10
|
+
SELECT
|
11
|
+
id::varchar,
|
12
|
+
stream_name::varchar,
|
13
|
+
type::varchar,
|
14
|
+
position::bigint,
|
15
|
+
global_position::bigint,
|
16
|
+
data::varchar,
|
17
|
+
metadata::varchar,
|
18
|
+
time::timestamp
|
19
|
+
FROM
|
20
|
+
messages
|
21
|
+
WHERE
|
22
|
+
stream_name = $1
|
23
|
+
ORDER BY
|
24
|
+
position DESC
|
25
|
+
LIMIT
|
26
|
+
1';
|
27
|
+
|
28
|
+
-- RAISE NOTICE '%', command;
|
29
|
+
|
30
|
+
RETURN QUERY EXECUTE command USING _stream_name;
|
31
|
+
END;
|
32
|
+
$$ LANGUAGE plpgsql
|
33
|
+
VOLATILE;
|
@@ -0,0 +1,45 @@
|
|
1
|
+
CREATE OR REPLACE FUNCTION get_stream_messages(
|
2
|
+
_stream_name varchar,
|
3
|
+
_position bigint DEFAULT 0,
|
4
|
+
_batch_size bigint DEFAULT 1000,
|
5
|
+
_condition varchar DEFAULT NULL
|
6
|
+
)
|
7
|
+
RETURNS SETOF message
|
8
|
+
AS $$
|
9
|
+
DECLARE
|
10
|
+
command text;
|
11
|
+
BEGIN
|
12
|
+
command := '
|
13
|
+
SELECT
|
14
|
+
id::varchar,
|
15
|
+
stream_name::varchar,
|
16
|
+
type::varchar,
|
17
|
+
position::bigint,
|
18
|
+
global_position::bigint,
|
19
|
+
data::varchar,
|
20
|
+
metadata::varchar,
|
21
|
+
time::timestamp
|
22
|
+
FROM
|
23
|
+
messages
|
24
|
+
WHERE
|
25
|
+
stream_name = $1 AND
|
26
|
+
position >= $2';
|
27
|
+
|
28
|
+
if _condition is not null then
|
29
|
+
command := command || ' AND
|
30
|
+
%s';
|
31
|
+
command := format(command, _condition);
|
32
|
+
end if;
|
33
|
+
|
34
|
+
command := command || '
|
35
|
+
ORDER BY
|
36
|
+
position ASC
|
37
|
+
LIMIT
|
38
|
+
$3';
|
39
|
+
|
40
|
+
-- RAISE NOTICE '%', command;
|
41
|
+
|
42
|
+
RETURN QUERY EXECUTE command USING _stream_name, _position, _batch_size;
|
43
|
+
END;
|
44
|
+
$$ LANGUAGE plpgsql
|
45
|
+
VOLATILE;
|
@@ -0,0 +1,68 @@
|
|
1
|
+
:stream_name, na(:position), na(:batch_size), :condition
|
2
|
+
|
3
|
+
|
4
|
+
module Defaults
|
5
|
+
def self.position
|
6
|
+
0
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.batch_size
|
10
|
+
1000
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
- - -
|
16
|
+
|
17
|
+
command := 'SELECT * FROM messages WHERE $1';
|
18
|
+
RETURN QUERY EXECUTE command USING where_clause;
|
19
|
+
|
20
|
+
- - -
|
21
|
+
select * from messages where metadata @> '{"correlationStreamName":"someCorrelation"}';
|
22
|
+
- - -
|
23
|
+
select * from get_messages($$metadata @> '{"correlationStreamName":"someCorrelation"}'$$);
|
24
|
+
- - -
|
25
|
+
|
26
|
+
|
27
|
+
_sql := format('
|
28
|
+
SELECT *
|
29
|
+
FROM (
|
30
|
+
SELECT p.person_id, p.name, p.team, %1$s(s.score)::int AS score
|
31
|
+
,rank() OVER (PARTITION BY p.team
|
32
|
+
ORDER BY %1$s(s.score) DESC)::int AS rnk
|
33
|
+
FROM person p
|
34
|
+
%2$s score s USING (person_id)
|
35
|
+
%3$s
|
36
|
+
GROUP BY 1
|
37
|
+
) sub
|
38
|
+
WHERE rnk < 3
|
39
|
+
ORDER BY team, rnk'
|
40
|
+
, _agg
|
41
|
+
, CASE WHEN _left_join THEN 'LEFT JOIN' ELSE 'JOIN' END
|
42
|
+
, CASE WHEN _where_name <> '' THEN 'WHERE p.name LIKE $1' ELSE '' END
|
43
|
+
);
|
44
|
+
|
45
|
+
-- debug -- quote when tested ok
|
46
|
+
-- RAISE NOTICE '%', _sql;
|
47
|
+
|
48
|
+
-- execute -- unquote when tested ok
|
49
|
+
RETURN QUERY EXECUTE _sql
|
50
|
+
|
51
|
+
|
52
|
+
- - -
|
53
|
+
|
54
|
+
CREATE OR REPLACE FUNCTION get_messages()
|
55
|
+
RETURNS SETOF messages
|
56
|
+
AS $$
|
57
|
+
DECLARE
|
58
|
+
_sql text;
|
59
|
+
BEGIN
|
60
|
+
|
61
|
+
_sql := 'RETURN QUERY SELECT * FROM messages'
|
62
|
+
|
63
|
+
-- RETURN QUERY EXECUTE _sql
|
64
|
+
|
65
|
+
RETURN QUERY SELECT * FROM messages;
|
66
|
+
END;
|
67
|
+
$$ LANGUAGE plpgsql
|
68
|
+
VOLATILE;
|
@@ -0,0 +1,14 @@
|
|
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;
|
@@ -0,0 +1,60 @@
|
|
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;
|
@@ -0,0 +1 @@
|
|
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);
|
@@ -0,0 +1 @@
|
|
1
|
+
CREATE INDEX CONCURRENTLY "messages_id_idx" ON "public"."messages" USING btree(id ASC NULLS LAST);
|
@@ -0,0 +1 @@
|
|
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
ADDED
@@ -0,0 +1,119 @@
|
|
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
|
+
createuser -s $user
|
31
|
+
echo
|
32
|
+
}
|
33
|
+
|
34
|
+
function create-database {
|
35
|
+
echo "Database name is: $database"
|
36
|
+
echo "Creating database \"$database\"..."
|
37
|
+
createdb $database
|
38
|
+
echo
|
39
|
+
}
|
40
|
+
|
41
|
+
function script_dir {
|
42
|
+
val="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
43
|
+
echo "$val"
|
44
|
+
}
|
45
|
+
|
46
|
+
function create-extensions {
|
47
|
+
echo "Creating extensions..."
|
48
|
+
base=$(script_dir)
|
49
|
+
psql $database -f $base/extensions.sql
|
50
|
+
echo
|
51
|
+
}
|
52
|
+
|
53
|
+
function create-table {
|
54
|
+
echo "Creating messages table..."
|
55
|
+
psql $database -f $base/table/messages-table.sql
|
56
|
+
echo
|
57
|
+
}
|
58
|
+
|
59
|
+
function create-types {
|
60
|
+
base=$(script_dir)
|
61
|
+
echo "Creating types..."
|
62
|
+
|
63
|
+
echo "message type"
|
64
|
+
psql $database -f $base/types/message.sql
|
65
|
+
|
66
|
+
echo
|
67
|
+
}
|
68
|
+
|
69
|
+
function create-functions {
|
70
|
+
echo "Creating functions..."
|
71
|
+
base=$(script_dir)
|
72
|
+
|
73
|
+
echo "hash_64 function"
|
74
|
+
psql $database -f $base/functions/hash-64.sql
|
75
|
+
|
76
|
+
echo "category function"
|
77
|
+
psql $database -f $base/functions/category.sql
|
78
|
+
|
79
|
+
echo "stream_version function"
|
80
|
+
psql $database -f $base/functions/stream-version.sql
|
81
|
+
|
82
|
+
echo "write_message function"
|
83
|
+
psql $database -f $base/functions/write-message.sql
|
84
|
+
|
85
|
+
echo "get_stream_messages function"
|
86
|
+
psql $database -f $base/functions/get-stream-messages.sql
|
87
|
+
|
88
|
+
echo "get_category_messages function"
|
89
|
+
psql $database -f $base/functions/get-category-messages.sql
|
90
|
+
|
91
|
+
echo "get_last_message function"
|
92
|
+
psql $database -f $base/functions/get-last-message.sql
|
93
|
+
|
94
|
+
echo
|
95
|
+
}
|
96
|
+
|
97
|
+
function create-indexes {
|
98
|
+
echo "Creating indexes..."
|
99
|
+
base=$(script_dir)
|
100
|
+
|
101
|
+
echo "messages_id_idx"
|
102
|
+
psql $database -f $base/indexes/messages-id.sql
|
103
|
+
|
104
|
+
echo "messages_category_global_position_idx"
|
105
|
+
psql $database -f $base/indexes/messages-category-global-position.sql
|
106
|
+
|
107
|
+
echo "messages_stream_name_position_uniq_idx"
|
108
|
+
psql $database -f $base/indexes/messages-stream-name-position-uniq.sql
|
109
|
+
|
110
|
+
echo
|
111
|
+
}
|
112
|
+
|
113
|
+
create-user
|
114
|
+
create-database
|
115
|
+
create-extensions
|
116
|
+
create-table
|
117
|
+
create-types
|
118
|
+
create-functions
|
119
|
+
create-indexes
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
set -e
|
4
|
+
|
5
|
+
echo
|
6
|
+
echo "Installing Functions"
|
7
|
+
echo "= = ="
|
8
|
+
echo
|
9
|
+
|
10
|
+
default_name=message_store
|
11
|
+
|
12
|
+
if [ -z ${DATABASE_NAME+x} ]; then
|
13
|
+
echo "(DATABASE_NAME is not set. Default will be used.)"
|
14
|
+
database=$default_name
|
15
|
+
else
|
16
|
+
database=$DATABASE_NAME
|
17
|
+
fi
|
18
|
+
|
19
|
+
echo
|
20
|
+
|
21
|
+
function script_dir {
|
22
|
+
val="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
23
|
+
echo "$val"
|
24
|
+
}
|
25
|
+
|
26
|
+
function create-types {
|
27
|
+
base=$(script_dir)
|
28
|
+
echo "Creating types..."
|
29
|
+
|
30
|
+
echo "message type"
|
31
|
+
psql $database -f $base/types/message.sql
|
32
|
+
|
33
|
+
echo
|
34
|
+
}
|
35
|
+
|
36
|
+
function create-functions {
|
37
|
+
echo "Creating functions..."
|
38
|
+
base=$(script_dir)
|
39
|
+
|
40
|
+
echo "hash_64 function"
|
41
|
+
psql $database -f $base/functions/hash-64.sql
|
42
|
+
|
43
|
+
echo "category function"
|
44
|
+
psql $database -f $base/functions/category.sql
|
45
|
+
|
46
|
+
echo "stream_version function"
|
47
|
+
psql $database -f $base/functions/stream-version.sql
|
48
|
+
|
49
|
+
echo "write_message function"
|
50
|
+
psql $database -f $base/functions/write-message.sql
|
51
|
+
|
52
|
+
echo "get_stream_messages function"
|
53
|
+
psql $database -f $base/functions/get-stream-messages.sql
|
54
|
+
|
55
|
+
echo "get_category_messages function"
|
56
|
+
psql $database -f $base/functions/get-category-messages.sql
|
57
|
+
|
58
|
+
echo "get_last_message function"
|
59
|
+
psql $database -f $base/functions/get-last-message.sql
|
60
|
+
|
61
|
+
echo
|
62
|
+
}
|
63
|
+
|
64
|
+
create-types
|
65
|
+
create-functions
|
@@ -0,0 +1,52 @@
|
|
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
|
@@ -0,0 +1,19 @@
|
|
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;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
DO $$
|
2
|
+
BEGIN
|
3
|
+
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'message') THEN
|
4
|
+
CREATE TYPE message AS (
|
5
|
+
id varchar,
|
6
|
+
stream_name varchar,
|
7
|
+
type varchar,
|
8
|
+
position bigint,
|
9
|
+
global_position bigint,
|
10
|
+
data varchar,
|
11
|
+
metadata varchar,
|
12
|
+
time timestamp
|
13
|
+
);
|
14
|
+
END IF;
|
15
|
+
END$$;
|
@@ -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=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
|
@@ -0,0 +1,11 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: evt-message_store-postgres-database
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- The Eventide Project
|
8
|
+
autorequire:
|
9
|
+
bindir: scripts
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: " "
|
14
|
+
email: opensource@eventide-project.org
|
15
|
+
executables:
|
16
|
+
- evt-pg-print-messages
|
17
|
+
- evt-pg-delete-db
|
18
|
+
- evt-pg-create-db
|
19
|
+
- evt-pg-recreate-db
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- database/clear-events-table.sh
|
24
|
+
- database/extensions.sql
|
25
|
+
- database/functions/category.sql
|
26
|
+
- database/functions/get-category-messages.sql
|
27
|
+
- database/functions/get-last-message.sql
|
28
|
+
- database/functions/get-stream-messages.sql
|
29
|
+
- database/functions/hash-64.sql
|
30
|
+
- database/functions/scratch.sql
|
31
|
+
- database/functions/stream-version.sql
|
32
|
+
- database/functions/write-message.sql
|
33
|
+
- database/indexes/messages-category-global-position.sql
|
34
|
+
- database/indexes/messages-id.sql
|
35
|
+
- database/indexes/messages-stream-name-position-uniq.sql
|
36
|
+
- database/install.sh
|
37
|
+
- database/install_functions.sh
|
38
|
+
- database/print-messages.sh
|
39
|
+
- database/table/messages-table.sql
|
40
|
+
- database/types/message.sql
|
41
|
+
- database/uninstall.sh
|
42
|
+
- scripts/evt-pg-create-db
|
43
|
+
- scripts/evt-pg-delete-db
|
44
|
+
- scripts/evt-pg-print-messages
|
45
|
+
- scripts/evt-pg-recreate-db
|
46
|
+
homepage: https://github.com/eventide-project/message-store-postgres-database
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 2.7.3
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Message store PostgreSQL database definition
|
70
|
+
test_files: []
|