mysqlknife 1.0.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a035195f41f18f9df67401eaf9aa6f9793945bf3
4
- data.tar.gz: 33276a6c3dfe02dd97230bba521a0491c6a1295a
3
+ metadata.gz: 51955f4209a8173426bf5358df753a980cc13a44
4
+ data.tar.gz: deb6a001c669f47c3e988a0d7d96a70bb0ff1bef
5
5
  SHA512:
6
- metadata.gz: 8402b7c4209d2fcf51aafbb12902492016840299083d9915a3b18e1b24d257d34474053b2fd8049d069420b541bbb7a4ca89bcef70ddc239d9f4cd83bace753e
7
- data.tar.gz: b3831427b4a10f7868dcbe673063c4b296abd0e4c1aa6b6b50be5c5ef7f07a7ec95b3eea6490f907e0657222b6b016c42c50937200abcabefecc5d1a2c05af19
6
+ metadata.gz: cf03569b5bfe77afb7568cb9f5dac22579e74fdda74f5159b3da047db17bac7d6c6297ea6a789628ce049f8f1403b8905474d09dec146ae7abc52179d2efd37a
7
+ data.tar.gz: 62c6e2964b203bcb7183b994ace384743e109d0e1a042dd4b7600175bfc16be44c9c60dc3160aeadf5da1bd58e53521113e5624628e6ff7fc28a9fc2b29da4e7
data/bin/mysqlknife CHANGED
@@ -18,6 +18,30 @@ global_option('-u', '--user STRING')
18
18
  global_option('-p', '--password [STRING]')
19
19
  global_option('-P', '--port [STRING]')
20
20
 
21
+ command :checksum do |c|
22
+ c.description = 'Checksum table.'
23
+ c.syntax = %w(mysqlknife checksum
24
+ --from h=127.0.0.1,P=3306,u=root,p=admin,d=demo_from
25
+ --to h=127.0.0.1,P=3306,u=root,p=admin,d=demo_to
26
+ --table foo).join(' ')
27
+ c.option '--from STRING', String, 'DNS to connect to source database.'
28
+ c.option '--to STRING', String, 'DNS to connect to destination database.'
29
+ c.option '--table STRING', String, 'Table name to compare.'
30
+
31
+ c.action do |args, options|
32
+ if options.from.nil? ||
33
+ options.to.nil? ||
34
+ options.table.nil?
35
+ puts c.syntax
36
+ else
37
+ unless Mysqlknife::Checksum.new(options.__hash__).equal?
38
+ puts "-- Table '#{options.table}' is not equal data."
39
+ exit 1
40
+ end
41
+ end
42
+ end
43
+ end
44
+
21
45
  command :swap do |c|
22
46
  c.description = 'Swap two databases.'
23
47
  c.syntax = %w(mysqlknife swap
data/lib/mysqlknife.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'mysqlknife/config'
4
4
  require 'mysqlknife/connection'
5
+ require 'mysqlknife/checksum'
5
6
  require 'mysqlknife/kill'
6
7
  require 'mysqlknife/sql'
7
8
  require 'mysqlknife/swap'
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ module Mysqlknife
4
+ class Checksum
5
+ def initialize(options = {})
6
+ dns_from = Config.new.dns(options[:from])
7
+ dns_to = Config.new.dns(options[:to])
8
+
9
+ @sum_from = sum(dns_from, options[:table])
10
+ @sum_to = sum(dns_to, options[:table])
11
+ end
12
+
13
+ def sum(dns, table)
14
+ conn = Connection.new(dns)
15
+ sql = Sql.new(dns[:database], table)
16
+ columns = conn.execute(sql.columns)
17
+ sql = sql.checksum(columns)
18
+ columns = conn.execute(sql)
19
+ columns.first['sum']
20
+ end
21
+
22
+ def equal?
23
+ (@sum_from == @sum_to)
24
+ end
25
+ end
26
+ end
@@ -6,7 +6,7 @@ module Mysqlknife
6
6
  class Config
7
7
  @configs = nil
8
8
 
9
- def initialize(file)
9
+ def initialize(file = nil)
10
10
  unless file.nil?
11
11
  path = File.expand_path(File.join(Dir.pwd, file))
12
12
  @configs = YAML.load_file(path) if File.exist?(path)
@@ -32,8 +32,29 @@ module Mysqlknife
32
32
  { host: 'localhost',
33
33
  port: '3306',
34
34
  user: 'root',
35
- pass: '' }
35
+ password: '' }
36
36
  .merge(options)
37
37
  end
38
+
39
+ def dns(options)
40
+ options.split(',')
41
+ .map { |c| c.split('=', 2) }
42
+ .reduce({}) do |m, (key, value)|
43
+ case key.to_sym
44
+ when :h
45
+ key = :host
46
+ when :u
47
+ key = :user
48
+ when :p
49
+ key = :password
50
+ when :P
51
+ key = :port
52
+ when :d
53
+ key = :database
54
+ end
55
+ m[key] = value
56
+ m
57
+ end
58
+ end
38
59
  end
39
60
  end
@@ -1,28 +1,19 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'rubygems'
4
+ require 'mysqlknife/config'
4
5
  require 'mysql2'
5
6
 
6
7
  module Mysqlknife
7
8
  class Connection
8
9
  def initialize(options = {}, database = nil)
9
- options = options(options)
10
+ options = Config.new.defaults(options)
10
11
 
11
12
  @host = options[:host]
12
13
  @port = options[:port]
13
14
  @username = options[:user]
14
15
  @password = options[:password]
15
- @database = database
16
- end
17
-
18
- def options(options)
19
- options.delete_if { |k, v| v.nil? }
20
-
21
- { host: 'localhost',
22
- port: '3306',
23
- user: 'root',
24
- password: '' }
25
- .merge(options)
16
+ @database = options[:database] unless database
26
17
  end
27
18
 
28
19
  def execute(sql)
@@ -2,8 +2,9 @@
2
2
 
3
3
  module Mysqlknife
4
4
  class Sql
5
- def initialize(database = nil)
5
+ def initialize(database = nil, table = nil)
6
6
  @database = database
7
+ @table = table
7
8
  end
8
9
 
9
10
  def use
@@ -80,5 +81,20 @@ module Mysqlknife
80
81
  def current_user_grants
81
82
  'SHOW GRANTS FOR CURRENT_USER;'
82
83
  end
84
+
85
+ def columns
86
+ %W(SELECT COLUMN_NAME
87
+ FROM information_schema.columns
88
+ WHERE table_schema = '#{@database}'
89
+ AND table_name = '#{@table}';).join(' ')
90
+ end
91
+
92
+ def checksum(columns)
93
+ md5 = columns.map do |column| %W(COALESCE(
94
+ #{column['COLUMN_NAME']},
95
+ '#{column['COLUMN_NAME']}')).join
96
+ end
97
+ "SELECT SUM(CRC32(CONCAT(#{md5.join(', ')}))) AS sum FROM #{@table};"
98
+ end
83
99
  end
84
100
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Mysqlknife
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysqlknife
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicola Strappazzon C.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-23 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.5.5
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.5.5
13
55
  description: MySQL knife tools
14
56
  email: nicola51980@gmail.com
15
57
  executables:
@@ -18,6 +60,7 @@ extensions: []
18
60
  extra_rdoc_files: []
19
61
  files:
20
62
  - bin/mysqlknife
63
+ - lib/mysqlknife/checksum.rb
21
64
  - lib/mysqlknife/config.rb
22
65
  - lib/mysqlknife/connection.rb
23
66
  - lib/mysqlknife/kill.rb