Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --extra-templates option #768

Closed
wants to merge 2 commits into from
Closed

Conversation

righ
Copy link

@righ righ commented Jun 6, 2020

#755

This is a change to add the --extra-templates option.
It works the same way as --templates, but we don't need to specify original templates.

The following is a log of the test run.

Preparing:

# build a binary at my sqlboiler repository dir.
root@517e8bbfecb1:/sqlboiler# go build -o sqlboiler .
# copy
root@517e8bbfecb1:/sqlboiler# cp sqlboiler /root/
root@517e8bbfecb1:/sqlboiler# cd /root/

root@517e8bbfecb1:~# mkdir templates

root@517e8bbfecb1:~# cat << EOL > templates/99_tablename.go.tpl
> {{- \$alias := .Aliases.Table .Table.Name -}}
>
> // GetTableName returns the real table name.
> func (_ {{\$alias.UpSingular}}) GetTableName() string { return "{{.Table.Name}}" }
> EOL

root@517e8bbfecb1:~# cat templates/99_tablename.go.tpl
{{- $alias := .Aliases.Table .Table.Name -}}

// GetTableName returns the real table name.
func (_ {{$alias.UpSingular}}) GetTableName() string { return "{{.Table.Name}}" }

Execution:

root@517e8bbfecb1:~# ./sqlboiler
Error: must provide a driver name

SQL Boiler generates a Go ORM from template files, tailored to your database schema.
Complete documentation is available at http://github.com/volatiletech/sqlboiler

Usage:
  sqlboiler [flags] <driver>

Examples:
sqlboiler psql

Flags:
      --add-global-variants        Enable generation for global variants
      --add-panic-variants         Enable generation for panic variants
      --add-soft-deletes           Enable soft deletion by updating deleted_at timestamp
  -c, --config string              Filename of config file to override default lookup
  -d, --debug                      Debug mode prints stack traces on error
      --extra-templates strings    The additional templates directory, that does not override the bindata's template folders in sqlboiler
  -h, --help                       help for sqlboiler
      --no-auto-timestamps         Disable automatic timestamps for created_at/updated_at
      --no-back-referencing        Disable back referencing in the loaded relationship structs
      --no-context                 Disable context.Context usage in the generated code
      --no-driver-templates        Disable parsing of templates defined by the database driver
      --no-hooks                   Disable hooks feature for your models
      --no-rows-affected           Disable rows affected in the generated API
      --no-tests                   Disable generated go test files
  -o, --output string              The name of the folder to output to (default "models")
  -p, --pkgname string             The name you wish to assign to your generated package (default "models")
  -r, --relation-tag string        Relationship struct tag name (default "-")
      --struct-tag-casing string   Decides the casing for go structure tag names. camel, title or snake (default snake) (default "snake")
  -t, --tag strings                Struct tags to be included on your models in addition to json, yaml, toml
      --tag-ignore strings         List of column names that should have tags values set to '-' (ignored during parsing)
      --templates strings          A templates directory, overrides the bindata'd template folders in sqlboiler
      --version                    Print the version
      --wipe                       Delete the output folder (rm -rf) before generation to ensure sanity

root@517e8bbfecb1:~# ./sqlboiler mysql --output models_mysql --pkgname models_mysql --wipe --extra-templates ./templates/

root@517e8bbfecb1:~# tail models_mysql/jets.go  -n20
        var exists bool
        sql := "select exists(select 1 from `jets` where `id`=? limit 1)"

        if boil.IsDebug(ctx) {
                writer := boil.DebugWriterFrom(ctx)
                fmt.Fprintln(writer, sql)
                fmt.Fprintln(writer, iD)
        }
        row := exec.QueryRowContext(ctx, sql, iD)

        err := row.Scan(&exists)
        if err != nil {
                return false, errors.Wrap(err, "models_mysql: unable to check if jets exists")
        }

        return exists, nil
}

// GetTableName returns the real table name.
func (_ Jet) GetTableName() string { return "jets" }

@Darkclainer
Copy link
Contributor

Don't you mind if I will amend README.md as additional commit in this PR?

@righ
Copy link
Author

righ commented Jun 9, 2020

@Darkclainer Of cource I do not. Thanks!

@Darkclainer
Copy link
Contributor

Darkclainer commented Jun 13, 2020

@righ
I have found that PR should be opened against dev branch: https://github.com/volatiletech/sqlboiler/blob/master/CONTRIBUTING.md

@righ righ changed the base branch from master to dev June 14, 2020 02:10
@righ
Copy link
Author

righ commented Jun 14, 2020

Okay, thanks for letting me know.
I changed the base and rebased this branch onto dev.

And I fixed your commit slightly to match the current code.

@aarondl
Copy link
Member

aarondl commented Jun 18, 2020

Hello @righ. I appreciate the PR! I don't think it's a good idea to add a second option for templates though. I quite like the explicitness of the current system and I think it's also important to ensure that sqlboiler upgrades don't change your main templates, since your overriding/extra templates can depend on things declared in those and there are many unexported things we could currently change that may break your templates.

I could possibly be convinced but as I see it there's no difference between:

--extra-templates my_dir

and

--templates my_sqlboiler_templates --templates my_dir

The second isn't really onerous enough for me to want to accept this. Besides if you're doing it from a Go module path then you can use: $GOPATH/pkg/mod/github.com/volatiletech/sqlboiler@version as a way of maintaining compatibility which is also nice.

Going to close for now.

@aarondl aarondl closed this Jun 18, 2020
@righ righ deleted the extra-templates branch June 18, 2020 14:19
@jaakkotuosa
Copy link

In case someone else is checking this issue, I would like to mention a more complete example of including the default templates:
--templates $GOPATH/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.1.2/templates --templates $GOPATH/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.1.2/templates_test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants