schema: add PrimaryKeyColumns method to Table, useful for 'without rowid' tables
This commit is contained in:
parent
5cbb657666
commit
85d544152f
@ -1,6 +1,7 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
||||||
@ -83,6 +84,23 @@ type Table struct {
|
|||||||
GoTypeName string
|
GoTypeName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimaryKeyColumns returns the ordered list of columns in this table's primary key.
|
||||||
|
// This can be useful for "without rowid" tables with composite primary keys.
|
||||||
|
//
|
||||||
|
// TODO: needs test
|
||||||
|
func (t Table) PrimaryKeyColumns() []Column {
|
||||||
|
pks := make([]Column, 0)
|
||||||
|
for _, c := range t.Columns {
|
||||||
|
if c.IsPrimaryKey {
|
||||||
|
pks = append(pks, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(pks, func(i, j int) bool {
|
||||||
|
return pks[i].PrimaryKeyRank < pks[j].PrimaryKeyRank
|
||||||
|
})
|
||||||
|
return pks
|
||||||
|
}
|
||||||
|
|
||||||
func (t Table) HasAutoTimestamps() (hasCreatedAt bool, hasUpdatedAt bool) {
|
func (t Table) HasAutoTimestamps() (hasCreatedAt bool, hasUpdatedAt bool) {
|
||||||
for _, c := range t.Columns {
|
for _, c := range t.Columns {
|
||||||
if c.Name == "created_at" && c.Type == "integer" {
|
if c.Name == "created_at" && c.Type == "integer" {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user