multinode/multinodedb: add dbx support

We want to use dbx ORM in our codebase to have consistency with satellite codebase

Change-Id: I0646c9ef05db54a7b8e92c543ff8517bada2c5b0
This commit is contained in:
crawter 2020-09-30 19:38:28 +03:00 committed by Yehor Butko
parent c4d5625965
commit fa39274ffd
7 changed files with 1103 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import (
"go.uber.org/zap"
"storj.io/storj/multinode"
"storj.io/storj/multinode/mutlinodedb/dbx"
"storj.io/storj/private/dbutil"
"storj.io/storj/private/dbutil/pgutil"
)
@ -26,7 +27,7 @@ var (
//
// architecture: Master Database
type multinodeDB struct {
//*dbx.DB
*dbx.DB
log *zap.Logger
driver string
@ -34,12 +35,6 @@ type multinodeDB struct {
source string
}
// Close closes database.
func (m multinodeDB) Close() error {
// TODO: will be implemented in dbx.
panic("implement me")
}
// New creates instance of database supports postgres.
func New(log *zap.Logger, databaseURL string) (multinode.DB, error) {
driver, source, implementation, err := dbutil.SplitConnStr(databaseURL)

View File

@ -0,0 +1,12 @@
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package dbx
import (
"github.com/spacemonkeygo/monkit/v3"
)
//go:generate sh gen.sh
var mon = monkit.Package()

View File

@ -0,0 +1,16 @@
#!/bin/sh
dbx schema -d pgx multinodedb.dbx .
dbx golang -d pgx -p dbx -t templates multinodedb.dbx .
( echo '//lint:file-ignore * generated file'; cat multinodedb.dbx.go ) > multinodedb.dbx.go.tmp && mv multinodedb.dbx.go.tmp multinodedb.dbx.go
gofmt -r "*sql.Tx -> tagsql.Tx" -w multinodedb.dbx.go
gofmt -r "*sql.Rows -> tagsql.Rows" -w multinodedb.dbx.go
perl -0777 -pi \
-e 's,\t_ "github.com/jackc/pgx/v4/stdlib"\n\),\t_ "github.com/jackc/pgx/v4/stdlib"\n\n\t"storj.io/storj/private/tagsql"\n\),' \
multinodedb.dbx.go
perl -0777 -pi \
-e 's/type DB struct \{\n\t\*sql\.DB/type DB struct \{\n\ttagsql.DB/' \
multinodedb.dbx.go
perl -0777 -pi \
-e 's/\tdb = &DB\{\n\t\tDB: sql_db,/\tdb = &DB\{\n\t\tDB: tagsql.Wrap\(sql_db\),/' \
multinodedb.dbx.go

View File

@ -0,0 +1,17 @@
// dbx.v1 golang multinodedb.dbx .
model node (
key id
field id blob
field name text ( updatable )
field api_secret blob
field logo blob ( updatable )
)
create node ( )
read one (
select node
where node.id = ?
)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
-- AUTOGENERATED BY storj.io/dbx
-- DO NOT EDIT
CREATE TABLE nodes (
id bytea NOT NULL,
name text NOT NULL,
api_secret bytea NOT NULL,
logo bytea NOT NULL,
PRIMARY KEY ( id )
);

View File

@ -0,0 +1,6 @@
{{- define "decl" }}
func (obj *{{ .ReceiverBase }}Impl) {{ .Signature }} {
defer mon.Task()(&ctx)(&err)
{{ .Body }}
}
{{ end -}}