|
3 | 3 | gem "omniauth-rails_csrf_protection"
|
4 | 4 | gem "letter_opener", group: :development
|
5 | 5 |
|
| 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 | + |
6 | 94 | after_bundle do
|
7 | 95 | git add: "."
|
8 | 96 |
|
|
0 commit comments