1. Sean Davis
  2. mysql2postgres

Commits

Paul Gallagher  committed 518a692

refactor for tests to use dynamically generated config files

  • Participants
  • Parent commits 59bbea8
  • Branches master

Comments (0)

Files changed (23)

File lib/mysql2psql.rb

View file
 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'
   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

File lib/mysql2psql/config.rb

View file
       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

File lib/mysql2psql/converter.rb

View file
       @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

File lib/mysql2psql/mysql_reader.rb

View file
+require 'mysql'
+
+class Mysql2psql
+
+  class MysqlReader
+    class Field
+    end
+  
+    class Table
+      attr_reader :name
+    
+      def initialize(reader, name)
+        @reader = reader
+        @name = name
+      end
+    
+      @@types = %w(tiny enum decimal short long float double null timestamp longlong int24 date time datetime year set blob string var_string char).inject({}) do |list, type|
+        list[eval("::Mysql::Field::TYPE_#{type.upcase}")] = type
+        list
+      end
+    
+      @@types[246] = "decimal"
+    
+      def columns
+        @columns ||= load_columns
+      end
+    
+      def convert_type(type)
+        case type
+        when /int.* unsigned/
+          "bigint"
+        when /bigint/
+          "bigint"
+        when "bit(1)"
+          "boolean"
+        when "tinyint(1)"
+          "boolean"
+        when /tinyint/
+          "tinyint"
+        when /int/
+          "integer"
+        when /varchar/
+          "varchar"
+        when /char/
+          "char"
+        when /(float|decimal)/
+          "decimal"
+        when /double/
+           "double precision"
+        else
+          type
+        end 
+      end
+    
+      def load_columns
+        @reader.reconnect
+        result = @reader.mysql.list_fields(name)
+        mysql_flags = ::Mysql::Field.constants.select {|c| c =~ /FLAG/}
+        fields = []
+        @reader.mysql.query("EXPLAIN `#{name}`") do |res|
+          while field = res.fetch_row do
+            length = field[1][/\((\d+)\)/, 1] if field[1] =~ /\((\d+)\)/
+            length = field[1][/\((\d+),(\d+)\)/, 1] if field[1] =~ /\((\d+),(\d+)\)/
+            desc = {
+              :name => field[0],
+              :table_name => name,
+              :type => convert_type(field[1]),
+              :length => length && length.to_i,
+              :decimals => field[1][/\((\d+),(\d+)\)/, 2],
+              :null => field[2] == "YES",
+              :primary_key => field[3] == "PRI",
+              :auto_increment => field[5] == "auto_increment"
+              }
+            desc[:default] = field[4] unless field[4].nil?
+            fields << desc
+          end
+        end
+ 
+        fields.select {|field| field[:auto_increment]}.each do |field|
+          @reader.mysql.query("SELECT max(`#{field[:name]}`) FROM `#{name}`") do |res|
+            field[:maxval] = res.fetch_row[0].to_i
+          end
+        end
+        fields
+      end
+    
+    
+      def indexes
+        load_indexes unless @indexes
+        @indexes 
+      end
+ 
+      def foreign_keys
+        load_indexes unless @foreign_keys
+        @foreign_keys
+      end
+    
+      def load_indexes
+        @indexes = []
+        @foreign_keys = []
+      
+        @reader.mysql.query("SHOW CREATE TABLE `#{name}`") do |result|
+          explain = result.fetch_row[1]
+          explain.split(/\n/).each do |line|
+            next unless line =~ / KEY /
+            index = {}
+            if match_data = /CONSTRAINT `(\w+)` FOREIGN KEY \(`(\w+)`\) REFERENCES `(\w+)` \(`(\w+)`\)/.match(line)
+              index[:name] = match_data[1]
+              index[:column] = match_data[2]
+              index[:ref_table] = match_data[3]
+              index[:ref_column] = match_data[4]
+              @foreign_keys << index
+            elsif match_data = /KEY `(\w+)` \((.*)\)/.match(line)
+              index[:name] = match_data[1]
+              index[:columns] = match_data[2].split(",").map {|col| col[/`(\w+)`/, 1]}
+              index[:unique] = true if line =~ /UNIQUE/
+              @indexes << index
+            elsif match_data = /PRIMARY KEY .*\((.*)\)/.match(line)
+              index[:primary] = true
+              index[:columns] = match_data[1].split(",").map {|col| col.strip.gsub(/`/, "")}
+              @indexes << index
+            end
+          end
+        end
+      end
+    
+      def count_rows
+        @reader.mysql.query("SELECT COUNT(*) FROM `#{name}`")  do |res|
+          return res.fetch_row[0].to_i
+        end
+      end
+    
+      def has_id?
+        !!columns.find {|col| col[:name] == "id"} 
+      end
+    
+      def count_for_pager
+        query = has_id? ? 'MAX(id)' : 'COUNT(*)'
+        @reader.mysql.query("SELECT #{query} FROM `#{name}`") do |res|
+          return res.fetch_row[0].to_i
+        end
+      end
+ 
+      def query_for_pager
+        query = has_id? ? 'WHERE id >= ? AND id < ?' : 'LIMIT ?,?'
+        "SELECT #{columns.map{|c| "`"+c[:name]+"`"}.join(", ")} FROM `#{name}` #{query}"
+      end
+    end
+  
+    def connect
+      @mysql = ::Mysql.connect(@host, @user, @passwd, @db, @port, @sock, @flag)
+      @mysql.query("SET NAMES utf8")
+      @mysql.query("SET SESSION query_cache_type = OFF")
+    end
+  
+    def reconnect
+      @mysql.close rescue false
+      connect
+    end
+  
+    def initialize(options)
+      @host, @user, @passwd, @db, @port, @sock, @flag = 
+        options.mysqlhostname('localhost'), options.mysqlusername, 
+        options.mysqlpassword, options.mysqldatabase, 
+        options.mysqlport, options.mysqlsocket
+      connect
+    end
+  
+    attr_reader :mysql
+  
+    def tables
+      @tables ||= @mysql.list_tables.map {|table| Table.new(self, table)}
+    end
+  
+    def paginated_read(table, page_size)
+      count = table.count_for_pager
+      return if count < 1
+      statement = @mysql.prepare(table.query_for_pager)
+      counter = 0
+      0.upto((count + page_size)/page_size) do |i|
+        statement.execute(i*page_size, table.has_id? ? (i+1)*page_size : page_size)
+        while row = statement.fetch
+          counter += 1
+          yield(row, counter)
+        end
+      end
+      counter
+    end
+  end
+
+end

File lib/mysql2psql/mysqlreader.rb

-require 'mysql'
-
-class Mysql2psql
-
-  class MysqlReader
-    class Field
-    end
-  
-    class Table
-      attr_reader :name
-    
-      def initialize(reader, name)
-        @reader = reader
-        @name = name
-      end
-    
-      @@types = %w(tiny enum decimal short long float double null timestamp longlong int24 date time datetime year set blob string var_string char).inject({}) do |list, type|
-        list[eval("::Mysql::Field::TYPE_#{type.upcase}")] = type
-        list
-      end
-    
-      @@types[246] = "decimal"
-    
-      def columns
-        @columns ||= load_columns
-      end
-    
-      def convert_type(type)
-        case type
-        when /int.* unsigned/
-          "bigint"
-        when /bigint/
-          "bigint"
-        when "bit(1)"
-          "boolean"
-        when "tinyint(1)"
-          "boolean"
-        when /tinyint/
-          "tinyint"
-        when /int/
-          "integer"
-        when /varchar/
-          "varchar"
-        when /char/
-          "char"
-        when /(float|decimal)/
-          "decimal"
-        when /double/
-           "double precision"
-        else
-          type
-        end 
-      end
-    
-      def load_columns
-        @reader.reconnect
-        result = @reader.mysql.list_fields(name)
-        mysql_flags = ::Mysql::Field.constants.select {|c| c =~ /FLAG/}
-        fields = []
-        @reader.mysql.query("EXPLAIN `#{name}`") do |res|
-          while field = res.fetch_row do
-            length = field[1][/\((\d+)\)/, 1] if field[1] =~ /\((\d+)\)/
-            length = field[1][/\((\d+),(\d+)\)/, 1] if field[1] =~ /\((\d+),(\d+)\)/
-            desc = {
-              :name => field[0],
-              :table_name => name,
-              :type => convert_type(field[1]),
-              :length => length && length.to_i,
-              :decimals => field[1][/\((\d+),(\d+)\)/, 2],
-              :null => field[2] == "YES",
-              :primary_key => field[3] == "PRI",
-              :auto_increment => field[5] == "auto_increment"
-              }
-            desc[:default] = field[4] unless field[4].nil?
-            fields << desc
-          end
-        end
- 
-        fields.select {|field| field[:auto_increment]}.each do |field|
-          @reader.mysql.query("SELECT max(`#{field[:name]}`) FROM `#{name}`") do |res|
-            field[:maxval] = res.fetch_row[0].to_i
-          end
-        end
-        fields
-      end
-    
-    
-      def indexes
-        load_indexes unless @indexes
-        @indexes 
-      end
- 
-      def foreign_keys
-        load_indexes unless @foreign_keys
-        @foreign_keys
-      end
-    
-      def load_indexes
-        @indexes = []
-        @foreign_keys = []
-      
-        @reader.mysql.query("SHOW CREATE TABLE `#{name}`") do |result|
-          explain = result.fetch_row[1]
-          explain.split(/\n/).each do |line|
-            next unless line =~ / KEY /
-            index = {}
-            if match_data = /CONSTRAINT `(\w+)` FOREIGN KEY \(`(\w+)`\) REFERENCES `(\w+)` \(`(\w+)`\)/.match(line)
-              index[:name] = match_data[1]
-              index[:column] = match_data[2]
-              index[:ref_table] = match_data[3]
-              index[:ref_column] = match_data[4]
-              @foreign_keys << index
-            elsif match_data = /KEY `(\w+)` \((.*)\)/.match(line)
-              index[:name] = match_data[1]
-              index[:columns] = match_data[2].split(",").map {|col| col[/`(\w+)`/, 1]}
-              index[:unique] = true if line =~ /UNIQUE/
-              @indexes << index
-            elsif match_data = /PRIMARY KEY .*\((.*)\)/.match(line)
-              index[:primary] = true
-              index[:columns] = match_data[1].split(",").map {|col| col.strip.gsub(/`/, "")}
-              @indexes << index
-            end
-          end
-        end
-      end
-    
-      def count_rows
-        @reader.mysql.query("SELECT COUNT(*) FROM `#{name}`")  do |res|
-          return res.fetch_row[0].to_i
-        end
-      end
-    
-      def has_id?
-        !!columns.find {|col| col[:name] == "id"} 
-      end
-    
-      def count_for_pager
-        query = has_id? ? 'MAX(id)' : 'COUNT(*)'
-        @reader.mysql.query("SELECT #{query} FROM `#{name}`") do |res|
-          return res.fetch_row[0].to_i
-        end
-      end
- 
-      def query_for_pager
-        query = has_id? ? 'WHERE id >= ? AND id < ?' : 'LIMIT ?,?'
-        "SELECT #{columns.map{|c| "`"+c[:name]+"`"}.join(", ")} FROM `#{name}` #{query}"
-      end
-    end
-  
-    def connect
-      @mysql = ::Mysql.connect(@host, @user, @passwd, @db, @port, @sock, @flag)
-      @mysql.query("SET NAMES utf8")
-      @mysql.query("SET SESSION query_cache_type = OFF")
-    end
-  
-    def reconnect
-      @mysql.close rescue false
-      connect
-    end
-  
-    def initialize(options)
-      @host, @user, @passwd, @db, @port, @sock, @flag = 
-        options.mysqlhostname('localhost'), options.mysqlusername, 
-        options.mysqlpassword, options.mysqldatabase, 
-        options.mysqlport, options.mysqlsocket
-      connect
-    end
-  
-    attr_reader :mysql
-  
-    def tables
-      @tables ||= @mysql.list_tables.map {|table| Table.new(self, table)}
-    end
-  
-    def paginated_read(table, page_size)
-      count = table.count_for_pager
-      return if count < 1
-      statement = @mysql.prepare(table.query_for_pager)
-      counter = 0
-      0.upto((count + page_size)/page_size) do |i|
-        statement.execute(i*page_size, table.has_id? ? (i+1)*page_size : page_size)
-        while row = statement.fetch
-          counter += 1
-          yield(row, counter)
-        end
-      end
-      counter
-    end
-  end
-
-end

File lib/mysql2psql/postgres_db_writer.rb

View file
 require 'pg'
 
+require 'mysql2psql/postgres_writer'
+
 class Mysql2psql
 
 class PostgresDbWriter < PostgresWriter

File lib/mysql2psql/postgres_file_writer.rb

View file
+require 'mysql2psql/postgres_writer'
+
 class Mysql2psql
 
 class PostgresFileWriter < PostgresWriter

File lib/mysql2psql/postgres_writer.rb

View file
 require 'pg'
 
+require 'mysql2psql/writer'
+
 class Mysql2psql
 
   class PostgresWriter < Writer

File test/fixtures/config_localmysql_to_db_convert_all.yml

-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

File test/fixtures/config_localmysql_to_db_convert_nothing.yml

-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

File test/fixtures/config_localmysql_to_file_convert_all.yml

-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

File test/fixtures/config_localmysql_to_file_convert_nothing.yml

-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

File test/fixtures/config_localmysql_to_file_data_only.yml

-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

File test/fixtures/config_localmysql_to_file_schema_only.yml

-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

File test/integration/convert_to_db_test.rb

View file
 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

File test/integration/convert_to_file_test.rb

View file
 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

File test/integration/converter_test.rb

View file
 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

File test/integration/mysql_reader_base_test.rb

View file
+require 'test_helper'
+
+require 'mysql2psql/mysql_reader'
+
+class MysqlReaderBaseTest < Test::Unit::TestCase
+
+  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
+  end
+  def teardown
+  end
+  def options
+    @@options
+  end
+  
+  def test_mysql_connection
+    assert_nothing_raised do
+      reader = Mysql2psql::MysqlReader.new(options)
+    end
+  end
+  def test_mysql_reconnect
+    assert_nothing_raised do
+      reader = Mysql2psql::MysqlReader.new(options)
+      reader.reconnect
+    end
+  end
+end

File test/integration/mysql_reader_test.rb

View file
+require 'test_helper'
+
+class MysqlReaderTest < Test::Unit::TestCase
+
+  class << self
+    def startup
+      seed_test_database
+      @@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
+  end
+  def teardown
+  end
+  def reader
+    @@reader
+  end
+  
+  def test_db_connection
+    assert_nothing_raised do
+      reader.mysql.ping
+    end
+  end
+  def test_tables_collection
+    values = reader.tables.select{|t| t.name == 'numeric_types_basics'}
+    assert_true values.length==1
+    assert_equal 'numeric_types_basics', values[0].name
+  end
+  def test_paginated_read
+    expected_rows=3
+    page_size=2
+    expected_pages=(1.0 * expected_rows / page_size).ceil
+    
+    row_count=my_row_count=0
+    table = reader.tables.select{|t| t.name == 'numeric_types_basics'}[0]
+    reader.paginated_read(table, page_size) do |row,counter|
+      row_count=counter
+      my_row_count+=1
+    end
+    assert_equal expected_rows, row_count
+    assert_equal expected_rows, my_row_count
+  end
+end

File test/integration/mysqlreader_base_test.rb

-require 'test_helper'
-
-require 'mysql2psql'
-
-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' )
-    end
-    def shutdown
-    end
-  end
-  def setup
-  end
-  def teardown
-  end
-  def options
-    @@options
-  end
-  
-  def test_mysql_connection
-    assert_nothing_raised do
-      reader = Mysql2psql::MysqlReader.new(options)
-    end
-  end
-  def test_mysql_reconnect
-    assert_nothing_raised do
-      reader = Mysql2psql::MysqlReader.new(options)
-      reader.reconnect
-    end
-  end
-end

File test/integration/mysqlreader_test.rb

-require 'test_helper'
-
-require 'mysql2psql'
-
-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'))
-    end
-    def shutdown
-    end
-  end
-  def setup
-  end
-  def teardown
-  end
-  def reader
-    @@reader
-  end
-  
-  def test_db_connection
-    assert_nothing_raised do
-      reader.mysql.ping
-    end
-  end
-  def test_tables_collection
-    values = reader.tables.select{|t| t.name == 'numeric_types_basics'}
-    assert_true values.length==1
-    assert_equal 'numeric_types_basics', values[0].name
-  end
-  def test_paginated_read
-    expected_rows=3
-    page_size=2
-    expected_pages=(1.0 * expected_rows / page_size).ceil
-    
-    row_count=my_row_count=0
-    table = reader.tables.select{|t| t.name == 'numeric_types_basics'}[0]
-    reader.paginated_read(table, page_size) do |row,counter|
-      row_count=counter
-      my_row_count+=1
-    end
-    assert_equal expected_rows, row_count
-    assert_equal expected_rows, my_row_count
-  end
-end

File test/integration/postgres_db_writer_base_test.rb

View file
 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

File test/lib/test_helper.rb

View file
 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)
   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