Skip to content

Commit

Permalink
Merge pull request #1414 from morganhein/feat/add-no-schema-argument
Browse files Browse the repository at this point in the history
wip: added --no-schema option scaffolding, needs tests
  • Loading branch information
stephenafamo authored Nov 13, 2024
2 parents e4d4b94 + 583bd69 commit dafaf41
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type templateData struct {
RQ string

// Control various generation features
// NoOutputSchema, which is also used for generation, is defined in the drivers configuration
AddGlobal bool
AddPanic bool
AddSoftDeletes bool
Expand Down
15 changes: 15 additions & 0 deletions drivers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ func (c Config) DefaultString(key, def string) string {
return str
}

// DefaultBool retrieves a non-empty bool or the default value provided.
func (c Config) DefaultBool(key string, def bool) bool {
b, ok := c[key]
if !ok {
return def
}

bul, ok := b.(bool)
if !ok {
return def
}

return bul
}

// Int retrieves an int, the bool says if it exists, is of the appropriate type,
// and is non-zero. Coerces float64 to int because JSON and Javascript kinda suck.
func (c Config) Int(key string) (int, bool) {
Expand Down
1 change: 1 addition & 0 deletions drivers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
ConfigBlacklist = "blacklist"
ConfigWhitelist = "whitelist"
ConfigSchema = "schema"
ConfigNoOutputSchema = "no-output-schema" // Determine if templates/output include a schema, even if we used one to gather info
ConfigAddEnumTypes = "add-enum-types"
ConfigEnumNullPrefix = "enum-null-prefix"
ConfigConcurrency = "concurrency"
Expand Down
13 changes: 11 additions & 2 deletions drivers/sqlboiler-psql/driver/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,20 @@ func (p *PostgresDriver) Assemble(config drivers.Config) (dbinfo *drivers.DBInfo
port := config.DefaultInt(drivers.ConfigPort, 5432)
sslmode := config.DefaultString(drivers.ConfigSSLMode, "require")
schema := config.DefaultString(drivers.ConfigSchema, "public")
noOutputSchema := config.DefaultBool(drivers.ConfigNoOutputSchema, false)
whitelist, _ := config.StringSlice(drivers.ConfigWhitelist)
blacklist, _ := config.StringSlice(drivers.ConfigBlacklist)
concurrency := config.DefaultInt(drivers.ConfigConcurrency, drivers.DefaultConcurrency)

useSchema := schema != "public"
switch {
case noOutputSchema:
break
case schema == "public":
noOutputSchema = true
default:
// just to be explicit, even though it's the default in the getter
noOutputSchema = false
}

p.addEnumTypes, _ = config[drivers.ConfigAddEnumTypes].(bool)
p.enumNullPrefix = strmangle.TitleCase(config.DefaultString(drivers.ConfigEnumNullPrefix, "Null"))
Expand Down Expand Up @@ -135,7 +144,7 @@ func (p *PostgresDriver) Assemble(config drivers.Config) (dbinfo *drivers.DBInfo
RQ: '"',

UseIndexPlaceholders: true,
UseSchema: useSchema,
UseSchema: !noOutputSchema,
UseDefaultKeyword: true,
},
}
Expand Down
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func main() {
rootCmd.PersistentFlags().BoolP("no-auto-timestamps", "", false, "Disable automatic timestamps for created_at/updated_at")
rootCmd.PersistentFlags().BoolP("no-driver-templates", "", false, "Disable parsing of templates defined by the database driver")
rootCmd.PersistentFlags().BoolP("no-back-referencing", "", false, "Disable back referencing in the loaded relationship structs")
rootCmd.PersistentFlags().BoolP("no-schema", "", false, "Disable generating a schema in the output")
rootCmd.PersistentFlags().BoolP("always-wrap-errors", "", false, "Wrap all returned errors with stacktraces, also sql.ErrNoRows")
rootCmd.PersistentFlags().BoolP("add-global-variants", "", false, "Enable generation for global variants")
rootCmd.PersistentFlags().BoolP("add-panic-variants", "", false, "Enable generation for panic variants")
Expand Down Expand Up @@ -213,11 +214,12 @@ func preRun(cmd *cobra.Command, args []string) error {

// Configure the driver
cmdConfig.DriverConfig = map[string]interface{}{
"whitelist": viper.GetStringSlice(driverName + ".whitelist"),
"blacklist": viper.GetStringSlice(driverName + ".blacklist"),
"add-enum-types": cmdConfig.AddEnumTypes,
"enum-null-prefix": cmdConfig.EnumNullPrefix,
"foreign-keys": cmdConfig.ForeignKeys,
"whitelist": viper.GetStringSlice(driverName + ".whitelist"),
"blacklist": viper.GetStringSlice(driverName + ".blacklist"),
drivers.ConfigNoOutputSchema: viper.GetBool("no-schema"),
"add-enum-types": cmdConfig.AddEnumTypes,
"enum-null-prefix": cmdConfig.EnumNullPrefix,
"foreign-keys": cmdConfig.ForeignKeys,
}

keys := allKeys(driverName)
Expand Down

0 comments on commit dafaf41

Please sign in to comment.