1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-08-06 22:32:51 +02:00

Reduce countries loaded for geonames to US only by default.

Schema loads in around 1min now.
This commit is contained in:
Lee Brown
2019-08-12 10:26:00 -08:00
parent 18b4e7f060
commit 05ff93555c
6 changed files with 36 additions and 20 deletions

View File

@ -11,6 +11,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
"github.com/huandu/go-sqlbuilder" "github.com/huandu/go-sqlbuilder"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -24,9 +25,12 @@ const (
geonamesTableName = "geonames" geonamesTableName = "geonames"
) )
var (
// List of country codes that will geonames will be downloaded for. // List of country codes that will geonames will be downloaded for.
ValidGeonameCountries = []string{ func ValidGeonameCountries(ctx context.Context) []string {
if webcontext.ContextEnv(ctx) == webcontext.Env_Dev {
return []string{"US"}
}
return []string{
"AD", "AR", "AS", "AT", "AU", "AX", "BD", "BE", "BG", "BM", "AD", "AR", "AS", "AT", "AU", "AX", "BD", "BE", "BG", "BM",
"BR", "BY", "CA", "CH", "CO", "CR", "CZ", "DE", "DK", "DO", "BR", "BY", "CA", "CH", "CO", "CR", "CZ", "DE", "DK", "DO",
"DZ", "ES", "FI", "FO", "FR", "GB", "GF", "GG", "GL", "GP", "DZ", "ES", "FI", "FO", "FR", "GB", "GF", "GG", "GL", "GP",
@ -36,7 +40,7 @@ var (
"PK", "PL", "PM", "PR", "PT", "RE", "RO", "RU", "SE", "SI", "PK", "PL", "PM", "PR", "PT", "RE", "RO", "RU", "SE", "SI",
"SJ", "SK", "SM", "TH", "TR", "UA", "US", "UY", "VA", "VI", "SJ", "SK", "SM", "TH", "TR", "UA", "US", "UY", "VA", "VI",
"WF", "YT", "ZA"} "WF", "YT", "ZA"}
) }
// FindGeonames .... // FindGeonames ....
func FindGeonames(ctx context.Context, dbConn *sqlx.DB, orderBy, where string, args ...interface{}) ([]*Geoname, error) { func FindGeonames(ctx context.Context, dbConn *sqlx.DB, orderBy, where string, args ...interface{}) ([]*Geoname, error) {
@ -194,7 +198,7 @@ func LoadGeonames(ctx context.Context, rr chan<- interface{}, countries ...strin
defer close(rr) defer close(rr)
if len(countries) == 0 { if len(countries) == 0 {
countries = ValidGeonameCountries countries = ValidGeonameCountries(ctx)
} }
for _, country := range countries { for _, country := range countries {

View File

@ -1,6 +1,7 @@
package schema package schema
import ( import (
"context"
"log" "log"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@ -8,7 +9,7 @@ import (
// initSchema runs before any migrations are executed. This happens when no other migrations // initSchema runs before any migrations are executed. This happens when no other migrations
// have previously been executed. // have previously been executed.
func initSchema(db *sqlx.DB, log *log.Logger, isUnittest bool) func(*sqlx.DB) error { func initSchema(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest bool) func(*sqlx.DB) error {
f := func(db *sqlx.DB) error { f := func(db *sqlx.DB) error {
return nil return nil
} }

View File

@ -18,7 +18,7 @@ import (
// migrationList returns a list of migrations to be executed. If the id of the // migrationList returns a list of migrations to be executed. If the id of the
// migration already exists in the migrations table it will be skipped. // migration already exists in the migrations table it will be skipped.
func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate.Migration { func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate.Migration {
return []*sqlxmigrate.Migration{ return []*sqlxmigrate.Migration{
// Create table users. // Create table users.
{ {
@ -213,7 +213,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
}, },
// Load new geonames table. // Load new geonames table.
{ {
ID: "20190731-02b", ID: "20190731-02h",
Migrate: func(tx *sql.Tx) error { Migrate: func(tx *sql.Tx) error {
schemas := []string{ schemas := []string{
@ -253,7 +253,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
} else { } else {
resChan := make(chan interface{}) resChan := make(chan interface{})
go geonames.LoadGeonames(context.Background(), resChan) go geonames.LoadGeonames(ctx, resChan)
for r := range resChan { for r := range resChan {
switch v := r.(type) { switch v := r.(type) {
@ -288,7 +288,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
}, },
// Load new countries table. // Load new countries table.
{ {
ID: "20190731-02d", ID: "20190731-02f",
Migrate: func(tx *sql.Tx) error { Migrate: func(tx *sql.Tx) error {
schemas := []string{ schemas := []string{
@ -489,7 +489,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
}, },
// Load new country_timezones table. // Load new country_timezones table.
{ {
ID: "20190731-03d", ID: "20190731-03e",
Migrate: func(tx *sql.Tx) error { Migrate: func(tx *sql.Tx) error {
queries := []string{ queries := []string{

View File

@ -1,21 +1,22 @@
package schema package schema
import ( import (
"context"
"log" "log"
"github.com/geeks-accelerator/sqlxmigrate" "github.com/geeks-accelerator/sqlxmigrate"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
func Migrate(masterDb *sqlx.DB, log *log.Logger, isUnittest bool) error { func Migrate(ctx context.Context, masterDb *sqlx.DB, log *log.Logger, isUnittest bool) error {
// Load list of Schema migrations and init new sqlxmigrate client // Load list of Schema migrations and init new sqlxmigrate client
migrations := migrationList(masterDb, log, isUnittest) migrations := migrationList(ctx, masterDb, log, isUnittest)
m := sqlxmigrate.New(masterDb, sqlxmigrate.DefaultOptions, migrations) m := sqlxmigrate.New(masterDb, sqlxmigrate.DefaultOptions, migrations)
m.SetLogger(log) m.SetLogger(log)
// Append any schema that need to be applied if this is a fresh migration // Append any schema that need to be applied if this is a fresh migration
// ie. the migrations database table does not exist. // ie. the migrations database table does not exist.
m.InitSchema(initSchema(masterDb, log, isUnittest)) m.InitSchema(initSchema(ctx, masterDb, log, isUnittest))
// Execute the migrations // Execute the migrations
return m.Migrate() return m.Migrate()

View File

@ -861,7 +861,6 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
log.Printf("\t%s\tDefaults set.", tests.Success) log.Printf("\t%s\tDefaults set.", tests.Success)
} }
r, err := regexp.Compile(`^(\d+)`) r, err := regexp.Compile(`^(\d+)`)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, errors.WithStack(err)

View File

@ -1,11 +1,14 @@
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"expvar" "expvar"
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
"log" "log"
"net/url" "net/url"
"os" "os"
"time"
"geeks-accelerator/oss/saas-starter-kit/internal/platform/flag" "geeks-accelerator/oss/saas-starter-kit/internal/platform/flag"
"geeks-accelerator/oss/saas-starter-kit/internal/schema" "geeks-accelerator/oss/saas-starter-kit/internal/schema"
@ -125,8 +128,16 @@ func main() {
// ========================================================================= // =========================================================================
// Start Migrations // Start Migrations
// Set the context with the required values to
// process the request.
v := webcontext.Values{
Now: time.Now(),
Env: cfg.Env,
}
ctx := context.WithValue(context.Background(), webcontext.KeyValues, &v)
// Execute the migrations // Execute the migrations
if err = schema.Migrate(masterDb, log, false); err != nil { if err = schema.Migrate(ctx, masterDb, log, false); err != nil {
log.Fatalf("main : Migrate : %v", err) log.Fatalf("main : Migrate : %v", err)
} }
log.Printf("main : Migrate : Completed") log.Printf("main : Migrate : Completed")