data_mask 0.1.3 → 0.2.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 +4 -4
- data/Gemfile +2 -2
- data/README.md +29 -4
- data/bin/console +3 -3
- data/lib/data_mask/config.rb +1 -1
- data/lib/data_mask/db_shell.rb +44 -0
- data/lib/data_mask/version.rb +1 -1
- data/lib/data_mask.rb +34 -14
- metadata +3 -3
- data/lib/data_mask/migrate.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f258feb7c76783490d8bf316e1525caf53e17e77
|
4
|
+
data.tar.gz: 46547b0f795ba1f275d5e5d6b1723a9dc730e270
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96d53a54db904ab7739e9f173f99bf27bb2661d3e11b22e68d39ccb3918fbe7d61f080d00f9f6d7b5ae04532d657ee72e0f76533357dd6d6be50ccefc2b5b9ea
|
7
|
+
data.tar.gz: db18c75ba6579a89fb1992bbb9f4e3ac8dee7b77c71bbc4f0e0a0e315b04305ee4797ca037ce35a39609a86a3a85a3c535de6a3ad1117020c1c486c51a98d382
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -25,18 +25,43 @@ Make your `database.yml` & `tasks.yml` imitated with examples in `test/config`
|
|
25
25
|
And use the gem like this:
|
26
26
|
|
27
27
|
```ruby
|
28
|
-
mask = Mask.new('path/with/your/database.yml_&_tasks.yml') # Default 'config/'
|
28
|
+
mask = DataMask::Mask.new('path/with/your/database.yml_&_tasks.yml') # Default 'config/'
|
29
29
|
mask.run
|
30
30
|
```
|
31
31
|
|
32
32
|
or
|
33
33
|
```ruby
|
34
|
-
mask = Mask.new('path/with/your/database.yml_&_task.yml') # Default 'config/'
|
34
|
+
mask = DataMask::Mask.new('path/with/your/database.yml_&_task.yml') # Default 'config/'
|
35
|
+
mask.tmp_db_clear
|
35
36
|
mask.operate_db('create')
|
36
37
|
mask.migrate
|
37
38
|
mask.play
|
38
39
|
```
|
39
40
|
|
41
|
+
And you may use `mask.export` to export the database to *.sql* file.
|
42
|
+
|
43
|
+
--------
|
44
|
+
|
45
|
+
In `.yml` files, you can use `%=` for eval the content.
|
46
|
+
|
47
|
+
You can also write yml like this:
|
48
|
+
|
49
|
+
```yml
|
50
|
+
password: '%= BCrypt::Password.create("123456")'
|
51
|
+
```
|
52
|
+
|
53
|
+
And what you need to do is only `require 'bcrypt'`.
|
54
|
+
|
55
|
+
|
56
|
+
If you need to set each row with different value, you can just use `each_row`, like:
|
57
|
+
```yml
|
58
|
+
users:
|
59
|
+
mobile: 18600000000
|
60
|
+
each_row:
|
61
|
+
email: '%= row[:id].to_s + "@zhulux.qa"'
|
62
|
+
```
|
63
|
+
the `row` this the object of each row, which type is Hash.
|
64
|
+
|
40
65
|
## Development
|
41
66
|
|
42
67
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -45,10 +70,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
45
70
|
|
46
71
|
## Contributing
|
47
72
|
|
48
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
49
|
-
|
73
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cuebyte/data_mask. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
50
74
|
|
51
75
|
## License
|
76
|
+
Authored by [cuebyte](https://github.com/cuebyte). Copyright (c) 2015 [ZhuluX Team](https://github.com/zhulux/).
|
52
77
|
|
53
78
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
54
79
|
|
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'data_mask'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "data_mask"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/lib/data_mask/config.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
module DataMask
|
2
|
+
class DBShell
|
3
|
+
attr_accessor :options
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def export(**opt)
|
10
|
+
cmd = ''
|
11
|
+
cmd += send("#{@options[:adapter]}_cmd", 'export')
|
12
|
+
cmd += "> %{database}-masking-#{Time.now.to_f.to_i}.sql" % @options if opt[:to_file]
|
13
|
+
cmd
|
14
|
+
end
|
15
|
+
|
16
|
+
def import(filepath=nil)
|
17
|
+
cmd = ''
|
18
|
+
cmd += send("#{@options[:adapter]}_cmd", 'import')
|
19
|
+
cmd += "< #{filepath}" if filepath
|
20
|
+
cmd
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def postgres_cmd(type)
|
26
|
+
cmd = "#{type=='export' ? 'pg_dump' : 'psql'} "
|
27
|
+
cmd += "-h %{host} " % @options if @options[:host]
|
28
|
+
cmd += "-p %{port} " % @options if @options[:port]
|
29
|
+
cmd += "-U %{user} " % @options if @options[:user]
|
30
|
+
cmd += "%{database} " % @options if @options[:database]
|
31
|
+
cmd
|
32
|
+
end
|
33
|
+
|
34
|
+
def mysql_cmd(type)
|
35
|
+
cmd = "#{type=='export' ? 'mysqldump' : 'mysql'} "
|
36
|
+
cmd += "-h %{host} " % @options if @options[:host]
|
37
|
+
cmd += "-P %{port} " % @options if @options[:port]
|
38
|
+
cmd += "-u %{user} " % @options if @options[:user]
|
39
|
+
cmd += "-p %{password} " % @options if @options[:password]
|
40
|
+
cmd += "%{database} " % @options
|
41
|
+
cmd
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/data_mask/version.rb
CHANGED
data/lib/data_mask.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'sequel'
|
2
2
|
|
3
3
|
require 'data_mask/config'
|
4
|
-
require 'data_mask/
|
4
|
+
require 'data_mask/db_shell'
|
5
|
+
|
5
6
|
module DataMask
|
6
7
|
class Mask
|
7
8
|
def initialize(path = 'config')
|
@@ -10,23 +11,35 @@ module DataMask
|
|
10
11
|
end
|
11
12
|
|
12
13
|
def operate_db(op)
|
13
|
-
|
14
|
-
begin
|
15
|
-
db.run("#{op.upcase} DATABASE %{database}" % @db_conf[:to])
|
16
|
-
rescue
|
17
|
-
end
|
18
|
-
end
|
14
|
+
execute_sql "#{op.upcase} DATABASE %{database}" % @db_conf[:to]
|
19
15
|
end
|
20
16
|
|
21
17
|
def migrate
|
22
|
-
|
18
|
+
remote = DBShell.new(@db_conf[:from]).export
|
19
|
+
local = DBShell.new(@db_conf[:to]).import
|
20
|
+
system "#{remote} | #{local}"
|
23
21
|
end
|
24
22
|
|
25
23
|
def play
|
26
24
|
mask(@db_conf[:to], @tasks)
|
27
25
|
end
|
28
26
|
|
27
|
+
def export
|
28
|
+
system DBShell.new(@db_conf[:to]).export(to_file = true)
|
29
|
+
end
|
30
|
+
|
31
|
+
def tmp_db_clear
|
32
|
+
return if
|
33
|
+
if @db_conf[:to][:adapter] == 'postgres'
|
34
|
+
# Force drop db while others may be connected
|
35
|
+
execute_sql 'select pg_terminate_backend(procpid)' \
|
36
|
+
" from pg_stat_activity where datname=’%{database}’" % @db_conf[:to]
|
37
|
+
end
|
38
|
+
execute_sql "DROP DATABASE IF EXISTS %{database}" % @db_conf[:to]
|
39
|
+
end
|
40
|
+
|
29
41
|
def run
|
42
|
+
tmp_db_clear
|
30
43
|
operate_db('create')
|
31
44
|
migrate
|
32
45
|
play
|
@@ -36,11 +49,8 @@ module DataMask
|
|
36
49
|
private
|
37
50
|
|
38
51
|
def build_url_without_db(data)
|
39
|
-
"%{adapter}://%{host}:%{port}" % data
|
40
|
-
|
41
|
-
|
42
|
-
def build_url(data)
|
43
|
-
"%{adapter}://%{host}:%{port}/%{database}" % data
|
52
|
+
return "%{adapter}://%{host}:%{port}" % data if data[:port]
|
53
|
+
"%{adapter}://%{host}" % data
|
44
54
|
end
|
45
55
|
|
46
56
|
def parse_mask(mask, binding)
|
@@ -52,7 +62,7 @@ module DataMask
|
|
52
62
|
end
|
53
63
|
|
54
64
|
def mask(config, tasks)
|
55
|
-
db = Sequel.connect(
|
65
|
+
db = Sequel.connect(config)
|
56
66
|
|
57
67
|
tasks.each do |table, task|
|
58
68
|
table = db[table]
|
@@ -70,5 +80,15 @@ module DataMask
|
|
70
80
|
|
71
81
|
db.disconnect
|
72
82
|
end
|
83
|
+
|
84
|
+
def execute_sql(sql)
|
85
|
+
Sequel.connect(build_url_without_db(@db_conf[:to])) do |db|
|
86
|
+
begin
|
87
|
+
db.run sql
|
88
|
+
rescue
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
73
93
|
end
|
74
94
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_mask
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- cuebyte
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,7 +71,7 @@ files:
|
|
71
71
|
- data_mask.gemspec
|
72
72
|
- lib/data_mask.rb
|
73
73
|
- lib/data_mask/config.rb
|
74
|
-
- lib/data_mask/
|
74
|
+
- lib/data_mask/db_shell.rb
|
75
75
|
- lib/data_mask/version.rb
|
76
76
|
homepage: https://github.com/cuebyte/data_mask
|
77
77
|
licenses:
|
data/lib/data_mask/migrate.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
module DataMask
|
2
|
-
module Migrate
|
3
|
-
def self.postgres(from, to)
|
4
|
-
remote = "pg_dump -h %{host} -p %{port} -U %{user} %{database}" % from
|
5
|
-
local = "psql -p %{port} %{database}" % to
|
6
|
-
system "#{remote} | #{local}"
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.mysql(from, to)
|
10
|
-
remote = "mysqldump -h %{host} -P %{port} -u %{user} %{database}" % from
|
11
|
-
local = "mysql -P %{port} %{database}" % to
|
12
|
-
system "#{remote} | #{local}"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|