legacy_data 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +49 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/examples/j2ee_petstore.sql +512 -0
- data/generators/models_from_tables/USAGE +6 -0
- data/generators/models_from_tables/models_from_tables_generator.rb +43 -0
- data/generators/models_from_tables/templates/model.rb +31 -0
- data/legacy_data.gemspec +69 -0
- data/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb +61 -0
- data/lib/legacy_data/schema.rb +155 -0
- data/lib/legacy_data/table_class_name_mapper.rb +52 -0
- data/lib/legacy_data/table_definition.rb +41 -0
- data/lib/legacy_data.rb +4 -0
- data/spec/legacy_data/schema_spec.rb +179 -0
- data/spec/legacy_data/table_class_name_mapper_spec.rb +91 -0
- data/spec/legacy_data/table_definition_spec.rb +73 -0
- data/spec/spec_helper.rb +15 -0
- metadata +97 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe LegacyData::TableClassNameMapper do
|
4
|
+
before :each do
|
5
|
+
LegacyData::TableClassNameMapper.clear_dictionary
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'without an input dictionary' do
|
9
|
+
before :each do
|
10
|
+
LegacyData::TableClassNameMapper.instance.stub!(:load_dictionary).and_return({})
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'without any naming convention' do
|
14
|
+
before :each do
|
15
|
+
LegacyData::TableClassNameMapper.naming_convention = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should handle tables with a singular name that ends with s' do
|
19
|
+
LegacyData::TableClassNameMapper.class_name_for('ADDRESS').should == 'Address'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should handle tables with an irregular pluralization name' do
|
23
|
+
LegacyData::TableClassNameMapper.class_name_for('TBPERSON').should == 'Tbperson'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should work on tables that do not have the prefix' do
|
27
|
+
LegacyData::TableClassNameMapper.class_name_for('PERSON').should == 'Person'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "with a 'tables start with TB' naming convention" do
|
32
|
+
before :each do
|
33
|
+
LegacyData::TableClassNameMapper.naming_convention = 'TB*'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should use the wildcard portion when the naming convention applied' do
|
37
|
+
LegacyData::TableClassNameMapper.class_name_for('TBPERSON').should == 'Person'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should use the full table name when the naming convention does not match' do
|
41
|
+
LegacyData::TableClassNameMapper.class_name_for('PERSON').should == 'Person'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'with an input dictionary' do
|
47
|
+
before :each do
|
48
|
+
LegacyData::TableClassNameMapper.instance.stub!(:load_dictionary).and_return({'some_table' => 'CustomClassName'})
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should use the dictionary mapping when one exists' do
|
52
|
+
LegacyData::TableClassNameMapper.class_name_for('some_table').should == 'CustomClassName'
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should use the algorithm when no dictionary mapping exists' do
|
56
|
+
LegacyData::TableClassNameMapper.class_name_for('ANOTHER_TABLE').should == 'AnotherTable'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'persisting the dictionary' do
|
61
|
+
before :each do
|
62
|
+
RAILS_ROOT = 'test_rails_root'
|
63
|
+
@dictionary_file_name = LegacyData::TableClassNameMapper.dictionary_file_name
|
64
|
+
end
|
65
|
+
after :each do
|
66
|
+
Object.send(:remove_const, :RAILS_ROOT) if RAILS_ROOT=='test_rails_root'
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should load the dictionary from a file' do
|
70
|
+
File.should_receive(:exists? ).with(@dictionary_file_name).and_return(true)
|
71
|
+
YAML.should_receive(:load_file).with(@dictionary_file_name).and_return(dictionary_from_file=mock)
|
72
|
+
|
73
|
+
LegacyData::TableClassNameMapper.dictionary.should == dictionary_from_file
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should give empty dictionary when file does not exist' do
|
77
|
+
File.should_receive(:exists? ).with(@dictionary_file_name).and_return(false)
|
78
|
+
|
79
|
+
LegacyData::TableClassNameMapper.dictionary.should == {}
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should save the dictionary to a file' do
|
83
|
+
File.should_receive(:open).with(@dictionary_file_name, 'w').and_yield(file=mock)
|
84
|
+
LegacyData::TableClassNameMapper.instance.should_receive(:dictionary).and_return(dictionary=mock)
|
85
|
+
YAML.should_receive(:dump).with(dictionary, file).and_return(yaml_dictionary=mock)
|
86
|
+
|
87
|
+
LegacyData::TableClassNameMapper.save_dictionary
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe LegacyData::TableDefinition do
|
4
|
+
[:table_name, :columns, :primary_key, :relations, :constraints].each do |option|
|
5
|
+
it "should save the #{option} on initialization" do
|
6
|
+
LegacyData::TableDefinition.new({option=>'some value'})[option].should == 'some value'
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should allow you to set class_name' do
|
10
|
+
table_definition = LegacyData::TableDefinition.new({})
|
11
|
+
table_definition.class_name = 'NewClassName'
|
12
|
+
table_definition[:class_name].should == 'NewClassName'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should reveal itself as a hash' do
|
16
|
+
params = {}
|
17
|
+
[:class_name, :table_name, :columns, :primary_key, :relations, :constraints].each { |field| params[field] = "#{field}_value" }
|
18
|
+
table_definition = LegacyData::TableDefinition.new(params)
|
19
|
+
table_definition.to_hash.should == params
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'join table' do
|
24
|
+
before :each do
|
25
|
+
@foreign_key_columns = ['one_table_id', 'another_table_id']
|
26
|
+
@belongs_to_relation = {'one_table' => {:foreign_key=>:one_table_id },
|
27
|
+
'another_table' => {:foreign_key=>:another_table_id} }
|
28
|
+
end
|
29
|
+
it 'should be a join table when it has only 2 columns and both are foreign keys' do
|
30
|
+
table_definition = LegacyData::TableDefinition.new(:columns=>@foreign_key_columns, :relations=> {:belongs_to=>@belongs_to_relation})
|
31
|
+
table_definition.should be_join_table
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should not be a join table when it has additional columns' do
|
35
|
+
table_definition = LegacyData::TableDefinition.new(:columns=>@foreign_key_columns.push(:another_column), :relations=> {:belongs_to=>@belongs_to_relation})
|
36
|
+
table_definition.should_not be_join_table
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should not be a join table when it does not have the belongs_to relation' do
|
40
|
+
table_definition = LegacyData::TableDefinition.new(:columns=>@foreign_key_columns, :relations=> {})
|
41
|
+
table_definition.should_not be_join_table
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'creating habtm' do
|
45
|
+
before :each do
|
46
|
+
@posts = LegacyData::TableDefinition.new(:table_name=>'posts',
|
47
|
+
:relations=>{:has_many =>{'tag_posts' => {:foreign_key=>:posts_id} },
|
48
|
+
:has_and_belongs_to_many=>{ } })
|
49
|
+
@tags = LegacyData::TableDefinition.new(:table_name=>'tags',
|
50
|
+
:relations=>{:has_many =>{'tag_posts' => {:foreign_key=>:tags_id } },
|
51
|
+
:has_and_belongs_to_many=>{ } })
|
52
|
+
@tag_posts = LegacyData::TableDefinition.new(:table_name=>'tag_posts',
|
53
|
+
:relations=>{:belongs_to =>{'posts' => {:foreign_key=>:posts_id},
|
54
|
+
'tags' => {:foreign_key=>:tags_id } } })
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'belonging to tables' do
|
58
|
+
it 'should tell you when it does not' do
|
59
|
+
@posts.belongs_to_tables.should == []
|
60
|
+
end
|
61
|
+
it 'should tell you when it belongs to 2 tables' do
|
62
|
+
@tag_posts.belongs_to_tables.sort.should == ['posts', 'tags']
|
63
|
+
end
|
64
|
+
end
|
65
|
+
it 'should convert a has_many into a habtm' do
|
66
|
+
@posts.convert_has_many_to_habtm(@tag_posts)
|
67
|
+
|
68
|
+
@posts.relations[:has_many ].should == {}
|
69
|
+
@posts.relations[:has_and_belongs_to_many].should == {'tags' => {:foreign_key=>:posts_id, :association_foreign_key=>:tags_id, :join_table=>:tag_posts} }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'activerecord'
|
5
|
+
gem 'activerecord-oracle_enhanced-adapter'
|
6
|
+
require 'activerecord'
|
7
|
+
require 'active_record/connection_adapters/oracle_enhanced_adapter'
|
8
|
+
|
9
|
+
require 'legacy_data'
|
10
|
+
require 'spec'
|
11
|
+
require 'spec/autorun'
|
12
|
+
|
13
|
+
Spec::Runner.configure do |config|
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: legacy_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Rothenberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-01 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Create ActiveRecord models from an existing database
|
36
|
+
email: alex@alexrothenberg.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- examples/j2ee_petstore.sql
|
52
|
+
- generators/models_from_tables/USAGE
|
53
|
+
- generators/models_from_tables/models_from_tables_generator.rb
|
54
|
+
- generators/models_from_tables/templates/model.rb
|
55
|
+
- legacy_data.gemspec
|
56
|
+
- lib/active_record/connection_adapters/oracle_enhanced_adapter.rb
|
57
|
+
- lib/legacy_data.rb
|
58
|
+
- lib/legacy_data/schema.rb
|
59
|
+
- lib/legacy_data/table_class_name_mapper.rb
|
60
|
+
- lib/legacy_data/table_definition.rb
|
61
|
+
- spec/legacy_data/schema_spec.rb
|
62
|
+
- spec/legacy_data/table_class_name_mapper_spec.rb
|
63
|
+
- spec/legacy_data/table_definition_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://github.com/alexrothenberg/legacy_data
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --charset=UTF-8
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
version:
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Create ActiveRecord models from an existing database
|
93
|
+
test_files:
|
94
|
+
- spec/legacy_data/schema_spec.rb
|
95
|
+
- spec/legacy_data/table_class_name_mapper_spec.rb
|
96
|
+
- spec/legacy_data/table_definition_spec.rb
|
97
|
+
- spec/spec_helper.rb
|