Class: SchemaEvolutionManager::Db
- Inherits:
-
Object
- Object
- SchemaEvolutionManager::Db
- Defined in:
- lib/schema-evolution-manager/db.rb
Instance Attribute Summary collapse
-
#psql_executable_with_options ⇒ Object
readonly
Returns the value of attribute psql_executable_with_options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#bootstrap! ⇒ Object
Installs schema_evolution_manager.
-
#initialize(url, opts = {}) ⇒ Db
constructor
A new instance of Db.
-
#psql_command(sql_command) ⇒ Object
executes a simple sql command.
-
#psql_file(filename, path) ⇒ Object
executes sql commands from a file in a single transaction.
-
#sanitized_url ⇒ Object
Returns a sanitized version of the URL with the password removed to prevent passwords from being logged or displayed in error messages.
-
#schema_schema_evolution_manager_exists? ⇒ Boolean
True if the specific schema exists; false otherwise.
Constructor Details
#initialize(url, opts = {}) ⇒ Db
Returns a new instance of Db.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/schema-evolution-manager/db.rb', line 7 def initialize(url, opts={}) @url = Preconditions.check_not_blank(url, "url cannot be blank") password = opts.delete(:password) @psql_executable_with_options = "psql" (opts.delete(:set) || []).each do |arg| @psql_executable_with_options << " --set #{arg}" end Preconditions.assert_empty_opts(opts) connection_data = ConnectionData.parse_url(@url) if password ENV['PGPASSFILE'] = Db.password_to_tempfile(connection_data.pgpass(password)) end end |
Instance Attribute Details
#psql_executable_with_options ⇒ Object (readonly)
Returns the value of attribute psql_executable_with_options.
5 6 7 |
# File 'lib/schema-evolution-manager/db.rb', line 5 def @psql_executable_with_options end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
5 6 7 |
# File 'lib/schema-evolution-manager/db.rb', line 5 def url @url end |
Instance Method Details
#bootstrap! ⇒ Object
Installs schema_evolution_manager. Automatically upgrades schema_evolution_manager.
25 26 27 28 29 30 31 32 |
# File 'lib/schema-evolution-manager/db.rb', line 25 def bootstrap! scripts = Scripts.new(self, Scripts::BOOTSTRAP_SCRIPTS) dir = File.join(Library.base_dir, "scripts") scripts.each_pending(dir) do |filename, path| psql_file(filename, path) scripts.record_as_run!(filename) end end |
#psql_command(sql_command) ⇒ Object
executes a simple sql command.
35 36 37 38 39 40 41 |
# File 'lib/schema-evolution-manager/db.rb', line 35 def psql_command(sql_command) Preconditions.assert_class(sql_command, String) template = "#{@psql_executable_with_options} --no-align --tuples-only --no-psqlrc --command \"%s\" %s" command = template % [sql_command, Shellwords.escape(@url)] command_to_log = template % [sql_command, sanitized_url] Library.system_or_error(command, command_to_log) end |
#psql_file(filename, path) ⇒ Object
executes sql commands from a file in a single transaction
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/schema-evolution-manager/db.rb', line 70 def psql_file(filename, path) Preconditions.assert_class(path, String) Preconditions.check_state(File.exist?(path), "File[%s] not found" % path) = Db.attribute_values(path).join(" ") Library.with_temp_file(:prefix => File.basename(path)) do |tmp| File.open(tmp, "w") do |out| out << "\\set ON_ERROR_STOP true\n\n" out << IO.read(path) end command = "#{@psql_executable_with_options} --file \"%s\" #{} %s" % [tmp, Shellwords.escape(@url)] Library.with_temp_file do |output| result = `#{command} > #{output} 2>&1`.strip status = $? if status.to_i > 0 errors = File.exist?(output) ? IO.read(output) : result raise ScriptError.new(self, filename, path, errors) end end end end |
#sanitized_url ⇒ Object
Returns a sanitized version of the URL with the password removed to prevent passwords from being logged or displayed in error messages
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/schema-evolution-manager/db.rb', line 139 def sanitized_url # Parse the URL to extract components if @url.include?("://") protocol, rest = @url.split("://", 2) lead, name = rest.split("/", 2) # Check if there's a username:password@ pattern if lead.include?("@") # Take the last element as host_part to handle passwords with @ symbols host_part = lead.split("@").last # Take everything before the last @ as user_part user_part = lead.split("@")[0..-2].join("@") if user_part.include?(":") # Remove password, keep only username (everything before the first colon) username = user_part.split(":", 2)[0] sanitized_lead = "#{username}:[REDACTED]@#{host_part}" else sanitized_lead = lead end "#{protocol}://#{sanitized_lead}/#{name}" else @url end else @url end end |
#schema_schema_evolution_manager_exists? ⇒ Boolean
True if the specific schema exists; false otherwise
96 97 98 99 |
# File 'lib/schema-evolution-manager/db.rb', line 96 def schema_schema_evolution_manager_exists? sql = "select count(*) from pg_namespace where nspname='%s'" % Db.schema_name psql_command(sql).to_i > 0 end |