vela 0.1.1

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzhlY2ZjZmU0OGY5MTI2MWY2MDhlN2YwN2E0YWIwMjUwNDUxMGZiZg==
5
+ data.tar.gz: !binary |-
6
+ NTAzNWVkZGZjNTIyN2U3NWU5ZTc1YTJjNGI0NWUxZmIzZjQ3M2YyMg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjBhZWJmNzI2YzEyZDFmMDI5NjExZTIzNmU4ZmI5ZjgyODdmM2NjM2YxZGJk
10
+ M2NkYWRhNTRmOTFmY2Q5NzIxYjMzMzg5NjFiMjFkMTU0MDE5ZjVjZjU1ZDJk
11
+ ODljNWNiNzg0NWUwYjBiOWU0MzdhNjgwNzhkZTZhZmE4ODA0YjM=
12
+ data.tar.gz: !binary |-
13
+ ODUxZjY1MzkwMGRkMTM2OWJhNWQ1YWZhZjlhOWIzMGY4Yzc0MmY3ZTNlODcx
14
+ OWNkNDkwM2MxYjc4NTJmNzA2MTFiMDUxYzE3M2QxYTI0ZTc0Y2IwN2Q0ZGQx
15
+ MTMwNDliZTU0MzUzNjMzNjdmMGI3OTY2NDBkMmRmYmM4ODU0YTE=
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vela.gemspec
4
+ gemspec
5
+ gem 'mysql2'
6
+ gem 'pg'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 tokhi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Vela
2
+
3
+ Vela is a simple light gem which clone a table records from mysql to postgresql and vice versa. This gem is helpful when you want to have the same table records in a different db.
4
+
5
+ However this gem is also able to clone the records of a table to another table even if the destination table has a different name and columns name.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'vela'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install vela
22
+
23
+ ## Usage
24
+ to clone data from `Mysql` to `Postgresql` (change the src and destination arguments based on your db settings )
25
+
26
+ ```ruby
27
+ v = Vela::DB.new
28
+ v.src_connection(user: 'root', password: 'pass', host: '127.0.0.1', database: 'testdb', adapter: 'mysql')
29
+ v.dst_connection(user: 'user', password: 'pass', host: '127.0.0.1', database: 'testpdb', adapter: 'postgresql')
30
+ v.dbsync("src_tbl", "dst_tbl") # destination table should already been exist
31
+
32
+ # if your destination tables' columns are different than the source table (e.g; clonse `col1` data to m_col1 column):
33
+ v.dbsync("src_tbl", "dst_tbl", {"col1" => "m_col1", "col2" => "m_col2"}) # you can add as much as column as you want
34
+
35
+ ```
36
+
37
+
38
+ to clone data from `Postgresql` to `Mysql` (change the src and destination arguments based on your db settings )
39
+
40
+ ```ruby
41
+ v = Vela::DB.new
42
+ v.src_connection(user: 'user', password: 'pass', host: '127.0.0.1', database: 'testpdb', adapter: 'postgresql')
43
+ v.dst_connection(user: 'root', password: 'pass', host: '127.0.0.1', database: 'testdb', adapter: 'mysql')
44
+ v.dbsync("src_tbl", "dst_tbl") # destination table should already been exist
45
+
46
+ # if your destination tables' columns are different than the source table (e.g; clonse `col1` data to m_col1 column):
47
+ v.dbsync("src_tbl", "dst_tbl", {"col1" => "m_col1", "col2" => "m_col2"}) # you can add as much as column as you want
48
+
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vela.
60
+
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
65
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "vela"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/vela.rb ADDED
@@ -0,0 +1,98 @@
1
+ require "vela/version"
2
+ require 'mysql2'
3
+ require 'pg'
4
+
5
+ module Vela
6
+ class DB
7
+ attr_reader :src_conn, :dst_conn, :src_adapter, :dst_adapter, :src_tbl, :dst_tbl, :columns
8
+ def initialize(src_tbl=nil,dst_tbl=nil, src_adapter=nil, dst_adapter=nil)
9
+ @src_conn = nil
10
+ @dst_conn = nil
11
+ @src_adapter = src_adapter
12
+ @dst_adapter = dst_adapter
13
+ @src_tbl = src_tbl
14
+ @dst_tbl = dst_tbl
15
+ @columns = nil
16
+ end
17
+
18
+ def src_connection(args)
19
+ @src_adapter = args[:adapter]
20
+ @src_conn = get_adapter_connection(args)
21
+ end
22
+
23
+ def dst_connection(args)
24
+ @dst_adapter = args[:adapter]
25
+ @dst_conn = get_adapter_connection(args)
26
+ end
27
+
28
+ def select_query
29
+ sqry = "SELECT * from #{@src_tbl} limit 50"
30
+ (@src_adapter.eql? "mysql") ? @src_conn.query(sqry) : @src_conn.exec(sqry)
31
+ end
32
+
33
+ def insert_query(columns, d_signs, rcrd)
34
+ begin
35
+
36
+ statement = "insert into %s (%s) values (%s)" % [@dst_tbl,columns,d_signs]
37
+ if @dst_adapter.eql? "postgresql"
38
+ @dst_conn.prepare("statement#{rcrd['id']}", statement)
39
+ @dst_conn.exec_prepared("statement#{rcrd['id']}",rcrd.values)
40
+ else
41
+ values = rcrd.values.to_s.tr!('[]', '').gsub('nil', 'null')
42
+
43
+ statement = "insert into %s (%s) values (%s)" % [@dst_tbl,columns,values]
44
+ @dst_conn.query(statement)
45
+ end
46
+
47
+ rescue Exception => e
48
+ puts e
49
+ end
50
+ end
51
+
52
+ def sql_sync
53
+ select_query.each do |rcrd|
54
+
55
+ d_signs = build_dollar_signs(rcrd)
56
+ rcrd = rename_keys(rcrd, @columns)
57
+ columns = rcrd.keys.to_s.tr('[]:"','')
58
+ insert_query(columns, d_signs, rcrd)
59
+ end
60
+
61
+ end # db_sql_sync
62
+
63
+ def dbsync(src_tbl, dst_tbl, options)
64
+ @columns = options
65
+ @src_tbl = src_tbl
66
+ @dst_tbl = dst_tbl
67
+ sql_sync
68
+ end
69
+
70
+ def get_adapter_connection(args)
71
+ case args[:adapter]
72
+ when "mysql"
73
+ Mysql2::Client.new(host: args[:host], user: args[:user], password: args[:password], database: args[:database])
74
+ when "postgresql"
75
+ PG.connect(host: args[:host], user: args[:user], password: args[:password], dbname: args[:database])
76
+ else
77
+ puts "adapter is not supported!"
78
+ end
79
+ end
80
+
81
+ # swap key and values
82
+ def rename_keys(record, cols)
83
+ cols.nil? ? record : Hash[record.map { |k, v| [(cols[k] || k).to_sym ,v] }]
84
+ end
85
+
86
+
87
+ def build_dollar_signs(rcrd)
88
+ d_signs = ''
89
+ rcrd.length.times do |i|
90
+ d_signs += "$#{i+1},"
91
+ end
92
+ # remove the last comma
93
+ d_signs.chomp!(',')
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,3 @@
1
+ module Vela
2
+ VERSION = "0.1.1"
3
+ end
data/vela.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vela/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vela"
8
+ spec.version = Vela::VERSION
9
+ spec.authors = ["tokhi"]
10
+ spec.email = ["shafi.tokhi@gmail.com"]
11
+
12
+ spec.summary = %q{Vela is a simple light gem which clone a table records from mysql to postgresql and vice versa.}
13
+ spec.description = %q{Vela is a simple light gem which clone a table records from mysql to postgresql and vice versa. This gem is helpful when you want to have the same table records in a different db.
14
+
15
+ However this gem is also able to clone the records of a table to another table even if the destination table has a different name and columns name.}
16
+ spec.homepage = "https://github.com/tokhi/vela"
17
+ spec.license = "MIT"
18
+
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vela
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - tokhi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ! 'Vela is a simple light gem which clone a table records from mysql
56
+ to postgresql and vice versa. This gem is helpful when you want to have the same
57
+ table records in a different db.
58
+
59
+
60
+ However this gem is also able to clone the records of a table to another table even
61
+ if the destination table has a different name and columns name.'
62
+ email:
63
+ - shafi.tokhi@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - .rspec
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - lib/vela.rb
78
+ - lib/vela/version.rb
79
+ - vela.gemspec
80
+ homepage: https://github.com/tokhi/vela
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Vela is a simple light gem which clone a table records from mysql to postgresql
104
+ and vice versa.
105
+ test_files: []