Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 6e62512

Browse files
committed
feat(cli): enhance SQLite introspection with autoincrement support
1 parent 6780461 commit 6e62512

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

  • packages/cli/src/actions/pull/provider

packages/cli/src/actions/pull/provider/sqlite.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,27 @@ export const sqlite: IntrospectionProvider = {
6363
"SELECT name, type, sql AS definition FROM sqlite_schema WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name",
6464
);
6565

66+
// SQLite maintains sqlite_sequence table for tables with AUTOINCREMENT columns
67+
// If a table has an entry here, its INTEGER PRIMARY KEY column is autoincrement
68+
const autoIncrementTables = new Set<string>();
69+
try {
70+
const seqRows = all<{ name: string }>("SELECT name FROM sqlite_sequence");
71+
for (const row of seqRows) {
72+
autoIncrementTables.add(row.name);
73+
}
74+
} catch {
75+
// sqlite_sequence table doesn't exist if no AUTOINCREMENT was ever used
76+
}
77+
6678
const tables: IntrospectedTable[] = [];
6779

6880
for (const t of tablesRaw) {
6981
const tableName = t.name;
7082
const schema = '';
7183

84+
// Check if this table has autoincrement (via sqlite_sequence)
85+
const hasAutoIncrement = autoIncrementTables.has(tableName);
86+
7287
// Columns with extended info; filter out hidden=1 (internal/rowid), mark computed if hidden=2 (generated)
7388
const columnsInfo = all<{
7489
cid: number;
@@ -88,7 +103,7 @@ export const sqlite: IntrospectionProvider = {
88103
unique: number;
89104
origin: string;
90105
partial: number;
91-
}>(`PRAGMA index_list('${tableNameEsc}')`);
106+
}>(`PRAGMA index_list('${tableNameEsc}')`).filter((r) => !r.name.startsWith('sqlite_autoindex_'));
92107

93108
// Unique columns detection via unique indexes with single column
94109
const uniqueSingleColumn = new Set<string>();
@@ -163,6 +178,13 @@ export const sqlite: IntrospectionProvider = {
163178

164179
const fk = fkByColumn.get(c.name);
165180

181+
// Determine default value - check for autoincrement
182+
// AUTOINCREMENT in SQLite can only be on INTEGER PRIMARY KEY column
183+
let defaultValue = c.dflt_value;
184+
if (hasAutoIncrement && c.pk) {
185+
defaultValue = 'autoincrement';
186+
}
187+
166188
columns.push({
167189
name: c.name,
168190
datatype: c.type || '',
@@ -178,7 +200,7 @@ export const sqlite: IntrospectionProvider = {
178200
pk: !!c.pk,
179201
computed: hidden === 2,
180202
nullable: c.notnull !== 1,
181-
default: c.dflt_value,
203+
default: defaultValue,
182204
options: [],
183205
unique: uniqueSingleColumn.has(c.name),
184206
unique_name: null,
@@ -189,7 +211,7 @@ export const sqlite: IntrospectionProvider = {
189211
}
190212

191213
const enums: IntrospectedEnum[] = []; // SQLite doesn't support enums
192-
214+
193215
return { tables, enums };
194216
} finally {
195217
db.close();
@@ -211,6 +233,14 @@ export const sqlite: IntrospectionProvider = {
211233
return factories;
212234
}
213235

236+
// Handle autoincrement
237+
if (val === 'autoincrement') {
238+
factories.push(
239+
defaultAttr.addArg((ab) => ab.InvocationExpr.setFunction(getFunctionRef('autoincrement', services))),
240+
);
241+
return factories;
242+
}
243+
214244
if (val === 'true' || val === 'false') {
215245
factories.push(defaultAttr.addArg((a) => a.BooleanLiteral.setValue(val === 'true')));
216246
return factories;

0 commit comments

Comments
 (0)