db_discovery_generator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
File without changes
data/README ADDED
@@ -0,0 +1,3 @@
1
+ README for db_discovery
2
+ =======================
3
+
data/USAGE ADDED
@@ -0,0 +1,19 @@
1
+ Description:
2
+ The db_discovery generator creates stub for a new models based on
3
+ information discovered from DB.
4
+
5
+ The generator creates a model classes in app/models, a test suites
6
+ in test/unit, test fixtures in test/fixtures/singular_name.yml,
7
+ and a migrations in db/migrate.
8
+
9
+ Example:
10
+ ./script/generate db_discovery --no-fields
11
+
12
+ This will create an discovered models:
13
+ Model: app/models/<table name>.rb
14
+ Test: test/unit/<table name>_test.rb
15
+ Fixtures: test/fixtures/<table name>s.yml
16
+ Migration: db/migrate/XXX_add_<table name>s.rb
17
+
18
+ If --no-fields is provided then table's available field
19
+ definitions will be skipped
@@ -0,0 +1,110 @@
1
+ class DbDiscoveryGenerator < Rails::Generator::Base
2
+ default_options :skip_migration => false
3
+
4
+ def initialize(runtime_args, runtime_options = {})
5
+ super
6
+ if options[:help] then
7
+ usage
8
+ exit
9
+ end
10
+ @no_fields = options['no_fields']
11
+ end
12
+
13
+ def manifest
14
+ record do |m|
15
+ m.directory 'app/models'
16
+
17
+ generate_models.each do
18
+ |table, model|
19
+ m.template 'model.rb',
20
+ "app/models/#{table.singularize.underscore}.rb",
21
+ :assigns => {
22
+ :table_name => table.singularize.underscore.camelize,
23
+ :model => model,
24
+ :no_fields => @no_fields
25
+ }
26
+ end
27
+ end
28
+ end
29
+
30
+ protected
31
+ def add_options!(opt)
32
+ opt.separator ''
33
+ opt.separator 'Options:'
34
+ opt.on("--no-fields",
35
+ "Don't show a list of available fields") { |v| options[:no_fields] = v }
36
+ opt.on("-h","--help",
37
+ "Show this help message") { |v| options[:help] = v }
38
+ end
39
+
40
+ def mysql_primary_key (table_name, name = nil)#:nodoc:
41
+ sql = "SHOW FIELDS FROM #{table_name}"
42
+ pk = []
43
+ @connection.execute(sql, name).each do
44
+ |field|
45
+ pk << field[0] if field[3] == 'PRI'
46
+ end
47
+ if pk.length > 1 then
48
+ ''
49
+ else
50
+ pk[0]
51
+ end
52
+ end
53
+
54
+
55
+ def generate_models
56
+ @connection = ActiveRecord::Base.connection
57
+ models = {}
58
+
59
+ @connection.tables.each do
60
+ | table |
61
+ # Model class, unit test, and fixtures.
62
+
63
+ if table =~ /[A-Z]/ then
64
+ set_table_name = " self.table_name = %( #{@connection.quote_column_name(table)} )"
65
+ else
66
+ set_table_name = " self.table_name = \"#{table}\""
67
+ end
68
+
69
+ columns = @connection.columns(@connection.quote_column_name(table))
70
+ adapterName = @connection.adapter_name
71
+ pk = ''
72
+ case adapterName
73
+ when 'PostgreSQL' then
74
+ pk, seq = @connection.pk_and_sequence_for(@connection.quote_column_name(table))
75
+ pk ||= ''
76
+ when 'MySQL' then
77
+ pk = mysql_primary_key(@connection.quote_column_name(table))
78
+ end
79
+
80
+
81
+ models[table] = {
82
+ :table => table,
83
+ :columns => columns,
84
+ :primary_key => pk,
85
+ :table_name => set_table_name
86
+ }
87
+ end
88
+
89
+ models.each do
90
+ |table, model|
91
+ model[:columns].each do
92
+ |c|
93
+ next if c.name.nil?
94
+ next if model[:primary_key] && c.name == model[:primary_key]
95
+ next if c.name !~ /^(.+)(?:_id|ID)$/
96
+ master = $1
97
+ master_model = models[master.pluralize] || models[master]
98
+ if master_model then
99
+ model[:belongs_to] = [] if model[:belongs_to].nil?
100
+ model[:belongs_to] << master.singularize.underscore
101
+ # TODO: Distinguish between has_one and has_many
102
+ # TODO: HABTM - hint (relation)_(relation)
103
+ master_model[:has_many] = [] if master_model[:has_many].nil?
104
+ master_model[:has_many] << table.pluralize.underscore
105
+ end
106
+ end
107
+ end
108
+ models
109
+ end
110
+ end
@@ -0,0 +1,5 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ first:
3
+ id: 1
4
+ another:
5
+ id: 2
@@ -0,0 +1,11 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ # t.column :name, :string
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :<%= table_name %>
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ class <%= model[:table].camelize %> < ActiveRecord::Base
2
+ <%= model[:table_name] %>
3
+ self.primary_key = "<%= model[:primary_key] %>"
4
+
5
+ <% model[:belongs_to].each do |b_t| -%>
6
+ belongs_to :<%= b_t %>
7
+ <% end if model[:belongs_to] -%>
8
+
9
+ <% model[:has_many].each do |h_m| -%>
10
+ has_many :<%= h_m %>
11
+ <% end if model[:has_many] -%>
12
+
13
+ <% if no_fields.nil? then -%>
14
+ # Fields information, just FYI.
15
+ #
16
+ <% model[:columns].each do |c| -%>
17
+ # Field: <%= c.name =~ /[A-Z]/ ? ActiveRecord::Base.connection.quote_column_name(c.name) : c.name %> <%= ', SQL Definition:' + c.sql_type if c.respond_to?('sql_type') %>
18
+ <% end %>
19
+ <% end %>
20
+ end
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
2
+
3
+ class <%= class_name %>Test < Test::Unit::TestCase
4
+ fixtures :<%= table_name %>
5
+
6
+ # Replace this with your real tests.
7
+ def test_truth
8
+ assert true
9
+ end
10
+ end
data/version.rb ADDED
@@ -0,0 +1,9 @@
1
+ module DbDiscovery #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: db_discovery_generator
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-10-14 00:00:00 -04:00
8
+ summary: db_discovery generator allows you to create the DB models with associations from existing DB
9
+ require_paths:
10
+ - ''
11
+ email: db_discovery@deeptown.org
12
+ homepage: http://db-discovery.rubyforge.org
13
+ rubyforge_project:
14
+ description: db_discovery generator allows you to create the DB models with associations from existing DB
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ post_install_message:
30
+ authors:
31
+ - Sergey Kuznetsov
32
+ files:
33
+ - version.rb
34
+ - db_discovery_generator.rb
35
+ - templates
36
+ - USAGE
37
+ - CHANGELOG
38
+ - README
39
+ - templates/fixtures.yml
40
+ - templates/migration.rb
41
+ - templates/model.rb
42
+ - templates/unit_test.rb
43
+ test_files: []
44
+ rdoc_options:
45
+ - "--quiet"
46
+ - "--title"
47
+ - db_discovery documentation
48
+ - "--opname"
49
+ - index.html
50
+ - "--line-numbers"
51
+ - "--main"
52
+ - README
53
+ - "--inline-source"
54
+ - "--exclude"
55
+ - "^(examples|extras)/"
56
+ extra_rdoc_files:
57
+ - README
58
+ - CHANGELOG
59
+ executables: []
60
+ extensions: []
61
+ requirements: []
62
+ dependencies: []