mysqlaudit 0.0.2 → 0.0.3
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.
- data/README.md +0 -1
- data/bin/mysqlaudit +22 -6
- data/lib/mysqlaudit/actions.rb +18 -23
- data/lib/mysqlaudit/audit.rb +9 -14
- data/lib/mysqlaudit/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
data/bin/mysqlaudit
CHANGED
@@ -24,7 +24,11 @@ command :install do |c|
|
|
24
24
|
c.option '--schema STRING', String, 'Schema name'
|
25
25
|
c.option '--table STRING', String, 'Table name'
|
26
26
|
c.action do |args, options|
|
27
|
-
Mysqlaudit::Actions.new(
|
27
|
+
Mysqlaudit::Actions.new({host: options.host,
|
28
|
+
user: options.user,
|
29
|
+
password: options.pass,
|
30
|
+
schema: options.schema,
|
31
|
+
table: options.table}).install
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
@@ -38,7 +42,12 @@ command :uninstall do |c|
|
|
38
42
|
c.option '--table STRING', String, 'Table name'
|
39
43
|
c.option '--[no-]drop-audit-table', 'Drop audit table'
|
40
44
|
c.action do |args, options|
|
41
|
-
Mysqlaudit::Actions.new(
|
45
|
+
Mysqlaudit::Actions.new({host: options.host,
|
46
|
+
user: options.user,
|
47
|
+
password: options.pass,
|
48
|
+
schema: options.schema,
|
49
|
+
table: options.table,
|
50
|
+
drop: options.drop_audit_table}).uninstall
|
42
51
|
end
|
43
52
|
end
|
44
53
|
|
@@ -52,10 +61,17 @@ command :rollback do |c|
|
|
52
61
|
c.option '--table STRING', String, 'Table name'
|
53
62
|
c.option '--statement STRING', String, 'Statement operation: insert, update, delete'
|
54
63
|
c.action do |args, options|
|
55
|
-
if
|
56
|
-
|
57
|
-
|
58
|
-
|
64
|
+
if !options.statement.nil?
|
65
|
+
if [:insert, :update, :delete].include? options.statement.to_sym
|
66
|
+
Mysqlaudit::Actions.new({host: options.host,
|
67
|
+
user: options.user,
|
68
|
+
password: options.pass,
|
69
|
+
schema: options.schema,
|
70
|
+
table: options.table,
|
71
|
+
statement: options.statement.to_sym}).rollback
|
72
|
+
else
|
73
|
+
puts "Invalid values on argument: --statement."
|
74
|
+
end
|
59
75
|
end
|
60
76
|
end
|
61
77
|
end
|
data/lib/mysqlaudit/actions.rb
CHANGED
@@ -4,25 +4,20 @@ module Mysqlaudit
|
|
4
4
|
class Actions
|
5
5
|
@audit = nil
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
$table = table
|
16
|
-
$drop = drop
|
17
|
-
$statement = statement
|
18
|
-
|
19
|
-
@audit = Mysqlaudit::Audit.new($host, $user, $password, $schema)
|
7
|
+
def initialize(options = {})
|
8
|
+
options.delete_if { |k, v| v.nil? }
|
9
|
+
$options = {host: 'localhost', user: 'root', password: ''}.merge(options)
|
10
|
+
|
11
|
+
@audit = Mysqlaudit::Audit.new($options[:host],
|
12
|
+
$options[:user],
|
13
|
+
$options[:password],
|
14
|
+
$options[:schema])
|
20
15
|
end
|
21
16
|
|
22
17
|
def install()
|
23
18
|
@audit.create_table()
|
24
19
|
|
25
|
-
if !$table
|
20
|
+
if !$options[:table]
|
26
21
|
tables = @audit.get_tables()
|
27
22
|
tables.each do | table |
|
28
23
|
@audit.create_trigger(table, :insert)
|
@@ -30,14 +25,14 @@ module Mysqlaudit
|
|
30
25
|
@audit.create_trigger(table, :delete)
|
31
26
|
end
|
32
27
|
else
|
33
|
-
@audit.create_trigger($table, :insert)
|
34
|
-
@audit.create_trigger($table, :update)
|
35
|
-
@audit.create_trigger($table, :delete)
|
28
|
+
@audit.create_trigger($options[:table], :insert)
|
29
|
+
@audit.create_trigger($options[:table], :update)
|
30
|
+
@audit.create_trigger($options[:table], :delete)
|
36
31
|
end
|
37
32
|
end
|
38
33
|
|
39
34
|
def uninstall()
|
40
|
-
if !$table
|
35
|
+
if !$options[:table]
|
41
36
|
tables = @audit.get_tables()
|
42
37
|
tables.each do | table |
|
43
38
|
@audit.drop_trigger(table, :insert)
|
@@ -45,18 +40,18 @@ module Mysqlaudit
|
|
45
40
|
@audit.drop_trigger(table, :delete)
|
46
41
|
end
|
47
42
|
else
|
48
|
-
@audit.drop_trigger($table, :insert)
|
49
|
-
@audit.drop_trigger($table, :update)
|
50
|
-
@audit.drop_trigger($table, :delete)
|
43
|
+
@audit.drop_trigger($options[:table], :insert)
|
44
|
+
@audit.drop_trigger($options[:table], :update)
|
45
|
+
@audit.drop_trigger($options[:table], :delete)
|
51
46
|
end
|
52
47
|
|
53
|
-
if $drop
|
48
|
+
if $options[:drop]
|
54
49
|
@audit.drop_table()
|
55
50
|
end
|
56
51
|
end
|
57
52
|
|
58
53
|
def rollback()
|
59
|
-
@audit.rollback($table, $statement
|
54
|
+
@audit.rollback($options[:table], $options[:statement])
|
60
55
|
end
|
61
56
|
end
|
62
57
|
end
|
data/lib/mysqlaudit/audit.rb
CHANGED
@@ -128,41 +128,36 @@ SQL
|
|
128
128
|
@mysql.query(sql).count == 1
|
129
129
|
end
|
130
130
|
|
131
|
-
def
|
132
|
-
columns = get_columns(
|
133
|
-
key = get_primary_key(
|
131
|
+
def rollback(table, statement)
|
132
|
+
columns = get_columns(table)
|
133
|
+
key = get_primary_key(table)
|
134
134
|
|
135
135
|
case statement
|
136
136
|
when :insert
|
137
137
|
sql = <<SQL
|
138
|
-
SELECT CONCAT('/* ', trigger_at, ' */ DELETE FROM #{
|
138
|
+
SELECT CONCAT('/* ', trigger_at, ' */ DELETE FROM #{table} WHERE id = ', MAX(primary_key),';') AS `row`
|
139
139
|
FROM audits
|
140
140
|
WHERE `type` = 'I'
|
141
|
-
AND `table` = '#{
|
141
|
+
AND `table` = '#{table}'
|
142
142
|
GROUP BY primary_key
|
143
143
|
ORDER BY trigger_at, primary_key, `column` ASC;
|
144
144
|
SQL
|
145
145
|
when :delete
|
146
|
-
sql = "SELECT CONCAT('/* ', trigger_at, ' */ INSERT INTO #{
|
146
|
+
sql = "SELECT CONCAT('/* ', trigger_at, ' */ INSERT INTO #{table} (#{key}, #{columns.join(', ')}) VALUES (', primary_key, ', ', "
|
147
147
|
columns.each do | column |
|
148
148
|
sql << " QUOTE(MAX(IF(`column` = '#{column}', `old`, NULL))), ', ', "
|
149
149
|
end
|
150
150
|
sql = sql.chomp(" ', ', ")
|
151
151
|
sql << "');'"
|
152
|
-
sql << ") AS `row` FROM audits WHERE `type` = 'D' AND `table` = '#{
|
152
|
+
sql << ") AS `row` FROM audits WHERE `type` = 'D' AND `table` = '#{table}' GROUP BY primary_key ORDER BY trigger_at, primary_key, `column` ASC;"
|
153
153
|
when :update
|
154
154
|
sql = "SELECT CONCAT('/* ', trigger_at, ' */ UPDATE users SET ', `column`, ' = ', QUOTE(old), ' WHERE id = ', primary_key, ';') AS `row`"
|
155
|
-
sql << "FROM audits WHERE `type` = 'U' AND `table` = '#{
|
155
|
+
sql << "FROM audits WHERE `type` = 'U' AND `table` = '#{table}' ORDER BY trigger_at, primary_key, `column` ASC;"
|
156
156
|
end
|
157
157
|
|
158
158
|
sql_result = @mysql.query(sql)
|
159
159
|
sql_result.each(:as => :array) do |row|
|
160
160
|
puts row
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
def rollback(table, statement)
|
165
|
-
build_pivot_query(table, statement)
|
166
|
-
end
|
161
|
+
end end
|
167
162
|
end
|
168
163
|
end
|
data/lib/mysqlaudit/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysqlaudit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: MySQL tool for audit all tables with triggers.
|
15
15
|
email: nicola51980@gmail.com
|