Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor for tests to use dynamically generated config files

  • Loading branch information...
commit 518a692f4485fe924aaad4d9e9185e57e8b0fe3c 1 parent 59bbea8
@tardate tardate authored
View
4 lib/mysql2psql.rb
@@ -2,7 +2,7 @@
require 'mysql2psql/version'
require 'mysql2psql/config'
require 'mysql2psql/converter'
-require 'mysql2psql/mysqlreader'
+require 'mysql2psql/mysql_reader'
require 'mysql2psql/writer'
require 'mysql2psql/postgres_writer'
require 'mysql2psql/postgres_db_writer.rb'
@@ -14,7 +14,7 @@ class Mysql2psql
attr_reader :options, :reader, :writer
def initialize(args)
- help if args.length==1 && args[0] =~ /-.?|-*he?l?p?/i
+ help if args.length==1 && args[0] =~ /^-.?|^-*he?l?p?$/i
configfile = args[0] || File.expand_path('mysql2psql.yml')
@options = Config.new( configfile, true )
end
View
61 lib/mysql2psql/config.rb
@@ -26,50 +26,75 @@ def reset_configfile(filepath)
end
file.close
end
-
- def self.template
- return <<EOS
+
+ def self.template(to_filename = nil, include_tables = [], exclude_tables = [], supress_data = false, supress_ddl = false, force_truncate = false)
+ configtext = <<EOS
mysql:
hostname: localhost
port: 3306
socket: /tmp/mysql.sock
- username: somename
- password: secretpassword
- database: somename
+ username: mysql2psql
+ password:
+ database: mysql2psql_test
destination:
# if file is given, output goes to file, else postgres
- file:
+ file: #{ to_filename ? to_filename : ''}
postgres:
hostname: localhost
port: 5432
- username: somename
- password: secretpassword
- database: somename
+ username: mysql2psql
+ password:
+ database: mysql2psql_test
# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
#tables:
#- table1
#- table2
+EOS
+ if include_tables.length>0
+ configtext += "\ntables:\n"
+ include_tables.each do |t|
+ configtext += "- #{t}\n"
+ end
+ end
+ configtext += <<EOS
+# if exclude_tables is given, exclude the listed tables from the conversion.
+#exclude_tables:
#- table3
#- table4
-# if exclude_tables is given, exclude the listed tables from the conversion.
-#exclude_tables:
-#- table5
-#- table6
+EOS
+ if exclude_tables.length>0
+ configtext += "\nexclude_tables:\n"
+ exclude_tables.each do |t|
+ configtext += "- #{t}\n"
+ end
+ end
+ if !supress_data.nil?
+ configtext += <<EOS
# if supress_data is true, only the schema definition will be exported/migrated, and not the data
-#supress_data: true
+supress_data: #{supress_data}
+EOS
+ end
+ if !supress_ddl.nil?
+ configtext += <<EOS
# if supress_ddl is true, only the data will be exported/imported, and not the schema
-#supress_ddl: false
+supress_ddl: #{supress_ddl}
+EOS
+ end
+ if !force_truncate.nil?
+ configtext += <<EOS
# if force_truncate is true, forces a table truncate before table loading
-#force_truncate: false
-
+force_truncate: #{force_truncate}
EOS
+ end
+ configtext
end
+
end
end
View
6 lib/mysql2psql/converter.rb
@@ -10,9 +10,9 @@ def initialize(reader, writer, options)
@options = options
@exclude_tables = options.exclude_tables([])
@only_tables = options.only_tables(nil)
- @supress_data = options.supress_data
- @supress_ddl = options.supress_ddl
- @force_truncate = options.force_truncate
+ @supress_data = options.supress_data(false)
+ @supress_ddl = options.supress_ddl(false)
+ @force_truncate = options.force_truncate(false)
end
def convert
View
0  lib/mysql2psql/mysqlreader.rb → lib/mysql2psql/mysql_reader.rb
File renamed without changes
View
2  lib/mysql2psql/postgres_db_writer.rb
@@ -1,5 +1,7 @@
require 'pg'
+require 'mysql2psql/postgres_writer'
+
class Mysql2psql
class PostgresDbWriter < PostgresWriter
View
2  lib/mysql2psql/postgres_file_writer.rb
@@ -1,3 +1,5 @@
+require 'mysql2psql/postgres_writer'
+
class Mysql2psql
class PostgresFileWriter < PostgresWriter
View
2  lib/mysql2psql/postgres_writer.rb
@@ -1,5 +1,7 @@
require 'pg'
+require 'mysql2psql/writer'
+
class Mysql2psql
class PostgresWriter < Writer
View
35 test/fixtures/config_localmysql_to_db_convert_all.yml
@@ -1,35 +0,0 @@
-mysql:
- hostname: localhost
- port: 3306
- socket: /tmp/mysql.sock
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-destination:
- # if file is given, output goes to file, else postgres
- file:
- postgres:
- hostname: localhost
- port: 5432
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
-#tables:
-#- notexist
-
-# if exclude_tables is given, exclude the listed tables from the conversion.
-#exclude_tables:
-#- table5
-#- table6
-
-# if supress_data is true, only the schema definition will be exported/migrated, and not the data
-supress_data: false
-
-# if supress_ddl is true, only the data will be exported/imported, and not the schema
-supress_ddl: false
-
-# if force_truncate is true, forces a table truncate before table loading
-#force_truncate: true
View
35 test/fixtures/config_localmysql_to_db_convert_nothing.yml
@@ -1,35 +0,0 @@
-mysql:
- hostname: localhost
- port: 3306
- socket: /tmp/mysql.sock
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-destination:
- # if file is given, output goes to file, else postgres
- file:
- postgres:
- hostname: localhost
- port: 5432
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
-tables:
-- notexist
-
-# if exclude_tables is given, exclude the listed tables from the conversion.
-exclude_tables:
-- table5
-- table6
-
-# if supress_data is true, only the schema definition will be exported/migrated, and not the data
-supress_data: true
-
-# if supress_ddl is true, only the data will be exported/imported, and not the schema
-supress_ddl: true
-
-# if force_truncate is true, forces a table truncate before table loading
-force_truncate: true
View
35 test/fixtures/config_localmysql_to_file_convert_all.yml
@@ -1,35 +0,0 @@
-mysql:
- hostname: localhost
- port: 3306
- socket: /tmp/mysql.sock
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-destination:
- # if file is given, output goes to file, else postgres
- file: test/fixtures/test_output_convert_all.sql
- postgres:
- hostname: localhost
- port: 5432
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
-#tables:
-#- notexist
-
-# if exclude_tables is given, exclude the listed tables from the conversion.
-#exclude_tables:
-#- table5
-#- table6
-
-# if supress_data is true, only the schema definition will be exported/migrated, and not the data
-supress_data: false
-
-# if supress_ddl is true, only the data will be exported/imported, and not the schema
-supress_ddl: false
-
-# if force_truncate is true, forces a table truncate before table loading
-#force_truncate: true
View
35 test/fixtures/config_localmysql_to_file_convert_nothing.yml
@@ -1,35 +0,0 @@
-mysql:
- hostname: localhost
- port: 3306
- socket: /tmp/mysql.sock
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-destination:
- # if file is given, output goes to file, else postgres
- file: test/fixtures/test_output_convert_nothing.sql
- postgres:
- hostname: localhost
- port: 5432
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
-tables:
-- notexist
-
-# if exclude_tables is given, exclude the listed tables from the conversion.
-exclude_tables:
-- table5
-- table6
-
-# if supress_data is true, only the schema definition will be exported/migrated, and not the data
-supress_data: true
-
-# if supress_ddl is true, only the data will be exported/imported, and not the schema
-supress_ddl: true
-
-# if force_truncate is true, forces a table truncate before table loading
-force_truncate: true
View
35 test/fixtures/config_localmysql_to_file_data_only.yml
@@ -1,35 +0,0 @@
-mysql:
- hostname: localhost
- port: 3306
- socket: /tmp/mysql.sock
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-destination:
- # if file is given, output goes to file, else postgres
- file: test/fixtures/test_output_convert_all.sql
- postgres:
- hostname: localhost
- port: 5432
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
-#tables:
-#- notexist
-
-# if exclude_tables is given, exclude the listed tables from the conversion.
-#exclude_tables:
-#- table5
-#- table6
-
-# if supress_data is true, only the schema definition will be exported/migrated, and not the data
-supress_data: false
-
-# if supress_ddl is true, only the data will be exported/imported, and not the schema
-supress_ddl: true
-
-# if force_truncate is true, forces a table truncate before table loading
-force_truncate: true
View
35 test/fixtures/config_localmysql_to_file_schema_only.yml
@@ -1,35 +0,0 @@
-mysql:
- hostname: localhost
- port: 3306
- socket: /tmp/mysql.sock
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-destination:
- # if file is given, output goes to file, else postgres
- file: test/fixtures/test_output_convert_all.sql
- postgres:
- hostname: localhost
- port: 5432
- username: mysql2psql
- password:
- database: mysql2psql_test
-
-# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
-#tables:
-#- notexist
-
-# if exclude_tables is given, exclude the listed tables from the conversion.
-#exclude_tables:
-#- table5
-#- table6
-
-# if supress_data is true, only the schema definition will be exported/migrated, and not the data
-supress_data: true
-
-# if supress_ddl is true, only the data will be exported/imported, and not the schema
-supress_ddl: false
-
-# if force_truncate is true, forces a table truncate before table loading
-force_truncate: false
View
6 test/integration/convert_to_db_test.rb
@@ -3,16 +3,18 @@
require 'mysql2psql'
class ConvertToDbTest < Test::Unit::TestCase
+
class << self
def startup
- configfile = "#{File.dirname(__FILE__)}/../fixtures/config_localmysql_to_db_convert_all.yml"
seed_test_database
- @@mysql2psql = Mysql2psql.new([configfile])
+ @@options=get_test_config_by_label(:localmysql_to_db_convert_all)
+ @@mysql2psql = Mysql2psql.new([@@options.filepath])
@@mysql2psql.convert
@@mysql2psql.writer.open
end
def shutdown
@@mysql2psql.writer.close
+ delete_files_for_test_config(@@options)
end
end
def setup
View
8 test/integration/convert_to_file_test.rb
@@ -3,17 +3,17 @@
require 'mysql2psql'
class ConvertToFileTest < Test::Unit::TestCase
-
+
class << self
def startup
- configfile = "#{File.dirname(__FILE__)}/../fixtures/config_localmysql_to_file_convert_all.yml"
seed_test_database
- @@mysql2psql = Mysql2psql.new([configfile])
+ @@options=get_test_config_by_label(:localmysql_to_file_convert_all)
+ @@mysql2psql = Mysql2psql.new([@@options.filepath])
@@mysql2psql.convert
@@content = IO.read(@@mysql2psql.options.destfile)
end
def shutdown
- File.delete(@@mysql2psql.options.destfile) if File.exists?(@@mysql2psql.options.destfile)
+ delete_files_for_test_config(@@options)
end
end
def setup
View
17 test/integration/converter_test.rb
@@ -3,13 +3,22 @@
require 'mysql2psql/converter'
class ConverterTest < Test::Unit::TestCase
- attr_accessor :options
+
+ class << self
+ def startup
+ seed_test_database
+ @@options=get_test_config_by_label(:localmysql_to_file_convert_nothing)
+ end
+ def shutdown
+ delete_files_for_test_config(@@options)
+ end
+ end
def setup
- seed_test_database
- @options = get_test_config( 'config_localmysql_to_file_convert_nothing.yml' )
end
def teardown
- File.delete(options.destfile) if File.exists?(options.destfile)
+ end
+ def options
+ @@options
end
def test_new_converter
View
9 test/integration/mysqlreader_base_test.rb → test/integration/mysql_reader_base_test.rb
@@ -1,15 +1,16 @@
require 'test_helper'
-require 'mysql2psql'
+require 'mysql2psql/mysql_reader'
+
+class MysqlReaderBaseTest < Test::Unit::TestCase
-class MysqlreaderBaseTest < Test::Unit::TestCase
- attr_accessor :options
class << self
def startup
seed_test_database
- @@options = get_test_config( 'config_localmysql_to_file_convert_nothing.yml' )
+ @@options = get_test_config_by_label(:localmysql_to_file_convert_nothing)
end
def shutdown
+ delete_files_for_test_config(@@options)
end
end
def setup
View
8 test/integration/mysqlreader_test.rb → test/integration/mysql_reader_test.rb
@@ -1,15 +1,15 @@
require 'test_helper'
-require 'mysql2psql'
+class MysqlReaderTest < Test::Unit::TestCase
-class MysqlreaderTest < Test::Unit::TestCase
- attr_accessor :reader
class << self
def startup
seed_test_database
- @@reader=get_test_reader(get_test_config('config_localmysql_to_file_convert_nothing.yml'))
+ @@options = get_test_config_by_label(:localmysql_to_file_convert_nothing)
+ @@reader=get_test_reader(@@options)
end
def shutdown
+ delete_files_for_test_config(@@options)
end
end
def setup
View
5 test/integration/postgres_db_writer_base_test.rb
@@ -3,13 +3,14 @@
require 'mysql2psql/postgres_db_writer'
class PostgresDbWriterBaseTest < Test::Unit::TestCase
- attr_accessor :options
+
class << self
def startup
seed_test_database
- @@options = get_test_config( 'config_localmysql_to_db_convert_nothing.yml' )
+ @@options=get_test_config_by_label(:localmysql_to_db_convert_nothing)
end
def shutdown
+ delete_files_for_test_config(@@options)
end
end
def setup
View
50 test/lib/test_helper.rb
@@ -10,35 +10,34 @@
require 'ext_test_unit'
def seed_test_database
- require 'mysql2psql/config'
- options=get_test_config('config_localmysql_to_file_convert_nothing.yml')
+ options=get_test_config_by_label(:localmysql_to_file_convert_nothing)
seedfilepath = "#{File.dirname(__FILE__)}/../fixtures/seed_integration_tests.sql"
rc=system("mysql -u#{options.mysqlusername} #{options.mysqldatabase} < #{seedfilepath}")
raise StandardError unless rc
return true
rescue
raise StandardError.new("Failed to seed integration test db. See README for setup requirements.")
-end
-
-def get_test_config(configfile)
- Mysql2psql::ConfigBase.new( "#{File.dirname(__FILE__)}/../fixtures/#{configfile}" )
-rescue
- raise StandardError.new("Failed to initialize options from #{configfile}. See README for setup requirements.")
+ensure
+ delete_files_for_test_config(options)
end
def get_test_reader(options)
+ require 'mysql2psql/mysql_reader'
Mysql2psql::MysqlReader.new(options)
rescue
raise StandardError.new("Failed to initialize integration test db. See README for setup requirements.")
end
def get_test_file_writer(options)
+ require 'mysql2psql/postgres_file_writer'
Mysql2psql::PostgresFileWriter.new(options.destfile)
-rescue
+rescue => e
+ puts e.inspect
raise StandardError.new("Failed to initialize file writer from #{options.inspect}. See README for setup requirements.")
end
def get_test_converter(options)
+ require 'mysql2psql/converter'
reader=get_test_reader(options)
writer=get_test_file_writer(options)
Mysql2psql::Converter.new(reader,writer,options)
@@ -54,3 +53,36 @@ def get_temp_file(basename)
path
end
+
+def get_new_test_config(to_file = true, include_tables = [], exclude_tables = [], supress_data = false, supress_ddl = false, force_truncate = false)
+ require 'mysql2psql/config'
+ require 'mysql2psql/config_base'
+ to_filename = to_file ? get_temp_file('mysql2psql_tmp_output') : nil
+ configtext = Mysql2psql::Config.template(to_filename, include_tables, exclude_tables, supress_data, supress_ddl, force_truncate)
+ configfile=get_temp_file('mysql2psql_tmp_config')
+ File.open(configfile, 'w') {|f| f.write(configtext) }
+ Mysql2psql::ConfigBase.new( configfile )
+rescue
+ raise StandardError.new("Failed to initialize options from #{configfile}. See README for setup requirements.")
+end
+
+def get_test_config_by_label(name)
+ case name
+ when :localmysql_to_file_convert_nothing
+ get_new_test_config(true, ['unobtainium'], ['kryptonite'], true, true, false)
+ when :localmysql_to_file_convert_all
+ get_new_test_config(true, [], [], false, false, false)
+ when :localmysql_to_db_convert_all
+ get_new_test_config(false, [], [], false, false, false)
+ when :localmysql_to_db_convert_nothing
+ get_new_test_config(false, ['unobtainium'], ['kryptonite'], true, true, false)
+ else
+ raise StandardError.new("Invalid label: #{name}")
+ end
+end
+
+def delete_files_for_test_config(config)
+ File.delete(config.destfile) if File.exists?(config.destfile)
+ File.delete(config.filepath) if File.exists?(config.filepath)
+rescue
+end
Please sign in to comment.
Something went wrong with that request. Please try again.