You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-06-17 00:17:59 +02:00
fix schema migration requiring web context
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/schema"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/service/cloudfront"
|
||||
@ -18,7 +19,6 @@ import (
|
||||
"github.com/iancoleman/strcase"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/schema"
|
||||
"gitlab.com/geeks-accelerator/oss/devops/pkg/devdeploy"
|
||||
)
|
||||
|
||||
@ -35,7 +35,7 @@ type Env = string
|
||||
|
||||
var (
|
||||
EnvDev Env = webcontext.Env_Dev
|
||||
EnvStage Env = webcontext.Env_Stage
|
||||
EnvStage Env = webcontext.Env_Stage
|
||||
EnvProd Env = webcontext.Env_Prod
|
||||
)
|
||||
|
||||
|
@ -14,8 +14,8 @@ import (
|
||||
type Function = string
|
||||
|
||||
var (
|
||||
Function_Ddlogscollector = "ddlogscollector"
|
||||
Function_YourNewFunction = "your-new-function"
|
||||
Function_Ddlogscollector = "ddlogscollector"
|
||||
Function_YourNewFunction = "your-new-function"
|
||||
)
|
||||
|
||||
// List of function names used by main.go for help.
|
||||
|
@ -4,13 +4,13 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/schema"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/schema"
|
||||
"gitlab.com/geeks-accelerator/oss/devops/pkg/devdeploy"
|
||||
)
|
||||
|
||||
// RunSchemaMigrationsForTargetEnv executes the build commands for a target service.
|
||||
// RunSchemaMigrationsForTargetEnv executes schema migrations for the target environment.
|
||||
func RunSchemaMigrationsForTargetEnv(log *log.Logger, awsCredentials devdeploy.AwsCredentials, targetEnv Env, isUnittest bool) error {
|
||||
|
||||
cfgCtx, err := NewConfigContext(targetEnv, awsCredentials)
|
||||
@ -34,5 +34,5 @@ func RunSchemaMigrationsForTargetEnv(log *log.Logger, awsCredentials devdeploy.A
|
||||
}
|
||||
defer masterDb.Close()
|
||||
|
||||
return schema.Migrate(context.Background(), masterDb, log, false)
|
||||
return schema.Migrate(context.Background(), targetEnv, masterDb, log, false)
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ecs"
|
||||
"github.com/iancoleman/strcase"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/geeks-accelerator/oss/devops/pkg/devdeploy"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
)
|
||||
@ -33,12 +33,12 @@ var ServiceNames = []Service{
|
||||
// ServiceConfig defines the settings for a service.
|
||||
type ServiceConfig struct {
|
||||
// Required flags.
|
||||
Name string `validate:"required" example:"web-api"`
|
||||
ServiceHostPrimary string `validate:"required" example:"example-project.com"`
|
||||
DesiredCount int `validate:"required" example:"2"`
|
||||
ServiceDir string `validate:"required"`
|
||||
Dockerfile string `validate:"required" example:"./cmd/web-api/Dockerfile"`
|
||||
ReleaseTag string `validate:"required"`
|
||||
Name string `validate:"required" example:"web-api"`
|
||||
ServiceHostPrimary string `validate:"required" example:"example-project.com"`
|
||||
DesiredCount int `validate:"required" example:"2"`
|
||||
ServiceDir string `validate:"required"`
|
||||
Dockerfile string `validate:"required" example:"./cmd/web-api/Dockerfile"`
|
||||
ReleaseTag string `validate:"required"`
|
||||
|
||||
// Optional flags.
|
||||
ServiceHostNames []string `validate:"omitempty" example:"subdomain.example-project.com"`
|
||||
@ -89,7 +89,7 @@ func NewServiceConfig(serviceName string, cfg *devdeploy.Config) (ServiceConfig,
|
||||
|
||||
// Set the hostnames for the service.
|
||||
if cfg.Env == EnvProd {
|
||||
srv.ServiceHostPrimary = "example.saasstartupkit.com"
|
||||
srv.ServiceHostPrimary = "example.saasstartupkit.com"
|
||||
|
||||
// Any hostname listed here that doesn't match the primary hostname will be updated in Route 53 but the
|
||||
// service itself will redirect any requests back to the primary hostname.
|
||||
@ -97,7 +97,7 @@ func NewServiceConfig(serviceName string, cfg *devdeploy.Config) (ServiceConfig,
|
||||
fmt.Sprintf("%s.example.saasstartupkit.com", cfg.Env),
|
||||
}
|
||||
} else {
|
||||
srv.ServiceHostPrimary = fmt.Sprintf("%s.example.saasstartupkit.com", cfg.Env)
|
||||
srv.ServiceHostPrimary = fmt.Sprintf("%s.example.saasstartupkit.com", cfg.Env)
|
||||
}
|
||||
|
||||
case ServiceWebApi:
|
||||
@ -111,8 +111,8 @@ func NewServiceConfig(serviceName string, cfg *devdeploy.Config) (ServiceConfig,
|
||||
|
||||
default:
|
||||
return ServiceConfig{}, errors.Wrapf(devdeploy.ErrInvalidService,
|
||||
"No service config defined for service '%s'",
|
||||
serviceName)
|
||||
"No service config defined for service '%s'",
|
||||
serviceName)
|
||||
}
|
||||
|
||||
// Set the docker file if no custom one has been defined for the service.
|
||||
@ -140,7 +140,6 @@ func (c ServiceConfig) BaseUrl() string {
|
||||
return fmt.Sprintf("%s://%s/", schema, c.ServiceHostPrimary)
|
||||
}
|
||||
|
||||
|
||||
// NewServiceContext returns the ServiceContext for a service that is configured for the target deployment env.
|
||||
func NewServiceContext(serviceName Service, cfg *devdeploy.Config) (*ServiceContext, error) {
|
||||
|
||||
@ -193,7 +192,6 @@ func NewServiceContext(serviceName Service, cfg *devdeploy.Config) (*ServiceCont
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// Service dependant settings.
|
||||
|
||||
@ -306,7 +304,6 @@ func NewServiceContext(serviceName Service, cfg *devdeploy.Config) (*ServiceCont
|
||||
return def, nil
|
||||
}
|
||||
|
||||
|
||||
// Define the ServiceContext for the web-api that will be used for build and deploy.
|
||||
case ServiceWebApi:
|
||||
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"geeks-accelerator/oss/saas-starter-kit/build/cicd/internal/config"
|
||||
"github.com/urfave/cli"
|
||||
"gitlab.com/geeks-accelerator/oss/devops/pkg/devdeploy"
|
||||
)
|
||||
|
||||
|
@ -17,7 +17,7 @@ import (
|
||||
|
||||
// Account represents the Account API method handler set.
|
||||
type Accounts struct {
|
||||
Repository *account.Repository
|
||||
Repository *account.Repository
|
||||
|
||||
// ADD OTHER STATE LIKE THE LOGGER AND CONFIG HERE.
|
||||
}
|
||||
|
@ -5,14 +5,14 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account/account_preference"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/mid"
|
||||
saasSwagger "geeks-accelerator/oss/saas-starter-kit/internal/mid/saas-swagger"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
||||
_ "geeks-accelerator/oss/saas-starter-kit/internal/platform/web/weberror"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account/account_preference"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/project"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/signup"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user"
|
||||
|
@ -12,18 +12,18 @@ import (
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account/account_preference"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/geonames"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/project"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/signup"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account/invite"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/mid"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/weberror"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/project"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/project_route"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/signup"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account/invite"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_auth"
|
||||
|
||||
"github.com/ikeikeikeike/go-sitemap-generator/v2/stm"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
@ -23,7 +23,7 @@ import (
|
||||
type Signup struct {
|
||||
SignupRepo *signup.Repository
|
||||
AuthRepo *user_auth.Repository
|
||||
GeoRepo *geonames.Repository
|
||||
GeoRepo *geonames.Repository
|
||||
MasterDB *sqlx.DB
|
||||
Renderer web.Renderer
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/geonames"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/geonames"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/datatable"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
||||
|
@ -20,15 +20,10 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account/account_preference"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/geonames"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/project"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/signup"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account/invite"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/cmd/web-app/handlers"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/account/account_preference"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/geonames"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/mid"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/devops"
|
||||
@ -39,8 +34,13 @@ import (
|
||||
template_renderer "geeks-accelerator/oss/saas-starter-kit/internal/platform/web/template-renderer"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/weberror"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/project"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/project_route"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/signup"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_account/invite"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/user_auth"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
|
@ -5,10 +5,10 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
"fmt"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/geonames"
|
||||
"github.com/geeks-accelerator/sqlxmigrate"
|
||||
|
@ -3,12 +3,24 @@ package schema
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
||||
"github.com/geeks-accelerator/sqlxmigrate"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
func Migrate(ctx context.Context, masterDb *sqlx.DB, log *log.Logger, isUnittest bool) error {
|
||||
// Migrate is the entry point for performing init schema and running all the migrations.
|
||||
func Migrate(ctx context.Context, targetEnv webcontext.Env, masterDb *sqlx.DB, log *log.Logger, isUnittest bool) error {
|
||||
|
||||
// Set the context with the required values to
|
||||
// process the request.
|
||||
v := webcontext.Values{
|
||||
Now: time.Now(),
|
||||
Env: targetEnv,
|
||||
}
|
||||
ctx = context.WithValue(ctx, webcontext.KeyValues, &v)
|
||||
|
||||
// Load list of Schema migrations and init new sqlxmigrate client
|
||||
migrations := migrationList(ctx, masterDb, log, isUnittest)
|
||||
m := sqlxmigrate.New(masterDb, sqlxmigrate.DefaultOptions, migrations)
|
||||
|
@ -9,11 +9,11 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
||||
"geeks-accelerator/oss/saas-starter-kit/internal/schema"
|
||||
"github.com/lib/pq"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/urfave/cli"
|
||||
sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
|
||||
sqlxtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/jmoiron/sqlx"
|
||||
)
|
||||
@ -59,54 +59,54 @@ func main() {
|
||||
Name: "env",
|
||||
Usage: fmt.Sprintf("target environment, one of [%s]",
|
||||
strings.Join(webcontext.EnvNames, ", ")),
|
||||
Value: "dev",
|
||||
Value: "dev",
|
||||
EnvVar: "ENV",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "host",
|
||||
Usage: "host",
|
||||
Value:"127.0.0.1:5433",
|
||||
Name: "host",
|
||||
Usage: "host",
|
||||
Value: "127.0.0.1:5433",
|
||||
EnvVar: "SCHEMA_DB_HOST",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "user",
|
||||
Usage: "username",
|
||||
Value: "postgres",
|
||||
Name: "user",
|
||||
Usage: "username",
|
||||
Value: "postgres",
|
||||
EnvVar: "SCHEMA_DB_USER",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "pass",
|
||||
Usage: "password",
|
||||
Value: "postgres",
|
||||
Name: "pass",
|
||||
Usage: "password",
|
||||
Value: "postgres",
|
||||
EnvVar: "SCHEMA_DB_PASS",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "database",
|
||||
Usage: "name of the default",
|
||||
Value: "shared",
|
||||
Name: "database",
|
||||
Usage: "name of the default",
|
||||
Value: "shared",
|
||||
EnvVar: "SCHEMA_DB_DATABASE",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "driver",
|
||||
Usage: "database drive to use for connection",
|
||||
Value: "postgres",
|
||||
Name: "driver",
|
||||
Usage: "database drive to use for connection",
|
||||
Value: "postgres",
|
||||
EnvVar: "SCHEMA_DB_DRIVER",
|
||||
},
|
||||
cli.BoolTFlag{
|
||||
Name: "disable-tls",
|
||||
Usage: "disable TLS for the database connection",
|
||||
Name: "disable-tls",
|
||||
Usage: "disable TLS for the database connection",
|
||||
EnvVar: "SCHEMA_DB_DISABLE_TLS",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
targetEnv := c.String("env")
|
||||
var dbInfo = DB {
|
||||
Host : c.String("host"),
|
||||
User : c.String("user"),
|
||||
Pass : c.String("pass"),
|
||||
Database : c.String("database"),
|
||||
targetEnv := c.String("env")
|
||||
var dbInfo = DB{
|
||||
Host: c.String("host"),
|
||||
User: c.String("user"),
|
||||
Pass: c.String("pass"),
|
||||
Database: c.String("database"),
|
||||
|
||||
Driver : c.String("driver"),
|
||||
Driver: c.String("driver"),
|
||||
DisableTLS: c.Bool("disable-tls"),
|
||||
}
|
||||
|
||||
@ -160,16 +160,10 @@ func runMigrate(log *log.Logger, targetEnv string, dbInfo DB) error {
|
||||
// =========================================================================
|
||||
// Start Migrations
|
||||
|
||||
// Set the context with the required values to
|
||||
// process the request.
|
||||
v := webcontext.Values{
|
||||
Now: time.Now(),
|
||||
Env: targetEnv,
|
||||
}
|
||||
ctx := context.WithValue(context.Background(), webcontext.KeyValues, &v)
|
||||
ctx := context.Background()
|
||||
|
||||
// Execute the migrations
|
||||
if err = schema.Migrate(ctx, masterDb, log, false); err != nil {
|
||||
if err = schema.Migrate(ctx, env, masterDb, log, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user