Skip to content

Commit

Permalink
Ensure "sqlite3" is supported in addition to "sqlite" for testing
Browse files Browse the repository at this point in the history
The current name is legacy and rails now uses "sqlite3", starting to
move in that direction simplifies some setups (e.g. sandbox creation
inside extensions).

Also cleaned up the database.yml template which was barely readable.
  • Loading branch information
elia committed Apr 19, 2023
1 parent 5111949 commit 8185b11
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ executors:
working_directory: *workdir
environment:
<<: *environment
DB: sqlite
DB: sqlite3
docker:
- image: *image

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end
# and https://github.com/rails/sprockets-rails/issues/369
gem 'sprockets', '~> 3'

dbs = ENV['DB_ALL'] ? 'all' : ENV.fetch('DB', 'sqlite')
dbs = ENV['DB_ALL'] ? 'all' : ENV.fetch('DB', 'sqlite3')
gem 'mysql2', '~> 0.5.0', require: false if dbs.match?(/all|mysql/)
gem 'pg', '~> 1.0', require: false if dbs.match?(/all|postgres/)
gem 'fast_sqlite', require: false if dbs.match?(/all|sqlite/)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ createuser --superuser --echo postgres # only the first time
bin/build
```

The `bin/build` script runs using PostgreSQL by default, but it can be overridden by setting the DB environment variable to `DB=sqlite` or `DB=mysql`. For example:
The `bin/build` script runs using PostgreSQL by default, but it can be overridden by setting the DB environment variable to `DB=sqlite3` or `DB=mysql`. For example:

```bash
env DB=mysql bin/build
Expand Down
2 changes: 1 addition & 1 deletion bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

# Target postgresql. Override with: `env DB=sqlite bin/build`
# Target postgresql. Override with: `env DB=sqlite3 bin/build`
export DB=${DB:-postgresql}

if [ -n "$COVERAGE" ]; then
Expand Down
2 changes: 1 addition & 1 deletion bin/sandbox
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mysql)
USERNAME=$DB_USERNAME
PASSWORD=$DB_PASSWORD
;;
sqlite|'')
sqlite3|sqlite|'')
RAILSDB="sqlite3"
;;
*)
Expand Down
159 changes: 47 additions & 112 deletions core/lib/generators/spree/dummy/templates/rails/database.yml
Original file line number Diff line number Diff line change
@@ -1,113 +1,48 @@
<% db = case ENV['DB']
when 'mysql'
'mysql'
when 'postgres', 'postgresql'
'postgres'
when 'sqlite', '', nil
'sqlite'
else
raise "Invalid DB specified: #{ENV['DB']}"
end %>
<% db_host = case db
when 'mysql'
ENV['DB_MYSQL_HOST'] || ENV['DB_HOST']
when 'postgres'
ENV['DB_POSTGRES_HOST'] || ENV['DB_HOST']
else
ENV['DB_HOST']
end %>
<% db_username = ENV['DB_USERNAME'] %>
<% db_password = ENV['DB_PASSWORD'] %>
<%=
require 'yaml'
<% case ENV['DB']
when 'mysql' %>
development:
adapter: mysql2
database: <%= options[:lib_name] %>_solidus_development
<% unless db_username.blank? %>
username: <%= db_username %>
<% end %>
<% unless db_password.blank? %>
password: <%= db_password %>
<% end %>
<% unless db_host.blank? %>
host: <%= db_host %>
<% end %>
encoding: utf8
test:
adapter: mysql2
database: <%= options[:lib_name] %>_solidus_test
<% unless db_username.blank? %>
username: <%= db_username %>
<% end %>
<% unless db_password.blank? %>
password: <%= db_password %>
<% end %>
<% unless db_host.blank? %>
host: <%= db_host %>
<% end %>
encoding: utf8
production:
adapter: mysql2
database: <%= options[:lib_name] %>_solidus_production
<% unless db_username.blank? %>
username: <%= db_username %>
<% end %>
<% unless db_password.blank? %>
password: <%= db_password %>
<% end %>
<% unless db_host.blank? %>
host: <%= db_host %>
<% end %>
encoding: utf8
<% when 'postgres', 'postgresql' %>
development:
adapter: postgresql
database: <%= options[:lib_name] %>_solidus_development
<% unless db_username.blank? %>
username: <%= db_username %>
<% end %>
<% unless db_password.blank? %>
password: <%= db_password %>
<% end %>
<% unless db_host.blank? %>
host: <%= db_host %>
<% end %>
min_messages: warning
test:
adapter: postgresql
database: <%= options[:lib_name] %>_solidus_test
<% unless db_username.blank? %>
username: <%= db_username %>
<% end %>
<% unless db_password.blank? %>
password: <%= db_password %>
<% end %>
<% unless db_host.blank? %>
host: <%= db_host %>
<% end %>
min_messages: warning
production:
adapter: postgresql
database: <%= options[:lib_name] %>_solidus_production
<% unless db_username.blank? %>
username: <%= db_username %>
<% end %>
<% unless db_password.blank? %>
password: <%= db_password %>
<% end %>
<% unless db_host.blank? %>
host: <%= db_host %>
<% end %>
min_messages: warning
<% when 'sqlite', '', nil %>
development:
adapter: sqlite3
database: db/solidus_development.sqlite3
test:
adapter: sqlite3
database: db/solidus_test.sqlite3
production:
adapter: sqlite3
database: db/solidus_production.sqlite3
<% end %>
lib = "#{options[:lib_name]}_solidus"
case ENV['DB'].presence
when /mysql/
default = {
adapter: 'mysql2',
encoding: 'utf8',
host: (ENV['DB_MYSQL_HOST'] || ENV['DB_HOST']).presence,
username: ENV['DB_PASSWORD'].presence,
password: ENV['DB_USERNAME'].presence,
}.compact
config = {
development: default.merge(database: "#{lib}_development"),
production: default.merge(database: "#{lib}_production"),
test: default.merge(database: "#{lib}_test"),
}
when /postgres/ then 'postgres'
default = {
adapter: 'postgresql',
encoding: 'utf8',
host: ENV['DB_POSTGRES_HOST'] || ENV['DB_HOST'],
username: ENV['DB_PASSWORD'].presence,
password: ENV['DB_USERNAME'].presence,
}.compact
config = {
development: default.merge(database: "#{lib}_development"),
production: default.merge(database: "#{lib}_production"),
test: default.merge(database: "#{lib}_test"),
}
when /sqlite/, nil then 'sqlite3'
default = {
adapter: sqlite3
}
config = {
development: default.merge(database: "db/#{lib}_development.sqlite3"),
production: default.merge(database: "db/#{lib}_production.sqlite3"),
test: default.merge(database: "db/#{lib}_test.sqlite3"),
}
else raise "Invalid DB specified: #{ENV['DB'].inspect}"
end
config.deep_stringify_keys.to_yaml
%>

0 comments on commit 8185b11

Please sign in to comment.