From bf1024de5500f3c8d2c3b1876894feb02ba26ea7 Mon Sep 17 00:00:00 2001 From: Carl Brasic <677224+brasic@users.noreply.github.com> Date: Wed, 27 Jun 2018 22:01:58 -0500 Subject: Support readonly option in SQLite3Adapter Readonly sqlite database files are very useful as a data format for storing configuration/lookup data that is too complicated for YAML files. But since such files would typically be committed to a source control repository, it's important to ensure that they are truly safe from being inadvertently modified. Unfortunately using unix permissions isn't enough, as sqlite will "helpfully" add the write bit to a database file whenever it's written to. sqlite3-ruby has supported a `:readonly` option since version 1.3.2 (see https://github.com/sparklemotion/sqlite3-ruby/commit/c20c9f5dd2990042) This simply passes that option through to the adapter if present in the config hash. I think this is best considered an adapter-specific option since no other supported database has an identical concept. --- .../cases/adapters/sqlite3/sqlite3_adapter_test.rb | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index ad155c7492..86cbe530a0 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -504,6 +504,39 @@ module ActiveRecord assert_deprecated { @conn.valid_alter_table_type?(:string) } end + def test_db_is_not_readonly_when_readonly_option_is_false + @conn = Base.sqlite3_connection database: ":memory:", + adapter: "sqlite3", + readonly: false + + assert_not_predicate(@conn.raw_connection, :readonly?) + end + + def test_db_is_not_readonly_when_readonly_option_is_unspecified + @conn = Base.sqlite3_connection database: ":memory:", + adapter: "sqlite3" + + assert_not_predicate(@conn.raw_connection, :readonly?) + end + + def test_db_is_readonly_when_readonly_option_is_true + @conn = Base.sqlite3_connection database: ":memory:", + adapter: "sqlite3", + readonly: true + + assert_predicate(@conn.raw_connection, :readonly?) + end + + def test_writes_are_not_permitted_to_readonly_databases + @conn = Base.sqlite3_connection database: ":memory:", + adapter: "sqlite3", + readonly: true + + assert_raises(ActiveRecord::StatementInvalid, /SQLite3::ReadOnlyException/) do + @conn.execute("CREATE TABLE test(id integer)") + end + end + private def assert_logged(logs) -- cgit v1.2.3