@@ -262,9 +262,7 @@ def add_column(table_name, column_name, type, **options) #:nodoc:
262
262
def remove_column ( table_name , column_name , type = nil , **options ) #:nodoc:
263
263
alter_table ( table_name ) do |definition |
264
264
definition . remove_column column_name
265
- definition . foreign_keys . delete_if do |_ , fk_options |
266
- fk_options [ :column ] == column_name . to_s
267
- end
265
+ definition . foreign_keys . delete_if { |fk | fk . column == column_name . to_s }
268
266
end
269
267
end
270
268
@@ -298,8 +296,8 @@ def change_column_null(table_name, column_name, null, default = nil) #:nodoc:
298
296
def change_column ( table_name , column_name , type , **options ) #:nodoc:
299
297
alter_table ( table_name ) do |definition |
300
298
definition [ column_name ] . instance_eval do
301
- self . type = type
302
- self . options . merge! ( options )
299
+ self . type = aliased_types ( type . to_s , type )
300
+ self . options . merge! ( options )
303
301
end
304
302
end
305
303
end
@@ -336,8 +334,12 @@ def build_insert_sql(insert) # :nodoc:
336
334
sql << " ON CONFLICT #{ insert . conflict_target } DO NOTHING"
337
335
elsif insert . update_duplicates?
338
336
sql << " ON CONFLICT #{ insert . conflict_target } DO UPDATE SET "
339
- sql << insert . touch_model_timestamps_unless { |column | "#{ column } IS excluded.#{ column } " }
340
- sql << insert . updatable_columns . map { |column | "#{ column } =excluded.#{ column } " } . join ( "," )
337
+ if insert . raw_update_sql?
338
+ sql << insert . raw_update_sql
339
+ else
340
+ sql << insert . touch_model_timestamps_unless { |column | "#{ column } IS excluded.#{ column } " }
341
+ sql << insert . updatable_columns . map { |column | "#{ column } =excluded.#{ column } " } . join ( "," )
342
+ end
341
343
end
342
344
343
345
sql
@@ -348,7 +350,7 @@ def shared_cache? # :nodoc:
348
350
end
349
351
350
352
def get_database_version # :nodoc:
351
- SQLite3Adapter ::Version . new ( query_value ( "SELECT sqlite_version(*)" ) )
353
+ SQLite3Adapter ::Version . new ( query_value ( "SELECT sqlite_version(*)" , "SCHEMA" ) )
352
354
end
353
355
354
356
def check_version
@@ -459,8 +461,13 @@ def copy_table(from, to, options = {})
459
461
options [ :rename ] [ column . name . to_sym ] ||
460
462
column . name ) : column . name
461
463
464
+ if column . has_default?
465
+ type = lookup_cast_type_from_column ( column )
466
+ default = type . deserialize ( column . default )
467
+ end
468
+
462
469
@definition . column ( column_name , column . type ,
463
- limit : column . limit , default : column . default ,
470
+ limit : column . limit , default : default ,
464
471
precision : column . precision , scale : column . scale ,
465
472
null : column . null , collation : column . collation ,
466
473
primary_key : column_name == from_primary_key
@@ -478,9 +485,6 @@ def copy_table(from, to, options = {})
478
485
def copy_table_indexes ( from , to , rename = { } )
479
486
indexes ( from ) . each do |index |
480
487
name = index . name
481
- # indexes sqlite creates for internal use start with `sqlite_` and
482
- # don't need to be copied
483
- next if name . start_with? ( "sqlite_" )
484
488
if to == "a#{ from } "
485
489
name = "t#{ name } "
486
490
elsif from == "a#{ to } "
@@ -520,20 +524,22 @@ def copy_table_contents(from, to, columns, rename = {})
520
524
end
521
525
522
526
def translate_exception ( exception , message :, sql :, binds :)
523
- case exception . message
524
527
# SQLite 3.8.2 returns a newly formatted error message:
525
528
# UNIQUE constraint failed: *table_name*.*column_name*
526
529
# Older versions of SQLite return:
527
530
# column *column_name* is not unique
528
- when / column(s)? .* (is|are) not unique/ , / UNIQUE constraint failed: .*/
531
+ if exception . message . match? ( /( column(s)? .* (is|are) not unique| UNIQUE constraint failed: .*)/i )
529
532
# DIFFERENCE: FQN
530
533
::ActiveRecord ::RecordNotUnique . new ( message , sql : sql , binds : binds )
531
- when / .* may not be NULL/ , / NOT NULL constraint failed: .*/
534
+ elsif exception . message . match? ( /( .* may not be NULL| NOT NULL constraint failed: .*)/i )
532
535
# DIFFERENCE: FQN
533
536
::ActiveRecord ::NotNullViolation . new ( message , sql : sql , binds : binds )
534
- when /FOREIGN KEY constraint failed/i
537
+ elsif exception . message . match? ( /FOREIGN KEY constraint failed/i )
535
538
# DIFFERENCE: FQN
536
539
::ActiveRecord ::InvalidForeignKey . new ( message , sql : sql , binds : binds )
540
+ elsif exception . message . match? ( /called on a closed database/i )
541
+ # DIFFERENCE: FQN
542
+ ::ActiveRecord ::ConnectionNotEstablished . new ( exception )
537
543
else
538
544
super
539
545
end
@@ -553,12 +559,12 @@ def table_structure_with_collation(table_name, basic_structure)
553
559
# Result will have following sample string
554
560
# CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
555
561
# "password_digest" varchar COLLATE "NOCASE");
556
- result = exec_query ( sql , "SCHEMA" ) . first
562
+ result = query_value ( sql , "SCHEMA" )
557
563
558
564
if result
559
565
# Splitting with left parentheses and discarding the first part will return all
560
566
# columns separated with comma(,).
561
- columns_string = result [ "sql" ] . split ( "(" , 2 ) . last
567
+ columns_string = result . split ( "(" , 2 ) . last
562
568
563
569
columns_string . split ( "," ) . each do |column_string |
564
570
# This regex will match the column name and collation type and will save
@@ -596,6 +602,9 @@ def connect
596
602
end
597
603
598
604
def configure_connection
605
+ # FIXME: missing from adapter
606
+ # @connection.busy_timeout(self.class.type_cast_config_to_integer(@config[:timeout])) if @config[:timeout]
607
+
599
608
execute ( "PRAGMA foreign_keys = ON" , "SCHEMA" )
600
609
end
601
610
0 commit comments