legacy_data 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. data/Rakefile +37 -1
  2. data/VERSION +1 -1
  3. data/examples/blog_migration.rb +26 -0
  4. data/examples/create_j2ee_petstore.sql +91 -0
  5. data/examples/delete_j2ee_petstore.sql +14 -0
  6. data/examples/drupal_6_14.sql +809 -0
  7. data/examples/generated/blog_mysql/comment.rb +11 -0
  8. data/examples/generated/blog_mysql/factories.rb +9 -0
  9. data/examples/generated/blog_mysql/post.rb +11 -0
  10. data/examples/generated/blog_mysql/tag.rb +11 -0
  11. data/{spec/functional/expected → examples/generated/blog_sqlite3}/comment.rb +3 -4
  12. data/examples/generated/blog_sqlite3/factories.rb +12 -0
  13. data/{spec/functional/expected → examples/generated/blog_sqlite3}/post.rb +3 -3
  14. data/examples/generated/blog_sqlite3/tag.rb +11 -0
  15. data/examples/generated/drupal_mysql/access.rb +12 -0
  16. data/examples/generated/drupal_mysql/action.rb +11 -0
  17. data/examples/generated/drupal_mysql/actions_aid.rb +11 -0
  18. data/examples/generated/drupal_mysql/authmap.rb +13 -0
  19. data/examples/generated/drupal_mysql/batch.rb +12 -0
  20. data/examples/generated/drupal_mysql/block.rb +13 -0
  21. data/examples/generated/drupal_mysql/blocks_role.rb +12 -0
  22. data/examples/generated/drupal_mysql/box.rb +13 -0
  23. data/examples/generated/drupal_mysql/cache.rb +12 -0
  24. data/examples/generated/drupal_mysql/cache_block.rb +12 -0
  25. data/examples/generated/drupal_mysql/cache_filter.rb +12 -0
  26. data/examples/generated/drupal_mysql/cache_form.rb +12 -0
  27. data/examples/generated/drupal_mysql/cache_menu.rb +12 -0
  28. data/examples/generated/drupal_mysql/cache_page.rb +12 -0
  29. data/examples/generated/drupal_mysql/cache_update.rb +12 -0
  30. data/examples/generated/drupal_mysql/comment.rb +12 -0
  31. data/examples/generated/drupal_mysql/factories.rb +370 -0
  32. data/examples/generated/drupal_mysql/filter.rb +13 -0
  33. data/examples/generated/drupal_mysql/filter_format.rb +13 -0
  34. data/examples/generated/drupal_mysql/flood.rb +12 -0
  35. data/examples/generated/drupal_mysql/history.rb +12 -0
  36. data/examples/generated/drupal_mysql/menu_custom.rb +11 -0
  37. data/examples/generated/drupal_mysql/menu_link.rb +12 -0
  38. data/examples/generated/drupal_mysql/menu_router.rb +12 -0
  39. data/examples/generated/drupal_mysql/node.rb +13 -0
  40. data/examples/generated/drupal_mysql/node_access.rb +12 -0
  41. data/examples/generated/drupal_mysql/node_comment_statistic.rb +12 -0
  42. data/examples/generated/drupal_mysql/node_counter.rb +12 -0
  43. data/examples/generated/drupal_mysql/node_revision.rb +12 -0
  44. data/examples/generated/drupal_mysql/node_type.rb +12 -0
  45. data/examples/generated/drupal_mysql/permission.rb +12 -0
  46. data/examples/generated/drupal_mysql/role.rb +12 -0
  47. data/examples/generated/drupal_mysql/session.rb +12 -0
  48. data/examples/generated/drupal_mysql/system.rb +12 -0
  49. data/examples/generated/drupal_mysql/term_data.rb +12 -0
  50. data/examples/generated/drupal_mysql/term_hierarchy.rb +12 -0
  51. data/examples/generated/drupal_mysql/term_node.rb +12 -0
  52. data/examples/generated/drupal_mysql/term_relation.rb +13 -0
  53. data/examples/generated/drupal_mysql/term_synonym.rb +12 -0
  54. data/examples/generated/drupal_mysql/uploaded_files.rb +12 -0
  55. data/examples/generated/drupal_mysql/url_alias.rb +12 -0
  56. data/examples/generated/drupal_mysql/user.rb +14 -0
  57. data/examples/generated/drupal_mysql/users_role.rb +12 -0
  58. data/examples/generated/drupal_mysql/variable.rb +11 -0
  59. data/examples/generated/drupal_mysql/vocabulary.rb +12 -0
  60. data/examples/generated/drupal_mysql/vocabulary_node_type.rb +12 -0
  61. data/examples/generated/drupal_mysql/watchdog.rb +12 -0
  62. data/examples/generated/j2ee_petstore_mysql/address.rb +11 -0
  63. data/examples/generated/j2ee_petstore_mysql/category.rb +11 -0
  64. data/examples/generated/j2ee_petstore_mysql/factories.rb +57 -0
  65. data/examples/generated/j2ee_petstore_mysql/id_gen.rb +12 -0
  66. data/examples/generated/j2ee_petstore_mysql/item.rb +12 -0
  67. data/examples/generated/j2ee_petstore_mysql/product.rb +11 -0
  68. data/examples/generated/j2ee_petstore_mysql/seller_contact_info.rb +11 -0
  69. data/examples/generated/j2ee_petstore_mysql/tag.rb +13 -0
  70. data/examples/generated/j2ee_petstore_mysql/tag_item.rb +13 -0
  71. data/examples/generated/j2ee_petstore_mysql/ziplocation.rb +11 -0
  72. data/examples/generated/j2ee_petstore_oracle/address.rb +11 -0
  73. data/examples/generated/j2ee_petstore_oracle/category.rb +11 -0
  74. data/examples/generated/j2ee_petstore_oracle/factories.rb +52 -0
  75. data/examples/generated/j2ee_petstore_oracle/id_gen.rb +12 -0
  76. data/examples/generated/j2ee_petstore_oracle/item.rb +15 -0
  77. data/examples/generated/j2ee_petstore_oracle/product.rb +12 -0
  78. data/examples/generated/j2ee_petstore_oracle/sellercontactinfo.rb +11 -0
  79. data/examples/generated/j2ee_petstore_oracle/tag.rb +13 -0
  80. data/examples/generated/j2ee_petstore_oracle/ziplocation.rb +11 -0
  81. data/examples/generated/j2ee_petstore_sqlite3/address.rb +12 -0
  82. data/examples/generated/j2ee_petstore_sqlite3/category.rb +12 -0
  83. data/examples/generated/j2ee_petstore_sqlite3/factories.rb +57 -0
  84. data/examples/generated/j2ee_petstore_sqlite3/id_gen.rb +13 -0
  85. data/examples/generated/j2ee_petstore_sqlite3/item.rb +13 -0
  86. data/examples/generated/j2ee_petstore_sqlite3/product.rb +12 -0
  87. data/examples/generated/j2ee_petstore_sqlite3/seller_contact_info.rb +12 -0
  88. data/examples/generated/j2ee_petstore_sqlite3/tag.rb +13 -0
  89. data/examples/generated/j2ee_petstore_sqlite3/tag_item.rb +14 -0
  90. data/examples/generated/j2ee_petstore_sqlite3/ziplocation.rb +11 -0
  91. data/generators/models_from_tables/models_from_tables_generator.rb +5 -36
  92. data/generators/models_from_tables/templates/model.rb +5 -39
  93. data/legacy_data.gemspec +187 -11
  94. data/lib/active_record/connection_adapters/oracleenhanced_adapter.rb +85 -0
  95. data/lib/legacy_data.rb +5 -2
  96. data/lib/legacy_data/schema.rb +10 -7
  97. data/lib/legacy_data/table_class_name_mapper.rb +1 -1
  98. data/lib/legacy_data/table_definition.rb +120 -5
  99. data/spec/expected/post.rb +3 -3
  100. data/spec/functional/blog_adapterspec.rb +45 -0
  101. data/spec/functional/database.yml +45 -0
  102. data/spec/functional/drupal_adapterspec.rb +54 -0
  103. data/spec/functional/functional_spec_helper.rb +24 -0
  104. data/spec/functional/j2ee_petstore_adapterspec.rb +57 -0
  105. data/spec/legacy_data/schema_spec.rb +7 -3
  106. data/spec/legacy_data/table_definition_spec.rb +101 -10
  107. data/spec/models_from_tables_generator_spec.rb +19 -7
  108. data/spec/spec_helper.rb +6 -18
  109. metadata +193 -10
  110. data/examples/j2ee_petstore.sql +0 -512
  111. data/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb +0 -61
  112. data/spec/functional/models_from_tables_spec.rb +0 -27
data/lib/legacy_data.rb CHANGED
@@ -1,12 +1,15 @@
1
+ require 'foreigner' #matthuhiggens-foreigner
2
+
1
3
  require 'legacy_data/table_definition'
2
4
  require 'legacy_data/schema'
3
5
  require 'legacy_data/table_class_name_mapper'
4
6
 
5
-
6
7
  module ActiveRecord
7
8
  Base.class_eval do
8
9
  if ['OracleEnhanced'].include? connection.adapter_name
9
- require "#{File.dirname(__FILE__)}/active_record/connection_adapters/#{connection.adapter_name.underscore}_adapter"
10
+ # require "#{File.dirname(__FILE__)}/active_record/connection_adapters/#{connection.adapter_name.underscore}_adapter"
11
+ require "#{File.dirname(__FILE__)}/active_record/connection_adapters/#{connection.adapter_name.downcase}_adapter"
10
12
  end
11
13
  end
12
14
  end
15
+
@@ -28,14 +28,14 @@ module LegacyData
28
28
  if table_name
29
29
  add_pending_table(table_name)
30
30
  else
31
- LegacyData::Schema.tables.each {|table| add_pending_table(table) }
31
+ self.tables.each {|table| add_pending_table(table) }
32
32
  end
33
33
  end
34
34
  def self.add_pending_table table_name
35
35
  table_definitions[table_name] = :pending if table_definitions[table_name].nil?
36
36
  end
37
37
  def self.next_table_to_process
38
- table_definitions.keys.detect {|table_name| table_definitions[table_name] == :pending }
38
+ table_definitions.keys.map(&:to_s).sort.detect {|table_name| table_definitions[table_name] == :pending }
39
39
  end
40
40
  def self.clear_table_definitions
41
41
  @tables = {}
@@ -88,6 +88,7 @@ module LegacyData
88
88
  elsif connection.respond_to?(:primary_key)
89
89
  @pk = connection.primary_key(table_name)
90
90
  end
91
+ @pk = 'no_primary_key' if @pk.nil?
91
92
  end
92
93
  @pk
93
94
  end
@@ -100,11 +101,13 @@ module LegacyData
100
101
  end
101
102
 
102
103
  def belongs_to_relations
103
- return {} unless connection.respond_to? :foreign_keys_for
104
-
104
+ return {} unless connection.respond_to? :foreign_keys
105
+
105
106
  belongs_to = {}
106
- connection.foreign_keys_for(table_name).each do |relation|
107
- belongs_to[relation.first.downcase] = {:foreign_key=>relation.second.downcase.to_sym}
107
+ connection.foreign_keys(table_name).each do |foreign_key|
108
+ options = {:foreign_key=>foreign_key.options[:column].downcase.to_sym}
109
+ options[:dependent] = :destroy if foreign_key.options[:dependent] == :delete
110
+ belongs_to[foreign_key.to_table.downcase] = options
108
111
  end
109
112
  belongs_to
110
113
  end
@@ -200,7 +203,7 @@ module LegacyData
200
203
 
201
204
  private
202
205
  def self.connection
203
- @conn ||= ActiveRecord::Base.connection
206
+ ActiveRecord::Base.connection
204
207
  end
205
208
  def connection
206
209
  self.class.connection
@@ -56,7 +56,7 @@ Done analyzing the tables.
56
56
  def compute_class_name table_name
57
57
  table_name =~ /#{naming_convention}/i
58
58
  stripped_table_name = $1 || table_name
59
- dictionary[table_name] = ActiveRecord::Base.class_name(stripped_table_name.downcase.pluralize)
59
+ dictionary[table_name] = ActiveRecord::Base.class_name(stripped_table_name.underscore.downcase.pluralize)
60
60
  end
61
61
 
62
62
  def self.log msg
@@ -10,14 +10,129 @@ module LegacyData
10
10
  self.send(key)
11
11
  end
12
12
 
13
- def to_hash
14
- hash = {}
15
- [:class_name, :table_name, :columns, :primary_key, :relations, :constraints].each {|field| hash[field] = self.send(field) }
16
- hash
13
+ def unconventional_table_name?
14
+ table_name != class_name.underscore.pluralize
15
+ end
16
+
17
+ def unconventional_primary_key?
18
+ puts self.inspect if primary_key.nil?
19
+ primary_key != 'id'
20
+ end
21
+
22
+ def relationships_to_s
23
+ relationships_text = []
24
+ [:has_many, :has_one, :belongs_to, :has_and_belongs_to_many].each do |relation_type|
25
+ is_singular_association = [:has_one, :belongs_to].include?(relation_type)
26
+ unless relations[relation_type].nil?
27
+ association_with_longest_name = longest_association_name(relations[relation_type], is_singular_association)
28
+
29
+ relations[relation_type].keys.sort.each do |table_name|
30
+ options = relations[relation_type][table_name]
31
+ class_for_table = TableDefinition.class_name_for(table_name)
32
+ association_name = class_for_table.underscore
33
+ association_name = association_name.pluralize unless is_singular_association
34
+ needs_class_name = (ActiveRecord::Base.class_name(association_name.pluralize) != class_for_table)
35
+ options[:class_name] = class_for_table if needs_class_name
36
+
37
+ spaces = association_with_longest_name.size - association_name.size
38
+ relationships_text << "#{relation_type} #{association_name.to_sym.inspect},#{' ' * spaces} #{options_to_s(options)}"
39
+ end
40
+ end
41
+ end
42
+ relationships_text.join "\n "
43
+ end
44
+
45
+ def class_name
46
+ TableDefinition.class_name_for table_name
47
+ end
48
+
49
+ def self.class_name_for table_name
50
+ LegacyData::TableClassNameMapper.class_name_for(table_name)
51
+ end
52
+
53
+ def longest_association_name(associations, is_singular_association)
54
+ association_names = associations.keys.map do |assoc|
55
+ association_name = TableDefinition.class_name_for(assoc).underscore
56
+ association_name = association_name.pluralize unless is_singular_association
57
+ association_name
58
+ end
59
+ association_with_longest_name = association_names.max { |a,b| a.length <=> b.length }
60
+ end
61
+
62
+ def options_to_s options
63
+ alphabetized_option_keys = options.keys.map(&:to_s).sort
64
+ alphabetized_option_keys.map do |key|
65
+ "#{key.to_sym.inspect} => #{options[key.to_sym].inspect}"
66
+ end.join(', ')
67
+ end
68
+
69
+ def constraints_to_s
70
+ alphabetized_constraints_types = constraints.keys.map(&:to_s).sort
71
+ constraints_text = alphabetized_constraints_types.map do |constraint_type|
72
+ self.send("#{constraint_type}_constraints_to_s") unless constraints[constraint_type.to_sym].blank?
73
+ end
74
+ constraints_text.flatten.reject(&:nil?).join("\n ")
75
+ end
76
+
77
+ def unique_constraints_to_s
78
+ cols_list = constraints[:unique].map {|cols| cols.downcase.to_sym.inspect}.join(', ')
79
+ "validates_uniqueness_of #{cols_list}"
80
+ end
81
+
82
+ def multi_column_unique_constraints_to_s
83
+ constraints[:multi_column_unique].map do |cols|
84
+ "#validates_uniqueness_of_multiple_column_constraint #{cols.inspect}"
85
+ end
86
+ end
87
+
88
+ def presence_of_constraints_to_s
89
+ cols_list = constraints[:presence_of].map {|cols| cols.downcase.to_sym.inspect}.join(', ')
90
+ "validates_presence_of #{cols_list}"
91
+ end
92
+
93
+ def inclusion_of_constraints_to_s
94
+ constraints[:inclusion_of].keys.map do |col|
95
+ <<-OUTPUT
96
+ def self.possible_values_for_#{col}
97
+ #{constraints[:inclusion_of][col].inspect}
98
+ end
99
+ validates_inclusion_of #{col.to_sym.inspect},
100
+ :in => possible_values_for_#{col},
101
+ :message => "is not one of (\#{possible_values_for_#{col}.join(', ')})"
102
+ OUTPUT
103
+ end
104
+ end
105
+
106
+ def numericality_of_constraints_to_s
107
+ [:allow_nil, :do_not_allow_nil].map do |nullable|
108
+ if constraints[:numericality_of][nullable].blank?
109
+ []
110
+ else
111
+ cols_list = constraints[:numericality_of][nullable].map {|cols| cols.downcase.to_sym.inspect}.join(', ')
112
+ "validates_numericality_of #{cols_list}#{", {:allow_nil=>true}" if nullable == :allow_nil }"
113
+ end
114
+ end unless constraints[:numericality_of].blank?
115
+ end
116
+
117
+ def custom_constraints_to_s
118
+ constraints[:custom].keys.map do |name|
119
+ <<-OUTPUT
120
+ validate #{"validate_#{name}".to_sym.inspect }
121
+ def validate_#{name}
122
+ # TODO: validate this SQL constraint
123
+ <<-SQL
124
+ #{constraints[:custom][name]}
125
+ SQL
126
+ end
127
+ OUTPUT
128
+ end
17
129
  end
18
130
 
19
131
  def join_table?
20
- (columns.size == 2) and relations[:belongs_to] and (relations[:belongs_to].values.map {|value| value[:foreign_key]}.map(&:to_s) == columns)
132
+ return false unless (columns.size == 2) and relations[:belongs_to] and (relations[:belongs_to].values.size == 2)
133
+ column_names = columns.map(&:name)
134
+ foreign_key_names = relations[:belongs_to].values.map {|value| value[:foreign_key]}.map(&:to_s)
135
+ return column_names.sort == foreign_key_names.sort
21
136
  end
22
137
 
23
138
  def belongs_to_relations
@@ -1,12 +1,12 @@
1
1
  class Post < ActiveRecord::Base
2
- set_table_name :posts
3
- set_primary_key :id
2
+
3
+
4
4
 
5
5
  # Relationships
6
6
  has_many :comments, :foreign_key => :comment_id
7
7
 
8
8
  # Constraints
9
- validates_uniqueness_of :title
10
9
  validates_presence_of :body
10
+ validates_uniqueness_of :title
11
11
  end
12
12
 
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/functional_spec_helper')
2
+
3
+
4
+ describe "Generating models from a blog #{ENV['ADAPTER']} database" do
5
+ before :all do
6
+ @adapter = ENV['ADAPTER']
7
+ @example = :blog
8
+
9
+ connection_info = connection_info_for(@example, @adapter)
10
+ pending("The #{@example} spec does not run for #{@adapter}") if connection_info.nil?
11
+ initialize_connection connection_info
12
+
13
+ require File.expand_path(File.dirname(__FILE__) + '/../../examples/blog_migration')
14
+ create_blog_tables
15
+
16
+ silence_warnings { RAILS_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../output/functional/#{@example}_#{@adapter}") }
17
+ FileUtils.mkdir_p(RAILS_ROOT + '/app/models')
18
+ FileUtils.mkdir_p(RAILS_ROOT + '/spec')
19
+
20
+ LegacyData::Schema.stub!(:log)
21
+
22
+ @expected_directory = File.expand_path("#{File.dirname(__FILE__)}/../../examples/generated/#{@example}_#{@adapter}")
23
+ end
24
+ after :all do
25
+ Object.send(:remove_const, :RAILS_ROOT)
26
+ end
27
+
28
+ before :each do #
29
+ pending("oracle does not yet work with t.foreign_key table creation") if @adapter == 'oracle'
30
+ FileUtils.rm(RAILS_ROOT + '/spec/factories.rb', :force => true)
31
+ invoke_generator('models_from_tables', ["--with-factories"], :create)
32
+ end
33
+
34
+ %w( post comment tag ).each do |model|
35
+ it "should generate the expected #{model} model" do
36
+ File.read(RAILS_ROOT + "/app/models/#{model}.rb").should == File.read("#{@expected_directory}/#{model}.rb")
37
+ end
38
+ end
39
+
40
+ it "should generated the expected factories" do
41
+ File.read(RAILS_ROOT + '/spec/factories.rb').should == File.read("#{@expected_directory}/factories.rb")
42
+ end
43
+
44
+ end
45
+
@@ -0,0 +1,45 @@
1
+ # CREATE DATABASE legacy_data_blog;
2
+ mysql_blog:
3
+ adapter: mysql
4
+ database: legacy_data_blog
5
+ username: root
6
+ password:
7
+
8
+ sqlite3_blog:
9
+ adapter: sqlite3
10
+ database: ':memory:'
11
+
12
+ # CREATE USER legacy_data_blog IDENTIFIED BY legacy_data_blog;
13
+ # GRANT ALL PRIVILEGES TO legacy_data_blog;
14
+ oracle_blog:
15
+ adapter: oracle_enhanced
16
+ database: localhost:1521/XE
17
+ username: legacy_data_blog
18
+ password: legacy_data_blog
19
+
20
+ # CREATE DATABASE drupal_6_14;
21
+ mysql_drupal:
22
+ adapter: mysql
23
+ database: drupal_6_14
24
+ username: root
25
+ password:
26
+
27
+ # CREATE DATABASE j2ee_petstore;
28
+ mysql_j2ee_petstore:
29
+ adapter: mysql
30
+ database: j2ee_petstore
31
+ username: root
32
+ password:
33
+
34
+ sqlite3_j2ee_petstore:
35
+ adapter: sqlite3
36
+ database: ':memory:'
37
+
38
+ # CREATE USER petstore IDENTIFIED BY petstore;
39
+ # GRANT ALL PRIVILEGES TO petstore;
40
+ oracle_j2ee_petstore:
41
+ adapter: oracle_enhanced
42
+ database: localhost:1521/XE
43
+ username: petstore
44
+ password: petstore
45
+
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/functional_spec_helper')
2
+
3
+ if ENV['ADAPTER'] == 'mysql'
4
+
5
+ def create_drupal_schema
6
+ execute_sql_script(File.expand_path(File.dirname(__FILE__) + '/../../examples/drupal_6_14.sql'))
7
+ end
8
+
9
+ describe "Generating models from a drupal 6.14 #{ENV['ADAPTER']} database" do
10
+ before :all do
11
+ @adapter = ENV['ADAPTER']
12
+ @example = :drupal
13
+
14
+ connection_info = connection_info_for(@example, @adapter)
15
+ pending("The #{@example} spec does not run for #{@adapter}") if connection_info.nil?
16
+ initialize_connection connection_info
17
+ create_drupal_schema
18
+
19
+ silence_warnings { RAILS_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../output/functional/#{@example}_#{@adapter}") }
20
+ FileUtils.mkdir_p(RAILS_ROOT + '/app/models')
21
+ FileUtils.mkdir_p(RAILS_ROOT + '/spec')
22
+
23
+ LegacyData::Schema.stub!(:log)
24
+
25
+ @expected_directory = File.expand_path("#{File.dirname(__FILE__)}/../../examples/generated/#{@example}_#{@adapter}")
26
+
27
+ LegacyData::TableClassNameMapper.dictionary['files'] = 'UploadedFiles' #to avoid collision with Ruby File class
28
+ LegacyData::TableClassNameMapper.dictionary['cache'] = 'Cache' #don't strip the e to make it cach
29
+
30
+ FileUtils.rm(RAILS_ROOT + '/spec/factories.rb', :force => true)
31
+ invoke_generator('models_from_tables', ["--with-factories"], :create)
32
+
33
+ end
34
+ after :all do
35
+ Object.send(:remove_const, :RAILS_ROOT)
36
+ end
37
+
38
+ %w( access action actions_aid authmap batch block blocks_role
39
+ box cache cache_block cache_filter cache_form cache_menu cache_page
40
+ cache_update comment filter filter_format flood history menu_custom
41
+ menu_link menu_router node node_access node_comment_statistic node_counter node_revision
42
+ node_type permission role session system term_data term_hierarchy
43
+ term_node term_relation term_synonym uploaded_files url_alias user users_role
44
+ variable vocabulary vocabulary_node_type watchdog ).each do |model|
45
+ it "should generate the expected #{model} model" do
46
+ File.read(RAILS_ROOT + "/app/models/#{model}.rb").should == File.read("#{@expected_directory}/#{model}.rb")
47
+ end
48
+ end
49
+
50
+ it "should generated the expected factories" do
51
+ File.read(RAILS_ROOT + '/spec/factories.rb').should == File.read("#{@expected_directory}/factories.rb")
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,27 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
+ ADAPTER = ENV['ADAPTER']
3
4
 
5
+
6
+ def initialize_connection connection_info
7
+ ActiveRecord::Base.establish_connection(connection_info)
8
+
9
+ begin
10
+ require "foreigner/connection_adapters/#{ActiveRecord::Base.connection.adapter_name.downcase}_adapter"
11
+ rescue LoadError
12
+ #foreigner does not support all adapters (i.e. sqlite3)
13
+ end
14
+ end
15
+
16
+
17
+ def connection_info_for example, adapter
18
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
19
+ config["#{adapter}_#{example}"]
20
+ end
21
+
22
+ def execute_sql_script script_filename
23
+ sql_script = File.read script_filename
24
+ sql_script.split(';').each do |sql_statement|
25
+ ActiveRecord::Base.connection.execute(sql_statement) unless sql_statement.blank?
26
+ end
27
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/functional_spec_helper')
2
+
3
+ def create_j2ee_petstore_schema
4
+ execute_sql_script(File.expand_path(File.dirname(__FILE__) + '/../../examples/delete_j2ee_petstore.sql') ) unless ENV['ADAPTER'] == 'sqlite3' rescue nil
5
+ execute_sql_script(File.expand_path(File.dirname(__FILE__) + '/../../examples/create_j2ee_petstore.sql') )
6
+ end
7
+
8
+ describe "Generating models from j2ee petstore #{ENV['ADAPTER']} database" do
9
+ before :all do
10
+ @adapter = ENV['ADAPTER']
11
+ @example = :j2ee_petstore
12
+
13
+ connection_info = connection_info_for(@example, @adapter)
14
+ pending("The #{@example} spec does not run for #{@adapter}") if connection_info.nil?
15
+ initialize_connection connection_info
16
+ create_j2ee_petstore_schema
17
+
18
+ silence_warnings { RAILS_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../output/functional/#{@example}_#{@adapter}") }
19
+ FileUtils.mkdir_p(RAILS_ROOT + '/app/models')
20
+ FileUtils.mkdir_p(RAILS_ROOT + '/spec')
21
+
22
+ LegacyData::Schema.stub!(:log)
23
+
24
+ @expected_directory = File.expand_path("#{File.dirname(__FILE__)}/../../examples/generated/#{@example}_#{@adapter}")
25
+
26
+ LegacyData::TableClassNameMapper.dictionary['files'] = 'UploadedFiles' #to avoid collision with Ruby File class
27
+ LegacyData::TableClassNameMapper.dictionary['cache'] = 'Cache' #don't strip the e to make it cach
28
+
29
+ FileUtils.rm(RAILS_ROOT + '/spec/factories.rb', :force => true)
30
+ invoke_generator('models_from_tables', ["--with-factories"], :create)
31
+
32
+ end
33
+
34
+
35
+ after :all do
36
+ Object.send(:remove_const, :RAILS_ROOT)
37
+ end
38
+
39
+ models = %w( address category id_gen item
40
+ product tag ziplocation )
41
+
42
+ if ENV['ADAPTER'] == 'oracle'
43
+ models + %w( sellercontactinfo )
44
+ else
45
+ models + %w( seller_contact_info tag_info)
46
+ end
47
+
48
+ models.each do |model|
49
+ it "should generate the expected #{model} model" do
50
+ File.read(RAILS_ROOT + "/app/models/#{model}.rb").should == File.read("#{@expected_directory}/#{model}.rb")
51
+ end
52
+ end
53
+
54
+ it "should generated the expected factories" do
55
+ File.read(RAILS_ROOT + '/spec/factories.rb').should == File.read("#{@expected_directory}/factories.rb")
56
+ end
57
+ end