db_meta 0.4.0 → 0.5.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 +5 -5
- data/.ruby-version +1 -0
- data/.travis.yml +33 -10
- data/.travis/oracle/download.sh +14 -0
- data/.travis/oracle/install.sh +32 -0
- data/.travis/setup_accounts.sh +8 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +45 -16
- data/README.md +1 -0
- data/Rakefile +2 -1
- data/db_meta.gemspec +15 -15
- data/docker/README.md +42 -0
- data/lib/db_meta.rb +10 -14
- data/lib/db_meta/abstract.rb +11 -21
- data/lib/db_meta/constant.rb +23 -25
- data/lib/db_meta/logger.rb +4 -7
- data/lib/db_meta/oracle/base.rb +9 -11
- data/lib/db_meta/oracle/connection.rb +3 -4
- data/lib/db_meta/oracle/helper.rb +7 -9
- data/lib/db_meta/oracle/objects.rb +45 -45
- data/lib/db_meta/oracle/oracle.rb +34 -30
- data/lib/db_meta/oracle/types/column.rb +15 -17
- data/lib/db_meta/oracle/types/comment.rb +2 -5
- data/lib/db_meta/oracle/types/constraint.rb +29 -32
- data/lib/db_meta/oracle/types/constraint_collection.rb +4 -6
- data/lib/db_meta/oracle/types/database_link.rb +4 -5
- data/lib/db_meta/oracle/types/function.rb +4 -5
- data/lib/db_meta/oracle/types/grant.rb +9 -10
- data/lib/db_meta/oracle/types/grant_collection.rb +4 -6
- data/lib/db_meta/oracle/types/index.rb +9 -11
- data/lib/db_meta/oracle/types/job.rb +2 -3
- data/lib/db_meta/oracle/types/lob.rb +2 -3
- data/lib/db_meta/oracle/types/materialized_view.rb +15 -18
- data/lib/db_meta/oracle/types/package.rb +7 -8
- data/lib/db_meta/oracle/types/package_body.rb +2 -3
- data/lib/db_meta/oracle/types/procedure.rb +4 -5
- data/lib/db_meta/oracle/types/queue.rb +24 -26
- data/lib/db_meta/oracle/types/sequence.rb +7 -8
- data/lib/db_meta/oracle/types/synonym.rb +6 -7
- data/lib/db_meta/oracle/types/synonym_collection.rb +4 -6
- data/lib/db_meta/oracle/types/table.rb +31 -34
- data/lib/db_meta/oracle/types/table_data_collection.rb +22 -18
- data/lib/db_meta/oracle/types/trigger.rb +12 -14
- data/lib/db_meta/oracle/types/type.rb +6 -7
- data/lib/db_meta/oracle/types/type_body.rb +2 -3
- data/lib/db_meta/oracle/types/view.rb +12 -14
- data/lib/db_meta/version.rb +1 -1
- metadata +27 -9
@@ -1,24 +1,23 @@
|
|
1
1
|
module DbMeta
|
2
2
|
module Oracle
|
3
3
|
class Column
|
4
|
-
|
5
4
|
attr_accessor :name, :type, :data_length, :data_precision, :data_scale, :nullable, :data_default, :comment
|
6
5
|
|
7
|
-
def initialize(args={})
|
6
|
+
def initialize(args = {})
|
8
7
|
end
|
9
8
|
|
10
9
|
def extract
|
11
|
-
buffer = "
|
10
|
+
buffer = ("%-30s" % @name).to_s
|
12
11
|
buffer << " #{convert_type}"
|
13
12
|
buffer << " DEFAULT #{@data_default.strip}" if @data_default.size > 0
|
14
|
-
|
13
|
+
buffer
|
15
14
|
end
|
16
15
|
|
17
|
-
def self.all(args={})
|
16
|
+
def self.all(args = {})
|
18
17
|
columns = []
|
19
18
|
connection = Connection.instance.get
|
20
19
|
cursor = connection.exec("select column_name, data_type, data_length, data_precision, data_scale, nullable, data_default from user_tab_columns where table_name = '#{args[:object_name]}' order by column_id")
|
21
|
-
while row = cursor.fetch
|
20
|
+
while (row = cursor.fetch)
|
22
21
|
column = Column.new(row)
|
23
22
|
column.name = row[0].to_s
|
24
23
|
column.type = row[1].to_s
|
@@ -30,7 +29,7 @@ module DbMeta
|
|
30
29
|
|
31
30
|
# column comments
|
32
31
|
cursor2 = connection.exec("select comments from user_col_comments where table_name = '#{args[:object_name]}' and column_name = '#{column.name}'")
|
33
|
-
while row2 = cursor2.fetch
|
32
|
+
while (row2 = cursor2.fetch)
|
34
33
|
column.comment = row2[0].to_s
|
35
34
|
end
|
36
35
|
cursor2.close
|
@@ -48,23 +47,22 @@ module DbMeta
|
|
48
47
|
|
49
48
|
def convert_type
|
50
49
|
case @type
|
51
|
-
when
|
52
|
-
buffer =
|
50
|
+
when "FLOAT"
|
51
|
+
buffer = @type.to_s
|
53
52
|
buffer << "(#{@data_precision})" unless @data_precision == 0
|
54
|
-
|
55
|
-
when
|
56
|
-
buffer =
|
53
|
+
buffer
|
54
|
+
when "NUMBER"
|
55
|
+
buffer = @type.to_s
|
57
56
|
buffer << "(#{@data_precision}" unless @data_precision == 0
|
58
57
|
buffer << ",#{@data_scale}" unless @data_scale == 0
|
59
58
|
buffer << ")" if buffer.include?("(")
|
60
|
-
|
59
|
+
buffer
|
61
60
|
when /CHAR|RAW/
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
"#{@type}(#{@data_length} BYTE)"
|
62
|
+
else
|
63
|
+
@type
|
65
64
|
end
|
66
65
|
end
|
67
|
-
|
68
66
|
end
|
69
67
|
end
|
70
68
|
end
|
@@ -3,18 +3,15 @@ module DbMeta
|
|
3
3
|
class Comment
|
4
4
|
attr_reader :text
|
5
5
|
|
6
|
-
def self.find(args={})
|
7
|
-
|
6
|
+
def self.find(args = {})
|
8
7
|
connection = Connection.instance.get
|
9
8
|
cursor = connection.exec("select comments from user_tab_comments where table_type = '#{args[:type]}' and table_name = '#{args[:name]}'")
|
10
|
-
while row = cursor.fetch
|
9
|
+
while (row = cursor.fetch)
|
11
10
|
@text = row[0]
|
12
11
|
end
|
13
|
-
|
14
12
|
ensure
|
15
13
|
connection.logoff
|
16
14
|
end
|
17
|
-
|
18
15
|
end
|
19
16
|
end
|
20
17
|
end
|
@@ -1,29 +1,29 @@
|
|
1
1
|
module DbMeta
|
2
2
|
module Oracle
|
3
3
|
class Constraint < Base
|
4
|
-
register_type(
|
4
|
+
register_type("CONSTRAINT")
|
5
5
|
|
6
6
|
attr_reader :constraint_type, :table_name, :search_condition, :referential_constraint, :delete_rule, :columns
|
7
7
|
|
8
|
-
def initialize(args={})
|
8
|
+
def initialize(args = {})
|
9
9
|
super(args)
|
10
10
|
|
11
11
|
@extract_type = :embedded
|
12
12
|
@columns = []
|
13
13
|
end
|
14
14
|
|
15
|
-
def fetch(args={})
|
15
|
+
def fetch(args = {})
|
16
16
|
connection = Connection.instance.get
|
17
17
|
cursor = connection.exec("select * from user_constraints where constraint_name = '#{@name}'")
|
18
18
|
cursor.fetch_hash do |item|
|
19
|
-
@constraint_type = translate_constraint_type(item[
|
20
|
-
@extract_type = :merged if @constraint_type ==
|
21
|
-
@table_name = item[
|
22
|
-
@search_condition = item[
|
23
|
-
@delete_rule = item[
|
24
|
-
|
25
|
-
if @constraint_type ==
|
26
|
-
constraint = Constraint.new(
|
19
|
+
@constraint_type = translate_constraint_type(item["CONSTRAINT_TYPE"])
|
20
|
+
@extract_type = :merged if @constraint_type == "FOREIGN KEY"
|
21
|
+
@table_name = item["TABLE_NAME"]
|
22
|
+
@search_condition = item["SEARCH_CONDITION"]
|
23
|
+
@delete_rule = item["DELETE_RULE"]
|
24
|
+
|
25
|
+
if @constraint_type == "FOREIGN KEY"
|
26
|
+
constraint = Constraint.new("OBJECT_TYPE" => "CONSTRAINT", "OBJECT_NAME" => item["R_CONSTRAINT_NAME"])
|
27
27
|
constraint.fetch
|
28
28
|
@referential_constraint = constraint
|
29
29
|
end
|
@@ -33,59 +33,56 @@ module DbMeta
|
|
33
33
|
# get affected columns
|
34
34
|
cursor = connection.exec("select * from user_cons_columns where constraint_name = '#{@name}' order by position")
|
35
35
|
cursor.fetch_hash do |item|
|
36
|
-
@columns << item[
|
36
|
+
@columns << item["COLUMN_NAME"]
|
37
37
|
end
|
38
38
|
cursor.close
|
39
|
-
|
40
39
|
ensure
|
41
40
|
connection.logoff
|
42
41
|
end
|
43
42
|
|
44
|
-
|
45
|
-
def extract(args={})
|
43
|
+
def extract(args = {})
|
46
44
|
buffer = []
|
47
45
|
buffer << "ALTER TABLE #{@table_name} ADD ("
|
48
46
|
buffer << " CONSTRAINT #{@name}"
|
49
47
|
|
50
48
|
case @constraint_type
|
51
|
-
when
|
49
|
+
when "CHECK"
|
52
50
|
buffer << " #{@constraint_type} (#{@search_condition})"
|
53
|
-
when
|
54
|
-
buffer << " #{@constraint_type} (#{@columns.join(
|
55
|
-
buffer << " REFERENCES #{@referential_constraint.table_name} (#{@referential_constraint.columns.join(
|
51
|
+
when "FOREIGN KEY"
|
52
|
+
buffer << " #{@constraint_type} (#{@columns.join(", ")})"
|
53
|
+
buffer << " REFERENCES #{@referential_constraint.table_name} (#{@referential_constraint.columns.join(", ")})"
|
56
54
|
else
|
57
|
-
buffer << " #{@constraint_type} (#{@columns.join(
|
55
|
+
buffer << " #{@constraint_type} (#{@columns.join(", ")})"
|
58
56
|
end
|
59
57
|
|
60
|
-
buffer << " ON DELETE CASCADE" if @delete_rule ==
|
58
|
+
buffer << " ON DELETE CASCADE" if @delete_rule == "CASCADE"
|
61
59
|
buffer << " ENABLE VALIDATE"
|
62
60
|
buffer << ");"
|
63
61
|
|
64
|
-
(0..buffer.size-1).each { |n|
|
62
|
+
(0..buffer.size - 1).each { |n| buffer[n] = ("-- " + buffer[n]) } if args[:comment] == true
|
65
63
|
|
66
64
|
buffer << nil
|
67
65
|
buffer.join("\n")
|
68
66
|
end
|
69
67
|
|
70
68
|
def self.sort_value(type)
|
71
|
-
[
|
69
|
+
["PRIMARY KEY", "FOREIGN KEY", "UNIQUE", "CHECK"].index(type)
|
72
70
|
end
|
73
71
|
|
74
72
|
private
|
75
73
|
|
76
74
|
def translate_constraint_type(type)
|
77
75
|
case type
|
78
|
-
when
|
79
|
-
|
80
|
-
when
|
81
|
-
|
82
|
-
when
|
83
|
-
|
84
|
-
when
|
85
|
-
|
76
|
+
when "P"
|
77
|
+
"PRIMARY KEY"
|
78
|
+
when "U"
|
79
|
+
"UNIQUE"
|
80
|
+
when "C"
|
81
|
+
"CHECK"
|
82
|
+
when "R"
|
83
|
+
"FOREIGN KEY"
|
86
84
|
end
|
87
85
|
end
|
88
|
-
|
89
86
|
end
|
90
87
|
end
|
91
88
|
end
|
@@ -5,7 +5,7 @@ module DbMeta
|
|
5
5
|
|
6
6
|
attr_reader :name, :type, :status, :extract_type, :collection
|
7
7
|
|
8
|
-
def initialize(args={})
|
8
|
+
def initialize(args = {})
|
9
9
|
@name = args[:name]
|
10
10
|
@type = args[:type]
|
11
11
|
@status = :valid
|
@@ -21,10 +21,10 @@ module DbMeta
|
|
21
21
|
@collection << object
|
22
22
|
end
|
23
23
|
|
24
|
-
def extract(args={})
|
24
|
+
def extract(args = {})
|
25
25
|
buffer = [block(@name)]
|
26
26
|
title = nil
|
27
|
-
@collection.sort_by{ |o| [o.table_name, o.name]}.each do |object|
|
27
|
+
@collection.sort_by { |o| [o.table_name, o.name] }.each do |object|
|
28
28
|
buffer << block(object.table_name, 40) if title != object.table_name
|
29
29
|
buffer << object.extract(args)
|
30
30
|
title = object.table_name
|
@@ -33,14 +33,12 @@ module DbMeta
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def ddl_drop
|
36
|
-
|
36
|
+
"-- will automatically be dropped with table object"
|
37
37
|
end
|
38
38
|
|
39
39
|
def system_object?
|
40
40
|
false
|
41
41
|
end
|
42
|
-
|
43
42
|
end
|
44
|
-
|
45
43
|
end
|
46
44
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module DbMeta
|
2
2
|
module Oracle
|
3
3
|
class DatabaseLink < Base
|
4
|
-
register_type(
|
4
|
+
register_type("DATABASE LINK")
|
5
5
|
|
6
6
|
attr_reader :username, :password, :host
|
7
7
|
|
8
|
-
def fetch(args={})
|
8
|
+
def fetch(args = {})
|
9
9
|
connection = Connection.instance.get
|
10
10
|
cursor = connection.exec("select username, password, host from user_db_links where db_link = '#{@name}'")
|
11
|
-
while row = cursor.fetch
|
11
|
+
while (row = cursor.fetch)
|
12
12
|
@username = row[0].to_s
|
13
13
|
@password = row[1].to_s
|
14
14
|
@host = row[2].to_s
|
@@ -18,7 +18,7 @@ module DbMeta
|
|
18
18
|
connection.logoff
|
19
19
|
end
|
20
20
|
|
21
|
-
def extract(args={})
|
21
|
+
def extract(args = {})
|
22
22
|
buffer = []
|
23
23
|
buffer << "CREATE DATABASE LINK #{@name}"
|
24
24
|
buffer << " CONNECT TO #{@username}"
|
@@ -27,7 +27,6 @@ module DbMeta
|
|
27
27
|
buffer << nil
|
28
28
|
buffer.join("\n")
|
29
29
|
end
|
30
|
-
|
31
30
|
end
|
32
31
|
end
|
33
32
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module DbMeta
|
2
2
|
module Oracle
|
3
3
|
class Function < Base
|
4
|
-
register_type(
|
4
|
+
register_type("FUNCTION")
|
5
5
|
|
6
6
|
attr_reader :source
|
7
7
|
|
@@ -9,7 +9,7 @@ module DbMeta
|
|
9
9
|
@source = ""
|
10
10
|
connection = Connection.instance.get
|
11
11
|
cursor = connection.exec("select text from user_source where type = 'FUNCTION' and name = '#{@name}' order by line")
|
12
|
-
while row = cursor.fetch
|
12
|
+
while (row = cursor.fetch)
|
13
13
|
@source << row[0].to_s
|
14
14
|
end
|
15
15
|
cursor.close
|
@@ -17,14 +17,13 @@ module DbMeta
|
|
17
17
|
connection.logoff
|
18
18
|
end
|
19
19
|
|
20
|
-
def extract(args={})
|
20
|
+
def extract(args = {})
|
21
21
|
buffer = [block(@name)]
|
22
22
|
buffer << "create or replace #{@source.strip}"
|
23
|
-
buffer <<
|
23
|
+
buffer << "/"
|
24
24
|
buffer << nil
|
25
25
|
buffer.join("\n")
|
26
26
|
end
|
27
|
-
|
28
27
|
end
|
29
28
|
end
|
30
29
|
end
|
@@ -1,25 +1,25 @@
|
|
1
1
|
module DbMeta
|
2
2
|
module Oracle
|
3
3
|
class Grant < Base
|
4
|
-
register_type(
|
4
|
+
register_type("GRANT")
|
5
5
|
|
6
6
|
attr_reader :grantee, :owner, :table_name, :grantor, :privilege, :grantable
|
7
7
|
|
8
|
-
def initialize(args={})
|
8
|
+
def initialize(args = {})
|
9
9
|
super(args)
|
10
10
|
@extract_type = :merged
|
11
11
|
end
|
12
12
|
|
13
|
-
def fetch(args={})
|
13
|
+
def fetch(args = {})
|
14
14
|
# definition is comma seperated in the name to prevent re-fetching table for every grant
|
15
|
-
@grantee, @owner, @table_name, @grantor, @privilege, @grantable = @name.split(
|
15
|
+
@grantee, @owner, @table_name, @grantor, @privilege, @grantable = @name.split(",")
|
16
16
|
end
|
17
17
|
|
18
|
-
def extract(args={})
|
18
|
+
def extract(args = {})
|
19
19
|
buffer = ""
|
20
|
-
buffer << (
|
20
|
+
buffer << ("%-30s" % "-- granted via #{@grantor}: ") if external_grant?
|
21
21
|
buffer << "GRANT #{"%-18s" % @privilege} ON #{"%-32s" % @table_name} TO #{@grantee}"
|
22
|
-
buffer << " WITH GRANT OPTION" if @grantable ==
|
22
|
+
buffer << " WITH GRANT OPTION" if @grantable == "YES"
|
23
23
|
buffer << ";"
|
24
24
|
buffer
|
25
25
|
end
|
@@ -27,7 +27,7 @@ module DbMeta
|
|
27
27
|
def ddl_drop
|
28
28
|
buffer = ""
|
29
29
|
|
30
|
-
buffer << (
|
30
|
+
buffer << ("%-30s" % "-- granted via #{@grantor}: ") if external_grant?
|
31
31
|
buffer << "REVOKE #{"%-18s" % @privilege} ON #{"%-32s" % @table_name} FROM #{@grantee};"
|
32
32
|
buffer
|
33
33
|
end
|
@@ -38,9 +38,8 @@ module DbMeta
|
|
38
38
|
|
39
39
|
def sort_value
|
40
40
|
return ["2", @grantor, @privilege, @table_name] if external_grant?
|
41
|
-
|
41
|
+
["1", @grantee, @privilege, @table_name]
|
42
42
|
end
|
43
|
-
|
44
43
|
end
|
45
44
|
end
|
46
45
|
end
|
@@ -5,7 +5,7 @@ module DbMeta
|
|
5
5
|
|
6
6
|
attr_reader :name, :type, :status, :extract_type, :collection
|
7
7
|
|
8
|
-
def initialize(args={})
|
8
|
+
def initialize(args = {})
|
9
9
|
@name = args[:name]
|
10
10
|
@type = args[:type]
|
11
11
|
@status = :valid
|
@@ -21,22 +21,20 @@ module DbMeta
|
|
21
21
|
@collection << object
|
22
22
|
end
|
23
23
|
|
24
|
-
def extract(args={})
|
24
|
+
def extract(args = {})
|
25
25
|
buffer = [block(@name)]
|
26
|
-
buffer << @collection.map{ |o| o.extract(args) }
|
26
|
+
buffer << @collection.map { |o| o.extract(args) }
|
27
27
|
buffer << nil
|
28
28
|
buffer.join("\n")
|
29
29
|
end
|
30
30
|
|
31
31
|
def ddl_drop
|
32
|
-
@collection.reverse_each.map{ |o| o.ddl_drop }.join("\n")
|
32
|
+
@collection.reverse_each.map { |o| o.ddl_drop }.join("\n")
|
33
33
|
end
|
34
34
|
|
35
35
|
def system_object?
|
36
36
|
false
|
37
37
|
end
|
38
|
-
|
39
38
|
end
|
40
|
-
|
41
39
|
end
|
42
40
|
end
|
@@ -1,21 +1,21 @@
|
|
1
1
|
module DbMeta
|
2
2
|
module Oracle
|
3
3
|
class Index < Base
|
4
|
-
register_type(
|
4
|
+
register_type("INDEX")
|
5
5
|
|
6
6
|
attr_reader :index_type, :table_name, :uniqueness, :tablespace
|
7
7
|
|
8
|
-
def initialize(args={})
|
8
|
+
def initialize(args = {})
|
9
9
|
super(args)
|
10
10
|
|
11
11
|
@extract_type = :embedded
|
12
12
|
@columns = []
|
13
13
|
end
|
14
14
|
|
15
|
-
def fetch(args={})
|
15
|
+
def fetch(args = {})
|
16
16
|
connection = Connection.instance.get
|
17
17
|
cursor = connection.exec("select index_type, table_name, uniqueness, tablespace_name from user_indexes where index_name = '#{@name}'")
|
18
|
-
while row = cursor.fetch
|
18
|
+
while (row = cursor.fetch)
|
19
19
|
@index_type = row[0].to_s
|
20
20
|
@table_name = row[1].to_s
|
21
21
|
@uniqueness = row[2].to_s
|
@@ -25,27 +25,25 @@ module DbMeta
|
|
25
25
|
|
26
26
|
# involved columns
|
27
27
|
cursor = connection.exec("select column_name from user_ind_columns where index_name = '#{@name}' order by column_position")
|
28
|
-
while row = cursor.fetch
|
28
|
+
while (row = cursor.fetch)
|
29
29
|
@columns << row[0].to_s
|
30
30
|
end
|
31
31
|
cursor.close
|
32
32
|
|
33
33
|
# columns for function based indexs
|
34
34
|
cursor = connection.exec("select column_expression, column_position from user_ind_expressions where index_name = '#{@name}' order by column_position")
|
35
|
-
while row = cursor.fetch
|
35
|
+
while (row = cursor.fetch)
|
36
36
|
idx = row[1].to_i - 1
|
37
|
-
@columns[idx] = row[0]
|
37
|
+
@columns[idx] = row[0] # replace sys_... entry
|
38
38
|
end
|
39
39
|
cursor.close
|
40
|
-
|
41
40
|
ensure
|
42
41
|
connection.logoff
|
43
42
|
end
|
44
43
|
|
45
|
-
def extract(args={})
|
46
|
-
"CREATE#{
|
44
|
+
def extract(args = {})
|
45
|
+
"CREATE#{@uniqueness == "UNIQUE" ? " UNIQUE" : nil} INDEX #{@name} ON #{@table_name}(#{@columns.join(", ")});"
|
47
46
|
end
|
48
|
-
|
49
47
|
end
|
50
48
|
end
|
51
49
|
end
|