mysql_to_pg_dump 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ba8104c56dca13a7775b2039a8524dd4cf2b1c5
4
+ data.tar.gz: cd465e9fe72f7200be4b151413d4f979575061b8
5
+ SHA512:
6
+ metadata.gz: b5ba0cd5e5f4983bc45e8f52f428afb206da9d9ddb14c7d175437399fb47758bd36e0fbf76e4cf0435eefa3d74a636a8e6714783a4a6121bdeddb47655443e54
7
+ data.tar.gz: 46f1f034b6727561f0ddc3ae127a9d2cef67e741e3e78af08b549fe51b46d6cecb7f6e4ac3eb47aceb0b4e2b6380d2d102b0e67767f55985b1a991dcd625b0f6
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Dimkarodinz
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.md ADDED
@@ -0,0 +1,44 @@
1
+ # MysqlToPgDump
2
+ This gem allows pull content from mysql db (on the remote server) and
3
+ load it into your local postgres database.
4
+ Technically, it is not a dump - but result is almost the same.
5
+
6
+ ## Usage
7
+ Copy content of remote mysql db to tmp/db_server_data as .txt files.
8
+ ```bash
9
+ $ rake db:pull your_server@123.4.5.6
10
+ ```
11
+ Replace local postgres db content to pulled.
12
+ ```bash
13
+ $ rake db:pull:load
14
+ # or
15
+ $ RAILS_ENV=staging rake db:pull:load
16
+ ```
17
+ Delete all files from tmp/db_server_data.
18
+ ```bash
19
+ $ rake db:pull:clean
20
+ ```
21
+ Pull, replace and clean junk - all of the above in one task.
22
+ ```bash
23
+ $ rake db:pull:force
24
+ ```
25
+
26
+ ## Installation
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'mysql_to_pg_dump'
31
+ ```
32
+
33
+ And then execute:
34
+ ```bash
35
+ $ bundle
36
+ ```
37
+
38
+ Or install it yourself as:
39
+ ```bash
40
+ $ gem install mysql_to_pg_dump
41
+ ```
42
+
43
+ ## License
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'MysqlToPgDump'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
@@ -0,0 +1,3 @@
1
+ module MysqlToPgDump
2
+ require 'mysql_to_pg_dump/task_uploader' if defined?(Rails)
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'mysql_to_pg_dump'
2
+ require 'rails'
3
+
4
+ module MysqlToPgDump
5
+ module TaskUploader
6
+ spec = Gem::Specification.find_by_name 'mysql_to_pg_dump'
7
+ load "#{spec.gem_dir}/lib/tasks/db.rake"
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module MysqlToPgDump
2
+ VERSION = '0.1.0'
3
+ end
data/lib/tasks/db.rake ADDED
@@ -0,0 +1,125 @@
1
+ require 'colorize'
2
+ require 'rake-progressbar'
3
+
4
+ namespace :db do
5
+ desc "Copies db content from production " \
6
+ "server into tmp/db_server_data"
7
+ task pull: :environment do
8
+ ARGV.each { |a| task a.to_sym do ; end }
9
+ server_addr =
10
+ ARGV[1].blank? ? server_addr_input : ARGV[1].to_s
11
+
12
+ if server_addr.include?("@")
13
+ bar = RakeProgressbar.new(db_tables.size)
14
+
15
+ system "ssh #{server_addr} 'mkdir -p #{tmp_location}'"
16
+ db_tables.each do |table|
17
+ system %{ssh #{server_addr} "echo '#{sql_select(table)}' | #{login_to_mysql} > #{file_to_save(table)}"}
18
+ bar.inc
19
+ end
20
+ bar.finished
21
+
22
+ system "scp -r #{server_addr}:#{tmp_location} tmp"
23
+ system "ssh #{server_addr} 'rm -rf #{tmp_location}'"
24
+
25
+ printf "Db data from production server " \
26
+ "has been pulled successfully\n".green
27
+ else
28
+ printf "No server address given." \
29
+ "Expecting format like 'server@123.4.5.6'\n".yellow
30
+ end
31
+ end
32
+
33
+ namespace :pull do
34
+ desc "Replaces current db data to pulled"
35
+ task load: :environment do
36
+ printf "Current env db data will be destroyed.\n".red
37
+ printf "Are you sure? (y/n)\n"
38
+ input = STDIN.gets.strip
39
+
40
+ if input == 'y'
41
+ if data_already_pulled?
42
+ clean_database
43
+ db_tables.each { |t| system %(psql -d #{dev['database']} -c "#{psql_import_query(t)}") }
44
+ printf "Your db data now is equal to production\n".green
45
+ else
46
+ printf "No pulled data. Run 'rake db:pull' first\n".yellow
47
+ end
48
+ else
49
+ printf "Canceled\n".blue
50
+ end
51
+ end
52
+
53
+ desc "Deletes pulled db data from local tmp/db_server_data"
54
+ task :clean do
55
+ system 'rm -f tmp/db_server_data/*'
56
+ printf "Pulled db data has been " \
57
+ "deleted from /tmp successfully\n".green
58
+ end
59
+
60
+ desc "Pulls remote mysql db data, then loads it to " \
61
+ "local postgres and cleans junk"
62
+ task force: ['db:pull', 'db:pull:load', 'db:pull:clean']
63
+ end
64
+
65
+ private
66
+
67
+ def server_addr_input
68
+ printf "Enter server address like 'server@123.4.5.6': "
69
+ STDIN.gets.strip
70
+ end
71
+
72
+ def data_already_pulled?
73
+ if %x{ls tmp}.split("\n").include? 'db_server_data'
74
+ %x(ls tmp/db_server_data).split("\n").size == db_tables.size
75
+ else
76
+ false
77
+ end
78
+ end
79
+
80
+ def psql_import_query table_name
81
+ "\\copy #{table_name} from " \
82
+ "'tmp/db_server_data/#{production['database']}_#{table_name}.txt' " \
83
+ "delimiter E'\\t' null as 'NULL' csv header"
84
+ end
85
+
86
+ def clean_database
87
+ task_names = %w(db:drop db:create db:migrate)
88
+ task_names.each { |t| Rake::Task[t].invoke }
89
+ end
90
+
91
+ def login_to_mysql
92
+ "mysql " \
93
+ "--user=#{production['username']} " \
94
+ "--password=#{production['password']} " \
95
+ "#{production['database']}"
96
+ end
97
+
98
+ def file_to_save table_name
99
+ "#{tmp_location}/#{production['database']}_#{table_name}.txt"
100
+ end
101
+
102
+ def sql_select table_name
103
+ "SELECT * FROM #{table_name};"
104
+ end
105
+
106
+ def db_tables
107
+ ActiveRecord::Base.connection.tables - ['schema_migrations']
108
+ end
109
+
110
+ def tmp_location
111
+ 'app/current/tmp/db_server_data'
112
+ end
113
+
114
+ def show_db_info env
115
+ Rails.application.config.database_configuration[env]
116
+ end
117
+
118
+ def dev
119
+ show_db_info 'development'
120
+ end
121
+
122
+ def production
123
+ show_db_info 'production'
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_to_pg_dump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dimkarodinz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-progressbar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Copies mysql db data from the remote server to the local postgres db
56
+ email:
57
+ - dimkarodin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/mysql_to_pg_dump.rb
66
+ - lib/mysql_to_pg_dump/task_uploader.rb
67
+ - lib/mysql_to_pg_dump/version.rb
68
+ - lib/tasks/db.rake
69
+ homepage: https://github.com/Dimkarodinz/mysql_to_pg_dump.git
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.5.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Almost mysql to postgres dump
93
+ test_files: []