11import type { MigrationMeta } from '~/migrator' ;
22import type { SingleStoreSession } from '~/singlestore-core' ;
3- import { sql } from '~/sql/sql.ts' ;
3+ import { type SQL , sql } from '~/sql/sql.ts' ;
44
55const CURRENT_MIGRATION_TABLE_VERSION = 1 ;
66
@@ -15,6 +15,23 @@ function getVersion(columns: string[]) {
1515 return 0 ;
1616}
1717
18+ // singlestore returns array of objects for .all, but singlestore-proxy -> array of arrays
19+ async function all < T > (
20+ session : SingleStoreSession ,
21+ sqlQuery : SQL ,
22+ resultMapper : ( row : any [ ] ) => T = ( ) => [ ] as T ,
23+ ) : Promise < T [ ] > {
24+ const result = await session . all ( sqlQuery ) as any [ ] | any [ ] [ ] ;
25+
26+ if ( result . length === 0 ) return [ ] ;
27+
28+ if ( Array . isArray ( result [ 0 ] ) ) {
29+ return ( result as any [ ] [ ] ) . map ( ( row ) => resultMapper ( row ) ) ;
30+ }
31+
32+ return result as T [ ] ;
33+ }
34+
1835/**
1936 * Map of upgrade functions. Each key is the version being upgraded FROM,
2037 * and the function upgrades the table to the next version.
@@ -39,15 +56,21 @@ const upgradeFunctions: Record<
3956 const table = sql `${ sql . identifier ( migrationsTable ) } ` ;
4057
4158 // 1. Add new columns
42- await session . execute ( sql `ALTER TABLE ${ table } ADD COLUMN \`name\` text` ) ;
59+ await session . execute ( sql `ALTER TABLE ${ table } ADD \`name\` text` ) ;
4360 await session . execute (
44- sql `ALTER TABLE ${ table } ADD COLUMN \`applied_at\` TIMESTAMP DEFAULT CURRENT_TIMESTAMP` ,
61+ sql `ALTER TABLE ${ table } ADD \`applied_at\` TIMESTAMP DEFAULT CURRENT_TIMESTAMP` ,
4562 ) ;
4663
4764 // 2. Read all existing DB migrations
4865 // Sort them by ids asc (order how they were applied)
49- const dbRows = await session . all < { id : number ; hash : string ; created_at : string } > (
66+ const dbRows = await all < { id : number ; hash : string ; created_at : string } > (
67+ session ,
5068 sql `SELECT id, hash, created_at FROM ${ table } ORDER BY id ASC` ,
69+ ( row ) => ( {
70+ id : row [ 0 ] ,
71+ hash : row [ 1 ] ,
72+ created_at : row [ 2 ] ,
73+ } ) ,
5174 ) ;
5275
5376 if ( dbRows . length === 0 ) {
@@ -105,21 +128,25 @@ export async function upgradeIfNeeded(
105128 localMigrations : MigrationMeta [ ] ,
106129) : Promise < UpgradeResult > {
107130 // Check if the table exists at all
108- const result = await session . all (
131+ const result = await all < { '1' : 1 } > (
132+ session ,
109133 sql `SELECT 1 FROM information_schema.tables
110134 WHERE table_name = ${ migrationsTable } ` ,
135+ ( row ) => ( { '1' : row [ 0 ] } ) ,
111136 ) ;
112137
113138 if ( result . length === 0 ) {
114139 return { newDb : true } ;
115140 }
116141
117142 // Table exists, check table shape
118- const rows = await session . all < { column_name : string } > (
143+ const rows = await all < { column_name : string } > (
144+ session ,
119145 sql `SELECT column_name as \`column_name\`
120146 FROM information_schema.columns
121147 WHERE table_name = ${ migrationsTable }
122148 ORDER BY ordinal_position` ,
149+ ( row ) => ( { column_name : row [ 0 ] } ) ,
123150 ) ;
124151
125152 const version = getVersion ( rows . map ( ( r ) => r . column_name ) ) ;
0 commit comments