-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Jokela
committed
Jun 27, 2012
1 parent
fecddf9
commit a0b99ee
Showing
9 changed files
with
210 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,6 @@ mysql2psql: | |
|
||
preserve_order: true | ||
|
||
remove_dump_file: true | ||
|
||
report_status: json # false, json, xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
|
||
class Mysql2psql | ||
|
||
class Connection | ||
|
||
attr_reader :conn, :adapter, :hostname, :login, :password, :database, :schema, :port, :environment, :jruby, :copy_manager, :stream, :is_copying | ||
|
||
def initialize(options) | ||
|
||
# Rails-centric stuffs | ||
@environment = ENV['RAILS_ENV'].nil? ? 'development' : ENV['RAILS_ENV'] | ||
|
||
if options.has_key?('config') and options['config'].has_key?('destination') and options['config']['destination'].has_key?(environment) | ||
|
||
pg_options = Config.new(YAML::load(options['config']['destination'][environment].to_yaml)) | ||
@hostname, @login, @password, @database, @port = pg_options.hostname('localhost'), pg_options.username, pg_options.password, pg_options.database, pg_options.port(5432).to_s | ||
@database, @schema = database.split(":") | ||
|
||
@adapter = pg_options.adapter("jdbcpostgresql") | ||
|
||
else | ||
raise "Unable to locate PostgreSQL destination environment in the configuration file" | ||
end | ||
|
||
if RUBY_PLATFORM == 'jruby' | ||
@jruby = true | ||
|
||
ActiveRecord::Base.establish_connection( | ||
:adapter => adapter, | ||
:database => database, | ||
:username => login, | ||
:password => password, | ||
:host => hostname, | ||
:port => port) | ||
|
||
@conn = ActiveRecord::Base.connection_pool.checkout | ||
|
||
unless conn.nil? | ||
raw_connection = conn.raw_connection | ||
@copy_manager = org.postgresql.copy.CopyManager.new(raw_connection.connection) | ||
else | ||
raise_nil_connection | ||
end | ||
|
||
else | ||
@jruby = false | ||
|
||
@conn = PG.connect( dbname: database, user: login, password: password, host: hostname, port: port ) | ||
|
||
unless conn.nil? | ||
|
||
else | ||
raise_nil_connection | ||
end | ||
|
||
end | ||
|
||
@is_copying = false | ||
|
||
end | ||
|
||
def execute(sql) | ||
|
||
if sql.match(/^COPY /) and ! is_copying | ||
sql.chomp! # cHomp! cHomp! | ||
|
||
if jruby | ||
@stream = copy_manager.copy_in(sql) | ||
else | ||
conn.exec( sql ) | ||
end | ||
|
||
@is_copying = true | ||
|
||
elsif sql.match(/^TRUNCATE /) and ! is_copying | ||
|
||
$stderr.puts "===> ERR: TRUNCATE is not implemented!" | ||
|
||
elsif sql.match(/^ALTER /) and ! is_copying | ||
|
||
$stderr.puts "===> ERR: ALTER is not implemented!" | ||
|
||
elsif is_copying | ||
|
||
if sql.match(/^\\\./) | ||
|
||
@is_copying = false | ||
|
||
if jruby | ||
stream.end_copy | ||
else | ||
conn.put_copy_end | ||
end | ||
|
||
else | ||
|
||
if jruby | ||
begin | ||
row = sql.to_java_bytes | ||
stream.write_to_copy(row, 0, row.length) | ||
rescue Exception => e | ||
stream.cancel_copy | ||
raise e | ||
end | ||
else | ||
|
||
begin | ||
|
||
until conn.put_copy_data( sql ) | ||
$stderr.puts " waiting for connection to be writable..." | ||
sleep 0.1 | ||
end | ||
|
||
rescue Exception => e | ||
$stderr.puts e | ||
raise e | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
def raise_nil_connection | ||
raise "No Connection" | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.