Skip to content

Commit 0ca1c67

Browse files
author
c9845
committed
Remove and rebuild duplicate translation funcs, fix some other bugs that prevented some usage.
Forgot run-translator funcs were already defined in other files. Removed recently added funcs and combined code. Previous code did not have package-level-global-var funcs for running translators. Add funcs to run deploy and update schema functionality for package-level-global-var. Export some defaults for use when not calling New().
1 parent 8e9eb0f commit 0ca1c67

File tree

6 files changed

+85
-69
lines changed

6 files changed

+85
-69
lines changed

sqldb-deploySchema.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,47 @@ func (c *Config) DeploySchema(opts *DeploySchemaOptions) (err error) {
167167
return
168168
}
169169

170+
// DeploySchema runs the DeployQueries and DeployFuncs specified in a config against
171+
// the database noted in the config. Use this to create your tables, create indexes,
172+
// etc. This will automatically issue a CREATE DATABASE IF NOT EXISTS query.
173+
//
174+
// DeployQueries will be translated via DeployQueryTranslators and any DeployQuery
175+
// errors will be processed by DeployQueryErrorHandlers. Neither of these steps apply
176+
// to DeployFuncs.
177+
//
178+
// Typically this func is run when a flag, i.e.: --deploy-db, is provided.
179+
func DeploySchema(opts *DeploySchemaOptions) (err error) {
180+
return cfg.DeploySchema(opts)
181+
}
182+
170183
// RunDeployQueryTranslators runs the list of DeployQueryTranslators on the provided
171-
// query. This is run in DeploySchema().
172-
func (c *Config) RunDeployQueryTranslators(q string) string {
184+
// query.
185+
//
186+
// This func is called in DeploySchema() but can also be called manually when you want
187+
// to translate a DeployQuery (for example, running a specific DeployQuery as part of
188+
// UpdateSchema).
189+
func (c *Config) RunDeployQueryTranslators(in string) (out string) {
190+
out = in
173191
for _, t := range c.DeployQueryTranslators {
174-
q = t(q)
192+
out = t(out)
193+
}
194+
195+
return out
196+
}
197+
198+
// RunDeployQueryTranslators runs the list of DeployQueryTranslators on the provided
199+
// query.
200+
//
201+
// This func is called in DeploySchema() but can also be called manually when you want
202+
// to translate a DeployQuery (for example, running a specific DeployQuery as part of
203+
// UpdateSchema).
204+
func RunDeployQueryTranslators(in string) (out string) {
205+
out = in
206+
for _, t := range cfg.DeployQueryTranslators {
207+
out = t(out)
175208
}
176209

177-
return q
210+
return out
178211
}
179212

180213
// runDeployQueryErrorHandlers runs the list of DeployQueryErrorHandlers when an error

sqldb-sqlite-mattn.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ const (
2424
sqliteDriverName = "sqlite3"
2525
)
2626

27+
// SQLiteDefaultPragmas defines the list of PRAGMA statments to configure SQLite that
28+
// we use by default.
29+
//
2730
// Placeholder so that this variable is defined for this SQLite library. The mattn
2831
// library defines some default PRAGMAs already so we do not need to define them here.
2932
// However, we need this variable defined since it is checked for in Connect().
30-
var sqliteDefaultPragmas = []string{}
33+
var SQLiteDefaultPragmas = []string{}

sqldb-sqlite-modernc.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ const (
2424
sqliteDriverName = "sqlite"
2525
)
2626

27+
// SQLiteDefaultPragmas defines the list of PRAGMA statments to configure SQLite that
28+
// we use by default.
29+
//
2730
// The [github.com/mattn/go-sqlite3] library sets some PRAGMAs by default. The
28-
// [modernc.org/sqlite]library does not define any default PRAGMAs. However, to
31+
// [modernc.org/sqlite] library does not define any default PRAGMAs. However, to
2932
// make switching between the two database libraries/drivers easier, we define
3033
// some PRAGMAs here to make using a SQLite database more consistent.
3134
//
3235
// https://github.com/mattn/go-sqlite3/blob/ae2a61f847e10e6dd771ecd4e1c55e0421cdc7f9/sqlite3.go#L1086
33-
var sqliteDefaultPragmas = []string{
36+
var SQLiteDefaultPragmas = []string{
3437
"PRAGMA busy_timeout = 5000",
3538
"PRAGMA synchronous = NORMAL",
3639
}

sqldb-updateSchema.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,43 @@ func (c *Config) UpdateSchema(opts *UpdateSchemaOptions) (err error) {
118118
return
119119
}
120120

121+
// UpdateSchema runs the UpdateQueries and UpdateFuncs specified in a config against
122+
// the database noted in the config. Use this to add columns, add indexes, rename
123+
// things, perform data changes, etc.
124+
//
125+
// UpdateQueries will be translated via UpdateQueryTranslators and any UpdateQuery
126+
// errors will be processed by UpdateQueryErrorHandlers. Neither of these steps apply
127+
// to UpdateFuncs.
128+
//
129+
// Typically this func is run when a flag, i.e.: --update-db, is provided.
130+
func UpdateSchema(opts *UpdateSchemaOptions) (err error) {
131+
return cfg.UpdateSchema(opts)
132+
}
133+
121134
// RunUpdateQueryTranslators runs the list of UpdateQueryTranslators on the provided
122-
// query. This is run in Update().
123-
func (c *Config) RunUpdateQueryTranslators(q string) string {
135+
// query.
136+
//
137+
// This func is called in UpdateSchema().
138+
func (c *Config) RunUpdateQueryTranslators(in string) (out string) {
139+
out = in
124140
for _, t := range c.UpdateQueryTranslators {
125-
q = t(q)
141+
out = t(out)
142+
}
143+
144+
return out
145+
}
146+
147+
// RunUpdateQueryTranslators runs the list of UpdateQueryTranslators on the provided
148+
// query.
149+
//
150+
// This func is called in UpdateSchema().
151+
func RunUpdateQueryTranslators(in string) (out string) {
152+
out = in
153+
for _, t := range cfg.UpdateQueryTranslators {
154+
out = t(out)
126155
}
127156

128-
return q
157+
return out
129158
}
130159

131160
// runUpdateQueryErrorHandlers runs the list of UpdateQueryErrorHandlers when an error

sqldb.go

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ var cfg *Config
344344
func New() *Config {
345345
c := new(Config)
346346

347-
c.SQLitePragmas = sqliteDefaultPragmas
348-
c.MapperFunc = defaultMapperFunc
347+
c.SQLitePragmas = SQLiteDefaultPragmas
348+
c.MapperFunc = DefaultMapperFunc
349349
c.LoggingLevel = LogLevelDefault
350350
c.ConnectionOptions = make(map[string]string)
351351

@@ -445,14 +445,14 @@ func Connect() (err error) {
445445
return cfg.Connect()
446446
}
447447

448-
// defaultMapperFunc is the default function used for handling column name formatting
448+
// DefaultMapperFunc is the default function used for handling column name formatting
449449
// when retrieving data from the database and matching up to struct field names. No
450450
// reformatting is done; the column names are returned exactly as they are noted in
451451
// the database schema. This is unlike [sqlx] that lowercases all column names and
452452
// thus requires struct tags to match up against exported struct fields.
453453
//
454454
// See: https://jmoiron.github.io/sqlx/#mapping
455-
func defaultMapperFunc(s string) string {
455+
func DefaultMapperFunc(s string) string {
456456
return s
457457
}
458458

@@ -691,55 +691,3 @@ func AddConnectionOption(key, value string) {
691691
func Type() dbType {
692692
return cfg.Type
693693
}
694-
695-
// RunDeployTranslators runs the list of translators defined on the given query.
696-
//
697-
// This is used when you aren't calling DeploySchema() but you still want to translate
698-
// a DeployQuery. For example, running a specific DeployQuery as part of UpdateSchema().
699-
func RunDeployTranslators(in string) (out string) {
700-
out = in
701-
for _, t := range cfg.DeployQueryTranslators {
702-
out = t(out)
703-
}
704-
705-
return out
706-
}
707-
708-
// RunDeployTranslators runs the list of translators defined on the given query.
709-
//
710-
// This is used when you aren't calling DeploySchema() but you still want to translate
711-
// a DeployQuery. For example, running a specific DeployQuery as part of UpdateSchema().
712-
func (c *Config) RunDeployTranslators(in string) (out string) {
713-
out = in
714-
for _, t := range c.DeployQueryTranslators {
715-
out = t(out)
716-
}
717-
718-
return out
719-
}
720-
721-
// RunUpdateTranslators runs the list of translators defined on the given query.
722-
//
723-
// This is used when you aren't calling UpdateSchema() but you still want to translate
724-
// an UpdateQuery.
725-
func RunUpdateTranslators(in string) (out string) {
726-
out = in
727-
for _, t := range cfg.UpdateQueryTranslators {
728-
out = t(out)
729-
}
730-
731-
return out
732-
}
733-
734-
// RunUpdateTranslators runs the list of translators defined on the given query.
735-
//
736-
// This is used when you aren't calling UpdateSchema() but you still want to translate
737-
// an UpdateQuery.
738-
func (c *Config) RunUpdateTranslators(in string) (out string) {
739-
out = in
740-
for _, t := range c.UpdateQueryTranslators {
741-
out = t(out)
742-
}
743-
744-
return out
745-
}

sqldb_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestNew(t *testing.T) {
1313
c := New()
14-
if len(c.SQLitePragmas) != len(sqliteDefaultPragmas) {
14+
if len(c.SQLitePragmas) != len(SQLiteDefaultPragmas) {
1515
t.FailNow()
1616
return
1717
}
@@ -336,7 +336,7 @@ func TestIsSQLite(t *testing.T) {
336336

337337
func TestDefaultMapperFunc(t *testing.T) {
338338
in := "asdfasdfasdf"
339-
out := defaultMapperFunc(in)
339+
out := DefaultMapperFunc(in)
340340
if in != out {
341341
t.Fatal("defaultMapperFunc modified provided string but should not have.")
342342
return

0 commit comments

Comments
 (0)