From 25347af99f212800baf05181f50e7a355ff861fe Mon Sep 17 00:00:00 2001 From: Manish Sharma Date: Mon, 18 Dec 2023 23:40:33 +0530 Subject: [PATCH] [FIX] For Sqlite3 db:drop command output --- .../connection_adapters/sqlite3/schema_statements.rb | 6 ++++++ .../active_record/connection_adapters/sqlite3_adapter.rb | 9 +++------ .../lib/active_record/tasks/sqlite_database_tasks.rb | 2 +- .../cases/adapters/sqlite3/sqlite3_create_folder_test.rb | 1 + activerecord/test/cases/fixtures_test.rb | 1 + .../test/cases/migration/pending_migrations_test.rb | 5 ++++- 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb index b5a38394fbd0a..f28e76b7075ac 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb @@ -129,6 +129,12 @@ def schema_creation # :nodoc SQLite3::SchemaCreation.new(self) end + def create_database + mode = ::SQLite3::Constants::Open::READWRITE | ::SQLite3::Constants::Open::CREATE + config = @connection_parameters.merge(flags: mode) + ::SQLite3::Database.new(config[:database].to_s, config) + end + private def valid_table_definition_options super + [:rename] diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 7445a272e1cf8..5f81cb5ebcf46 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -30,12 +30,8 @@ class SQLite3Adapter < AbstractAdapter class << self def new_client(config) ::SQLite3::Database.new(config[:database].to_s, config) - rescue Errno::ENOENT => error - if error.message.include?("No such file or directory") - raise ActiveRecord::NoDatabaseError - else - raise - end + rescue ::SQLite3::CantOpenException => _error + raise ActiveRecord::NoDatabaseError end def dbconsole(config, options = {}) @@ -114,6 +110,7 @@ def initialize(...) end end + @config[:flags] = ::SQLite3::Constants::Open::READWRITE if @config[:flags].nil? && !@config[:readonly] @config[:strict] = ConnectionAdapters::SQLite3Adapter.strict_strings_by_default unless @config.key?(:strict) @connection_parameters = @config.merge(database: @config[:database].to_s, results_as_hash: true) @use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index d568c41307228..bacc5e112a631 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -16,6 +16,7 @@ def create raise DatabaseAlreadyExists if File.exist?(db_config.database) establish_connection + connection.create_database connection end @@ -71,7 +72,6 @@ def connection def establish_connection(config = db_config) ActiveRecord::Base.establish_connection(config) - connection.connect! end def run_cmd(cmd, args, out) diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb index 2ecf38de53862..d9165efd81b17 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb @@ -14,6 +14,7 @@ def test_sqlite_creates_directory adapter: "sqlite3", timeout: 100, ) + @conn.create_database @conn.connect! assert Dir.exist? dir.join("db") diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 6fa8e6b1d861e..2d7d49830f643 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -1599,6 +1599,7 @@ def setup handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new ActiveRecord::Base.connection_handler = handler handler.establish_connection(db_config) + ActiveRecord::Base.connection.create_database ActiveRecord::Base.connects_to(database: { writing: :default, reading: :readonly }) diff --git a/activerecord/test/cases/migration/pending_migrations_test.rb b/activerecord/test/cases/migration/pending_migrations_test.rb index b19d90ad83c58..31ba5e281808a 100644 --- a/activerecord/test/cases/migration/pending_migrations_test.rb +++ b/activerecord/test/cases/migration/pending_migrations_test.rb @@ -14,7 +14,10 @@ class PendingMigrationsTest < ActiveRecord::TestCase @original_configurations = ActiveRecord::Base.configurations ActiveRecord::Base.configurations = base_config - ActiveRecord::Base.establish_connection(:primary) + conn_secondary = ActiveRecord::Base.establish_connection(:secondary) + conn_secondary.connection.create_database + conn_primary = ActiveRecord::Base.establish_connection(:primary) + conn_primary.connection.create_database end teardown do