From 85d544152fce56cf84ee56ca35432c590a1a7466 Mon Sep 17 00:00:00 2001 From: wispem-wantex Date: Sun, 15 Feb 2026 20:49:35 +0000 Subject: [PATCH] schema: add PrimaryKeyColumns method to Table, useful for 'without rowid' tables --- pkg/schema/table.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/schema/table.go b/pkg/schema/table.go index 0d18fe3..248d1ac 100644 --- a/pkg/schema/table.go +++ b/pkg/schema/table.go @@ -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" {