Module: DatabaseHelpers
- Defined in:
- lib/tasks/task_helper.rb
Overview
Database helper methods for dad:db namespace
Instance Method Summary collapse
-
#create_database_sql(database, username, env = nil) ⇒ Object
Helper method to write database creation SQL commands.
-
#multiple_databases?(props) ⇒ Boolean
Helper method to check if a configuration is multiple databases (Rails 8 style).
Instance Method Details
#create_database_sql(database, username, env = nil) ⇒ Object
Helper method to write database creation SQL commands
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tasks/task_helper.rb', line 160 def create_database_sql(database, username, env = nil) system("echo 'drop database if exists #{database};' >> tmp/create_databases.sql") system("echo 'create database #{database};' >> tmp/create_databases.sql") # test環境はRailsの並列テストが database-0, database-1 ... を追加で作るため、 # それらにも権限が及ぶようワイルドカードでGRANTする(既存の"_"はリテラル一致させるためエスケープする) # バッククォートで囲むと、mysqlクライアントが行中の"\_"をコマンドと誤解釈するのを防ぎつつ、 # GRANTのdb_nameパターンとしては(通常のバッククォート識別子と異なり)バックスラッシュエスケープと # ワイルドカードが有効なまま解釈される escaped_database = database.gsub('_', '\\_') grant_target = env == 'test' ? "`#{escaped_database}%`" : database system("echo 'grant all on #{grant_target}.* to \"#{username}\"@\"%\";' >> tmp/create_databases.sql") if ENV['FILE'] system("echo 'grant all on #{grant_target}.* to \"#{username}\"@localhost;' >> tmp/create_databases.sql") system("echo 'grant file on *.* to \"#{username}\"@localhost;' >> tmp/create_databases.sql") end end |
#multiple_databases?(props) ⇒ Boolean
Helper method to check if a configuration is multiple databases (Rails 8 style)
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/tasks/task_helper.rb', line 147 def multiple_databases?(props) return false unless props.is_a?(Hash) # Standard Rails database config keys standard_keys = ['adapter', 'collation', 'database', 'encoding', 'host', 'max_connections', 'migrations_paths', 'password', 'port', 'url', 'username'] # If it has a 'database' key directly, it's a single database config return false if props.key?('database') # If it has keys that are not standard keys and those values are hashes with 'database' keys, it's multiple databases props.any? do |key, value| !standard_keys.include?(key.to_s) && value.is_a?(Hash) && value.key?('database') end end |