From dbb43567d73d76554ad0b4c0f3845f4e0023f77f Mon Sep 17 00:00:00 2001 From: wispem-wantex Date: Thu, 31 Jul 2025 22:35:37 -0700 Subject: [PATCH] Fix a bug where non-rowid primary keys would be identified as 'not indexed' for foreign key indexes rule --- pkg/checks/check.go | 7 +++---- pkg/checks/check_test.go | 7 ++++--- test_schemas/success.sql | 5 +++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/checks/check.go b/pkg/checks/check.go index a15d1e0..cb541af 100644 --- a/pkg/checks/check.go +++ b/pkg/checks/check.go @@ -104,14 +104,13 @@ var Checks = map[string]Check{ from tables join pragma_index_list(tables.name) as indexes join pragma_index_info(indexes.name) as columns - + union - + select table_name, column_name from columns - where column_name = 'rowid' - and is_primary_key != 0 -- 'pk' is either 0, or the 1-based index of the column within the primary key + where is_primary_key != 0 -- 'pk' is either 0, or the 1-based index of the column within the primary key ), foreign_keys as ( select * from columns where is_foreign_key = 1 ) diff --git a/pkg/checks/check_test.go b/pkg/checks/check_test.go index 0e46cdc..4268825 100644 --- a/pkg/checks/check_test.go +++ b/pkg/checks/check_test.go @@ -54,7 +54,8 @@ func TestFailureCases(t *testing.T) { } func TestSuccessCase(t *testing.T) { - db, err := checks.OpenSchema("../../test_schemas/success.sql") + file := "../../test_schemas/success.sql" + db, err := checks.OpenSchema(file) if err != nil { t.Fatalf("failed to open database: %v", err) } @@ -64,8 +65,8 @@ func TestSuccessCase(t *testing.T) { if err != nil { t.Errorf("failed to execute check '%s': %v", check.Name, err) } - if len(results) > 0 { - t.Errorf("Should have passed, but didn't: %s", "test_schemas/success.sql") + for _, r := range results { + t.Errorf("Unexpected error in file %q: %#v", file, r) } } } diff --git a/test_schemas/success.sql b/test_schemas/success.sql index bbe486c..e41809b 100644 --- a/test_schemas/success.sql +++ b/test_schemas/success.sql @@ -11,3 +11,8 @@ create table stuff2 ( stuff_id integer references stuff(rowid), alternative_stuff_id integer references stuff(amount) ) strict; + +create table stuff3 ( + weird_pk3 integer primary key, + stuff2_id integer not null references stuff2(weird_pk) +) strict;