Module: HTM::Config::Database
- Included in:
- HTM::Config
- Defined in:
- lib/htm/config/database.rb
Instance Method Summary collapse
-
#actual_database_name ⇒ String?
Extract the actual database name from URL or config.
- #database_config ⇒ Object
- #database_configured? ⇒ Boolean
-
#database_host ⇒ String?
The database host.
-
#database_name ⇒ String?
The database name.
-
#database_password ⇒ String?
The database password.
-
#database_port ⇒ Integer?
The database port.
-
#database_url ⇒ Object
Database convenience methods.
-
#database_user ⇒ String?
The database user.
-
#expected_database_name ⇒ String
Returns the expected database name based on service.name and environment.
-
#parse_database_url ⇒ Hash?
Parse database URL into component hash.
-
#valid_database_name? ⇒ Boolean
Check if the database name matches the expected convention.
-
#validate_database! ⇒ true
Validate that database is configured for the current environment.
-
#validate_database_name! ⇒ true
Validate that the database name follows the naming convention.
Instance Method Details
#actual_database_name ⇒ String?
Extract the actual database name from URL or config
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/htm/config/database.rb', line 134 def actual_database_name url = database&.url if url && !url.empty? # Parse database name from URL: postgresql://user@host:port/dbname uri = URI.parse(url) rescue nil return uri&.path&.sub(%r{^/}, '') end database&.name end |
#database_config ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/htm/config/database.rb', line 44 def database_config url = database_url return {} unless url uri = URI.parse(url) # Coercion now merges env vars with SCHEMA defaults, so pool_size/timeout # are always available even when only HTM_DATABASE__URL is set { adapter: 'postgresql', host: uri.host, port: uri.port || 5432, database: uri.path&.sub(%r{^/}, ''), username: uri.user, password: uri.password, pool: database.pool_size.to_i, timeout: database.timeout.to_i, sslmode: database.sslmode, encoding: 'unicode', prepared_statements: false, advisory_locks: false }.compact end |
#database_configured? ⇒ Boolean
68 69 70 71 |
# File 'lib/htm/config/database.rb', line 68 def database_configured? url = database_url (url && !url.empty?) || (database.name && !database.name.empty?) end |
#database_host ⇒ String?
Returns the database host.
20 21 22 |
# File 'lib/htm/config/database.rb', line 20 def database_host database.host end |
#database_name ⇒ String?
Returns the database name.
30 31 32 |
# File 'lib/htm/config/database.rb', line 30 def database_name database.name end |
#database_password ⇒ String?
Returns the database password.
40 41 42 |
# File 'lib/htm/config/database.rb', line 40 def database_password database.password end |
#database_port ⇒ Integer?
Returns the database port.
25 26 27 |
# File 'lib/htm/config/database.rb', line 25 def database_port database.port end |
#database_url ⇒ Object
Database convenience methods
74 75 76 77 78 79 |
# File 'lib/htm/config/database.rb', line 74 def database_url url = database.url return url if url && !url.empty? build_database_url end |
#database_user ⇒ String?
Returns the database user.
35 36 37 |
# File 'lib/htm/config/database.rb', line 35 def database_user database.user end |
#expected_database_name ⇒ String
Returns the expected database name based on service.name and environment
127 128 129 |
# File 'lib/htm/config/database.rb', line 127 def expected_database_name "#{service_name}_#{environment}" end |
#parse_database_url ⇒ Hash?
Parse database URL into component hash
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/htm/config/database.rb', line 194 def parse_database_url url = database&.url return nil if url.nil? || url.empty? uri = URI.parse(url) rescue nil return nil unless uri # Parse query string for sslmode query_params = URI.decode_www_form(uri.query || '').to_h sslmode = query_params['sslmode'] { host: uri.host, port: uri.port, name: uri.path&.sub(%r{^/}, ''), user: uri.user, password: uri.password, sslmode: sslmode }.compact end |
#valid_database_name? ⇒ Boolean
Check if the database name matches the expected convention
183 184 185 |
# File 'lib/htm/config/database.rb', line 183 def valid_database_name? actual_database_name == expected_database_name end |
#validate_database! ⇒ true
Validate that database is configured for the current environment
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/htm/config/database.rb', line 85 def validate_database! validate_environment! unless database_configured? raise HTM::ConfigurationError, "No database configured for environment '#{environment}'. " \ "Set HTM_DATABASE__URL or HTM_DATABASE__NAME, " \ "or add database.name to the '#{environment}:' section in your config." end true end |
#validate_database_name! ⇒ true
Validate that the database name follows the naming convention
Database names must be: HTM::Config#service_name_HTM::Config#environment
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/htm/config/database.rb', line 161 def validate_database_name! actual = actual_database_name expected = expected_database_name return true if actual == expected raise HTM::ConfigurationError, "Database name '#{actual}' does not match expected '#{expected}'.\n" \ "Database names must follow the convention: {service_name}_{environment}\n " \ "Service name: #{service_name}\n " \ "Environment: #{environment}\n " \ "Expected: #{expected}\n " \ "Actual: #{actual}\n\n" \ "Either:\n " \ "- Set HTM_DATABASE__URL to point to '#{expected}'\n " \ "- Set HTM_DATABASE__NAME=#{expected}\n " \ "- Change HTM_ENV to match the database suffix" end |