Skip to content

Commit

Permalink
Use storage/ instead of db/ for sqlite3 db files (#46699)
Browse files Browse the repository at this point in the history
* Use storage/ instead of db/ for sqlite3 db files

db/ should be for configuration only, not data. This will make it easier to mount a single volume into a container for testing, development, and even sqlite3 in production.
  • Loading branch information
dhh committed Dec 12, 2022
1 parent 16eaf44 commit d18fc32
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 149 deletions.
6 changes: 3 additions & 3 deletions actionmailbox/test/dummy/config/database.yml
Expand Up @@ -11,15 +11,15 @@ default: &default

development:
<<: *default
database: db/development.sqlite3
database: storage/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
database: storage/test.sqlite3

production:
<<: *default
database: db/production.sqlite3
database: storage/production.sqlite3
6 changes: 3 additions & 3 deletions actiontext/test/dummy/config/database.yml
Expand Up @@ -11,15 +11,15 @@ default: &default

development:
<<: *default
database: db/development.sqlite3
database: storage/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
database: storage/test.sqlite3

production:
<<: *default
database: db/production.sqlite3
database: storage/production.sqlite3
2 changes: 1 addition & 1 deletion activerecord/CHANGELOG.md
Expand Up @@ -697,7 +697,7 @@
```yaml
development:
<<: *default
database: db/development.sqlite3
database: storage/development.sqlite3
foreign_keys: false
```

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/connection_handling.rb
Expand Up @@ -272,7 +272,7 @@ def primary_class? # :nodoc:
#
# ActiveRecord::Base.connection_db_config
# #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
# @name="primary", @config={pool: 5, timeout: 5000, database: "db/development.sqlite3", adapter: "sqlite3"}>
# @name="primary", @config={pool: 5, timeout: 5000, database: "storage/development.sqlite3", adapter: "sqlite3"}>
#
# Use only for reading.
def connection_db_config
Expand Down
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/core.rb
Expand Up @@ -53,19 +53,19 @@ def self.destroy_association_async_job
#
# development:
# adapter: sqlite3
# database: db/development.sqlite3
# database: storage/development.sqlite3
#
# production:
# adapter: sqlite3
# database: db/production.sqlite3
# database: storage/production.sqlite3
#
# ...would result in ActiveRecord::Base.configurations to look like this:
#
# #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[
# #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
# @name="primary", @config={adapter: "sqlite3", database: "db/development.sqlite3"}>,
# @name="primary", @config={adapter: "sqlite3", database: "storage/development.sqlite3"}>,
# #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90 @env_name="production",
# @name="primary", @config={adapter: "sqlite3", database: "db/production.sqlite3"}>
# @name="primary", @config={adapter: "sqlite3", database: "storage/production.sqlite3"}>
# ]>
def self.configurations=(config)
@@configurations = ActiveRecord::DatabaseConfigurations.new(config)
Expand Down
Expand Up @@ -204,13 +204,13 @@ def test_symbolized_configurations_assignment
development: {
primary: {
adapter: "sqlite3",
database: "test/db/development.sqlite3",
database: "test/storage/development.sqlite3",
},
},
test: {
primary: {
adapter: "sqlite3",
database: "test/db/test.sqlite3",
database: "test/storage/test.sqlite3",
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -418,7 +418,7 @@ def test_do_not_dump_foreign_keys_when_bypassed_by_config
ActiveRecord::Base.establish_connection(
{
adapter: "sqlite3",
database: "test/db/test.sqlite3",
database: "test/storage/test.sqlite3",
foreign_keys: false,
}
)
Expand Down
6 changes: 3 additions & 3 deletions guides/source/configuring.md
Expand Up @@ -2772,7 +2772,7 @@ Here's the section of the default configuration file (`config/database.yml`) wit
```yaml
development:
adapter: sqlite3
database: db/development.sqlite3
database: storage/development.sqlite3
pool: 5
timeout: 5000
```
Expand Down Expand Up @@ -2844,7 +2844,7 @@ If you choose to use SQLite3 and are using JRuby, your `config/database.yml` wil
```yaml
development:
adapter: jdbcsqlite3
database: db/development.sqlite3
database: storage/development.sqlite3
```
#### Configuring a MySQL or MariaDB Database for JRuby Platform
Expand Down Expand Up @@ -3198,7 +3198,7 @@ Active Record database connections are managed by `ActiveRecord::ConnectionAdapt
```ruby
development:
adapter: sqlite3
database: db/development.sqlite3
database: storage/development.sqlite3
pool: 5
timeout: 5000
```
Expand Down
6 changes: 6 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,9 @@
* Always generate the storage/ directory with rails new to ensure there's a stable place to
put permanent files, and a single mount point for containers to map. Then default sqlite3 databases
to live there instead of db/, which is only meant for configuration, not data.

*DHH*

* Rails console now disables `IRB`'s autocompletion feature in production by default.

Setting `IRB_USE_AUTOCOMPLETE=true` can override this default.
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -406,7 +406,7 @@ def create_system_test_files
end

def create_storage_files
build(:storage) unless skip_active_storage?
build(:storage)
end

def delete_app_assets_if_api_option
Expand Down
Expand Up @@ -10,15 +10,15 @@ default: &default

development:
<<: *default
database: db/development.sqlite3
database: storage/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
database: storage/test.sqlite3

production:
<<: *default
database: db/production.sqlite3
database: storage/production.sqlite3
Expand Up @@ -11,15 +11,15 @@ default: &default

development:
<<: *default
database: db/development.sqlite3
database: storage/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
database: storage/test.sqlite3

production:
<<: *default
database: db/production.sqlite3
database: storage/production.sqlite3
10 changes: 1 addition & 9 deletions railties/lib/rails/generators/rails/app/templates/gitignore.tt
Expand Up @@ -7,12 +7,6 @@
# Ignore bundler config.
/.bundle

<% if sqlite3? -%>
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-*

<% end -%>
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
Expand All @@ -26,16 +20,14 @@
!/tmp/pids/.keep
<% end -%>

<% unless skip_active_storage? -%>
# Ignore uploaded files in development.
# Ignore storage (uploaded files in development and any SQLite databases).
/storage/*
<% if keeps? -%>
!/storage/.keep
/tmp/storage/*
!/tmp/storage/
!/tmp/storage/.keep
<% end -%>
<% end -%>
<% unless options.api? -%>

/public/assets
Expand Down
Expand Up @@ -9,8 +9,6 @@
/<%= dummy_path %>/db/*.sqlite3-*
<% end -%>
/<%= dummy_path %>/log/*.log
<% unless skip_active_storage? -%>
/<%= dummy_path %>/storage/
<% end -%>
/<%= dummy_path %>/tmp/
<% end -%>
6 changes: 3 additions & 3 deletions railties/test/application/dbconsole_test.rb
Expand Up @@ -29,7 +29,7 @@ def test_use_value_defined_in_environment_file_in_database_yml

app_file "config/environments/development.rb", <<-RUBY
Rails.application.configure do
config.database = "db/development.sqlite3"
config.database = "storage/development.sqlite3"
end
RUBY

Expand All @@ -49,11 +49,11 @@ def test_respect_environment_option
development:
<<: *default
database: db/development.sqlite3
database: storage/development.sqlite3
production:
<<: *default
database: db/production.sqlite3
database: storage/production.sqlite3
YAML

primary, replica = PTY.open
Expand Down
4 changes: 2 additions & 2 deletions railties/test/application/multi_db_rake_test.rb
Expand Up @@ -43,10 +43,10 @@ def test_migrations_paths_takes_first
development:
primary:
<<: *default
database: db/development.sqlite3
database: storage/development.sqlite3
animals:
<<: *default
database: db/development_animals.sqlite3
database: storage/development_animals.sqlite3
migrations_paths:
- db/animals_migrate
- db/common
Expand Down

0 comments on commit d18fc32

Please sign in to comment.