Skip to content

Commit 0dff577

Browse files
committed
feat: support pg
1 parent 7a8ebd8 commit 0dff577

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This template is designed for personal use and open-source projects. It provides a quick start for Rails applications with essential features pre-configured, requiring minimal setup.
44

5-
- **Database**: SQLite3 for simplicity and ease of setup
5+
- **Database**: Your choice of SQLite (default) or PostgreSQL
66
- **Defaults**:
77
- Solid Cache for efficient caching
88
- Solid Queue for background job processing
@@ -20,6 +20,8 @@ This template is designed for personal use and open-source projects. It provides
2020
rails new <app_name> -c tailwind -m https://raw.githubusercontent.com/bastos/rails-template/refs/heads/main/template.rb
2121
```
2222

23+
During setup, the template will ask if you want to use PostgreSQL instead of the default SQLite database.
24+
2325
## License
2426

2527
This project is released under the [MIT License](https://opensource.org/licenses/MIT).

template.rb

+88
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,94 @@
33
gem "omniauth-rails_csrf_protection"
44
gem "letter_opener", group: :development
55

6+
# Ask the user if they want to use PostgreSQL
7+
use_postgres = yes?("Use PostgreSQL instead of the default SQLite? (y/N)")
8+
9+
if use_postgres
10+
say "Configuring for PostgreSQL...", :green
11+
12+
# Add pg gem
13+
gem 'pg', '~> 1.1' # From Rails 7.1 defaults, adjust if needed
14+
15+
# Remove sqlite3 gem line (will be added by default if not specified in `rails new`)
16+
# This ensures it's removed even if the user didn't use --database=postgresql initially
17+
gsub_file 'Gemfile', /^gem\s+["']sqlite3["'].*$/, ''
18+
19+
# Remove existing database.yml if it exists
20+
remove_file 'config/database.yml'
21+
22+
# Create new database.yml for Postgres using the app_name variable available in the template context
23+
create_file 'config/database.yml' do <<~YAML
24+
# PostgreSQL. Versions 9.3 and up are supported.
25+
#
26+
# Install the pg driver:
27+
# gem install pg
28+
# On macOS with Homebrew:
29+
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
30+
# On Windows:
31+
# gem install pg
32+
# Choose the win32 build.
33+
# Install PostgreSQL and put its /bin directory on your path.
34+
#
35+
# Configure Using Gemfile
36+
# gem "pg"
37+
#
38+
default: &default
39+
adapter: postgresql
40+
encoding: unicode
41+
# For details on connection pooling, see Rails configuration guide
42+
# https://guides.rubyonrails.org/configuring.html#database-pooling
43+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
44+
45+
development:
46+
<<: *default
47+
database: #{app_name}_development
48+
# The specified database role being used to connect to postgres.
49+
# To create additional roles in postgres see `$ createuser --help`.
50+
# When running `$ rails db:setup`, the development database will be created.
51+
# Examples:
52+
#
53+
# domain: '/var/run/postgresql'
54+
# host: localhost
55+
# port: 5432
56+
# username: #{app_name}
57+
# password: ''
58+
59+
test:
60+
<<: *default
61+
database: #{app_name}_test
62+
63+
# As with config/credentials.yml, you never want to store sensitive information,
64+
# like your database password, in your source code. If your source code is
65+
# ever seen by anyone, they now have access to your database.
66+
#
67+
# Instead, provide the password or database URL as an environment variable when you
68+
# deploy your application. For example:
69+
#
70+
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
71+
#
72+
# If the connection URL is provided in the special DATABASE_URL environment
73+
# variable, Rails will automatically merge its configuration values on top of
74+
# the values provided in this file. Alternatively, you can specify a connection
75+
# URL environment variable explicitly:
76+
#
77+
# production:
78+
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
79+
#
80+
production:
81+
<<: *default
82+
database: #{app_name}_production
83+
username: #{app_name}
84+
password: <%= ENV["#{app_name.upcase}_DATABASE_PASSWORD"] %>
85+
YAML
86+
end
87+
else
88+
# Explicitly add sqlite3 gem if not using Postgres, in case it was removed by mistake or skipped
89+
# This makes the default choice more robust.
90+
gem 'sqlite3', '~> 1.4'
91+
say "Using default SQLite.", :yellow
92+
end
93+
694
after_bundle do
795
git add: "."
896

0 commit comments

Comments
 (0)