Class: Mysql2postgres::Connection
- Inherits:
-
Object
- Object
- Mysql2postgres::Connection
- Defined in:
- lib/mysql2postgres/connection.rb
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#copy_manager ⇒ Object
readonly
Returns the value of attribute copy_manager.
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#is_copying ⇒ Object
readonly
Returns the value of attribute is_copying.
-
#login ⇒ Object
readonly
Returns the value of attribute login.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
- #clear_schema ⇒ Object
- #execute(sql) ⇒ Object
-
#finish ⇒ Object
we’re done talking to the database, so close the connection cleanly.
-
#flush ⇒ Object
ensure that the copy is completed, in case we hadn’t seen a ‘.’ in the data stream.
-
#initialize(pg_options) ⇒ Connection
constructor
A new instance of Connection.
-
#load_file(path) ⇒ Object
given a file containing psql syntax at path, pipe it down to the database.
- #open ⇒ Object
- #raise_nil_connection ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(pg_options) ⇒ Connection
Returns a new instance of Connection.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mysql2postgres/connection.rb', line 16 def initialize() @hostname = [:hostname] || 'localhost' @login = [:username] @password = [:password] @database = [:database] @port = ([:port] || 5432).to_s @database, @schema = database.split ':' @conn = open raise_nil_connection if conn.nil? @is_copying = false @current_statement = '' end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def conn @conn end |
#copy_manager ⇒ Object (readonly)
Returns the value of attribute copy_manager.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def copy_manager @copy_manager end |
#database ⇒ Object (readonly)
Returns the value of attribute database.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def database @database end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def hostname @hostname end |
#is_copying ⇒ Object (readonly)
Returns the value of attribute is_copying.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def @is_copying end |
#login ⇒ Object (readonly)
Returns the value of attribute login.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def login @login end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def port @port end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def schema @schema end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
5 6 7 |
# File 'lib/mysql2postgres/connection.rb', line 5 def stream @stream end |
Instance Method Details
#clear_schema ⇒ Object
102 103 104 105 106 107 |
# File 'lib/mysql2postgres/connection.rb', line 102 def clear_schema statements = ['DROP SCHEMA PUBLIC CASCADE', 'CREATE SCHEMA PUBLIC'] statements.each do |statement| run_statement statement end end |
#execute(sql) ⇒ Object
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 |
# File 'lib/mysql2postgres/connection.rb', line 49 def execute(sql) if sql.match(/^COPY /) && ! # sql.chomp! # cHomp! cHomp! conn.exec sql @is_copying = true elsif sql.match(/^(ALTER|CREATE|DROP|SELECT|SET|TRUNCATE) /) && ! @current_statement = sql elsif if sql.chomp == '\.' || sql.chomp.match(/^$/) flush else begin until conn.put_copy_data sql warn ' waiting for connection to be writable...' sleep 0.1 end rescue StandardError => e @is_copying = false warn e raise e end end elsif @current_statement.length.positive? @current_statement << ' ' @current_statement << sql end return unless @current_statement.match?(/;$/) run_statement @current_statement @current_statement = '' end |
#finish ⇒ Object
we’re done talking to the database, so close the connection cleanly.
83 84 85 |
# File 'lib/mysql2postgres/connection.rb', line 83 def finish @conn.finish end |
#flush ⇒ Object
ensure that the copy is completed, in case we hadn’t seen a ‘.’ in the data stream.
41 42 43 44 45 46 47 |
# File 'lib/mysql2postgres/connection.rb', line 41 def flush conn.put_copy_end rescue StandardError => e warn e ensure @is_copying = false end |
#load_file(path) ⇒ Object
given a file containing psql syntax at path, pipe it down to the database.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mysql2postgres/connection.rb', line 88 def load_file(path) if @conn File.open path, 'r:UTF-8' do |file| file.each_line do |line| execute line end flush end finish else raise_nil_connection end end |
#open ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/mysql2postgres/connection.rb', line 32 def open @conn = PG::Connection.open dbname: database, user: login, password: password, host: hostname, port: port end |
#raise_nil_connection ⇒ Object
109 110 111 |
# File 'lib/mysql2postgres/connection.rb', line 109 def raise_nil_connection raise 'No Connection' end |
#tables ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/mysql2postgres/connection.rb', line 113 def tables result = run_statement <<~SQL_TABLES SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE' SQL_TABLES result.map { |t| t['table_name'] } end |