Class: Mysql2postgres::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql2postgres/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(pg_options)
  @hostname = pg_options[:hostname] || 'localhost'
  @login = pg_options[:username]
  @password = pg_options[:password]
  @database = pg_options[:database]
  @port = (pg_options[: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

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def conn
  @conn
end

#copy_managerObject (readonly)

Returns the value of attribute copy_manager.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def copy_manager
  @copy_manager
end

#databaseObject (readonly)

Returns the value of attribute database.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def database
  @database
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def hostname
  @hostname
end

#is_copyingObject (readonly)

Returns the value of attribute is_copying.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def is_copying
  @is_copying
end

#loginObject (readonly)

Returns the value of attribute login.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def 
  @login
end

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def port
  @port
end

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/mysql2postgres/connection.rb', line 5

def schema
  @schema
end

#streamObject (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_schemaObject



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 /) && !is_copying
    # sql.chomp!   # cHomp! cHomp!
    conn.exec sql
    @is_copying = true
  elsif sql.match(/^(ALTER|CREATE|DROP|SELECT|SET|TRUNCATE) /) && !is_copying
    @current_statement = sql
  elsif is_copying
    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

#finishObject

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

#flushObject

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

#openObject



32
33
34
35
36
37
38
# File 'lib/mysql2postgres/connection.rb', line 32

def open
  @conn = PG::Connection.open dbname: database,
                              user: ,
                              password: password,
                              host: hostname,
                              port: port
end

#raise_nil_connectionObject



109
110
111
# File 'lib/mysql2postgres/connection.rb', line 109

def raise_nil_connection
  raise 'No Connection'
end

#tablesObject



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