benhutton-mysql2psql 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +19 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +34 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +148 -0
- data/Rakefile +95 -0
- data/bin/mysql2psql +7 -0
- data/lib/mysql2psql/config.rb +142 -0
- data/lib/mysql2psql/config_base.rb +39 -0
- data/lib/mysql2psql/converter.rb +62 -0
- data/lib/mysql2psql/errors.rb +16 -0
- data/lib/mysql2psql/mysql_reader.rb +230 -0
- data/lib/mysql2psql/postgres_db_writer.rb +201 -0
- data/lib/mysql2psql/postgres_file_writer.rb +152 -0
- data/lib/mysql2psql/postgres_writer.rb +168 -0
- data/lib/mysql2psql/version.rb +9 -0
- data/lib/mysql2psql/writer.rb +6 -0
- data/lib/mysql2psql.rb +41 -0
- data/mysql2psql.gemspec +106 -0
- data/test/fixtures/config_all_options.yml +50 -0
- data/test/fixtures/seed_integration_tests.sql +119 -0
- data/test/integration/convert_to_db_test.rb +137 -0
- data/test/integration/convert_to_file_test.rb +121 -0
- data/test/integration/converter_test.rb +28 -0
- data/test/integration/mysql_reader_base_test.rb +34 -0
- data/test/integration/mysql_reader_test.rb +41 -0
- data/test/integration/postgres_db_writer_base_test.rb +22 -0
- data/test/lib/ext_test_unit.rb +33 -0
- data/test/lib/test_helper.rb +131 -0
- data/test/units/config_base_test.rb +49 -0
- data/test/units/config_test.rb +73 -0
- data/test/units/postgres_file_writer_test.rb +29 -0
- metadata +220 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f28d90b5a9100a93f9739bdaeba79548d7d18720
|
4
|
+
data.tar.gz: 0dce5eeec6734e5f70d7b1e84715c20ed28eaac2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4bae782d1fb37f864de841670b9fd905737cef6f2df276322156276bee521e72991b783c0c85ba9adebe2091f5f58fc6e7595312a129aba6ebf7727e8f991466
|
7
|
+
data.tar.gz: b38082d08dcdc7af7f740ec792cb33f0364d95be5ebc5872c973f0ac096805c7659dcae364700790a4a79b8bb0b1728c496c1fbd086a67108828bb2de669d404
|
data/CHANGELOG
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
0.2.2 Feb 13, 2012
|
2
|
+
===
|
3
|
+
|
4
|
+
* merge contributions from: James Coleman, Aaron Peckham, James Tippett
|
5
|
+
* breaking change: note that "supress*" configuration parameters now use correct spelling. Existing config files you may have will need housekeeping.
|
6
|
+
* new configuration options: suppress_sequence_update; suppress_indexes; use_timezones
|
7
|
+
* adds a Gemfile to help setup development environment
|
8
|
+
|
9
|
+
0.1.1 May 3, 2011
|
10
|
+
===
|
11
|
+
|
12
|
+
* updated for pg driver ~> 0.11.0
|
13
|
+
* add a check for mysql cmd, e.g. mysql or mysql5
|
14
|
+
* remove deprecated feature per gem -v 1.7.2
|
15
|
+
|
16
|
+
0.1.0 September 18, 2010
|
17
|
+
===
|
18
|
+
|
19
|
+
* Initial packaging as a gem
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mysql2psql (0.2.0)
|
5
|
+
mysql (= 2.8.1)
|
6
|
+
mysql2psql
|
7
|
+
pg (~> 0.11.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
git (1.2.5)
|
13
|
+
jeweler (1.5.2)
|
14
|
+
bundler (~> 1.0.0)
|
15
|
+
git (>= 1.2.5)
|
16
|
+
rake
|
17
|
+
json (1.6.5)
|
18
|
+
mysql (2.8.1)
|
19
|
+
pg (0.11.0)
|
20
|
+
rake (0.9.2.2)
|
21
|
+
rdoc (3.12)
|
22
|
+
json (~> 1.4)
|
23
|
+
test-unit (2.4.7)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.0.21)
|
30
|
+
jeweler (~> 1.5.2)
|
31
|
+
mysql2psql!
|
32
|
+
rake (~> 0.9.2.2)
|
33
|
+
rdoc (~> 3.12)
|
34
|
+
test-unit (>= 2.1.1)
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2010 name <email>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
= MYSQL2PSQL - MySQL to PostgreSQL Migration Tool
|
2
|
+
|
3
|
+
Tool for converting mysql database to postgresql.
|
4
|
+
It can create postgresql dump from mysql database or directly load data from mysql to
|
5
|
+
postgresql (at about 100 000 records per minute).
|
6
|
+
|
7
|
+
It can translate now most data types and indexes, but if you experience some problems, feel free
|
8
|
+
to contact me, I'll help you.
|
9
|
+
|
10
|
+
== Installation
|
11
|
+
Mysql2psql is packaged as a gem. Install as usual (use sudo if required).
|
12
|
+
|
13
|
+
$ gem install mysql2psql
|
14
|
+
|
15
|
+
After installation, the "mysql2psql" command will be available.
|
16
|
+
When run, it will generate a default configuration file in the current directory if a config
|
17
|
+
file is not already found. The default configuration filename is mysql2psql.yml.
|
18
|
+
|
19
|
+
$ mysql2psql
|
20
|
+
|
21
|
+
You can use an alternate config file by passing the filename/path as a parameter:
|
22
|
+
|
23
|
+
$ mysql2psql ../another/world.yml
|
24
|
+
|
25
|
+
== Running Migrations
|
26
|
+
See the configuration file for documentation about settings. Key choices include:
|
27
|
+
* whether to dump to a file, or migrate direct to PostgreSQL
|
28
|
+
* migrate only the schema definition, only the data, or both schema and data
|
29
|
+
|
30
|
+
|
31
|
+
After editing the configuration file and launching tool, you will see something like..
|
32
|
+
|
33
|
+
Creating table friendships...
|
34
|
+
Created table friendships
|
35
|
+
Loading friendships...
|
36
|
+
620000 of 638779 rows loaded. [ETA: 2010/01/21 21:32 (00h:00m:01s)]
|
37
|
+
638779 rows loaded in 1min 3s
|
38
|
+
Indexing table friendships...
|
39
|
+
Indexed table friendships
|
40
|
+
Table creation 0 min, loading 1 min, indexing 0 min, total 1 min
|
41
|
+
|
42
|
+
|
43
|
+
== Database driver dependencies
|
44
|
+
Mysql2psql uses the 'mysql' and 'pg' gems for database connectivity.
|
45
|
+
Mysql2psql gem installation should automatically install these too.
|
46
|
+
|
47
|
+
If you encounter any issues with db connectivity, verify that the 'mysql' and 'pg' gems are installed and working correctly first.
|
48
|
+
The 'mysql' gem in particular may need a hint on where to find the mysql headers and libraries:
|
49
|
+
|
50
|
+
$ [sudo] gem install mysql -- --with-mysql-dir=/usr/local/mysql
|
51
|
+
|
52
|
+
NB: With Ruby 1.8.x, the gem install will usually complain about "No definition for ..." errors. These are non-fatal and just affect the documentation install. This doesn't happen with Ruby 1.9.2.
|
53
|
+
|
54
|
+
== Contributing to Mysql2psql
|
55
|
+
|
56
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
57
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
58
|
+
* Fork the project
|
59
|
+
* Start a feature/bugfix branch
|
60
|
+
* Commit and push until you are happy with your contribution
|
61
|
+
* Make sure to add tests for it. This is important so no-one unintentionally breaks it in a future version.
|
62
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so the gem maintainers can cherry-pick around it.
|
63
|
+
* Send a pull request
|
64
|
+
|
65
|
+
== Getting the source
|
66
|
+
Mysql2psql was first produced by Max Lapshin <max@maxidoors.ru> who maintains the master
|
67
|
+
repository at https://github.com/maxlapshin/mysql2postgres
|
68
|
+
|
69
|
+
The gem bundling and refactoring for testing was done by Paul Gallagher <gallagher.paul@gmail.com> and
|
70
|
+
the repository is at https://github.com/tardate/mysql2postgres
|
71
|
+
|
72
|
+
Gem packaging is done with Jeweler. Note that on windows this means you must run under mingw or other shell that supports git to do the gem bundling (but you can gem install the built gem in the normal Windows shell).
|
73
|
+
|
74
|
+
== Setting up for development
|
75
|
+
|
76
|
+
The project includes a Gemfile so with bundler you can install gem dependencies with:
|
77
|
+
|
78
|
+
gem install bundler # if not already installed
|
79
|
+
bundle install
|
80
|
+
|
81
|
+
Note comments 'Database driver dependencies' regarding issues that you may encounter with the mysql gem.
|
82
|
+
|
83
|
+
== Running tests
|
84
|
+
If you fork/clone the project, you will want to run the test suite (and add to it if you are developing new features or fixing bugs).
|
85
|
+
|
86
|
+
A suite of tests are provided and are run using rake (rake -T for more info)
|
87
|
+
rake test:units
|
88
|
+
rake test:integration
|
89
|
+
rake test
|
90
|
+
|
91
|
+
Rake without parameters (or "rake test") will run both the unit and integration tests.
|
92
|
+
|
93
|
+
Unit tests are small standalone tests of the mysql2psql codebase that have no external dependencies (like a database)
|
94
|
+
|
95
|
+
Integration tests require suitable mysql and postgres databases to be setup in advance.
|
96
|
+
Running the integration tests *will* rewrite data and schema for the "mysql2psql_test" databases in mysql and postgres. Don't use this database for anything else!
|
97
|
+
|
98
|
+
Setting up mysql on localhost:3306
|
99
|
+
- create a database called "mysql2psql_test"
|
100
|
+
- setup a user "mysql2psql" with no password
|
101
|
+
|
102
|
+
e.g.
|
103
|
+
|
104
|
+
mysqladmin -uroot -p create mysql2psql_test
|
105
|
+
mysql -uroot -p -e "grant all on mysql2psql_test.* to 'mysql2psql'@'localhost';"
|
106
|
+
# verify connection:
|
107
|
+
mysql -umysql2psql -e "select database(),'yes' as connected" mysql2psql_test
|
108
|
+
|
109
|
+
|
110
|
+
Setting up PostgreSQL on localhost:5432
|
111
|
+
- create a database called "mysql2psql_test"
|
112
|
+
- setup a role (user) "mysql2psql" with no password
|
113
|
+
|
114
|
+
e.g.
|
115
|
+
|
116
|
+
psql postgres -c "create role mysql2psql with login"
|
117
|
+
psql postgres -c "create database mysql2psql_test with owner mysql2psql encoding='UTF8'"
|
118
|
+
# verify connection:
|
119
|
+
psql mysql2psql_test -U mysql2psql -c "\c"
|
120
|
+
|
121
|
+
|
122
|
+
== Notes, Limitations, Outstanding Issues..
|
123
|
+
|
124
|
+
* more test coverage, as always...
|
125
|
+
|
126
|
+
|
127
|
+
== Contributors
|
128
|
+
Project founded by Max Lapshin <max@maxidoors.ru>
|
129
|
+
|
130
|
+
Contributors (roughly git log order):
|
131
|
+
- Anton Ageev <anton@ageev.name>
|
132
|
+
- Samuel Tribehou <cracoucax@gmail.com>
|
133
|
+
- Marco Nenciarini <marco.nenciarini@devise.it>
|
134
|
+
- James Nobis <jnobis@jnobis.controldocs.com>
|
135
|
+
- quel <github@quelrod.net>
|
136
|
+
- Holger Amann <keeney@fehu.org>
|
137
|
+
- Maxim Dobriakov <closer.main@gmail.com>
|
138
|
+
- Michael Kimsal <mgkimsal@gmail.com>
|
139
|
+
- Jacob Coby <jcoby@portallabs.com>
|
140
|
+
- Neszt Tibor <neszt@tvnetwork.hu>
|
141
|
+
- Miroslav Kratochvil <exa.exa@gmail.com>
|
142
|
+
- {Paul Gallagher}[https://github.com/tardate] <gallagher.paul@gmail.com>
|
143
|
+
- {James Coleman}[https://github.com/jcoleman] <jtc331@gmail.com>
|
144
|
+
- {Aaron Peckham}[https://github.com/apeckham]
|
145
|
+
- {James Tippett}[https://github.com/jtippett]
|
146
|
+
- {Tim Morgan}[https://github.com/seven1m]
|
147
|
+
- {dakhota}[https://github.com/dakhota]
|
148
|
+
- {Matthew Soldo}[https://github.com/mattsoldo]
|
data/Rakefile
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift('lib')
|
5
|
+
require 'mysql2psql/version'
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'jeweler'
|
9
|
+
Jeweler::Tasks.new do |gem|
|
10
|
+
gem.name = "mysql2psql"
|
11
|
+
gem.version = Mysql2psql::Version::STRING
|
12
|
+
gem.summary = %Q{Tool for converting mysql database to postgresql}
|
13
|
+
gem.description = %Q{It can create postgresql dump from mysql database or directly load data from mysql to
|
14
|
+
postgresql (at about 100 000 records per minute). Translates most data types and indexes.}
|
15
|
+
gem.email = "gallagher.paul@gmail.com"
|
16
|
+
gem.homepage = "https://github.com/tardate/mysql2postgres"
|
17
|
+
gem.authors = [
|
18
|
+
"Max Lapshin <max@maxidoors.ru>",
|
19
|
+
"Anton Ageev <anton@ageev.name>",
|
20
|
+
"Samuel Tribehou <cracoucax@gmail.com>",
|
21
|
+
"Marco Nenciarini <marco.nenciarini@devise.it>",
|
22
|
+
"James Nobis <jnobis@jnobis.controldocs.com>",
|
23
|
+
"quel <github@quelrod.net>",
|
24
|
+
"Holger Amann <keeney@fehu.org>",
|
25
|
+
"Maxim Dobriakov <closer.main@gmail.com>",
|
26
|
+
"Michael Kimsal <mgkimsal@gmail.com>",
|
27
|
+
"Jacob Coby <jcoby@portallabs.com>",
|
28
|
+
"Neszt Tibor <neszt@tvnetwork.hu>",
|
29
|
+
"Miroslav Kratochvil <exa.exa@gmail.com>",
|
30
|
+
"Paul Gallagher <gallagher.paul@gmail.com>",
|
31
|
+
"James Coleman <jtc331@gmail.com>",
|
32
|
+
"Aaron Peckham",
|
33
|
+
"James Tippett",
|
34
|
+
"Tim Morgan",
|
35
|
+
"dakhota",
|
36
|
+
"Matthew Soldo"
|
37
|
+
]
|
38
|
+
gem.add_dependency "mysql", "= 2.8.1"
|
39
|
+
gem.add_dependency "pg", "~> 0.11.0"
|
40
|
+
gem.add_development_dependency "test-unit", ">= 2.1.1"
|
41
|
+
gem.add_development_dependency "rake", "~> 0.9.2.2"
|
42
|
+
gem.add_development_dependency "rdoc", "~> 3.12"
|
43
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
44
|
+
end
|
45
|
+
Jeweler::GemcutterTasks.new
|
46
|
+
rescue LoadError
|
47
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
48
|
+
end
|
49
|
+
|
50
|
+
require 'rake/testtask'
|
51
|
+
namespace :test do
|
52
|
+
Rake::TestTask.new(:units) do |test|
|
53
|
+
test.libs << 'lib' << 'test/lib'
|
54
|
+
test.pattern = 'test/units/*test.rb'
|
55
|
+
test.verbose = true
|
56
|
+
end
|
57
|
+
|
58
|
+
Rake::TestTask.new(:integration) do |test|
|
59
|
+
test.libs << 'lib' << 'test/lib'
|
60
|
+
test.pattern = 'test/integration/*test.rb'
|
61
|
+
test.verbose = true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
desc "Run all tests"
|
66
|
+
task :test do
|
67
|
+
Rake::Task['test:units'].invoke
|
68
|
+
Rake::Task['test:integration'].invoke
|
69
|
+
end
|
70
|
+
|
71
|
+
begin
|
72
|
+
require 'rcov/rcovtask'
|
73
|
+
Rcov::RcovTask.new do |test|
|
74
|
+
test.libs << 'lib'
|
75
|
+
test.libs << 'test/lib'
|
76
|
+
test.pattern = 'test/**/*test.rb'
|
77
|
+
test.verbose = true
|
78
|
+
test.rcov_opts << "--exclude gems/*"
|
79
|
+
end
|
80
|
+
rescue LoadError
|
81
|
+
task :rcov do
|
82
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
task :default => :test
|
87
|
+
|
88
|
+
require 'rdoc/task'
|
89
|
+
RDoc::Task.new do |rdoc|
|
90
|
+
version = Mysql2psql::Version::STRING
|
91
|
+
|
92
|
+
rdoc.rdoc_dir = 'rdoc'
|
93
|
+
rdoc.title = "mysql2psql #{version}"
|
94
|
+
rdoc.rdoc_files.include("README*", "lib/**/*.rb")
|
95
|
+
end
|
data/bin/mysql2psql
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'mysql2psql/config_base'
|
2
|
+
|
3
|
+
class Mysql2psql
|
4
|
+
|
5
|
+
class Config < ConfigBase
|
6
|
+
|
7
|
+
def initialize(configfilepath, generate_default_if_not_found = true)
|
8
|
+
unless File.exists?(configfilepath)
|
9
|
+
reset_configfile(configfilepath) if generate_default_if_not_found
|
10
|
+
if File.exists?(configfilepath)
|
11
|
+
raise Mysql2psql::ConfigurationFileInitialized.new("\n
|
12
|
+
No configuration file found.
|
13
|
+
A new file has been initialized at: #{configfilepath}
|
14
|
+
Please review the configuration and retry..\n\n\n")
|
15
|
+
else
|
16
|
+
raise Mysql2psql::ConfigurationFileNotFound.new("cannot load config file #{configfilepath}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
super(configfilepath)
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset_configfile(filepath)
|
23
|
+
file = File.new(filepath,'w')
|
24
|
+
self.class.template.each_line do | line|
|
25
|
+
file.puts line
|
26
|
+
end
|
27
|
+
file.close
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns template file text given +options+ hash which may include:
|
31
|
+
# :to_filename - default: nil
|
32
|
+
# :include_tables - default: []
|
33
|
+
# :exclude_tables - default: []
|
34
|
+
# :suppress_data - default: false
|
35
|
+
# :suppress_ddl - default: false
|
36
|
+
# :suppress_sequence_update - default: false
|
37
|
+
# :suppress_indexes - default: false
|
38
|
+
# :force_truncate - default: false
|
39
|
+
# :use_timezones - default: false
|
40
|
+
def self.template(options={})
|
41
|
+
configtext = <<EOS
|
42
|
+
mysql:
|
43
|
+
hostname: localhost
|
44
|
+
port: 3306
|
45
|
+
socket:
|
46
|
+
username: mysql2psql
|
47
|
+
password:
|
48
|
+
database: mysql2psql_test
|
49
|
+
|
50
|
+
destination:
|
51
|
+
# if file is given, output goes to file, else postgres
|
52
|
+
file: #{ options[:to_filename] || ''}
|
53
|
+
postgres:
|
54
|
+
hostname: localhost
|
55
|
+
port: 5432
|
56
|
+
username: mysql2psql
|
57
|
+
password:
|
58
|
+
database: mysql2psql_test
|
59
|
+
|
60
|
+
# if tables is given, only the listed tables will be converted. leave empty to convert all tables.
|
61
|
+
#tables:
|
62
|
+
#- table1
|
63
|
+
#- table2
|
64
|
+
EOS
|
65
|
+
include_tables = options[:include_tables] || []
|
66
|
+
if include_tables.length>0
|
67
|
+
configtext += "\ntables:\n"
|
68
|
+
include_tables.each do |t|
|
69
|
+
configtext += "- #{t}\n"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
configtext += <<EOS
|
73
|
+
# if exclude_tables is given, exclude the listed tables from the conversion.
|
74
|
+
#exclude_tables:
|
75
|
+
#- table3
|
76
|
+
#- table4
|
77
|
+
|
78
|
+
EOS
|
79
|
+
exclude_tables = options[:exclude_tables] || []
|
80
|
+
if exclude_tables.length>0
|
81
|
+
configtext += "\nexclude_tables:\n"
|
82
|
+
exclude_tables.each do |t|
|
83
|
+
configtext += "- #{t}\n"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
if !options[:suppress_data].nil?
|
88
|
+
configtext += <<EOS
|
89
|
+
|
90
|
+
# if suppress_data is true, only the schema definition will be exported/migrated, and not the data
|
91
|
+
suppress_data: #{options[:suppress_data]}
|
92
|
+
EOS
|
93
|
+
end
|
94
|
+
|
95
|
+
if !options[:suppress_ddl].nil?
|
96
|
+
configtext += <<EOS
|
97
|
+
|
98
|
+
# if suppress_ddl is true, only the data will be exported/imported, and not the schema
|
99
|
+
suppress_ddl: #{options[:suppress_ddl]}
|
100
|
+
EOS
|
101
|
+
end
|
102
|
+
|
103
|
+
if !options[:suppress_sequence_update].nil?
|
104
|
+
configtext += <<EOS
|
105
|
+
|
106
|
+
# if suppress_sequence_update is true, the sequences for serial (auto-incrementing) columns
|
107
|
+
# will not be update to the current maximum value of that column in the database
|
108
|
+
# if suppress_ddl is not set to true, then this option is implied to be false as well (unless overridden here)
|
109
|
+
suppress_sequence_update: #{options[:suppress_sequence_update]}
|
110
|
+
EOS
|
111
|
+
end
|
112
|
+
|
113
|
+
if !options[:force_truncate].nil?
|
114
|
+
configtext += <<EOS
|
115
|
+
|
116
|
+
# if force_truncate is true, forces a table truncate before table loading
|
117
|
+
force_truncate: #{options[:force_truncate]}
|
118
|
+
EOS
|
119
|
+
end
|
120
|
+
|
121
|
+
if !options[:use_timezones].nil?
|
122
|
+
configtext += <<EOS
|
123
|
+
|
124
|
+
# if use_timezones is true, timestamp/time columns will be created in postgres as "with time zone"
|
125
|
+
# rather than "without time zone"
|
126
|
+
use_timezones: #{options[:use_timezones]}
|
127
|
+
EOS
|
128
|
+
end
|
129
|
+
|
130
|
+
if !options[:suppress_indexes].nil?
|
131
|
+
configtext += <<EOS
|
132
|
+
|
133
|
+
# if suppress_indexes is true, indexes will not be exported/migrated
|
134
|
+
suppress_indexes: #{options[:suppress_indexes]}
|
135
|
+
EOS
|
136
|
+
end
|
137
|
+
configtext
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'mysql2psql/errors'
|
3
|
+
|
4
|
+
class Mysql2psql
|
5
|
+
|
6
|
+
class ConfigBase
|
7
|
+
attr_reader :config, :filepath
|
8
|
+
|
9
|
+
def initialize(configfilepath)
|
10
|
+
@filepath=configfilepath
|
11
|
+
@config = YAML::load(File.read(filepath))
|
12
|
+
end
|
13
|
+
def [](key)
|
14
|
+
self.send( key )
|
15
|
+
end
|
16
|
+
def method_missing(name, *args)
|
17
|
+
token=name.to_s
|
18
|
+
default = args.length>0 ? args[0] : ''
|
19
|
+
must_be_defined = default == :none
|
20
|
+
case token
|
21
|
+
when /mysql/i
|
22
|
+
key=token.sub( /^mysql/, '' )
|
23
|
+
value=config["mysql"][key]
|
24
|
+
when /pg/i
|
25
|
+
key=token.sub( /^pg/, '' )
|
26
|
+
value=config["destination"]["postgres"][key]
|
27
|
+
when /dest/i
|
28
|
+
key=token.sub( /^dest/, '' )
|
29
|
+
value=config["destination"][key]
|
30
|
+
when /only_tables/i
|
31
|
+
value=config["tables"]
|
32
|
+
else
|
33
|
+
value=config[token]
|
34
|
+
end
|
35
|
+
value.nil? ? ( must_be_defined ? (raise Mysql2psql::UninitializedValueError.new("no value and no default for #{name}")) : default ) : value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class Mysql2psql
|
2
|
+
|
3
|
+
class Converter
|
4
|
+
attr_reader :reader, :writer, :options
|
5
|
+
attr_reader :exclude_tables, :only_tables, :suppress_data, :suppress_ddl, :supress_sequence_update, :force_truncate, :suppress_indexes
|
6
|
+
|
7
|
+
def initialize(reader, writer, options)
|
8
|
+
@reader = reader
|
9
|
+
@writer = writer
|
10
|
+
@options = options
|
11
|
+
@exclude_tables = options.exclude_tables([])
|
12
|
+
@only_tables = options.only_tables(nil)
|
13
|
+
@suppress_data = options.suppress_data(false)
|
14
|
+
@suppress_ddl = options.suppress_ddl(false)
|
15
|
+
@supress_sequence_update = options.supress_sequence_update(false)
|
16
|
+
@suppress_indexes = options.suppress_indexes(false)
|
17
|
+
@force_truncate = options.force_truncate(false)
|
18
|
+
@use_timezones = options.use_timezones(false)
|
19
|
+
end
|
20
|
+
|
21
|
+
def convert
|
22
|
+
_time1 = Time.now
|
23
|
+
|
24
|
+
tables = reader.tables.
|
25
|
+
reject {|table| @exclude_tables.include?(table.name)}.
|
26
|
+
select {|table| @only_tables ? @only_tables.include?(table.name) : true}
|
27
|
+
|
28
|
+
tables.each do |table|
|
29
|
+
writer.write_sequence_update(table, options)
|
30
|
+
end if !(@suppress_sequence_update && @suppress_ddl)
|
31
|
+
|
32
|
+
tables.each do |table|
|
33
|
+
writer.write_table(table, {:use_timezones => @use_timezones})
|
34
|
+
end unless @suppress_ddl
|
35
|
+
|
36
|
+
_time2 = Time.now
|
37
|
+
tables.each do |table|
|
38
|
+
writer.truncate(table) if force_truncate && !suppress_ddl
|
39
|
+
writer.write_contents(table, reader)
|
40
|
+
end unless @suppress_data
|
41
|
+
|
42
|
+
_time3 = Time.now
|
43
|
+
tables.each do |table|
|
44
|
+
writer.write_indexes(table) unless @suppress_indexes
|
45
|
+
end unless @suppress_ddl
|
46
|
+
tables.each do |table|
|
47
|
+
writer.write_constraints(table)
|
48
|
+
end unless @suppress_ddl
|
49
|
+
|
50
|
+
|
51
|
+
writer.close
|
52
|
+
_time4 = Time.now
|
53
|
+
puts "Table creation #{((_time2 - _time1) / 60).round} min, loading #{((_time3 - _time2) / 60).round} min, indexing #{((_time4 - _time3) / 60).round} min, total #{((_time4 - _time1) / 60).round} min"
|
54
|
+
return 0
|
55
|
+
rescue => e
|
56
|
+
$stderr.puts "Mysql2psql: conversion failed: #{e.to_s}"
|
57
|
+
$stderr.puts e.backtrace
|
58
|
+
return -1
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
class Mysql2psql
|
3
|
+
|
4
|
+
class GeneralError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class ConfigurationError < StandardError
|
8
|
+
end
|
9
|
+
class UninitializedValueError < ConfigurationError
|
10
|
+
end
|
11
|
+
class ConfigurationFileNotFound < ConfigurationError
|
12
|
+
end
|
13
|
+
class ConfigurationFileInitialized < ConfigurationError
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|