add storage and mux handle with handlers
This commit is contained in:
1
storage/migrations/postgres/000001_init_schema.down.sql
Normal file
1
storage/migrations/postgres/000001_init_schema.down.sql
Normal file
@@ -0,0 +1 @@
|
||||
drop table if exists dashboard, package, module, issue, comment;
|
37
storage/migrations/postgres/000001_init_schema.up.sql
Normal file
37
storage/migrations/postgres/000001_init_schema.up.sql
Normal file
@@ -0,0 +1,37 @@
|
||||
create table if not exists dashboard (
|
||||
id serial not null unique primary key ,
|
||||
"uniq_id" uuid not null unique default gen_random_uuid() ,
|
||||
package integer
|
||||
);
|
||||
|
||||
create table if not exists comment (
|
||||
id serial not null unique primary key ,
|
||||
text text ,
|
||||
created timestamp not null default current_timestamp ,
|
||||
updated timestamp
|
||||
);
|
||||
|
||||
create table if not exists module (
|
||||
id serial not null unique primary key ,
|
||||
name varchar not null ,
|
||||
version varchar not null
|
||||
);
|
||||
|
||||
create table if not exists issue (
|
||||
id serial not null unique primary key ,
|
||||
--package integer references package(id) ,
|
||||
modules integer[] ,
|
||||
status integer default 0 ,
|
||||
"desc" varchar
|
||||
);
|
||||
|
||||
create table if not exists package (
|
||||
id serial not null unique primary key ,
|
||||
name varchar not null ,
|
||||
url varchar ,
|
||||
modules integer[] ,
|
||||
issues integer[] ,
|
||||
comments integer[]
|
||||
);
|
||||
|
||||
|
7
storage/postgres/quries.go
Normal file
7
storage/postgres/quries.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package postgres
|
||||
|
||||
const (
|
||||
queryListPackage = `
|
||||
select * from package;
|
||||
`
|
||||
)
|
83
storage/postgres/storage.go
Normal file
83
storage/postgres/storage.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"go.unistack.org/unistack-org/pkgdash/config"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/pgx"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
)
|
||||
|
||||
const (
|
||||
pathMigration = `migrations/postgres`
|
||||
)
|
||||
|
||||
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 := pgx.WithInstance(s.db, &pgx.Config{
|
||||
MigrationsTable: pgx.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 := pgx.WithInstance(s.db, &pgx.Config{
|
||||
MigrationsTable: pgx.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
|
||||
}
|
32
storage/storage.go
Normal file
32
storage/storage.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"go.unistack.org/unistack-org/pkgdash/models"
|
||||
"go.unistack.org/unistack-org/pkgdash/storage/postgres"
|
||||
|
||||
cmsstorage "go.unistack.org/cms-service/storage"
|
||||
)
|
||||
|
||||
//go:embed migrations
|
||||
var fs embed.FS
|
||||
|
||||
var (
|
||||
storages = cmsstorage.NewStorageInterface()
|
||||
)
|
||||
|
||||
func init() {
|
||||
storages.RegisterStorage("postgres", postgres.NewStorageFS(fs))
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
cmsstorage.Migrator
|
||||
|
||||
List(ctx context.Context) ([]*models.Package, error)
|
||||
}
|
||||
|
||||
func NewStorage(name string, db *sql.DB) (interface{}, error) {
|
||||
return storages.NewStorage(name, db)
|
||||
}
|
Reference in New Issue
Block a user