add sqlite

This commit is contained in:
Денис Евстигнеев 2023-08-10 22:16:14 +03:00
parent a5a6c683a4
commit 6eb76b1d15
11 changed files with 364 additions and 11 deletions

1
.gitignore vendored
View File

@ -27,3 +27,4 @@ bin/
tmp/ tmp/
/pkgdash /pkgdash
*.sqlite

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/jackc/pgtype v1.14.0 github.com/jackc/pgtype v1.14.0
github.com/lib/pq v1.10.2 github.com/lib/pq v1.10.2
github.com/mattn/go-sqlite3 v1.14.16
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
go.unistack.org/cms-service v0.0.1 go.unistack.org/cms-service v0.0.1
go.unistack.org/micro-client-http/v3 v3.9.3 go.unistack.org/micro-client-http/v3 v3.9.3

1
go.sum
View File

@ -747,6 +747,7 @@ github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vq
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=

View File

@ -2,16 +2,17 @@ package handler
import ( import (
"context" "context"
"io"
"net/http"
cmsstorage "go.unistack.org/cms-service/storage" cmsstorage "go.unistack.org/cms-service/storage"
"go.unistack.org/micro/v3" "go.unistack.org/micro/v3"
"go.unistack.org/micro/v3/errors" "go.unistack.org/micro/v3/errors"
"go.unistack.org/unistack-org/pkgdash/config" "go.unistack.org/unistack-org/pkgdash/config"
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate" pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
"go.unistack.org/unistack-org/pkgdash/service/client_git" cligit "go.unistack.org/unistack-org/pkgdash/service/client_git"
"go.unistack.org/unistack-org/pkgdash/storage" "go.unistack.org/unistack-org/pkgdash/storage"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"io"
"net/http"
) )
type Handler struct { type Handler struct {
@ -22,7 +23,7 @@ type Handler struct {
protojson.MarshalOptions protojson.MarshalOptions
protojson.UnmarshalOptions protojson.UnmarshalOptions
git client_git.Client git cligit.Client
chanUrl chan *pb.AddPackageRsp chanUrl chan *pb.AddPackageRsp
} }
@ -150,7 +151,7 @@ func (h *Handler) AddPackage(w http.ResponseWriter, r *http.Request) {
logger.Debug(ctx, "Success finish addPackage") logger.Debug(ctx, "Success finish addPackage")
} }
func NewHandler(svc micro.Service, w writer, client client_git.Client) *Handler { func NewHandler(svc micro.Service, w writer, client cligit.Client) *Handler {
h := &Handler{ h := &Handler{
svc: svc, svc: svc,
writer: w, writer: w,

View File

@ -7,11 +7,12 @@ import (
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate" pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
"go.unistack.org/unistack-org/pkgdash/storage" "go.unistack.org/unistack-org/pkgdash/storage"
"go.unistack.org/unistack-org/pkgdash/storage/postgres" "go.unistack.org/unistack-org/pkgdash/storage/postgres"
"go.unistack.org/unistack-org/pkgdash/storage/sqlite"
"google.golang.org/protobuf/types/known/wrapperspb" "google.golang.org/protobuf/types/known/wrapperspb"
"testing" "testing"
) )
func TestClient(t *testing.T) { func TestClientPG(t *testing.T) {
dsn := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable", "test", "123", "localhost", "5432", "postgres") dsn := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable", "test", "123", "localhost", "5432", "postgres")
conn, err := sql.Open("postgres", dsn) conn, err := sql.Open("postgres", dsn)
if err != nil { if err != nil {
@ -47,3 +48,39 @@ func TestClient(t *testing.T) {
<-cli.Done() <-cli.Done()
} }
func TestClientLite(t *testing.T) {
conn, err := sql.Open("sqlite3", "../../identifier.sqlite")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
if err = conn.Ping(); err != nil {
t.Fatal(err)
}
st, err := sqlite.NewStorage(conn)
if err != nil {
t.Fatal(err)
}
s, ok := st.(storage.Storage)
if !ok {
t.Fatal("typecast error")
}
ctx, cancel := context.WithCancel(context.Background())
_ = cancel
cli := NewClient(1)
ch := cli.Run(ctx, s)
data := &pb.AddPackageRsp{
Name: wrapperspb.String("test"),
Url: wrapperspb.String("https://github.com/dantedenis/service_history.git"),
}
ch <- data
<-cli.Done()
}

View File

@ -56,26 +56,26 @@ func NewService(ctx context.Context) (micro.Service, error) {
h := handler.NewHandler(svc, writer, client_git.NewClient(5)) h := handler.NewHandler(svc, writer, client_git.NewClient(5))
if err := svc.Init( if err = svc.Init(
micro.AfterStart(func(_ context.Context) error { micro.AfterStart(func(_ context.Context) error {
return h.Init(svc.Options().Context) return h.Init(svc.Options().Context)
}), }),
micro.BeforeStart(func(ctx context.Context) error { micro.BeforeStart(func(ctx context.Context) error {
if err := config.Load(ctx, cs, config.LoadOverride(true)); err != nil { if err = config.Load(ctx, cs, config.LoadOverride(true)); err != nil {
return err return err
} }
if err := config.Validate(ctx, cfg); err != nil { if err = config.Validate(ctx, cfg); err != nil {
return err return err
} }
if err := svc.Init( if err = svc.Init(
micro.Name(cfg.Service.Name), micro.Name(cfg.Service.Name),
micro.Version(cfg.Service.Version), micro.Version(cfg.Service.Version),
); err != nil { ); err != nil {
return err return err
} }
if err := svc.Server("http").Init( if err = svc.Server("http").Init(
server.Address(cfg.App.Address), server.Address(cfg.App.Address),
server.Name(cfg.Service.Name), server.Name(cfg.Service.Name),
server.Version(cfg.Service.Version), server.Version(cfg.Service.Version),

View File

@ -0,0 +1,5 @@
drop table if exists dashboard ;
drop table if exists package ;
drop table if exists module ;
drop table if exists issue ;
drop table if exists comment;

View File

@ -0,0 +1,38 @@
create table if not exists dashboard (
id integer primary key autoincrement not null ,
"uuid" uuid not null unique ,
package integer[] default '{}'
);
create table if not exists comment (
id integer primary key autoincrement not null ,
"text" text ,
created timestamp not null default current_timestamp ,
updated timestamp
);
create table if not exists module (
id integer primary key autoincrement not null ,
name varchar not null ,
version varchar not null ,
last_version varchar not null
);
create table if not exists issue (
id serial not null unique primary key ,
--package integer references package(id) ,
modules integer[] default '[]',
status integer default 0 ,
"desc" varchar
);
create table if not exists package (
id integer primary key autoincrement not null ,
name varchar not null ,
url varchar ,
modules integer[] default '[]',
issues integer[] default '[]',
comments integer[] default '[]'
);

28
storage/sqlite/quries.go Normal file
View File

@ -0,0 +1,28 @@
package sqlite
const (
queryListPackage = `
select
id,
name,
url,
comments
--modules,
--issues,
from package;
`
queryAddComment = `
insert into comment(text) values ($1) ;
update package
set comments = json_insert(comments, '$[#]', ( select last_insert_rowid() as id from comment ))
where id = $2 ;
`
queryAddPackage = `
insert into package(name, url, modules) values ($1, $2, $3);
`
queryInsMsgGetIDs = `
insert into module(name, version, last_version) values
%s
returning id;
`
)

239
storage/sqlite/storage.go Normal file
View File

@ -0,0 +1,239 @@
package sqlite
import (
"context"
"database/sql"
"embed"
"errors"
"fmt"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/lib/pq"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/unistack-org/pkgdash/config"
"go.unistack.org/unistack-org/pkgdash/models"
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
)
const (
pathMigration = `migrations/sqlite`
)
type Postgres struct {
db *sql.DB
fs embed.FS
}
func NewStorage(db *sql.DB) (interface{}, error) {
return &Postgres{db: db}, nil
}
func NewStorageFS(fs embed.FS) func(*sql.DB) (interface{}, error) {
return func(db *sql.DB) (interface{}, error) {
return &Postgres{db: db, fs: fs}, nil
}
}
func (s *Postgres) MigrateUp() error {
driver, err := sqlite.WithInstance(s.db, &sqlite.Config{
MigrationsTable: sqlite.DefaultMigrationsTable,
DatabaseName: config.ServiceName,
})
if err != nil {
return err
}
source, err := iofs.New(s.fs, pathMigration)
if err != nil {
return err
}
// TODO: pass own logger
m, err := migrate.NewWithInstance("fs", source, config.ServiceName, driver)
if err != nil {
return err
}
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}
return nil
}
func (s *Postgres) MigrateDown() error {
driver, err := sqlite.WithInstance(s.db, &sqlite.Config{
MigrationsTable: sqlite.DefaultMigrationsTable,
DatabaseName: config.ServiceName,
})
if err != nil {
return err
}
source, err := iofs.New(s.fs, pathMigration)
if err != nil {
return err
}
// TODO: pass own logger
m, err := migrate.NewWithInstance("fs", source, config.ServiceName, driver)
if err != nil {
return err
}
if err = m.Down(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return err
}
return nil
}
func (s *Postgres) UpdatePackage(ctx context.Context, rsp *pb.UpdatePackageRsp) error {
panic("need implement")
}
func (s *Postgres) ListPackage(ctx context.Context) (models.ListPackage, error) {
rows, err := s.db.QueryContext(ctx, queryListPackage)
if err != nil {
return nil, err
}
defer func() {
if err = rows.Close(); err != nil {
return
}
err = rows.Err()
}()
result := make([]*models.Package, 0)
for rows.Next() {
tmp := &models.Package{}
if err = rows.Scan(
&tmp.ID,
&tmp.Name,
&tmp.URL,
pq.Array(&tmp.Comments),
); err != nil {
return nil, err
}
}
return result, err
}
func (s *Postgres) AddComment(ctx context.Context, rsp *pb.AddCommentRsp) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() {
if err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
logger.Errorf(ctx, "AddComment: unable to rollback: %v", rollbackErr)
}
} else {
err = tx.Commit()
}
}()
res, err := tx.ExecContext(ctx, queryAddComment, rsp.Text, rsp.IdPackage.Value)
if err != nil {
return err
}
if aff, affErr := res.RowsAffected(); err != nil {
err = affErr
} else if aff == 0 {
err = errors.New("rows affected is 0")
}
return err
}
func (s *Postgres) AddPackage(ctx context.Context, rsp *pb.AddPackageRsp) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() {
if err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
logger.Errorf(ctx, "AddPackage: unable to rollback: %v", rollbackErr)
}
} else {
err = tx.Commit()
}
}()
res, err := tx.ExecContext(ctx, queryAddPackage, rsp.Name.Value, rsp.Url.Value, pq.Array(rsp.Modules))
if err != nil {
return err
}
if aff, affErr := res.RowsAffected(); err != nil {
err = affErr
} else if aff == 0 {
err = errors.New("rows affected is 0")
}
return err
}
func (s *Postgres) InsertButchModules(ctx context.Context, rsp []models.Module) ([]uint64, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
logger.Errorf(ctx, "AddPackage: unable to rollback: %v", rollbackErr)
}
} else {
err = tx.Commit()
}
}()
query := generateQuery(rsp)
rows, err := tx.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer func() {
if err = rows.Close(); err != nil {
return
}
err = rows.Err()
}()
result := make([]uint64, 0)
for rows.Next() {
tmp := uint64(0)
if err = rows.Scan(&tmp); err != nil {
return nil, err
}
result = append(result, tmp)
}
return result, err
}
func generateQuery(rsp []models.Module) string {
const pattern = `%c('%s', '%s', '%s')`
build := strings.Builder{}
comma := ' '
for i := range rsp {
str := fmt.Sprintf(pattern, comma, rsp[i].Name, rsp[i].Version, rsp[i].LastVersion)
build.WriteString(str)
comma = ','
}
return fmt.Sprintf(queryInsMsgGetIDs, build.String())
}

View File

@ -7,6 +7,7 @@ import (
"go.unistack.org/unistack-org/pkgdash/models" "go.unistack.org/unistack-org/pkgdash/models"
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate" pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
"go.unistack.org/unistack-org/pkgdash/storage/postgres" "go.unistack.org/unistack-org/pkgdash/storage/postgres"
"go.unistack.org/unistack-org/pkgdash/storage/sqlite"
cmsstorage "go.unistack.org/cms-service/storage" cmsstorage "go.unistack.org/cms-service/storage"
) )
@ -20,6 +21,7 @@ var (
func init() { func init() {
storages.RegisterStorage("postgres", postgres.NewStorageFS(fs)) storages.RegisterStorage("postgres", postgres.NewStorageFS(fs))
storages.RegisterStorage("sqlite", sqlite.NewStorageFS(fs))
} }
type Storage interface { type Storage interface {