Module: PgSync::Utils
- Included in:
- Client, DataSource, Init, SchemaSync, Sync, TableSync, Task, TaskResolver
- Defined in:
- lib/pgsync/utils.rb
Constant Summary collapse
- COLOR_CODES =
{ red: 31, green: 32, yellow: 33, cyan: 36 }
Instance Method Summary collapse
- #colorize(message, color) ⇒ Object
- #confirm_tables_exist(data_source, tasks, description) ⇒ Object
- #db_config_file(db) ⇒ Object
- #deprecated(message) ⇒ Object
- #escape(value) ⇒ Object
- #first_schema ⇒ Object
- #friendly_name(table) ⇒ Object
- #log(message = nil) ⇒ Object
- #monotonic_time ⇒ Object
- #output ⇒ Object
- #quote_ident(value) ⇒ Object
- #quote_ident_full(ident) ⇒ Object
-
#quote_string(s) ⇒ Object
activerecord.
- #task_name(task) ⇒ Object
- #warning(message) ⇒ Object
Instance Method Details
#colorize(message, color) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/pgsync/utils.rb', line 14 def colorize(, color) if output.tty? "\e[#{COLOR_CODES[color]}m#{}\e[0m" else end end |
#confirm_tables_exist(data_source, tasks, description) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/pgsync/utils.rb', line 38 def confirm_tables_exist(data_source, tasks, description) tasks.map(&:table).each do |table| unless data_source.table_exists?(table) raise Error, "Table not found in #{description}: #{table}" end end end |
#db_config_file(db) ⇒ Object
34 35 36 |
# File 'lib/pgsync/utils.rb', line 34 def db_config_file(db) ".pgsync-#{db}.yml" end |
#deprecated(message) ⇒ Object
26 27 28 |
# File 'lib/pgsync/utils.rb', line 26 def deprecated() warning "[DEPRECATED] #{}" end |
#escape(value) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/pgsync/utils.rb', line 74 def escape(value) if value.is_a?(String) "'#{quote_string(value)}'" else value end end |
#first_schema ⇒ Object
46 47 48 |
# File 'lib/pgsync/utils.rb', line 46 def first_schema @first_schema ||= source.search_path.find { |sp| sp != "pg_catalog" } end |
#friendly_name(table) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/pgsync/utils.rb', line 54 def friendly_name(table) if table.schema == first_schema table.name else table.full_name end end |
#log(message = nil) ⇒ Object
10 11 12 |
# File 'lib/pgsync/utils.rb', line 10 def log( = nil) output.puts end |
#monotonic_time ⇒ Object
87 88 89 |
# File 'lib/pgsync/utils.rb', line 87 def monotonic_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
#output ⇒ Object
30 31 32 |
# File 'lib/pgsync/utils.rb', line 30 def output $stderr end |
#quote_ident(value) ⇒ Object
70 71 72 |
# File 'lib/pgsync/utils.rb', line 70 def quote_ident(value) PG::Connection.quote_ident(value) end |
#quote_ident_full(ident) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/pgsync/utils.rb', line 62 def quote_ident_full(ident) if ident.is_a?(Table) || ident.is_a?(Sequence) [quote_ident(ident.schema), quote_ident(ident.name)].join(".") else # temp table names are strings quote_ident(ident) end end |
#quote_string(s) ⇒ Object
activerecord
83 84 85 |
# File 'lib/pgsync/utils.rb', line 83 def quote_string(s) s.gsub(/\\/, '\&\&').gsub(/'/, "''") end |
#task_name(task) ⇒ Object
50 51 52 |
# File 'lib/pgsync/utils.rb', line 50 def task_name(task) friendly_name(task.table) end |
#warning(message) ⇒ Object
22 23 24 |
# File 'lib/pgsync/utils.rb', line 22 def warning() log colorize(, :yellow) end |