Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m committed Oct 19, 2019
2 parents 6c1d2c2 + 1c19a1a commit 633326f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
6 changes: 3 additions & 3 deletions dialect_cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
const nameCockroach = "cockroach"
const portCockroach = "26257"

const selectTablesQueryCockroach = "select table_name from information_schema.tables where table_schema = 'public' and table_type = 'BASE TABLE' and table_catalog = ?"
const selectTablesQueryCockroachV1 = "select table_name from information_schema.tables where table_schema = ?"
const selectTablesQueryCockroach = "select table_name from information_schema.tables where table_schema = 'public' and table_type = 'BASE TABLE' and table_name <> ? and table_catalog = ?"
const selectTablesQueryCockroachV1 = "select table_name from information_schema.tables where table_name <> ? and table_schema = ?"

func init() {
AvailableDialects = append(AvailableDialects, nameCockroach)
Expand Down Expand Up @@ -209,7 +209,7 @@ func (p *cockroach) TruncateAll(tx *Connection) error {
tableQuery := p.tablesQuery()

var tables []table
if err := tx.RawQuery(tableQuery, tx.Dialect.Details().Database).All(&tables); err != nil {
if err := tx.RawQuery(tableQuery, tx.MigrationTableName(), tx.Dialect.Details().Database).All(&tables); err != nil {
return err
}

Expand Down
11 changes: 10 additions & 1 deletion dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ func (commonDialect) Lock(fn func() error) error {
}

func (commonDialect) Quote(key string) string {
return fmt.Sprintf(`"%s"`, key)
parts := strings.Split(key, ".")

for i, part := range parts {
part = strings.Trim(part, `"`)
part = strings.TrimSpace(part)

parts[i] = fmt.Sprintf(`"%v"`, part)
}

return strings.Join(parts, ".")
}

func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable) error {
Expand Down
9 changes: 9 additions & 0 deletions dialect_postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ func Test_PostgreSQL_Connection_String_Failure(t *testing.T) {
r.Error(err)
r.Equal("postgres", cd.Dialect)
}

func Test_PostgreSQL_Quotable(t *testing.T) {
r := require.New(t)
p := postgresql{}

r.Equal(`"table_name"`, p.Quote("table_name"))
r.Equal(`"schema"."table_name"`, p.Quote("schema.table_name"))
r.Equal(`"schema"."table name"`, p.Quote(`"schema"."table name"`))
}
10 changes: 10 additions & 0 deletions genny/fizz/ctable/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func Test_New(t *testing.T) {
},
`create_table("widgets") {
t.Timestamps()
}`,
},
{
&Options{
TableName: "widget",
Name: "create_widgets",
ForceDefaultTimestamps: true,
},
`create_table("widgets") {
t.Timestamps()
}`,
},
{
Expand Down
3 changes: 2 additions & 1 deletion genny/fizz/ctable/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ func (opts *Options) Validate() error {
if len(opts.TableName) == 0 {
return errors.New("you must set a name for your table")
}
opts.TableName = name.New(opts.TableName).Tableize().String()
if len(opts.Path) == 0 {
opts.Path = "migrations"
}
if len(opts.Name) == 0 {
timestamp := nowFunc().UTC().Format("20060102150405")
opts.Name = fmt.Sprintf("%s_create_%s", timestamp, name.New(opts.TableName).Tableize())
opts.Name = fmt.Sprintf("%s_create_%s", timestamp, opts.TableName)
}
if len(opts.Type) == 0 {
opts.Type = "fizz"
Expand Down

0 comments on commit 633326f

Please sign in to comment.