Files
wehub-resource-sync a06f331eb8
install-script / powershell-syntax (push) Waiting to run
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
install-script / install (macos-14) (push) Waiting to run
install-script / install (ubuntu-latest) (push) Waiting to run
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

76 lines
2.4 KiB
Go

package sql
import (
"strings"
"testing"
)
func sampleLiveSchema() *LiveSchema {
return &LiveSchema{
Dialect: "postgres",
Columns: []LiveColumn{
{Schema: "public", Table: "users", Name: "id", DataType: "bigint", Nullable: false, Ordinal: 1, IsPrimaryKey: true},
{Schema: "public", Table: "users", Name: "email", DataType: "text", Nullable: false, Ordinal: 2},
{Schema: "public", Table: "orders", Name: "id", DataType: "bigint", Nullable: false, Ordinal: 1, IsPrimaryKey: true},
{Schema: "public", Table: "orders", Name: "user_id", DataType: "bigint", Nullable: true, Ordinal: 2},
},
ForeignKeys: []LiveForeignKey{
{Schema: "public", Table: "orders", Column: "user_id", RefSchema: "public", RefTable: "users", RefColumn: "id"},
},
}
}
func TestLiveSchema_ToDDL(t *testing.T) {
ddl := sampleLiveSchema().ToDDL()
for _, want := range []string{
"CREATE TABLE orders (",
"CREATE TABLE users (",
"id bigint NOT NULL",
"email text NOT NULL",
"PRIMARY KEY (id)",
"ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id);",
} {
if !strings.Contains(ddl, want) {
t.Errorf("DDL missing %q\n---\n%s", want, ddl)
}
}
}
func TestLiveSchema_ToDDL_RoundTripsThroughExtractor(t *testing.T) {
// The whole point of emitting DDL is that the existing SQL extractor
// turns it into the same table nodes as a migration would.
ddl := sampleLiveSchema().ToDDL()
tables := ExtractCreateTables(ddl)
got := map[string]bool{}
for _, tr := range tables {
got[tr.Table] = true
if tr.Op != "create" {
t.Errorf("table %s op = %q, want create", tr.Table, tr.Op)
}
}
for _, want := range []string{"users", "orders"} {
if !got[want] {
t.Errorf("ExtractCreateTables did not recover table %q from generated DDL (got %v)", want, got)
}
}
// The generated table IDs match the canonical db::postgres:: scheme.
if id := TableNodeID("postgres", "", "users"); id != "db::postgres::users" {
t.Errorf("unexpected table node id %q", id)
}
}
func TestLiveSchema_ToDDL_NonPublicSchemaQualified(t *testing.T) {
ls := &LiveSchema{
Dialect: "postgres",
Columns: []LiveColumn{
{Schema: "billing", Table: "invoices", Name: "id", DataType: "uuid", Nullable: false, Ordinal: 1, IsPrimaryKey: true},
},
}
ddl := ls.ToDDL()
if !strings.Contains(ddl, "CREATE TABLE billing.invoices (") {
t.Errorf("non-public schema should be qualified: %s", ddl)
}
}