@@ -141,7 +141,7 @@ fn update_tables(
141141 let mut create = CreateTableStatement :: from ( table) ;
142142 if table. direct {
143143 for column in & table. columns {
144- create. push_column ( & column. name , & column . type_name ) ;
144+ create. push_any_column ( & column. name ) ;
145145 }
146146 }
147147
@@ -184,24 +184,20 @@ fn direct_table_migration(
184184
185185 struct ExistingColumn < ' a > {
186186 column : & ' a Column ,
187- index_in_table : usize ,
188187 found_in_old : bool ,
189188 }
190189
191190 let mut new_columns: Vec < _ > = new
192191 . columns
193192 . iter ( )
194- . enumerate ( )
195- . map ( |( i, column) | ExistingColumn {
193+ . map ( |column| ExistingColumn {
196194 column,
197- index_in_table : i,
198195 found_in_old : false ,
199196 } )
200197 . collect ( ) ;
201198 new_columns. sort_by ( |a, b| a. column . name . cmp ( & b. column . name ) ) ;
202199
203200 let mut deleted_columns = vec ! [ ] ;
204- let mut changed_column_types = vec ! [ ] ;
205201
206202 for old_column in & old. columns {
207203 let Ok ( new_column_index) =
@@ -214,18 +210,12 @@ fn direct_table_migration(
214210 let new_column = & mut new_columns[ new_column_index] ;
215211 new_column. found_in_old = true ;
216212
217- if !new_column
218- . column
219- . type_name
220- . eq_ignore_ascii_case ( & old_column. type_name )
221- {
222- changed_column_types. push ( ( new_column. index_in_table , & new_column. column . type_name ) ) ;
223- }
213+ // For found columns, the type doesn't matter as we generate ANY types for all of them.
224214 }
225215
226216 new_columns. retain ( |c| !c. found_in_old ) ;
227217
228- if new_columns. is_empty ( ) && deleted_columns. is_empty ( ) && changed_column_types . is_empty ( ) {
218+ if new_columns. is_empty ( ) && deleted_columns. is_empty ( ) {
229219 return Ok ( ( ) ) ; // Nothing to migrate.
230220 }
231221
@@ -250,48 +240,11 @@ fn direct_table_migration(
250240 }
251241 }
252242
253- if !changed_column_types. is_empty ( ) {
254- // To change column types, we change the CREATE TABLE statement for the table. As long as
255- // we do this in a way that doesn't alter the order of existing columns, this doesn't
256- // corrupt data (column types in non-strict tables only affects type affinity for inserts
257- // and updates). The proper way to run this migration requires copying data, which we want
258- // to avoid.
259- let schema_writable_before = db. has_writable_schema ( ) ;
260- if !schema_writable_before {
261- db. set_writable_schema ( true ) ?;
262- }
263-
264- let mut new_create_table = CreateTableStatement :: from ( new) ;
265- let mut changed_column_types = changed_column_types. iter ( ) . peekable ( ) ;
266-
267- for ( i, column) in old. columns . iter ( ) . enumerate ( ) {
268- let changed_type = changed_column_types
269- . next_if ( |( index, _) | * index == i)
270- . map ( |( _, type_name) | type_name. as_str ( ) ) ;
271-
272- new_create_table. push_column ( & column. name , changed_type. unwrap_or ( & column. type_name ) ) ;
273- }
274-
275- let new_create_table = new_create_table. finish ( ) ;
276-
277- {
278- let stmt = db
279- . prepare_v2 ( "UPDATE sqlite_schema SET sql = ? WHERE type = 'table' AND name = ?" ) ?;
280- stmt. bind_text ( 1 , & new_create_table. sql , Destructor :: STATIC ) ?;
281- stmt. bind_text ( 2 , & new. name , Destructor :: STATIC ) ?;
282- stmt. exec ( ) ?;
283- }
284-
285- if !schema_writable_before {
286- db. set_writable_schema ( false ) ?;
287- }
288- }
289-
290243 // Add new columns, drop old ones
291244 for new_column in new_columns {
292245 let mut stmt = SqlBuffer :: new ( ) ;
293246 stmt. alter_table ( & new. name ) ;
294- stmt. add_column ( new_column. column ) ;
247+ stmt. add_column ( & new_column. column . name , "ANY" ) ;
295248 db. exec_safe_str ( & stmt. sql ) ?;
296249 }
297250
0 commit comments