@@ -11,6 +11,7 @@ import {
11
11
runCreateTableWithIndexAndTrigger ,
12
12
runCreateTimeTrigger ,
13
13
runDropTable ,
14
+ runRenameTable ,
14
15
} from './run'
15
16
import { type ParsedCreateTableSQL , parseExistDB } from './parseExist'
16
17
@@ -42,10 +43,6 @@ export type SyncOptions<T extends Schema> = {
42
43
* do not restore data from old table to new table
43
44
*/
44
45
truncateIfExists ?: boolean | Array < keyof T & string >
45
- /**
46
- * reserve old data in temp, clear after destroy
47
- */
48
- reserveOldData ?: boolean
49
46
/**
50
47
* trigger on update success
51
48
* @param db kysely instance
@@ -64,7 +61,6 @@ export async function syncTables<T extends Schema>(
64
61
logger ?: DBLogger ,
65
62
) : Promise < StatusResult > {
66
63
const {
67
- reserveOldData,
68
64
truncateIfExists = [ ] ,
69
65
log,
70
66
version : { current, skipSyncWhenSame } = { } ,
@@ -134,6 +130,10 @@ export async function syncTables<T extends Schema>(
134
130
return { ready : false , error : e }
135
131
} )
136
132
133
+ /**
134
+ * diff table data
135
+ * @see {@link https://sqlite.org/lang_altertable.html official doc } 7. Making Other Kinds Of Table Schema Changes
136
+ */
137
137
async function diffTable (
138
138
trx : Transaction < any > ,
139
139
tableName : string ,
@@ -158,35 +158,26 @@ export async function syncTables<T extends Schema>(
158
158
debug ( 'different table structure, update' )
159
159
const tempTableName = `_temp_${ tableName } `
160
160
161
- // 1. copy struct to temporary table
162
- // @ts -expect-error existColumns.columns has parsed column type
163
- await runCreateTable ( trx , tempTableName , existColumns , true )
164
-
165
- // 2. copy all data to temporary table
166
- await trx . insertInto ( tempTableName )
167
- . expression ( eb => eb . selectFrom ( tableName ) . selectAll ( ) )
168
- . execute ( )
161
+ // 1. create target table with temp name
162
+ const _triggerOptions = await runCreateTable ( trx , tempTableName , props )
169
163
170
- // 3. remove exist table
171
- await runDropTable ( trx , tableName )
172
-
173
- // 4. create target table
174
- const _triggerOptions = await runCreateTable ( trx , tableName , props )
175
-
176
- // 5. diff and restore data from temporary table to target table
164
+ // 2. diff and restore data from source table to target table
177
165
if ( restoreColumnList . length ) {
178
- await trx . insertInto ( tableName )
166
+ await trx . insertInto ( tempTableName )
179
167
. columns ( restoreColumnList )
180
- . expression ( eb => eb . selectFrom ( tempTableName ) . select ( restoreColumnList ) )
168
+ . expression ( eb => eb . selectFrom ( tableName ) . select ( restoreColumnList ) )
181
169
. execute ( )
182
170
}
171
+ // 3. remove old table
172
+ await runDropTable ( trx , tableName )
173
+
174
+ // 4. rename temp table to target table name
175
+ await runRenameTable ( trx , tempTableName , tableName )
183
176
184
- // 6 . add indexes and triggers
177
+ // 5 . add indexes and triggers
185
178
await runCreateTableIndex ( trx , tableName , index )
186
179
await runCreateTimeTrigger ( trx , tableName , _triggerOptions )
187
180
188
- // 7. if not reserve old data, remove temporary table
189
- ! reserveOldData && await runDropTable ( trx , tempTableName )
190
181
debug ( `restore columns: ${ restoreColumnList } ` )
191
182
}
192
183
}
0 commit comments