schema: add PrimaryKeyColumns method to Table, useful for 'without rowid' tables

This commit is contained in:
wispem-wantex 2026-02-15 20:49:35 +00:00
parent 5cbb657666
commit 85d544152f

View File

@ -1,6 +1,7 @@
package schema
import (
"sort"
"strings"
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
@ -83,6 +84,23 @@ type Table struct {
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) {
for _, c := range t.Columns {
if c.Name == "created_at" && c.Type == "integer" {