Initial import (again)
[srvx.git] / patches / log-pgsql.txt
1 -- This patch assumes a database prepared with the script below.
2 -- Once this is set up, you can create a log destination using the
3 -- "pgsql:" schema followed by a normal PostgreSQL connect string.
4 -- For example:
5 -- "logs" {
6 --   "*.*" "pgsql:host=postgres.testnet.com port=5432 dbname=srvx user=srvx password=TeStNeT requiressl=1";
7 -- };
8 -- some of those options may be omitted, in which case the PostgreSQL
9 -- client library will use defaults.  You may use hostaddr=10.0.0.7
10 -- instead of host=postgres.testnet.com, if the database server does
11 -- not have a name in DNS or in /etc/hosts.
12
13 CREATE TABLE srvx_modules (
14   i_id SMALLINT PRIMARY KEY,
15   s_name VARCHAR(32) UNIQUE NOT NULL);
16
17 CREATE TABLE srvx_bots (
18   i_id SMALLINT PRIMARY KEY,
19   s_name VARCHAR(32) UNIQUE NOT NULL);
20
21 CREATE TABLE srvx_audit (
22   i_module SMALLINT NOT NULL REFERENCES srvx_modules(i_id),
23   i_severity SMALLINT NOT NULL,
24   i_bot SMALLINT NOT NULL REFERENCES srvx_bots(i_id),
25   t_when TIMESTAMP NOT NULL,
26   c_channel_name VARCHAR(200),
27   c_user_nick VARCHAR(30) NOT NULL,
28   c_user_account VARCHAR(30),
29   c_user_hostmask VARCHAR(80),
30   c_command VARCHAR(500) NOT NULL);
31
32 CREATE TABLE srvx_replay (
33   i_module SMALLINT NOT NULL REFERENCES srvx_modules(i_id),
34   t_when TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
35   b_is_write BOOLEAN NOT NULL,
36   c_text VARCHAR(510) NOT NULL);
37
38 CREATE TABLE srvx_log (
39   i_module SMALLINT NOT NULL REFERENCES srvx_modules(i_id),
40   i_severity SMALLINT NOT NULL,
41   t_when TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
42   c_message VARCHAR(1024) NOT NULL);
43
44 CREATE SEQUENCE srvx_modules_id_seq MINVALUE 0 MAXVALUE 32767;
45 CREATE FUNCTION srvx_module_id (VARCHAR(32)) RETURNS SMALLINT
46   LANGUAGE 'plpgsql' STABLE STRICT AS '
47   DECLARE
48     name ALIAS FOR $1;
49     new_id srvx_modules.i_id%TYPE;
50   BEGIN
51     SELECT INTO new_id i_id FROM srvx_modules WHERE s_name=name;
52     IF NOT FOUND THEN
53       SELECT INTO new_id nextval(''srvx_modules_id_seq'');
54       INSERT INTO srvx_modules (i_id, s_name) VALUES (new_id, name);
55     END IF;
56     RETURN new_id;
57   END;';
58
59 CREATE SEQUENCE srvx_bots_id_seq MINVALUE 0 MAXVALUE 32767;
60 CREATE FUNCTION srvx_bot_id (VARCHAR(32)) RETURNS SMALLINT
61   LANGUAGE 'plpgsql' STABLE STRICT AS '
62   DECLARE
63     name ALIAS FOR $1;
64     new_id srvx_bots.i_id%TYPE;
65   BEGIN
66     SELECT INTO new_id i_id FROM srvx_bots WHERE s_name=name;
67     IF NOT FOUND THEN
68       SELECT INTO new_id nextval(''srvx_bots_id_seq'');
69       INSERT INTO srvx_bots (i_id, s_name) VALUES (new_id, name);
70     END IF;
71     RETURN new_id;
72   END;';