sqlite_lint: add lint checks for rowid and without rowid
Some checks failed
CI / build-docker (push) Successful in 7s
CI / build-docker-bootstrap (push) Has been skipped
CI / release-test (push) Failing after 15s

This commit is contained in:
wispem-wantex 2026-01-10 17:04:20 -08:00
parent 560e461b00
commit 2ac9d8e775
7 changed files with 70 additions and 6 deletions

View File

@ -133,4 +133,63 @@ var Checks = []Check{
return return
}, },
}, },
{
Name: "forbid_rowid_on_without_rowid_table",
Explanation: "Tables that are `without rowid` may not have a `rowid` column",
Execute: func(s schema.Schema) (ret []CheckResult) {
for tblName, tbl := range s.Tables {
if !tbl.IsWithoutRowid {
continue
}
for _, column := range tbl.Columns {
if column.Name == "rowid" {
ret = append(ret, CheckResult{
ErrorMsg: "rowid on 'without rowid' table",
TableName: tblName,
ColumnName: column.Name,
})
}
}
}
return
},
},
{
Name: "require_explicit_rowid",
Explanation: "All tables should have an explicit `rowid integer primary key` column, unless they\n" +
"are declared `without rowid`.",
Execute: func(s schema.Schema) (ret []CheckResult) {
tbl_loop:
for tblName, tbl := range s.Tables {
if tbl.IsWithoutRowid {
continue
}
for _, column := range tbl.Columns {
if column.Name == "rowid" {
if !column.IsPrimaryKey {
ret = append(ret, CheckResult{
ErrorMsg: "`rowid` column not declared \"primary key\"",
TableName: tblName,
ColumnName: column.Name,
})
}
if column.Type != "integer" {
ret = append(ret, CheckResult{
ErrorMsg: "non-integer `rowid` column",
TableName: tblName,
ColumnName: column.Name,
})
}
continue tbl_loop
}
}
ret = append(ret, CheckResult{
ErrorMsg: "no `rowid` column",
TableName: tblName,
ColumnName: "",
})
}
return
},
},
} }

View File

@ -20,12 +20,17 @@ func TestFailureCases(t *testing.T) {
{"test_schemas/failure-has-ints.sql", []string{"forbid_int_type"}}, {"test_schemas/failure-has-ints.sql", []string{"forbid_int_type"}},
{"test_schemas/failure-has-nulls.sql", []string{"require_not_null"}}, {"test_schemas/failure-has-nulls.sql", []string{"require_not_null"}},
{"test_schemas/failure-no-strict.sql", []string{"require_strict"}}, {"test_schemas/failure-no-strict.sql", []string{"require_strict"}},
{"test_schemas/failure-rowid-on-without-rowid.sql", []string{"forbid_rowid_on_without_rowid_table"}},
{"test_schemas/failure-missing-rowid.sql", []string{"require_explicit_rowid"}},
{"test_schemas/failure-non-integer-rowid.sql", []string{"require_explicit_rowid"}},
{"test_schemas/failure-non-primary-key-rowid.sql", []string{"require_explicit_rowid"}},
{"test_schemas/failure-total.sql", []string{ {"test_schemas/failure-total.sql", []string{
"require_not_null", "require_not_null",
"require_explicit_primary_key", "require_explicit_primary_key",
"forbid_int_type", "forbid_int_type",
"require_strict", "require_strict",
"require_indexes_for_foreign_keys", "require_indexes_for_foreign_keys",
"require_explicit_rowid",
}}, }},
} }

View File

@ -5,7 +5,7 @@ create table stuff (
) strict; ) strict;
create table stuff2 ( create table stuff2 (
weird_pk integer primary key, rowid integer primary key,
label text not null unique, label text not null unique,
stuff_id integer references stuff(rowid), stuff_id integer references stuff(rowid),
alternative_stuff_id integer references stuff(amount) alternative_stuff_id integer references stuff(amount)

View File

@ -6,7 +6,7 @@ create table stuff (
create index index_stuff_amount on stuff (amount); create index index_stuff_amount on stuff (amount);
create table stuff2 ( create table stuff2 (
weird_pk integer primary key, rowid integer primary key,
label text not null unique, label text not null unique,
stuff_id int references stuff(rowid), stuff_id int references stuff(rowid),
alternative_stuff_id integer references stuff(amount) alternative_stuff_id integer references stuff(amount)

View File

@ -6,7 +6,7 @@ create table stuff (
create index index_stuff_amount on stuff (amount); create index index_stuff_amount on stuff (amount);
create table stuff2 ( create table stuff2 (
weird_pk integer primary key, rowid integer primary key,
label text not null unique, label text not null unique,
stuff_id integer references stuff(rowid), stuff_id integer references stuff(rowid),
alternative_stuff_id integer references stuff(amount) alternative_stuff_id integer references stuff(amount)

View File

@ -6,7 +6,7 @@ create table stuff (
create index index_stuff_amount on stuff (amount); create index index_stuff_amount on stuff (amount);
create table stuff2 ( create table stuff2 (
weird_pk integer primary key, rowid integer primary key,
label text not null unique, label text not null unique,
stuff_id integer references stuff(rowid), stuff_id integer references stuff(rowid),
alternative_stuff_id integer references stuff(amount) alternative_stuff_id integer references stuff(amount)

View File

@ -10,9 +10,9 @@ create table stuff2 (
label text not null unique, label text not null unique,
stuff_id integer references stuff(rowid), stuff_id integer references stuff(rowid),
alternative_stuff_id integer references stuff(amount) alternative_stuff_id integer references stuff(amount)
) strict; ) strict, without rowid;
create table stuff3 ( create table stuff3 (
weird_pk3 integer primary key, rowid integer primary key,
stuff2_id integer not null references stuff2(weird_pk) stuff2_id integer not null references stuff2(weird_pk)
) strict; ) strict;