-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
69 lines (56 loc) · 1.47 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package grpc
import (
"context"
"github.com/alexfalkowski/go-service/meta"
v1 "github.com/alexfalkowski/migrieren/api/migrieren/v1"
"github.com/alexfalkowski/migrieren/migrate"
"github.com/alexfalkowski/migrieren/migrate/migrator"
"go.uber.org/fx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ServerParams for gRPC.
type ServerParams struct {
fx.In
Config *migrate.Config
Migrator migrator.Migrator
}
// NewServer for gRPC.
func NewServer(params ServerParams) v1.ServiceServer {
return &Server{config: params.Config, migrator: params.Migrator}
}
// Server for gRPC.
type Server struct {
v1.UnimplementedServiceServer
migrator migrator.Migrator
config *migrate.Config
}
// Migrate for gRPC.
func (s *Server) Migrate(ctx context.Context, req *v1.MigrateRequest) (*v1.MigrateResponse, error) {
db := req.GetDatabase()
ver := req.GetVersion()
resp := &v1.MigrateResponse{
Migration: &v1.Migration{
Database: db,
Version: ver,
},
}
d := s.config.Database(db)
if d == nil {
return resp, status.Error(codes.NotFound, db+": not found")
}
u, err := d.GetURL()
if err != nil {
return resp, status.Error(codes.Internal, err.Error())
}
logs, err := s.migrator.Migrate(ctx, d.Source, u, ver)
if err != nil {
return resp, status.Error(codes.Internal, err.Error())
}
resp.Meta = s.meta(ctx)
resp.Migration.Logs = logs
return resp, nil
}
func (s *Server) meta(ctx context.Context) map[string]string {
return meta.CamelStrings(ctx, "")
}