schemadoc 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8be2a5b6a3d0335eb86a1abd2093775456b169da
4
- data.tar.gz: 9ba48b2c07fff20c4656cba60ff81110e5e6d298
3
+ metadata.gz: f06e6c98d47ee913781b80f3c648d29fbb5a773b
4
+ data.tar.gz: 491654a1965027fbb7505b76156c51cb32943e66
5
5
  SHA512:
6
- metadata.gz: 026db4ef5fe8f33185e224e54e3a965ed9d9a36ff5ffe6b1a872c7f61b458169d2f4a2850aef79e886970b9d5e97c1d528b4cd3cebdefd3c5c569c4d1b3badf4
7
- data.tar.gz: 39f6197348498ed3a7128570b2fd4c9637b1eab005058204df47e8e1d26dcd1f12197f92056beb415bb9f92e8f65420e0c701b836a9a5572e0beeaf8bea5fa27
6
+ metadata.gz: 2620d6fff436224317dbd625c80780236dac30e5a9a451ba05d536687716e267d5aad198a18272189a01e32cee553fa1eb2afaf7fa52fe92a9f8ef39e0f12bea
7
+ data.tar.gz: ff27127fa5f0e0f7347ce9da1f292db9fbc146b337f6ab57de7d4011ae46156bd0b312a5a68a81d1a59cbd15ad611b9b4175a97b1417b3191d0b0a2a30418803
data/.gemtest ADDED
File without changes
data/Manifest.txt CHANGED
@@ -2,5 +2,10 @@ HISTORY.md
2
2
  Manifest.txt
3
3
  README.md
4
4
  Rakefile
5
+ bin/schemadoc
5
6
  lib/schemadoc.rb
7
+ lib/schemadoc/cli/opts.rb
8
+ lib/schemadoc/cli/runner.rb
6
9
  lib/schemadoc/version.rb
10
+ lib/schemadoc/worker.rb
11
+ test/config/football.yml
data/Rakefile CHANGED
@@ -17,6 +17,11 @@ Hoe.spec 'schemadoc' do
17
17
  self.readme_file = 'README.md'
18
18
  self.history_file = 'HISTORY.md'
19
19
 
20
+ self.extra_deps = [
21
+ ['logutils'],
22
+ ['fetcher']
23
+ ]
24
+
20
25
  self.licenses = ['Public Domain']
21
26
 
22
27
  self.spec_extras = {
data/bin/schemadoc ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ###################
4
+ # == DEV TIPS:
5
+ #
6
+ # For local testing run like:
7
+ #
8
+ # ruby -Ilib bin/schemadoc
9
+ #
10
+ # Set the executable bit in Linux. Example:
11
+ #
12
+ # % chmod a+x bin/schemadoc
13
+ #
14
+
15
+ require 'schemadoc'
16
+
17
+ SchemaDoc.main
18
+
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ module SchemaDoc
5
+
6
+ class Opts
7
+
8
+ def config_name=(value)
9
+ @config_name = value
10
+ end
11
+
12
+ def config_name
13
+ @config_name || 'schemadoc.yml'
14
+ end
15
+
16
+ def config_path=(value)
17
+ @config_path = value
18
+ end
19
+
20
+ def config_path
21
+ @config_path || '.'
22
+ end
23
+
24
+
25
+ def output_path=( value )
26
+ @output_path = value
27
+ end
28
+
29
+ def output_path
30
+ @output_path || '.'
31
+ end
32
+
33
+ end # class Opts
34
+
35
+ end # module SchemaDoc
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ # more core and stlibs
5
+
6
+ require 'optparse'
7
+
8
+ # our own code (for command line interface/cli)
9
+
10
+ require 'schemadoc/cli/opts'
11
+
12
+
13
+ module SchemaDoc
14
+
15
+ class Runner
16
+
17
+ include LogUtils::Logging
18
+
19
+ attr_reader :opts
20
+
21
+ def initialize
22
+ @opts = Opts.new
23
+ end
24
+
25
+ def run( args )
26
+ opt=OptionParser.new do |cmd|
27
+
28
+ cmd.banner = "Usage: schemadoc [options]"
29
+
30
+ ### use -c and --config / why? why not??
31
+ ### use -n and --name / why? why not??
32
+
33
+ cmd.on( '-f', '--file NAME', "Configuration name (default is '#{opts.config_name}')" ) do |s|
34
+ opts.config_name = s
35
+ end
36
+ cmd.on( '-d', '--dir PATH', "Configuration path (default is '#{opts.config_path}')" ) do |s|
37
+ opts.config_path = s
38
+ end
39
+
40
+ cmd.on( '-o', '--output PATH', "Output path (default is '#{opts.output_path}')" ) do |s|
41
+ opts.output_path = s
42
+ end
43
+
44
+ # todo: find different letter for debug trace switch (use v for version?)
45
+ cmd.on( '-v', '--verbose', 'Show debug trace' ) do
46
+ logger = LogUtils::Logger.root
47
+ logger.level = :debug
48
+ end
49
+
50
+ usage =<<EOS
51
+
52
+ schemadoc #{VERSION} - Lets you document your database tables, columns, etc.
53
+
54
+ #{cmd.help}
55
+
56
+ Examples:
57
+ schemadoc
58
+ schemadoc -f football.yml
59
+
60
+ Further information:
61
+ https://github.com/rubylibs/schemadoc
62
+
63
+ EOS
64
+
65
+ ## todo: also add -? if possible as alternative
66
+ cmd.on_tail( '-h', '--help', 'Show this message' ) do
67
+ puts usage
68
+ exit
69
+ end
70
+ end
71
+
72
+ opt.parse!( args )
73
+
74
+ puts SchemaDoc.banner
75
+
76
+ config = YAML.load_file( "#{opts.config_path}/#{opts.config_name}" )
77
+ pp config
78
+
79
+ worker = Worker.new( config ).run()
80
+
81
+ puts 'Done.'
82
+
83
+ end # method run
84
+
85
+ end # class Runner
86
+
87
+ end # module SchemaDoc
88
+
@@ -3,8 +3,8 @@
3
3
  module SchemaDoc
4
4
 
5
5
  MAJOR = 0
6
- MINOR = 0
7
- PATCH = 1
6
+ MINOR = 1
7
+ PATCH = 0
8
8
  VERSION = [MAJOR,MINOR,PATCH].join('.')
9
9
 
10
10
  def self.version
@@ -0,0 +1,180 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # generate
5
+ # json
6
+ # for schema doc
7
+ # (see simple datasets package schema from okfn ???)
8
+
9
+
10
+ module SchemaDoc
11
+
12
+ class Worker
13
+
14
+ def initialize( config )
15
+ ## split into db config (for connection) and
16
+ ## schemadoc config
17
+
18
+ @config = {}
19
+ @db_config = {}
20
+
21
+ config.each do |k,v|
22
+ if k == 'database'
23
+ @db_config = v # note: discard key; use hash as connection spec
24
+ else
25
+ @config[ k ] = v
26
+ end
27
+ end
28
+
29
+ puts "database connection spec:"
30
+ pp @db_config
31
+ end
32
+
33
+
34
+ def connect
35
+ @con = AbstractModel.connection_for( @db_config )
36
+ end
37
+
38
+ def dump_schema
39
+ @con.tables.sort.map do |name|
40
+ puts "#{name} : #{name.class.name}"
41
+ end
42
+ puts ''
43
+
44
+ @con.tables.sort.each do |name|
45
+ puts "#{name}"
46
+ puts "-" * name.size
47
+
48
+ @con.columns( name ).each do |col|
49
+ puts " #{col.name} #{col.sql_type}, #{col.default}, #{col.null} : #{col.class.name}"
50
+ end
51
+ puts ''
52
+ end
53
+ end
54
+
55
+ def build_schema
56
+ ####
57
+ # build schema hash
58
+
59
+ schemas = {}
60
+
61
+ @con.tables.sort.each do |name|
62
+
63
+ t = { name: name,
64
+ columns: []
65
+ }
66
+ @con.columns( name ).each do |col|
67
+ t[:columns] << {
68
+ name: col.name,
69
+ type: col.sql_type,
70
+ default: col.default,
71
+ null: col.null
72
+ }
73
+ end
74
+
75
+ schema_name = find_schema_for_table(name)
76
+ puts "add '#{name}' to schema '#{schema_name}'"
77
+
78
+ schema = schemas[schema_name]
79
+ if schema.nil?
80
+ # note: use schema_name from config (do NOT use key - might be different)
81
+ schema = { name: schema_name, tables: [] }
82
+ schemas[schema_name] = schema
83
+ end
84
+ schema[:tables] << t
85
+ end
86
+
87
+ data = { schemas: [] }
88
+ schemas.each do |k,v|
89
+ data[:schemas] << v ## turn schemas into an array w/ name, tables, etc.
90
+ end
91
+
92
+ data
93
+ end
94
+
95
+
96
+ def build_index
97
+ ####
98
+ # build symbol index hash
99
+
100
+ symbols = {}
101
+
102
+ letters = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
103
+
104
+ ## add a to z [26 entries]
105
+
106
+ letters.each do |letter|
107
+ symbols[letter] = {
108
+ name: letter,
109
+ tables: [],
110
+ columns: []
111
+ }
112
+ end
113
+
114
+ @con.tables.sort.each do |name|
115
+
116
+ table_key = name[0].upcase
117
+ symbols[table_key][:tables] << name
118
+
119
+ @con.columns( name ).each do |col|
120
+ col_key = col.name[0].upcase
121
+ symbols[col_key][:columns] << "#{col.name} in #{name}"
122
+ end
123
+ end
124
+
125
+ data = []
126
+ symbols.each do |k,v|
127
+ data << v # turn data json into an array of letters (ever letter is a hash w/ name,tables,columns,etc.)
128
+ end
129
+
130
+ data
131
+ end
132
+
133
+
134
+ def run( opts={} )
135
+ connect()
136
+ dump_schema()
137
+
138
+ schema = build_schema()
139
+ index = build_index()
140
+
141
+ ## pp schema
142
+
143
+ File.open( 'database.json', 'w') do |f|
144
+ f.write JSON.pretty_generate( schema )
145
+ end
146
+
147
+ File.open( 'symbols.json', 'w') do |f|
148
+ f.write JSON.pretty_generate( index )
149
+ end
150
+ end
151
+
152
+
153
+ private
154
+
155
+ class AbstractModel < ActiveRecord::Base
156
+ self.abstract_class = true # no table; class just used for getting db connection
157
+
158
+ def self.connection_for( key_or_spec )
159
+ establish_connection( key_or_spec )
160
+ connection
161
+ end
162
+ end # class AbstractModel
163
+
164
+
165
+ def find_schema_for_table( name )
166
+
167
+ @config.each do |k,h|
168
+ tables = h['tables'] || []
169
+ if tables.include?( name )
170
+ return k
171
+ end
172
+ end
173
+
174
+ ## no entry found; assume default schema (e.g. first schema listed in config)
175
+ @config.keys.first
176
+ end
177
+
178
+ end
179
+
180
+ end # module SchemaDoc
data/lib/schemadoc.rb CHANGED
@@ -1,9 +1,32 @@
1
1
  # encoding: utf-8
2
2
 
3
+ # stdlibs
4
+
5
+ require 'pp'
6
+ require 'yaml'
7
+ require 'json'
8
+
9
+ # 3rd party gems/libs
10
+ require 'logutils'
11
+ require 'active_record'
12
+
13
+
3
14
  # our own code
4
15
 
5
16
  require 'schemadoc/version' # let it always go first
17
+ require 'schemadoc/worker'
18
+
19
+
20
+ module SchemaDoc
21
+
22
+ def self.main
23
+ ## NB: only load (require) cli code if called
24
+ require 'schemadoc/cli/runner'
25
+
26
+ Runner.new.run( ARGV )
27
+ end
6
28
 
29
+ end # module SchemaDoc
7
30
 
8
31
 
9
32
 
@@ -0,0 +1,42 @@
1
+ ###################
2
+ # connection spec
3
+
4
+ database:
5
+ adapter: sqlite3
6
+ database: ./football.db
7
+
8
+
9
+ ###############
10
+ # main tables
11
+
12
+ football:
13
+ name: Football
14
+
15
+
16
+ ############
17
+ # world tables
18
+
19
+ world:
20
+ name: World
21
+ tables:
22
+ - continents
23
+ - countries
24
+ - regions
25
+ - cities
26
+ - places
27
+ - names
28
+ - langs
29
+ - usages
30
+
31
+ ##########
32
+ # support tables
33
+
34
+ support:
35
+ name: Support
36
+ tables:
37
+ - logs
38
+ - props
39
+ - tags
40
+ - taggings
41
+
42
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemadoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
@@ -10,6 +10,34 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logutils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fetcher
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: rdoc
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -40,19 +68,26 @@ dependencies:
40
68
  version: '3.13'
41
69
  description: schemadoc - document your database schemas (tables, columns, etc.)
42
70
  email: opensport@googlegroups.com
43
- executables: []
71
+ executables:
72
+ - schemadoc
44
73
  extensions: []
45
74
  extra_rdoc_files:
46
75
  - HISTORY.md
47
76
  - Manifest.txt
48
77
  - README.md
49
78
  files:
79
+ - ".gemtest"
50
80
  - HISTORY.md
51
81
  - Manifest.txt
52
82
  - README.md
53
83
  - Rakefile
84
+ - bin/schemadoc
54
85
  - lib/schemadoc.rb
86
+ - lib/schemadoc/cli/opts.rb
87
+ - lib/schemadoc/cli/runner.rb
55
88
  - lib/schemadoc/version.rb
89
+ - lib/schemadoc/worker.rb
90
+ - test/config/football.yml
56
91
  homepage: https://github.com/rubylibs/schemadoc
57
92
  licenses:
58
93
  - Public Domain