Skip to content

Commit 12b0de9

Browse files
chrispadercodex
andauthored
fix(mock): refresh raw columns after schema changes
Co-authored-by: Codex <noreply@openai.com>
1 parent 21a451b commit 12b0de9

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎packages/react-native-nitro-sqlite/src/__tests__/mock.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ describe('Node SQLite mock', () => {
8686
})
8787
})
8888

89+
it.each([0, 1])(
90+
'refreshes raw prepared column names after a schema change, id=%s',
91+
async (id) => {
92+
const database = open({ name: 'raw-schema-change' })
93+
database.execute('CREATE TABLE items (id INTEGER)')
94+
database.execute('INSERT INTO items VALUES (1)')
95+
const statement = database.prepare('SELECT * FROM items WHERE id = ?')
96+
database.execute(
97+
"ALTER TABLE items ADD COLUMN label TEXT DEFAULT 'added'",
98+
)
99+
100+
const result = statement.executeRaw([id])
101+
expect(result.columnNames).toEqual(['id', 'label'])
102+
expect(result.rows).toEqual(id === 1 ? [[1, 'added']] : [])
103+
104+
database.execute('ALTER TABLE items RENAME COLUMN label TO name')
105+
const asyncResult = await statement.executeRawAsync([id])
106+
expect(asyncResult.columnNames).toEqual(['id', 'name'])
107+
expect(asyncResult.rows).toEqual(result.rows)
108+
statement.finalize()
109+
},
110+
)
111+
89112
it('binds repeated named parameters in first-occurrence order', () => {
90113
const database = open({ name: 'named' })
91114
expect(

‎packages/react-native-nitro-sqlite/src/mock/query.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ export function executePreparedRaw(
4949
let columnNames: string[] = []
5050
let rows: SQLiteValue[][] = []
5151
if (statement.reader) {
52-
columnNames = statement.columns().map((column) => column.name)
5352
try {
5453
// better-sqlite3's types keep the object row type after raw(true).
5554
const rawStatement = statement.raw(true) as unknown as RawStatement
56-
rows = rawStatement
57-
.all(...bindings)
58-
.map((row) =>
59-
row.map((value, index) => normalizeValue(value, columnNames[index]!)),
60-
)
55+
const rawRows = rawStatement.all(...bindings)
56+
// Execution may reprepare the statement after a schema change.
57+
columnNames = statement.columns().map((column) => column.name)
58+
rows = rawRows.map((row) =>
59+
row.map((value, index) => normalizeValue(value, columnNames[index]!)),
60+
)
6161
} finally {
6262
// Raw mode stays on the statement, and prepared statements also serve execute().
6363
statement.raw(false)

0 commit comments

Comments
 (0)