work in progress: msgstore: store message
This commit is contained in:
parent
8d9de5b972
commit
e8a6696bcb
5 changed files with 75 additions and 13 deletions
19
storage/msgstore/msgstore.go
Normal file
19
storage/msgstore/msgstore.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package msgstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
lib "git.sr.ht/~cco/go-scopes"
|
||||||
|
"git.sr.ht/~cco/go-scopes/storage/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Store(db *sql.Storage, msg lib.Message) {
|
||||||
|
q := fmt.Sprintf(sql_insert_msg, "messages")
|
||||||
|
fmt.Println(q)
|
||||||
|
db.Exec(q, msg.Domain(), msg.Action(), msg.Class(), msg.Item(), msg.Payload())
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
sql_insert_msg = `insert into %s (domain, action, class, item, payload)
|
||||||
|
values ($1, $2, $3, $4, $5)`
|
||||||
|
)
|
17
storage/msgstore/sql/tables_pg.sql
Normal file
17
storage/msgstore/sql/tables_pg.sql
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
create table messages (
|
||||||
|
id bigserial not null primary key,
|
||||||
|
domain text,
|
||||||
|
action text,
|
||||||
|
class text,
|
||||||
|
item text,
|
||||||
|
sender text,
|
||||||
|
payload jsonb,
|
||||||
|
tstamp timestamptz default current_timestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
-- indexes
|
||||||
|
|
||||||
|
create index idx_msg on messages using btree (domain, action, class, item);
|
||||||
|
create index idx_msg_item on messages using btree (domain, class, item);
|
||||||
|
--create index idx_msg_action on messages using btree (action); -- obsolete
|
||||||
|
|
17
storage/msgstore/sql/tables_sqlite.sql
Normal file
17
storage/msgstore/sql/tables_sqlite.sql
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
create table messages (
|
||||||
|
id integer not null primary key,
|
||||||
|
domain text,
|
||||||
|
action text,
|
||||||
|
class text,
|
||||||
|
item text,
|
||||||
|
sender text,
|
||||||
|
payload jsonb,
|
||||||
|
tstamp timestamptz default current_timestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
-- indexes
|
||||||
|
|
||||||
|
create index idx_msg on messages (domain, action, class, item);
|
||||||
|
create index idx_msg_item on messages (domain, class, item);
|
||||||
|
--create index idx_msg_action on messages using btree (action); -- obsolete
|
||||||
|
|
|
@ -34,6 +34,6 @@ func OverridesSql() config.Settings {
|
||||||
return config.Settings{
|
return config.Settings{
|
||||||
//SQLITE_CONNSTR: ":memory",
|
//SQLITE_CONNSTR: ":memory",
|
||||||
SQLITE_CONNSTR: "data/scopes.sqlite",
|
SQLITE_CONNSTR: "data/scopes.sqlite",
|
||||||
PGSQL_CONNSTR: "user=ccotest password=cco dbname=ccotest",
|
PGSQL_CONNSTR: "user=ccotest password=cco dbname=scpstest",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
tbase "testing"
|
tbase "testing"
|
||||||
|
|
||||||
"git.sr.ht/~cco/go-scopes/common/testing"
|
"git.sr.ht/~cco/go-scopes/common/testing"
|
||||||
|
"git.sr.ht/~cco/go-scopes/core/message"
|
||||||
|
"git.sr.ht/~cco/go-scopes/storage/msgstore"
|
||||||
"git.sr.ht/~cco/go-scopes/storage/sql"
|
"git.sr.ht/~cco/go-scopes/storage/sql"
|
||||||
_ "git.sr.ht/~cco/go-scopes/storage/sql/pgsql"
|
_ "git.sr.ht/~cco/go-scopes/storage/sql/pgsql"
|
||||||
_ "git.sr.ht/~cco/go-scopes/storage/sql/sqlite"
|
_ "git.sr.ht/~cco/go-scopes/storage/sql/sqlite"
|
||||||
|
@ -28,6 +30,7 @@ func TestPgsql(tb *tbase.T) {
|
||||||
|
|
||||||
func DoTests(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
func DoTests(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||||
t.Run("base", func(t *testing.T) { BaseTest(t, cfg, db) })
|
t.Run("base", func(t *testing.T) { BaseTest(t, cfg, db) })
|
||||||
|
t.Run("msgstore", func(t *testing.T) { MsgstoreTest(t, cfg, db) })
|
||||||
}
|
}
|
||||||
|
|
||||||
type greet struct {
|
type greet struct {
|
||||||
|
@ -45,18 +48,23 @@ func (g *greet) ScanP(rows *sql.Rows) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||||
db.Exec(insert)
|
db.Exec(sql_insert)
|
||||||
//data := sql.QueryCol[string](db, query, 2)
|
//data := sql.QueryCol[string](db, query, 2)
|
||||||
data := sql.QueryData[greet](db, query)
|
data := sql.QueryData[greet](db, sql_query)
|
||||||
t.AssertEqual(len(data), 2)
|
t.AssertEqual(len(data), 2)
|
||||||
t.AssertEqual(data[0].label, "Hello World")
|
t.AssertEqual(data[0].label, "Hello World")
|
||||||
t.AssertEqual(data[1].label, "Good Afternoon")
|
t.AssertEqual(data[1].label, "Good Afternoon")
|
||||||
data = sql.QueryDataP[*greet](db, query)
|
data = sql.QueryDataP[*greet](db, sql_query)
|
||||||
t.AssertEqual(len(data), 2)
|
t.AssertEqual(len(data), 2)
|
||||||
t.AssertEqual(data[0].label, "Hello World")
|
t.AssertEqual(data[0].label, "Hello World")
|
||||||
t.AssertEqual(data[1].label, "Good Afternoon")
|
t.AssertEqual(data[1].label, "Good Afternoon")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MsgstoreTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||||
|
msg := message.SimpleMessage("data")
|
||||||
|
msgstore.Store(db, msg)
|
||||||
|
}
|
||||||
|
|
||||||
func resetSqlite(db *sql.Storage) {
|
func resetSqlite(db *sql.Storage) {
|
||||||
db.Exec(drop_table)
|
db.Exec(drop_table)
|
||||||
db.Exec(sqlite_create_table)
|
db.Exec(sqlite_create_table)
|
||||||
|
@ -69,23 +77,24 @@ func resetPgsql(db *sql.Storage) {
|
||||||
|
|
||||||
// SQL statements
|
// SQL statements
|
||||||
|
|
||||||
var drop_table = `drop table test`
|
const (
|
||||||
|
drop_table = `drop table test`
|
||||||
|
|
||||||
var insert = `insert into test (label) values
|
sql_insert = `insert into test (label) values
|
||||||
('Hello World'),
|
('Hello World'),
|
||||||
('Good Afternoon')`
|
('Good Afternoon')`
|
||||||
|
|
||||||
var query = `select id, label from test `
|
sql_query = `select id, label from test `
|
||||||
|
|
||||||
//where id = $1`
|
//where id = $1`
|
||||||
|
// database-specific SQL statements
|
||||||
|
|
||||||
// database-specific SQL statements
|
// note: ... integer ... primary key ... automatically fills row sequentially
|
||||||
|
sqlite_create_table = `create table test (
|
||||||
// note: ... integer ... primary key ... automatically fills row sequentially
|
|
||||||
var sqlite_create_table = `create table test (
|
|
||||||
id integer not null primary key,
|
id integer not null primary key,
|
||||||
label text)`
|
label text)`
|
||||||
|
|
||||||
var pgsql_create_table = `create table test (
|
pgsql_create_table = `create table test (
|
||||||
id serial not null primary key,
|
id serial not null primary key,
|
||||||
label text)`
|
label text)`
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue