Class: ActivePostgres::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- ActivePostgres::Generators::InstallGenerator
- Defined in:
- lib/active_postgres/generators/active_postgres/install_generator.rb
Instance Method Summary collapse
- #create_config_file ⇒ Object
- #show_credentials_template ⇒ Object
- #show_next_steps ⇒ Object
- #update_database_yml ⇒ Object
Instance Method Details
#create_config_file ⇒ Object
11 12 13 |
# File 'lib/active_postgres/generators/active_postgres/install_generator.rb', line 11 def create_config_file template 'postgres.yml.erb', 'config/postgres.yml' end |
#show_credentials_template ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/active_postgres/generators/active_postgres/install_generator.rb', line 97 def show_credentials_template puts "\nš Add to Rails credentials (rails credentials:edit):" puts puts 'postgres:' puts ' # Rails application database credentials' puts " username: #{boring_app_name}" puts " password: \"#{generate_secure_password}\"" puts " database: #{boring_app_name}_production" puts ' primary_host: YOUR_PRIMARY_IP # Update after provisioning (e.g., 10.8.0.100)' puts ' replica_host: YOUR_REPLICA_IP # Update after provisioning (e.g., 10.8.0.101)' puts ' port: 5432' puts ' statement_timeout: 15s' puts " application_name: #{boring_app_name}" puts puts ' # PostgreSQL server setup credentials (for active_postgres rake tasks)' puts " superuser_password: \"#{generate_secure_password}\"" puts " replication_password: \"#{generate_secure_password}\"" puts " repmgr_password: \"#{generate_secure_password}\"" puts " monitoring_password: \"#{generate_secure_password}\"" puts " grafana_admin_password: \"#{generate_secure_password}\"" puts puts 'š Secure passwords have been auto-generated above.' puts 'š Update primary_host and replica_host with your actual IPs after provisioning.' end |
#show_next_steps ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_postgres/generators/active_postgres/install_generator.rb', line 122 def show_next_steps puts "\nā ActivePostgres installed!" puts "\nš Next steps:" puts ' 1. Either:' puts ' a) Edit config/postgres.yml with your database servers, OR' puts ' b) Use Terraform to auto-generate config/postgres.yml' puts ' 2. Add secrets to Rails credentials (see above)' puts ' 3. Deploy PostgreSQL HA: rails active_postgres:setup' puts ' 4. Check health: rails active_postgres:health' puts "\nš See config/postgres.example.yml in gem for full examples" end |
#update_database_yml ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_postgres/generators/active_postgres/install_generator.rb', line 15 def update_database_yml if File.exist?('config/database.yml') contents = File.read('config/database.yml') # Extract production section production_section = contents[/^production:.*?(?=^[a-z_]+:|\z)/m] || '' # Check if we have the NEW Rails credentials-based config in production section if production_section.include?('Generated by active_postgres') && production_section.include?('Rails.application.credentials.dig(:postgres') say_status :skipped, 'config/database.yml already has ActivePostgres production config' return end # Check if old ENV-based config exists if production_section.include?('Generated by active_postgres') && production_section.include?('ENV.fetch') say_status :info, 'Found old ENV-based ActivePostgres config' return unless yes?('Replace with Rails credentials-based config? (y/n)', :yellow) end # Remove existing production section if present if contents =~ /^production:/m if contents.include?('Generated by active_postgres') say_status :info, 'Replacing ActivePostgres production config' else say_status :info, 'Found existing production config' return unless yes?('Replace with ActivePostgres config? (y/n)', :yellow) end lines = contents.lines new_lines = [] skip_section = false skip_boring_comment = false lines.each do |line| # Skip the "Generated by active_postgres" comment before production section if line =~ /^# Generated by active_postgres/ skip_boring_comment = true next end if line =~ /^production:/ skip_section = true skip_boring_comment = false next elsif skip_section && (line =~ /^[a-z_]+:/ || line =~ /^# [A-Z]/) skip_section = false end new_lines << line unless skip_section || skip_boring_comment end # Remove trailing whitespace lines new_lines.pop while new_lines.last&.strip&.empty? contents = new_lines.join end # Append ActivePostgres production config directly contents += "\n" unless contents.end_with?("\n") contents += generate_production_config File.write('config/database.yml', contents) say_status :updated, 'config/database.yml (added ActivePostgres production config)' else create_file 'config/database.yml', <<~YAML default: &default adapter: postgresql encoding: unicode pool: <%= ENV.fetch("DB_POOL") { ENV.fetch("RAILS_MAX_THREADS") { 5 } } %> development: <<: *default database: #{boring_app_name}_development test: <<: *default database: #{boring_app_name}_test #{generate_production_config} YAML end end |