Skip to content

Commit a6fd2b1

Browse files
committed
Update adapter to match on in Rails more
1 parent d1f128d commit a6fd2b1

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

lib/arjdbc/sqlite3/adapter.rb

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ def add_column(table_name, column_name, type, **options) #:nodoc:
262262
def remove_column(table_name, column_name, type = nil, **options) #:nodoc:
263263
alter_table(table_name) do |definition|
264264
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 }
268266
end
269267
end
270268

@@ -298,8 +296,8 @@ def change_column_null(table_name, column_name, null, default = nil) #:nodoc:
298296
def change_column(table_name, column_name, type, **options) #:nodoc:
299297
alter_table(table_name) do |definition|
300298
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)
303301
end
304302
end
305303
end
@@ -336,8 +334,12 @@ def build_insert_sql(insert) # :nodoc:
336334
sql << " ON CONFLICT #{insert.conflict_target} DO NOTHING"
337335
elsif insert.update_duplicates?
338336
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
341343
end
342344

343345
sql
@@ -348,7 +350,7 @@ def shared_cache? # :nodoc:
348350
end
349351

350352
def get_database_version # :nodoc:
351-
SQLite3Adapter::Version.new(query_value("SELECT sqlite_version(*)"))
353+
SQLite3Adapter::Version.new(query_value("SELECT sqlite_version(*)", "SCHEMA"))
352354
end
353355

354356
def check_version
@@ -459,8 +461,13 @@ def copy_table(from, to, options = {})
459461
options[:rename][column.name.to_sym] ||
460462
column.name) : column.name
461463

464+
if column.has_default?
465+
type = lookup_cast_type_from_column(column)
466+
default = type.deserialize(column.default)
467+
end
468+
462469
@definition.column(column_name, column.type,
463-
limit: column.limit, default: column.default,
470+
limit: column.limit, default: default,
464471
precision: column.precision, scale: column.scale,
465472
null: column.null, collation: column.collation,
466473
primary_key: column_name == from_primary_key
@@ -478,9 +485,6 @@ def copy_table(from, to, options = {})
478485
def copy_table_indexes(from, to, rename = {})
479486
indexes(from).each do |index|
480487
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_")
484488
if to == "a#{from}"
485489
name = "t#{name}"
486490
elsif from == "a#{to}"
@@ -520,20 +524,22 @@ def copy_table_contents(from, to, columns, rename = {})
520524
end
521525

522526
def translate_exception(exception, message:, sql:, binds:)
523-
case exception.message
524527
# SQLite 3.8.2 returns a newly formatted error message:
525528
# UNIQUE constraint failed: *table_name*.*column_name*
526529
# Older versions of SQLite return:
527530
# 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)
529532
# DIFFERENCE: FQN
530533
::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)
532535
# DIFFERENCE: FQN
533536
::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)
535538
# DIFFERENCE: FQN
536539
::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)
537543
else
538544
super
539545
end
@@ -553,12 +559,12 @@ def table_structure_with_collation(table_name, basic_structure)
553559
# Result will have following sample string
554560
# CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
555561
# "password_digest" varchar COLLATE "NOCASE");
556-
result = exec_query(sql, "SCHEMA").first
562+
result = query_value(sql, "SCHEMA")
557563

558564
if result
559565
# Splitting with left parentheses and discarding the first part will return all
560566
# columns separated with comma(,).
561-
columns_string = result["sql"].split("(", 2).last
567+
columns_string = result.split("(", 2).last
562568

563569
columns_string.split(",").each do |column_string|
564570
# This regex will match the column name and collation type and will save
@@ -596,6 +602,9 @@ def connect
596602
end
597603

598604
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+
599608
execute("PRAGMA foreign_keys = ON", "SCHEMA")
600609
end
601610

0 commit comments

Comments
 (0)