Modify postgres query to get is_unique for indexes
* Remove unused SnakeCase function * Fix formatting bug in relationship structs
This commit is contained in:
parent
7517ad3ced
commit
d92a439c54
5 changed files with 14 additions and 52 deletions
|
@ -110,12 +110,21 @@ func (p *PostgresDriver) Columns(tableName string) ([]bdb.Column, error) {
|
||||||
|
|
||||||
rows, err := p.dbConn.Query(`
|
rows, err := p.dbConn.Query(`
|
||||||
select column_name, data_type, column_default, is_nullable,
|
select column_name, data_type, column_default, is_nullable,
|
||||||
(
|
(select exists(
|
||||||
select cast(count(*) as bit) as is_unique
|
select 1
|
||||||
from information_schema.constraint_column_usage as ccu
|
from information_schema.constraint_column_usage as ccu
|
||||||
inner join information_schema.table_constraints tc on ccu.constraint_name = tc.constraint_name
|
inner join information_schema.table_constraints tc on ccu.constraint_name = tc.constraint_name
|
||||||
where ccu.table_name = c.table_name and ccu.column_name = c.column_name and tc.constraint_type = 'UNIQUE'
|
where ccu.table_name = c.table_name and ccu.column_name = c.column_name and tc.constraint_type = 'UNIQUE'
|
||||||
) as is_unique
|
)) OR (select exists(
|
||||||
|
select 1
|
||||||
|
from
|
||||||
|
pg_indexes pgix
|
||||||
|
inner join pg_class pgc on pgix.indexname = pgc.relname and pgc.relkind = 'i'
|
||||||
|
inner join pg_index pgi on pgi.indexrelid = pgc.oid
|
||||||
|
inner join pg_attribute pga on pga.attrelid = pgi.indrelid and pga.attnum = ANY(pgi.indkey)
|
||||||
|
where
|
||||||
|
pgix.schemaname = 'public' and pgix.tablename = c.table_name and pga.attname = c.column_name and pgi.indisunique = true
|
||||||
|
)) as is_unique
|
||||||
from information_schema.columns as c
|
from information_schema.columns as c
|
||||||
where table_name=$1 and table_schema = 'public';
|
where table_name=$1 and table_schema = 'public';
|
||||||
`, tableName)
|
`, tableName)
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"github.com/jinzhu/inflection"
|
"github.com/jinzhu/inflection"
|
||||||
)
|
)
|
||||||
|
@ -140,22 +139,6 @@ func CamelCase(name string) string {
|
||||||
return strings.Join(splits, "")
|
return strings.Join(splits, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SnakeCase converts TitleCase variable names to snake_case format.
|
|
||||||
func SnakeCase(name string) string {
|
|
||||||
runes := []rune(name)
|
|
||||||
length := len(runes)
|
|
||||||
|
|
||||||
var out []rune
|
|
||||||
for i := 0; i < length; i++ {
|
|
||||||
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1]) || unicode.IsDigit(runes[i-1])) {
|
|
||||||
out = append(out, '_')
|
|
||||||
}
|
|
||||||
out = append(out, unicode.ToLower(runes[i]))
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MakeStringMap converts a map[string]string into the format:
|
// MakeStringMap converts a map[string]string into the format:
|
||||||
// "key": "value", "key": "value"
|
// "key": "value", "key": "value"
|
||||||
func MakeStringMap(types map[string]string) string {
|
func MakeStringMap(types map[string]string) string {
|
||||||
|
|
|
@ -209,35 +209,6 @@ func TestCamelCase(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSnakeCase(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
In string
|
|
||||||
Out string
|
|
||||||
}{
|
|
||||||
{"ID", "id"},
|
|
||||||
{"ID9", "id9"},
|
|
||||||
{"Thing", "thing"},
|
|
||||||
{"Thing92", "thing92"},
|
|
||||||
{"ThingGUID", "thing_guid"},
|
|
||||||
{"ThingGUIDTwo", "thing_guid_two"},
|
|
||||||
{"ThingStuff", "thing_stuff"},
|
|
||||||
{"ThingStuff1GUID", "thing_stuff1_guid"},
|
|
||||||
{"ThingStuff12GUID", "thing_stuff12_guid"},
|
|
||||||
{"ThingStuff123GUID", "thing_stuff123_guid"},
|
|
||||||
{"GUIDThingStuffID123", "guid_thing_stuff_id123"},
|
|
||||||
{"GUIDThingStuffID12", "guid_thing_stuff_id12"},
|
|
||||||
{"GUIDThingStuffID1", "guid_thing_stuff_id1"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, test := range tests {
|
|
||||||
if out := SnakeCase(test.In); out != test.Out {
|
|
||||||
t.Errorf("%d) Want: %q, got %q", i, test.Out, out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMakeStringMap(t *testing.T) {
|
func TestMakeStringMap(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
|
@ -119,7 +119,6 @@ var templateStringMappers = map[string]func(string) string{
|
||||||
"toUpper": strings.ToUpper,
|
"toUpper": strings.ToUpper,
|
||||||
"titleCase": strmangle.TitleCase,
|
"titleCase": strmangle.TitleCase,
|
||||||
"camelCase": strmangle.CamelCase,
|
"camelCase": strmangle.CamelCase,
|
||||||
"snakeCase": strmangle.SnakeCase,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// templateFunctions is a map of all the functions that get passed into the
|
// templateFunctions is a map of all the functions that get passed into the
|
||||||
|
|
|
@ -27,8 +27,8 @@ type {{$modelName}}Relationships struct {
|
||||||
{{end -}}
|
{{end -}}
|
||||||
{{- range .Table.ToManyRelationships -}}
|
{{- range .Table.ToManyRelationships -}}
|
||||||
{{- if .ForeignColumnUnique -}}
|
{{- if .ForeignColumnUnique -}}
|
||||||
{{- template "relationship_to_one_struct_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table .) -}}
|
{{- template "relationship_to_one_struct_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table .)}}
|
||||||
{{- else -}}
|
{{else -}}
|
||||||
{{- $rel := textsFromRelationship $dot.Tables $dot.Table . -}}
|
{{- $rel := textsFromRelationship $dot.Tables $dot.Table . -}}
|
||||||
{{$rel.Function.Name}} {{$rel.ForeignTable.Slice}}
|
{{$rel.Function.Name}} {{$rel.ForeignTable.Slice}}
|
||||||
{{end -}}{{/* if ForeignColumnUnique */}}
|
{{end -}}{{/* if ForeignColumnUnique */}}
|
||||||
|
|
Loading…
Add table
Reference in a new issue