sequel-schema-dot-generator 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/lib/sequel/schema/dot/generator.rb +18 -22
- data/lib/sequel/schema/dot/generator/schema_source_types/base.rb +29 -0
- data/lib/sequel/schema/dot/generator/schema_source_types/column.rb +40 -0
- data/lib/sequel/schema/dot/generator/schema_source_types/model.rb +36 -0
- data/lib/sequel/schema/dot/generator/version.rb +1 -1
- data/sequel-schema-dot-generator.gemspec +1 -1
- metadata +8 -5
@@ -1,4 +1,7 @@
|
|
1
1
|
require 'sequel/schema/dot/generator/version'
|
2
|
+
require 'sequel/schema/dot/generator/schema_source_types/base'
|
3
|
+
require 'sequel/schema/dot/generator/schema_source_types/column'
|
4
|
+
require 'sequel/schema/dot/generator/schema_source_types/model'
|
2
5
|
require 'erb'
|
3
6
|
require 'logger'
|
4
7
|
require 'sequel'
|
@@ -22,6 +25,7 @@ module Sequel
|
|
22
25
|
# :logger [Logger] optional Will be used for logging
|
23
26
|
# :dot_template_path [String] optional Template is supposed to be ERB template. Will be used as template for resulting Dot file
|
24
27
|
# :colored_associations [Boolean] optional Whether lines representing associations will be draw in color (=False)
|
28
|
+
# :schema_source_type [Symbol] optional How will be data acquired (:column|:model)
|
25
29
|
#
|
26
30
|
def initialize params
|
27
31
|
check_params params
|
@@ -30,26 +34,31 @@ module Sequel
|
|
30
34
|
@logger = params[:logger] || Logger.new(STDERR)
|
31
35
|
@dot_template_path = params[:dot_template_path] || File.join(File.dirname(__FILE__), 'generator/diagram.dot.erb')
|
32
36
|
@colored_associations = params[:colored_associations]
|
37
|
+
initialize_ss params[:schema_source_type]
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize_ss type
|
41
|
+
sst_params = @db, @db.tables
|
42
|
+
|
43
|
+
if type == :model
|
44
|
+
@schema_source = SchemaSourceType::Model.new *sst_params
|
45
|
+
end
|
46
|
+
|
47
|
+
# Column Source as fallback
|
48
|
+
@schema_source ||= SchemaSourceType::Column.new *sst_params
|
33
49
|
end
|
34
50
|
|
35
51
|
# @return [String] Content which can be passed to dot parsing tool
|
36
52
|
def generate
|
37
|
-
relations = []
|
38
53
|
tables = []
|
39
54
|
|
40
55
|
@db.tables.each do |table_name|
|
41
56
|
next if table_name == :schema_info
|
42
57
|
tables << [table_name, @db.schema(table_name)]
|
43
|
-
|
44
|
-
# foreign keys by <table>_id name format
|
45
|
-
# @todo Read it from model
|
46
|
-
@db[table_name].columns.select{|cn|cn=~/_id$/}.each do |column_name|
|
47
|
-
foreign_column = column_name.to_s
|
48
|
-
foreign_table = existing_table_name foreign_column.gsub(/_id$/, '')
|
49
|
-
relations << [foreign_table, 'id', table_name, foreign_column]
|
50
|
-
end
|
51
58
|
end
|
52
59
|
|
60
|
+
relations = @schema_source.relations
|
61
|
+
|
53
62
|
if @colored_associations
|
54
63
|
edge_colors = random_color_set relations.count
|
55
64
|
else
|
@@ -61,19 +70,6 @@ module Sequel
|
|
61
70
|
|
62
71
|
private
|
63
72
|
|
64
|
-
# @param [String] name
|
65
|
-
#
|
66
|
-
# @return [String] form of name param in which it is name of existing table
|
67
|
-
def existing_table_name name
|
68
|
-
tables = @db.tables.map(&:to_s)
|
69
|
-
table_name = name if tables.include? name
|
70
|
-
table_name ||= name.pluralize if tables.include? name.pluralize
|
71
|
-
table_name ||= name.singularize if tables.include? name.singularize
|
72
|
-
table_name ||= 'association_table_not_found'
|
73
|
-
|
74
|
-
table_name
|
75
|
-
end
|
76
|
-
|
77
73
|
# @param [Integer] number of colors we want to generate
|
78
74
|
#
|
79
75
|
# @return [Array] of random and unique colors
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Schema
|
3
|
+
module Dot
|
4
|
+
module Generator
|
5
|
+
module SchemaSourceType
|
6
|
+
class Base
|
7
|
+
# Initialize Schema Source Type
|
8
|
+
#
|
9
|
+
# Tables could be retrieved from db connection, but there is plan to provide
|
10
|
+
# control over what tables will be in output structure
|
11
|
+
#
|
12
|
+
# @param [Sequel::Database] db for which should be acquired data
|
13
|
+
# @param [Array] tables for which should be acquired data
|
14
|
+
def initialize db, tables
|
15
|
+
@tables = tables
|
16
|
+
@db = db
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Array] associations for tables given to constructor
|
20
|
+
# Each item is array of [<foreign_table>, <id>, <table_name>, <foreign_column>]
|
21
|
+
def relations
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Schema
|
3
|
+
module Dot
|
4
|
+
module Generator
|
5
|
+
module SchemaSourceType
|
6
|
+
class Column < SchemaSourceType::Base
|
7
|
+
def relations
|
8
|
+
relations = []
|
9
|
+
|
10
|
+
@tables.each do |table_name|
|
11
|
+
@db[table_name].columns.select{|cn|cn=~/_id$/}.each do |column_name|
|
12
|
+
foreign_column = column_name.to_s
|
13
|
+
foreign_table = existing_table_name foreign_column.gsub(/_id$/, '')
|
14
|
+
relations << [foreign_table, 'id', table_name, foreign_column]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
relations
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# @param [String] name
|
24
|
+
#
|
25
|
+
# @return [String] form of name param in which it is name of existing table
|
26
|
+
def existing_table_name name
|
27
|
+
tables = @db.tables.map(&:to_s)
|
28
|
+
table_name = name if tables.include? name
|
29
|
+
table_name ||= name.pluralize if tables.include? name.pluralize
|
30
|
+
table_name ||= name.singularize if tables.include? name.singularize
|
31
|
+
table_name ||= 'association_table_not_found'
|
32
|
+
|
33
|
+
table_name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Schema
|
3
|
+
module Dot
|
4
|
+
module Generator
|
5
|
+
module SchemaSourceType
|
6
|
+
class Model < SchemaSourceType::Base
|
7
|
+
def relations
|
8
|
+
relations = {}
|
9
|
+
|
10
|
+
@tables.collect do |table_name|
|
11
|
+
model = table_name.to_s.singularize.classify.constantize
|
12
|
+
model.associations.each do |assoc|
|
13
|
+
assoc_info = model.association_reflection(assoc)
|
14
|
+
|
15
|
+
right_key = assoc_info[:join_table].to_s+assoc_info[:right_key].to_s
|
16
|
+
left_key = assoc_info[:join_table].to_s+assoc_info[:left_key].to_s
|
17
|
+
|
18
|
+
if assoc_info[:type] == :many_to_many
|
19
|
+
relations[right_key] = [assoc_info[:join_table].to_s, assoc_info[:right_key], assoc_info[:class_name].constantize.implicit_table_name, assoc_info[:class_name].constantize.primary_key]
|
20
|
+
relations[left_key] = [assoc_info[:join_table].to_s, assoc_info[:left_key], assoc_info[:model].implicit_table_name, assoc_info[:model].primary_key]
|
21
|
+
else
|
22
|
+
# one_to_many
|
23
|
+
table_name = assoc_info[:model].implicit_table_name.to_s
|
24
|
+
table_key = assoc_info[:key].to_s
|
25
|
+
relations[right_key] = [table_name, table_key, assoc_info[:class_name].constantize.implicit_table_name, assoc_info[:class_name].constantize.primary_key]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
relations.values
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = %w(development@rooland.cz)
|
11
11
|
spec.description = %q{This gem makes it easier to generate database schema overview image}
|
12
12
|
spec.summary = %q{Dot language format generator for Sequel schema structure}
|
13
|
-
spec.homepage = '
|
13
|
+
spec.homepage = 'http://www.rooland.cz/sequel-schema-dot-generator-gem'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-schema-dot-generator
|
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-03-
|
12
|
+
date: 2013-03-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -90,9 +90,12 @@ files:
|
|
90
90
|
- Rakefile
|
91
91
|
- lib/sequel/schema/dot/generator.rb
|
92
92
|
- lib/sequel/schema/dot/generator/diagram.dot.erb
|
93
|
+
- lib/sequel/schema/dot/generator/schema_source_types/base.rb
|
94
|
+
- lib/sequel/schema/dot/generator/schema_source_types/column.rb
|
95
|
+
- lib/sequel/schema/dot/generator/schema_source_types/model.rb
|
93
96
|
- lib/sequel/schema/dot/generator/version.rb
|
94
97
|
- sequel-schema-dot-generator.gemspec
|
95
|
-
homepage:
|
98
|
+
homepage: http://www.rooland.cz/sequel-schema-dot-generator-gem
|
96
99
|
licenses:
|
97
100
|
- MIT
|
98
101
|
post_install_message:
|
@@ -107,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
110
|
version: '0'
|
108
111
|
segments:
|
109
112
|
- 0
|
110
|
-
hash: -
|
113
|
+
hash: -3800259323698899955
|
111
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
115
|
none: false
|
113
116
|
requirements:
|
@@ -116,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
119
|
version: '0'
|
117
120
|
segments:
|
118
121
|
- 0
|
119
|
-
hash: -
|
122
|
+
hash: -3800259323698899955
|
120
123
|
requirements: []
|
121
124
|
rubyforge_project:
|
122
125
|
rubygems_version: 1.8.25
|