Rename Exclude -> Blacklist

This commit is contained in:
Aaron L 2016-09-08 22:41:57 -07:00
parent c65c1f6a2c
commit 16b6a2b176
10 changed files with 40 additions and 41 deletions

View file

@ -223,12 +223,12 @@ not to pass them through the command line or environment variables:
| Name | Default |
| --- | --- |
| basedir | none |
| schema | "public" |
| schema | "public" |
| pkgname | "models" |
| output | "models" |
| whitelist | [ ] |
| exclude | [ ] |
| tag | [ ] |
| whitelist | [] |
| blacklist | [] |
| tag | [] |
| debug | false |
| no-hooks | false |
| no-tests | false |
@ -261,14 +261,14 @@ Examples:
sqlboiler postgres
Flags:
-b, --basedir string The base directory has the templates and templates_test folders
-b, --blacklist stringSlice Do not include these tables in your generated package
-w, --whitelist stringSlice Only include these tables in your generated package
-x, --exclude stringSlice Tables to be excluded from the generated package
-s, --schema string The name of your database schema, for databases that support real schemas (default "public")
-p, --pkgname string The name you wish to assign to your generated package (default "models")
-o, --output string The name of the folder to output to (default "models")
-t, --tag stringSlice Struct tags to be included on your models in addition to json, yaml, toml
-d, --debug Debug mode prints stack traces on error
--basedir string The base directory has the templates and templates_test folders
--no-auto-timestamps Disable automatic timestamps for created_at/updated_at
--no-hooks Disable hooks feature for your models
--no-tests Disable generated go test files

View file

@ -9,12 +9,12 @@ import (
type MockDriver struct{}
// TableNames returns a list of mock table names
func (m *MockDriver) TableNames(schema string, whitelist, exclude []string) ([]string, error) {
func (m *MockDriver) TableNames(schema string, whitelist, blacklist []string) ([]string, error) {
if len(whitelist) > 0 {
return whitelist, nil
}
tables := []string{"pilots", "jets", "airports", "licenses", "hangars", "languages", "pilot_languages"}
return strmangle.SetComplement(tables, exclude), nil
return strmangle.SetComplement(tables, blacklist), nil
}
// Columns returns a list of mock columns

View file

@ -73,9 +73,8 @@ func (m *MySQLDriver) UseLastInsertID() bool {
// TableNames connects to the postgres database and
// retrieves all table names from the information_schema where the
// table schema is public. It excludes common migration tool tables
// such as gorp_migrations
func (m *MySQLDriver) TableNames(schema string, whitelist, exclude []string) ([]string, error) {
// table schema is public.
func (m *MySQLDriver) TableNames(schema string, whitelist, blacklist []string) ([]string, error) {
var names []string
query := fmt.Sprintf(`select table_name from information_schema.tables where table_schema = ?`)
@ -85,10 +84,10 @@ func (m *MySQLDriver) TableNames(schema string, whitelist, exclude []string) ([]
for _, w := range whitelist {
args = append(args, w)
}
} else if len(exclude) > 0 {
query += fmt.Sprintf("and table_name not in (%s);", strings.Repeat(",?", len(exclude))[1:])
for _, e := range exclude {
args = append(args, e)
} else if len(blacklist) > 0 {
query += fmt.Sprintf("and table_name not in (%s);", strings.Repeat(",?", len(blacklist))[1:])
for _, b := range blacklist {
args = append(args, b)
}
}

View file

@ -82,8 +82,8 @@ func (p *PostgresDriver) UseLastInsertID() bool {
// TableNames connects to the postgres database and
// retrieves all table names from the information_schema where the
// table schema is schema. It uses a whitelist and exclude list.
func (p *PostgresDriver) TableNames(schema string, whitelist, exclude []string) ([]string, error) {
// table schema is schema. It uses a whitelist and blacklist.
func (p *PostgresDriver) TableNames(schema string, whitelist, blacklist []string) ([]string, error) {
var names []string
query := fmt.Sprintf(`select table_name from information_schema.tables where table_schema = ?`)
@ -93,10 +93,10 @@ func (p *PostgresDriver) TableNames(schema string, whitelist, exclude []string)
for _, w := range whitelist {
args = append(args, w)
}
} else if len(exclude) > 0 {
query += fmt.Sprintf("and table_name not in (%s);", strmangle.Placeholders(len(exclude), 1, 1))
for _, e := range exclude {
args = append(args, e)
} else if len(blacklist) > 0 {
query += fmt.Sprintf("and table_name not in (%s);", strmangle.Placeholders(len(blacklist), 1, 1))
for _, b := range blacklist {
args = append(args, b)
}
}

View file

@ -6,7 +6,7 @@ import "github.com/pkg/errors"
// Interface for a database driver. Functionality required to support a specific
// database type (eg, MySQL, Postgres etc.)
type Interface interface {
TableNames(schema string, whitelist, exclude []string) ([]string, error)
TableNames(schema string, whitelist, blacklist []string) ([]string, error)
Columns(schema, tableName string) ([]Column, error)
PrimaryKeyInfo(schema, tableName string) (*PrimaryKey, error)
ForeignKeyInfo(schema, tableName string) ([]ForeignKey, error)
@ -25,11 +25,11 @@ type Interface interface {
}
// Tables returns the metadata for all tables, minus the tables
// specified in the exclude slice.
func Tables(db Interface, schema string, whitelist, exclude []string) ([]Table, error) {
// specified in the blacklist.
func Tables(db Interface, schema string, whitelist, blacklist []string) ([]Table, error) {
var err error
names, err := db.TableNames(schema, whitelist, exclude)
names, err := db.TableNames(schema, whitelist, blacklist)
if err != nil {
return nil, errors.Wrap(err, "unable to get table names")
}

View file

@ -13,12 +13,12 @@ func (m mockDriver) UseLastInsertID() bool { return false }
func (m mockDriver) Open() error { return nil }
func (m mockDriver) Close() {}
func (m mockDriver) TableNames(whitelist, exclude []string) ([]string, error) {
func (m mockDriver) TableNames(whitelist, blacklist []string) ([]string, error) {
if len(whitelist) > 0 {
return whitelist, nil
}
tables := []string{"pilots", "jets", "airports", "licenses", "hangars", "languages", "pilot_languages"}
return strmangle.SetComplement(tables, exclude), nil
return strmangle.SetComplement(tables, blacklist), nil
}
// Columns returns a list of mock columns

View file

@ -8,7 +8,7 @@ type Config struct {
OutFolder string
BaseDir string
WhitelistTables []string
ExcludeTables []string
BlacklistTables []string
Tags []string
Debug bool
NoTests bool

12
main.go
View file

@ -63,8 +63,8 @@ func main() {
rootCmd.PersistentFlags().StringP("output", "o", "models", "The name of the folder to output to")
rootCmd.PersistentFlags().StringP("schema", "s", "public", "The name of your database schema, for databases that support real schemas")
rootCmd.PersistentFlags().StringP("pkgname", "p", "models", "The name you wish to assign to your generated package")
rootCmd.PersistentFlags().StringP("basedir", "b", "", "The base directory has the templates and templates_test folders")
rootCmd.PersistentFlags().StringSliceP("exclude", "x", nil, "Tables to be excluded from the generated package")
rootCmd.PersistentFlags().StringP("basedir", "", "", "The base directory has the templates and templates_test folders")
rootCmd.PersistentFlags().StringSliceP("blacklist", "b", nil, "Do not include these tables in your generated package")
rootCmd.PersistentFlags().StringSliceP("whitelist", "w", nil, "Only include these tables in your generated package")
rootCmd.PersistentFlags().StringSliceP("tag", "t", nil, "Struct tags to be included on your models in addition to json, yaml, toml")
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug mode prints stack traces on error")
@ -118,12 +118,12 @@ func preRun(cmd *cobra.Command, args []string) error {
}
// BUG: https://github.com/spf13/viper/issues/200
// Look up the value of ExcludeTables & Tags directly from PFlags in Cobra if we
// Look up the value of blacklist, whitelist & tags directly from PFlags in Cobra if we
// detect a malformed value coming out of viper.
// Once the bug is fixed we'll be able to move this into the init above
cmdConfig.ExcludeTables = viper.GetStringSlice("exclude")
if len(cmdConfig.ExcludeTables) == 1 && strings.HasPrefix(cmdConfig.ExcludeTables[0], "[") {
cmdConfig.ExcludeTables, err = cmd.PersistentFlags().GetStringSlice("exclude")
cmdConfig.BlacklistTables = viper.GetStringSlice("blacklist")
if len(cmdConfig.BlacklistTables) == 1 && strings.HasPrefix(cmdConfig.BlacklistTables[0], "[") {
cmdConfig.BlacklistTables, err = cmd.PersistentFlags().GetStringSlice("blacklist")
if err != nil {
return err
}

View file

@ -59,7 +59,7 @@ func New(config *Config) (*State, error) {
return nil, errors.Wrap(err, "unable to connect to the database")
}
err = s.initTables(config.Schema, config.WhitelistTables, config.ExcludeTables)
err = s.initTables(config.Schema, config.WhitelistTables, config.BlacklistTables)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize tables")
}
@ -241,9 +241,9 @@ func (s *State) initDriver(driverName string) error {
}
// initTables retrieves all "public" schema table names from the database.
func (s *State) initTables(schema string, whitelist, exclude []string) error {
func (s *State) initTables(schema string, whitelist, blacklist []string) error {
var err error
s.Tables, err = bdb.Tables(s.Driver, schema, whitelist, exclude)
s.Tables, err = bdb.Tables(s.Driver, schema, whitelist, blacklist)
if err != nil {
return errors.Wrap(err, "unable to fetch table data")
}

View file

@ -37,10 +37,10 @@ func TestNew(t *testing.T) {
}()
config := &Config{
DriverName: "mock",
PkgName: "models",
OutFolder: out,
ExcludeTables: []string{"hangars"},
DriverName: "mock",
PkgName: "models",
OutFolder: out,
BlacklistTables: []string{"hangars"},
}
state, err = New(config)