rmre 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/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +45 -0
- data/README.rdoc +12 -0
- data/Rakefile +9 -0
- data/bin/rmre +89 -11
- data/lib/rmre.rb +1 -0
- data/lib/rmre/active_record/schema_dumper.rb +27 -0
- data/lib/rmre/generator.rb +46 -42
- data/lib/rmre/load_file.eruby +14 -0
- data/lib/rmre/model.eruby +17 -0
- data/lib/rmre/version.rb +1 -1
- data/rmre.gemspec +1 -0
- data/spec/rmre/generator_spec.rb +39 -7
- metadata +57 -62
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rmre (0.0.2)
|
5
|
+
activerecord (>= 3.0.0)
|
6
|
+
erubis (~> 2.6.6)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
abstract (1.0.0)
|
12
|
+
activemodel (3.2.1)
|
13
|
+
activesupport (= 3.2.1)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
activerecord (3.2.1)
|
16
|
+
activemodel (= 3.2.1)
|
17
|
+
activesupport (= 3.2.1)
|
18
|
+
arel (~> 3.0.0)
|
19
|
+
tzinfo (~> 0.3.29)
|
20
|
+
activesupport (3.2.1)
|
21
|
+
i18n (~> 0.6)
|
22
|
+
multi_json (~> 1.0)
|
23
|
+
arel (3.0.0)
|
24
|
+
builder (3.0.0)
|
25
|
+
diff-lcs (1.1.3)
|
26
|
+
erubis (2.6.6)
|
27
|
+
abstract (>= 1.0.0)
|
28
|
+
i18n (0.6.0)
|
29
|
+
multi_json (1.1.0)
|
30
|
+
rspec (2.8.0)
|
31
|
+
rspec-core (~> 2.8.0)
|
32
|
+
rspec-expectations (~> 2.8.0)
|
33
|
+
rspec-mocks (~> 2.8.0)
|
34
|
+
rspec-core (2.8.0)
|
35
|
+
rspec-expectations (2.8.0)
|
36
|
+
diff-lcs (~> 1.1.2)
|
37
|
+
rspec-mocks (2.8.0)
|
38
|
+
tzinfo (0.3.31)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
rmre!
|
45
|
+
rspec
|
data/README.rdoc
CHANGED
@@ -6,6 +6,12 @@ key if tables and columns naming doesn't follow Rails convention. It also tries
|
|
6
6
|
all foreign keys data from a database if DBE is MySql, PostgreSQL, Oracle or MS SQL and
|
7
7
|
sets models relationships on a basic level through belongs_to and has_many declarations.
|
8
8
|
|
9
|
+
= Installation
|
10
|
+
|
11
|
+
Rmre gem is on the Rubygems and you can install it with
|
12
|
+
|
13
|
+
gem install rmre
|
14
|
+
|
9
15
|
= How to use
|
10
16
|
|
11
17
|
Rmre is very simple to use:
|
@@ -51,6 +57,12 @@ it mathes whether name of the table starts with passed patterns:
|
|
51
57
|
|
52
58
|
with create models only for tables names with prefixes ec_ or vi_.
|
53
59
|
|
60
|
+
== Test databases
|
61
|
+
|
62
|
+
If you want to try *Rmre* and you do not have sample database you can use
|
63
|
+
_Sakila_ at http://dev.mysql.com/doc/sakila/en/sakila.html#sakila-installation for MySQL and
|
64
|
+
_Pagila_ at http://pgfoundry.org/projects/dbsamples for PostgreSQL.
|
65
|
+
|
54
66
|
= TODO
|
55
67
|
|
56
68
|
* Improve filtering
|
data/Rakefile
ADDED
data/bin/rmre
CHANGED
@@ -1,21 +1,30 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
-
|
4
|
-
require
|
3
|
+
|
4
|
+
require "yaml"
|
5
|
+
require "optparse"
|
6
|
+
require "erubis"
|
7
|
+
require "rmre"
|
5
8
|
|
6
9
|
options = {:db => {}}
|
7
10
|
optparse = OptionParser.new do |opts|
|
11
|
+
opts.banner = "Usage: rmre -a ADAPTER -d DATABASE [options]"
|
12
|
+
|
8
13
|
opts.on('-h', '--help', 'Display this screen') do
|
9
14
|
puts opts
|
10
15
|
exit
|
11
16
|
end
|
12
17
|
|
13
|
-
|
14
|
-
|
18
|
+
opts.on('-f', '--file [FILE]',
|
19
|
+
'Without file name creates settings and models loading RMRE files; otherwise loads settings from file') do |f|
|
20
|
+
options[:file] = f.nil? ? true : f
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on('-a', '--adapter ADAPTER', 'ActiveRecord adapter to use') do |a|
|
15
24
|
options[:db][:adapter] = a
|
16
25
|
end
|
17
26
|
|
18
|
-
opts.on('-d', '--database DATABASE', '
|
27
|
+
opts.on('-d', '--database DATABASE', 'Database name') do |d|
|
19
28
|
options[:db][:database] = d
|
20
29
|
end
|
21
30
|
|
@@ -29,6 +38,16 @@ optparse = OptionParser.new do |opts|
|
|
29
38
|
options[:db][:password] = p
|
30
39
|
end
|
31
40
|
|
41
|
+
options[:db][:port] = nil
|
42
|
+
opts.on('--port [PORT]', 'Port to use for database connection') do |p|
|
43
|
+
options[:db][:port] = p
|
44
|
+
end
|
45
|
+
|
46
|
+
options[:db][:timeout] = 5000
|
47
|
+
opts.on('-t', '--timeout [TIMEOUT]', 'Database connection timeout') do |t|
|
48
|
+
options[:db][:timeout] = t
|
49
|
+
end
|
50
|
+
|
32
51
|
opts.on('-m', '--mode [MODE]', 'MS SQL conenction mode (ODBC only)') do |m|
|
33
52
|
options[:db][:mode] = m
|
34
53
|
end
|
@@ -36,23 +55,82 @@ optparse = OptionParser.new do |opts|
|
|
36
55
|
opts.on('-n', '--dsn [DSN]', 'ODBC DSN name (MS SQL only)') do |n|
|
37
56
|
options[:db][:dsn] = n
|
38
57
|
end
|
39
|
-
|
58
|
+
|
40
59
|
options[:out_path] = File.expand_path(File.join(Dir.tmpdir, "rmre_models"))
|
41
60
|
opts.on('-o', '--out [PATH]', 'Path where models will be generated (default <TMP>/rmre_models)') do |p|
|
61
|
+
p.tr!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
|
42
62
|
options[:out_path] = p
|
43
63
|
end
|
44
64
|
|
45
65
|
opts.on('-s', '--host [HOST]', 'IP address or name of the host') do |s|
|
46
66
|
options[:db][:host] = s
|
47
67
|
end
|
48
|
-
|
68
|
+
|
49
69
|
opts.on('-i', '--include pattern1,pattern2', Array, 'Include prefixes') do |i|
|
50
70
|
options[:include] = i
|
51
71
|
end
|
72
|
+
|
73
|
+
opts.on('-v', '--version', "Prints gem version") do |v|
|
74
|
+
puts "Rmre version #{Rmre::VERSION}"
|
75
|
+
exit
|
76
|
+
end
|
77
|
+
|
78
|
+
opts.on('--dump-schema [FILE]', 'Create schema migration to FILE') do |ds_file|
|
79
|
+
options[:dump_schema] = ds_file
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
begin
|
84
|
+
optparse.parse!
|
85
|
+
rescue OptionParser::ParseError => pe
|
86
|
+
puts pe.message
|
87
|
+
puts optparse
|
88
|
+
exit
|
52
89
|
end
|
53
90
|
|
54
|
-
|
91
|
+
if options[:file] == true
|
92
|
+
options[:db].merge!(:adapter=>'adapter_name',:database=>'db_name') if options[:db][:adapter].nil?
|
93
|
+
options.delete(:file)
|
94
|
+
file_name = 'rmre_db'
|
95
|
+
file_path = File.join(options[:out_path], "#{file_name}.yml")
|
96
|
+
puts "Generating file #{file_path}..."
|
97
|
+
FileUtils.mkdir_p options[:out_path] unless File.exist? options[:out_path]
|
98
|
+
File.open(file_path, "w") do |file|
|
99
|
+
file.print options.to_yaml
|
100
|
+
end
|
101
|
+
|
102
|
+
file_path = File.join(options[:out_path], "#{file_name}.rb")
|
103
|
+
puts "Generating file #{file_path}..."
|
104
|
+
File.open(file_path, "w") do |file|
|
105
|
+
eruby = Erubis::Eruby.new(File.read(File.join(File.expand_path("../../lib/rmre", __FILE__), 'load_file.eruby')))
|
106
|
+
file.write eruby.result(:out_path => options[:out_path],
|
107
|
+
:file_name => "#{file_name}")
|
108
|
+
end
|
109
|
+
else
|
110
|
+
options = YAML.load_file(options[:file]) if options[:file] && File.exists?(options[:file])
|
55
111
|
|
56
|
-
|
57
|
-
|
58
|
-
|
112
|
+
unless options[:db][:adapter]
|
113
|
+
puts "Missing required arguments -a (--adapter) and -d (--database)"
|
114
|
+
puts optparse
|
115
|
+
exit
|
116
|
+
end
|
117
|
+
|
118
|
+
generator = Rmre::Generator.new(options[:db], options[:out_path], options[:include])
|
119
|
+
|
120
|
+
begin
|
121
|
+
generator.connect
|
122
|
+
rescue Exception => e
|
123
|
+
puts e.message
|
124
|
+
exit
|
125
|
+
end
|
126
|
+
|
127
|
+
if options[:dump_schema]
|
128
|
+
puts "Dumping schema to #{options[:dump_schema]}..."
|
129
|
+
File.open(options[:dump_schema], 'w') do |file|
|
130
|
+
generator.dump_schema(file)
|
131
|
+
end
|
132
|
+
else
|
133
|
+
puts "Generating models..."
|
134
|
+
generator.create_models(generator.connection.tables)
|
135
|
+
end
|
136
|
+
end
|
data/lib/rmre.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_record/base"
|
3
|
+
require "active_record/schema_dumper"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
class SchemaDumper
|
7
|
+
def self.dump_with_fk(connection=ActiveRecord::Base.connection, foreign_keys=[], stream=STDOUT)
|
8
|
+
new(connection).dump_with_fk(foreign_keys, stream)
|
9
|
+
stream
|
10
|
+
end
|
11
|
+
|
12
|
+
def dump_with_fk(foreign_keys, stream)
|
13
|
+
header(stream)
|
14
|
+
tables(stream)
|
15
|
+
|
16
|
+
foreign_keys.each do |fk|
|
17
|
+
stream.puts <<-SQL
|
18
|
+
execute "ALTER TABLE #{fk['from_table']} ADD CONSTRAINT fk_#{fk['from_table']}_#{fk['to_table']}
|
19
|
+
FOREIGN KEY (#{fk['from_column']}) REFERENCES #{fk['to_table']}(#{fk['to_column']})"
|
20
|
+
SQL
|
21
|
+
end
|
22
|
+
|
23
|
+
trailer(stream)
|
24
|
+
stream
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/rmre/generator.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "tmpdir"
|
2
2
|
require "fileutils"
|
3
|
-
require "
|
3
|
+
require "erubis"
|
4
|
+
require "rmre/active_record/schema_dumper"
|
4
5
|
|
5
6
|
module Rmre
|
6
7
|
class Generator
|
@@ -8,7 +9,7 @@ module Rmre
|
|
8
9
|
attr_reader :output_path
|
9
10
|
|
10
11
|
SETTINGS_ROOT = File.expand_path('../../../../db', __FILE__)
|
11
|
-
|
12
|
+
|
12
13
|
def initialize(options, out_path, include)
|
13
14
|
@connection_options = options
|
14
15
|
@connection = nil
|
@@ -18,37 +19,41 @@ module Rmre
|
|
18
19
|
|
19
20
|
def connect
|
20
21
|
return if @connection_options.empty?
|
21
|
-
|
22
|
+
|
22
23
|
ActiveRecord::Base.establish_connection(@connection_options)
|
23
24
|
@connection = ActiveRecord::Base.connection
|
24
25
|
end
|
25
26
|
|
26
27
|
def create_models(tables)
|
27
28
|
return unless tables.is_a? Array
|
28
|
-
|
29
|
+
|
29
30
|
FileUtils.mkdir_p(@output_path) if !Dir.exists?(@output_path)
|
30
31
|
|
31
32
|
tables.each do |table_name|
|
32
33
|
create_model(table_name) if process?(table_name)
|
33
34
|
end
|
34
35
|
end
|
35
|
-
|
36
|
+
|
37
|
+
def dump_schema(stream)
|
38
|
+
ActiveRecord::SchemaDumper.dump_with_fk(connection, foreign_keys, stream)
|
39
|
+
end
|
40
|
+
|
36
41
|
def create_model(table_name)
|
37
|
-
File.open(File.join(output_path, "#{table_name}.rb"), "w") do |file|
|
42
|
+
File.open(File.join(output_path, "#{table_name.tableize.singularize}.rb"), "w") do |file|
|
38
43
|
constraints = []
|
39
44
|
|
40
45
|
foreign_keys.each do |fk|
|
41
46
|
src = constraint_src(table_name, fk)
|
42
47
|
constraints << src unless src.nil?
|
43
48
|
end
|
44
|
-
|
49
|
+
|
45
50
|
file.write generate_model_source(table_name, constraints)
|
46
51
|
end
|
47
52
|
end
|
48
53
|
|
49
54
|
def process?(table_name)
|
50
55
|
return true if @include_prefixes.nil? || @include_prefixes.empty?
|
51
|
-
|
56
|
+
|
52
57
|
@include_prefixes.each do |prefix|
|
53
58
|
return true if table_name =~ /^#{prefix}/
|
54
59
|
end
|
@@ -79,22 +84,21 @@ module Rmre
|
|
79
84
|
def constraint_src(table_name, fk={})
|
80
85
|
src = nil
|
81
86
|
if fk['from_table'] == table_name
|
82
|
-
src = "belongs_to :#{fk['to_table']}, :class_name => '#{fk['to_table'].classify}', :foreign_key => :#{fk['from_column']}"
|
87
|
+
src = "belongs_to :#{fk['to_table'].downcase}, :class_name => '#{fk['to_table'].tableize.classify}', :foreign_key => :#{fk['from_column']}"
|
83
88
|
elsif fk['to_table'] == table_name
|
84
|
-
src = "has_many :#{fk['from_table'].pluralize}, :class_name => '#{fk['from_table'].classify}'"
|
89
|
+
src = "has_many :#{fk['from_table'].downcase.pluralize}, :class_name => '#{fk['from_table'].tableize.classify}'"
|
85
90
|
end
|
86
91
|
src
|
87
92
|
end
|
88
|
-
|
93
|
+
|
89
94
|
def generate_model_source(table_name, constraints)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
src << "\nend\n"
|
95
|
+
eruby = Erubis::Eruby.new(File.read(File.join(File.expand_path("../", __FILE__), 'model.eruby')))
|
96
|
+
eruby.result(:table_name => table_name,
|
97
|
+
:primary_key => connection.primary_key(table_name),
|
98
|
+
:constraints => constraints,
|
99
|
+
:has_type_column => connection.columns(table_name).find { |col| col.name == 'type' })
|
96
100
|
end
|
97
|
-
|
101
|
+
|
98
102
|
def mysql_foreign_keys
|
99
103
|
sql = <<-SQL
|
100
104
|
select
|
@@ -117,16 +121,16 @@ SELECT tc.table_name as from_table,
|
|
117
121
|
ccu.table_name AS to_table,
|
118
122
|
ccu.column_name AS to_column
|
119
123
|
FROM information_schema.table_constraints tc
|
120
|
-
LEFT JOIN information_schema.key_column_usage kcu
|
124
|
+
LEFT JOIN information_schema.key_column_usage kcu
|
121
125
|
ON tc.constraint_catalog = kcu.constraint_catalog
|
122
126
|
AND tc.constraint_schema = kcu.constraint_schema
|
123
127
|
AND tc.constraint_name = kcu.constraint_name
|
124
|
-
|
125
|
-
LEFT JOIN information_schema.referential_constraints rc
|
128
|
+
|
129
|
+
LEFT JOIN information_schema.referential_constraints rc
|
126
130
|
ON tc.constraint_catalog = rc.constraint_catalog
|
127
131
|
AND tc.constraint_schema = rc.constraint_schema
|
128
132
|
AND tc.constraint_name = rc.constraint_name
|
129
|
-
LEFT JOIN information_schema.constraint_column_usage ccu
|
133
|
+
LEFT JOIN information_schema.constraint_column_usage ccu
|
130
134
|
ON rc.unique_constraint_catalog = ccu.constraint_catalog
|
131
135
|
AND rc.unique_constraint_schema = ccu.constraint_schema
|
132
136
|
AND rc.unique_constraint_name = ccu.constraint_name
|
@@ -138,25 +142,25 @@ SQL
|
|
138
142
|
|
139
143
|
def mssql_foreign_keys
|
140
144
|
sql = <<-SQL
|
141
|
-
SELECT C.TABLE_NAME [from_table],
|
142
|
-
KCU.COLUMN_NAME [from_column],
|
143
|
-
C2.TABLE_NAME [to_table],
|
145
|
+
SELECT C.TABLE_NAME [from_table],
|
146
|
+
KCU.COLUMN_NAME [from_column],
|
147
|
+
C2.TABLE_NAME [to_table],
|
144
148
|
KCU2.COLUMN_NAME [to_column]
|
145
|
-
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
|
146
|
-
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
|
147
|
-
ON C.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA
|
148
|
-
AND C.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
|
149
|
-
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
|
150
|
-
ON C.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
|
151
|
-
AND C.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
|
152
|
-
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C2
|
153
|
-
ON RC.UNIQUE_CONSTRAINT_SCHEMA = C2.CONSTRAINT_SCHEMA
|
154
|
-
AND RC.UNIQUE_CONSTRAINT_NAME = C2.CONSTRAINT_NAME
|
155
|
-
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2
|
156
|
-
ON C2.CONSTRAINT_SCHEMA = KCU2.CONSTRAINT_SCHEMA
|
157
|
-
AND C2.CONSTRAINT_NAME = KCU2.CONSTRAINT_NAME
|
158
|
-
AND KCU.ORDINAL_POSITION = KCU2.ORDINAL_POSITION
|
159
|
-
WHERE C.CONSTRAINT_TYPE = 'FOREIGN KEY'
|
149
|
+
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
|
150
|
+
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
|
151
|
+
ON C.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA
|
152
|
+
AND C.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
|
153
|
+
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
|
154
|
+
ON C.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
|
155
|
+
AND C.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
|
156
|
+
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C2
|
157
|
+
ON RC.UNIQUE_CONSTRAINT_SCHEMA = C2.CONSTRAINT_SCHEMA
|
158
|
+
AND RC.UNIQUE_CONSTRAINT_NAME = C2.CONSTRAINT_NAME
|
159
|
+
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2
|
160
|
+
ON C2.CONSTRAINT_SCHEMA = KCU2.CONSTRAINT_SCHEMA
|
161
|
+
AND C2.CONSTRAINT_NAME = KCU2.CONSTRAINT_NAME
|
162
|
+
AND KCU.ORDINAL_POSITION = KCU2.ORDINAL_POSITION
|
163
|
+
WHERE C.CONSTRAINT_TYPE = 'FOREIGN KEY'
|
160
164
|
SQL
|
161
165
|
connection.select_all(sql)
|
162
166
|
end
|
@@ -166,9 +170,9 @@ SQL
|
|
166
170
|
connection.tables.each do |table|
|
167
171
|
connection.foreign_keys(table).each do |oracle_fk|
|
168
172
|
table_fk = { 'from_table' => oracle_fk.from_table,
|
169
|
-
'from_column' => oracle_fk.options[:
|
173
|
+
'from_column' => oracle_fk.options[:columns][0],
|
170
174
|
'to_table' => oracle_fk.to_table,
|
171
|
-
'to_column' => oracle_fk.options[:
|
175
|
+
'to_column' => oracle_fk.options[:references][0] }
|
172
176
|
fk << table_fk
|
173
177
|
end
|
174
178
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
<%= "dir = File.join('#{out_path}', '*.rb')" %>
|
5
|
+
Dir.glob(dir) { |file| require file }
|
6
|
+
|
7
|
+
def connect
|
8
|
+
<%= "settings_file = './#{file_name}.yml'" %>
|
9
|
+
exit unless File.exists?(settings_file)
|
10
|
+
settings = YAML.load_file(settings_file)
|
11
|
+
ActiveRecord::Base.establish_connection(settings[:db])
|
12
|
+
ActiveRecord::Base.connection
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= table_name.tableize.classify %> < ActiveRecord::Base
|
2
|
+
<% unless table_name == table_name.tableize %>
|
3
|
+
<%= "set_table_name '#{table_name}'" -%>
|
4
|
+
<% end %>
|
5
|
+
|
6
|
+
<% unless "id" == primary_key || primary_key.nil? %>
|
7
|
+
<%= "set_primary_key :#{primary_key}" -%>
|
8
|
+
<% end %>
|
9
|
+
|
10
|
+
<% if has_type_column %>
|
11
|
+
<%= "set_inheritance_column :ruby_type" -%>
|
12
|
+
<% end %>
|
13
|
+
|
14
|
+
<% constraints.each do |constraint| %>
|
15
|
+
<%= constraint -%>
|
16
|
+
<% end %>
|
17
|
+
end
|
data/lib/rmre/version.rb
CHANGED
data/rmre.gemspec
CHANGED
data/spec/rmre/generator_spec.rb
CHANGED
@@ -7,21 +7,28 @@ module Rmre
|
|
7
7
|
:database => 'db',
|
8
8
|
:username => 'user',
|
9
9
|
:password => 'pass'},
|
10
|
-
:out_path => '
|
10
|
+
:out_path => File.join(Dir.tmpdir, 'gne-test'),
|
11
11
|
:include => ['incl1_', 'incl2_']}
|
12
12
|
end
|
13
|
+
|
14
|
+
let(:generator) do |gen|
|
15
|
+
gen = Generator.new(settings[:db], settings[:out_path], settings[:include])
|
16
|
+
connection = double("db_connection")
|
17
|
+
connection.stub(:columns).and_return([])
|
18
|
+
gen.stub(:connection).and_return(connection)
|
19
|
+
gen
|
20
|
+
end
|
13
21
|
|
14
|
-
let(:generator) { Generator.new(settings[:db], settings[:out_path], settings[:include]) }
|
15
22
|
let(:tables) { %w(incl1_tbl1 incl1_tbl2 incl2_tbl1 user processes) }
|
16
|
-
|
17
|
-
it "should flag table
|
23
|
+
|
24
|
+
it "should flag table incl1_tbl1 for processing" do
|
18
25
|
generator.process?('incl1_tbl1').should be_true
|
19
26
|
end
|
20
27
|
|
21
|
-
it "should not flag table
|
28
|
+
it "should not flag table 'processes' for processing" do
|
22
29
|
generator.process?('processes').should be_false
|
23
30
|
end
|
24
|
-
|
31
|
+
|
25
32
|
it "should process three tables from the passed array of tables" do
|
26
33
|
generator.stub(:create_model)
|
27
34
|
|
@@ -33,12 +40,37 @@ module Rmre
|
|
33
40
|
generator.stub_chain(:connection, :primary_key).and_return("id")
|
34
41
|
generator.send(:generate_model_source, 'incl1_tbl1', []).should match(/set_table_name \'incl1_tbl1\'/)
|
35
42
|
end
|
36
|
-
|
43
|
+
|
37
44
|
it "should create three model files" do
|
38
45
|
generator.stub_chain(:connection, :primary_key).and_return("id")
|
39
46
|
generator.stub(:foreign_keys).and_return([])
|
40
47
|
generator.create_models(tables)
|
41
48
|
Dir.glob(File.join(generator.output_path, "*.rb")).should have(3).items
|
42
49
|
end
|
50
|
+
|
51
|
+
it "should create prettified file names" do
|
52
|
+
file = double("model_file")
|
53
|
+
file.stub(:write)
|
54
|
+
|
55
|
+
generator.connection.stub(:primary_key).and_return('')
|
56
|
+
|
57
|
+
File.stub(:open).and_yield(file)
|
58
|
+
File.should_receive(:open).with(/tbl_user/, "w")
|
59
|
+
file.should_receive(:write).with(/class TblUser/)
|
60
|
+
|
61
|
+
generator.create_model("TBL_USERS")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should set primary key if PK column is not id" do
|
65
|
+
file = double("model_file")
|
66
|
+
file.stub(:write)
|
67
|
+
|
68
|
+
generator.connection.stub(:primary_key).and_return("usr_id")
|
69
|
+
|
70
|
+
File.stub(:open).and_yield(file)
|
71
|
+
file.should_receive(:write).with(/set_primary_key :usr_id/)
|
72
|
+
|
73
|
+
generator.create_model("users")
|
74
|
+
end
|
43
75
|
end
|
44
76
|
end
|
metadata
CHANGED
@@ -1,107 +1,102 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rmre
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Bosko Ivanisevic
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-02-19 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: activerecord
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &20264180 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 3
|
30
|
-
- 0
|
31
|
-
- 0
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
32
21
|
version: 3.0.0
|
33
22
|
type: :runtime
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: rspec
|
37
23
|
prerelease: false
|
38
|
-
|
24
|
+
version_requirements: *20264180
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: erubis
|
27
|
+
requirement: &20263340 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.6.6
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *20263340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &20262920 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
- 0
|
45
|
-
version: "0"
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
46
44
|
type: :development
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *20262920
|
47
|
+
description: Rmre creates ActiveRecord models for legacy database with all constraints
|
48
|
+
found.
|
49
|
+
email:
|
50
50
|
- bosko.ivanisevic@gmail.com
|
51
|
-
executables:
|
51
|
+
executables:
|
52
52
|
- rmre
|
53
53
|
extensions: []
|
54
|
-
|
55
|
-
extra_rdoc_files:
|
54
|
+
extra_rdoc_files:
|
56
55
|
- README.rdoc
|
57
56
|
- LICENSE.txt
|
58
|
-
files:
|
57
|
+
files:
|
59
58
|
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- Gemfile.lock
|
60
61
|
- LICENSE.txt
|
61
62
|
- README.rdoc
|
63
|
+
- Rakefile
|
62
64
|
- bin/rmre
|
63
65
|
- lib/rmre.rb
|
66
|
+
- lib/rmre/active_record/schema_dumper.rb
|
64
67
|
- lib/rmre/generator.rb
|
68
|
+
- lib/rmre/load_file.eruby
|
69
|
+
- lib/rmre/model.eruby
|
65
70
|
- lib/rmre/version.rb
|
66
71
|
- rmre.gemspec
|
67
72
|
- spec/rmre/generator_spec.rb
|
68
73
|
- spec/spec_helper.rb
|
69
|
-
has_rdoc: true
|
70
74
|
homepage: http://github.com/bosko/rmre
|
71
75
|
licenses: []
|
72
|
-
|
73
76
|
post_install_message:
|
74
|
-
rdoc_options:
|
77
|
+
rdoc_options:
|
75
78
|
- --title
|
76
79
|
- Rmre -- Rails Models Reverse Engineering
|
77
80
|
- --main
|
78
81
|
- README.rdoc
|
79
|
-
require_paths:
|
82
|
+
require_paths:
|
80
83
|
- lib
|
81
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
85
|
none: false
|
83
|
-
requirements:
|
84
|
-
- -
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
|
87
|
-
|
88
|
-
version: "0"
|
89
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
91
|
none: false
|
91
|
-
requirements:
|
92
|
-
- -
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
segments:
|
95
|
-
- 1
|
96
|
-
- 3
|
97
|
-
- 6
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
98
95
|
version: 1.3.6
|
99
96
|
requirements: []
|
100
|
-
|
101
97
|
rubyforge_project: rmre
|
102
|
-
rubygems_version: 1.
|
98
|
+
rubygems_version: 1.8.10
|
103
99
|
signing_key:
|
104
100
|
specification_version: 3
|
105
101
|
summary: The easiest way to create ActiveRecord models for legacy database
|
106
102
|
test_files: []
|
107
|
-
|