diff --git a/examples/demo/etc/etc.go b/examples/demo/etc/etc.go index f3c9fd3..9a02d4e 100644 --- a/examples/demo/etc/etc.go +++ b/examples/demo/etc/etc.go @@ -7,8 +7,8 @@ import ( "git.sr.ht/~cco/go-scopes/core/action" "git.sr.ht/~cco/go-scopes/logging" "git.sr.ht/~cco/go-scopes/server" + sql "git.sr.ht/~cco/go-scopes/storage" msgstore "git.sr.ht/~cco/go-scopes/storage/message" - "git.sr.ht/~cco/go-scopes/storage/sql" ) func Config() lib.Config { diff --git a/examples/demo/main.go b/examples/demo/main.go index 7400dbc..42161a1 100644 --- a/examples/demo/main.go +++ b/examples/demo/main.go @@ -4,7 +4,7 @@ import ( "demo/etc" "git.sr.ht/~cco/go-scopes/app" - _ "git.sr.ht/~cco/go-scopes/storage/sql/pgsql" + _ "git.sr.ht/~cco/go-scopes/storage/db/pgsql" ) func main() { diff --git a/storage/sql/pgsql/pgsql.go b/storage/db/pgsql/pgsql.go similarity index 100% rename from storage/sql/pgsql/pgsql.go rename to storage/db/pgsql/pgsql.go diff --git a/storage/sql/sqlite/msgstore/tables.sql b/storage/db/sqlite/msgstore/tables.sql similarity index 100% rename from storage/sql/sqlite/msgstore/tables.sql rename to storage/db/sqlite/msgstore/tables.sql diff --git a/storage/sql/sqlite/sqlite.go b/storage/db/sqlite/sqlite.go similarity index 100% rename from storage/sql/sqlite/sqlite.go rename to storage/db/sqlite/sqlite.go diff --git a/storage/message/message.go b/storage/message/message.go index f7f4548..36132a1 100644 --- a/storage/message/message.go +++ b/storage/message/message.go @@ -2,7 +2,7 @@ package message import ( lib "git.sr.ht/~cco/go-scopes" - "git.sr.ht/~cco/go-scopes/storage/sql" + sql "git.sr.ht/~cco/go-scopes/storage" ) func Store(act lib.Action) bool { diff --git a/storage/sql/sql.go b/storage/storage.go similarity index 98% rename from storage/sql/sql.go rename to storage/storage.go index fa7cbea..7e7f8c2 100644 --- a/storage/sql/sql.go +++ b/storage/storage.go @@ -1,4 +1,4 @@ -package sql +package storage import ( "database/sql" @@ -167,5 +167,5 @@ func (db *Storage) logErr(err error, info, inp string) error { } const ( - sql_drop = `drop table %s` + sql_drop = `drop table if exists %s` ) diff --git a/storage/tracking/sql_code.go b/storage/tracking/sql_code.go new file mode 100644 index 0000000..8b57b89 --- /dev/null +++ b/storage/tracking/sql_code.go @@ -0,0 +1,15 @@ +package tracking + +const sql_table = ` +create table {{ .tablename }} ( + trackid {{ .idType }} primary key, + {{- range .headFields -}}{{ . }} text, + {{ end -}} + timestamp timestamptz default current_timestamp, + data {{ .jsonType -}} +); + +{{ range $i, $cols := .indexes }} +create index idx_{{ .i }} on {{ .tablename }} ({{ .cols }}); +{{- end }} +create index idx_timestamp on {{ .tablename }} (timestamp);` diff --git a/storage/tracking/tracking.go b/storage/tracking/tracking.go new file mode 100644 index 0000000..850a978 --- /dev/null +++ b/storage/tracking/tracking.go @@ -0,0 +1,15 @@ +// Package `tracking` defines a generic track (sort of record) type +// and a container type that allows storing of tracks in a SQL database. +package tracking + +import ( + "time" + + lib "git.sr.ht/~cco/go-scopes" +) + +type Track struct { + head map[string]string + timestamp *time.Time + data lib.Payload +} diff --git a/tests/etc/etc_sql.go b/tests/etc/etc_sql.go index 4a25158..8196143 100644 --- a/tests/etc/etc_sql.go +++ b/tests/etc/etc_sql.go @@ -2,7 +2,7 @@ package etc import ( "git.sr.ht/~cco/go-scopes/config" - "git.sr.ht/~cco/go-scopes/storage/sql" + sql "git.sr.ht/~cco/go-scopes/storage" ) func ConfigSqlite() *sql.Cfg { diff --git a/tests/storage_test.go b/tests/storage_test.go index 0e69fc7..fa76708 100644 --- a/tests/storage_test.go +++ b/tests/storage_test.go @@ -5,10 +5,10 @@ import ( "git.sr.ht/~cco/go-scopes/common/testing" "git.sr.ht/~cco/go-scopes/core/message" + sql "git.sr.ht/~cco/go-scopes/storage" + _ "git.sr.ht/~cco/go-scopes/storage/db/pgsql" + _ "git.sr.ht/~cco/go-scopes/storage/db/sqlite" msgstore "git.sr.ht/~cco/go-scopes/storage/message" - "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/sqlite" "git.sr.ht/~cco/go-scopes/tests/etc" )