redhillonrails_core 1.0.9.1 → 1.1.0

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.
Files changed (58) hide show
  1. data/CHANGELOG +7 -0
  2. data/README.rdoc +31 -47
  3. data/lib/redhillonrails_core/active_record/base.rb +63 -0
  4. data/lib/redhillonrails_core/active_record/connection_adapters/abstract_adapter.rb +75 -0
  5. data/lib/redhillonrails_core/active_record/connection_adapters/column.rb +25 -0
  6. data/lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb +30 -0
  7. data/lib/redhillonrails_core/active_record/connection_adapters/index_definition.rb +17 -0
  8. data/lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb +78 -0
  9. data/lib/redhillonrails_core/active_record/connection_adapters/mysql_column.rb +12 -0
  10. data/lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb +160 -0
  11. data/lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb +111 -0
  12. data/lib/redhillonrails_core/active_record/connection_adapters/table_definition.rb +31 -0
  13. data/lib/redhillonrails_core/active_record/schema.rb +27 -0
  14. data/lib/redhillonrails_core/active_record/schema_dumper.rb +70 -0
  15. data/lib/redhillonrails_core.rb +20 -26
  16. data/redhillonrails_core.gemspec +34 -40
  17. data/spec/connections/mysql/connection.rb +18 -0
  18. data/spec/connections/mysql2/connection.rb +18 -0
  19. data/spec/connections/postgresql/connection.rb +15 -0
  20. data/spec/connections/sqlite3/connection.rb +14 -0
  21. data/spec/foreign_key_spec.rb +100 -0
  22. data/spec/index_definition_spec.rb +145 -0
  23. data/spec/index_spec.rb +67 -0
  24. data/spec/models/comment.rb +5 -0
  25. data/spec/models/post.rb +6 -0
  26. data/spec/models/user.rb +5 -0
  27. data/spec/schema/schema.rb +21 -0
  28. data/spec/schema_dumper_spec.rb +117 -0
  29. data/spec/spec_helper.rb +16 -0
  30. data/spec/support/reference.rb +66 -0
  31. metadata +32 -57
  32. data/.document +0 -5
  33. data/.gitignore +0 -23
  34. data/.rvmrc +0 -1
  35. data/Gemfile +0 -20
  36. data/Gemfile.lock +0 -50
  37. data/Rakefile +0 -76
  38. data/VERSION +0 -1
  39. data/examples/example_helper.rb +0 -44
  40. data/examples/postgresql_index_parser_example.rb +0 -198
  41. data/lib/red_hill_consulting/core/active_record/base.rb +0 -61
  42. data/lib/red_hill_consulting/core/active_record/connection_adapters/abstract_adapter.rb +0 -71
  43. data/lib/red_hill_consulting/core/active_record/connection_adapters/column.rb +0 -21
  44. data/lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb +0 -26
  45. data/lib/red_hill_consulting/core/active_record/connection_adapters/index_definition.rb +0 -13
  46. data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb +0 -37
  47. data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql5_adapter.rb +0 -40
  48. data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_adapter.rb +0 -103
  49. data/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_column.rb +0 -8
  50. data/lib/red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter.rb +0 -178
  51. data/lib/red_hill_consulting/core/active_record/connection_adapters/schema_statements.rb +0 -23
  52. data/lib/red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter.rb +0 -109
  53. data/lib/red_hill_consulting/core/active_record/connection_adapters/table_definition.rb +0 -27
  54. data/lib/red_hill_consulting/core/active_record/schema.rb +0 -25
  55. data/lib/red_hill_consulting/core/active_record/schema_dumper.rb +0 -66
  56. data/lib/red_hill_consulting/core/active_record.rb +0 -4
  57. data/lib/red_hill_consulting/core.rb +0 -4
  58. data/tasks/db/comments.rake +0 -9
@@ -0,0 +1,111 @@
1
+ module RedhillonrailsCore
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+ module Sqlite3Adapter
5
+ def self.included(base)
6
+ base.class_eval do
7
+ alias_method_chain :tables, :redhillonrails_core
8
+ end
9
+ end
10
+
11
+ def move_table(from, to, options = {}, &block) #:nodoc:
12
+ copy_table(from, to, options, &block)
13
+ drop_table(from, options)
14
+ end
15
+
16
+ def add_foreign_key(from_table_name, from_column_names, to_table_name, to_column_names, options = {})
17
+ initialize_sqlite3_foreign_key_table
18
+ from_column_names = Array(from_column_names)
19
+ to_column_names = Array(to_column_names)
20
+ fk_name = options[:name] || ["fk", from_table_name, *to_column_names].join("_")
21
+
22
+ columns = %w(name from_table_name from_column_names to_table_name to_column_names)
23
+ values = [fk_name, from_table_name, from_column_names.join(","), to_table_name, to_column_names.join(",")]
24
+
25
+ quoted_values = values.map { |x| quote(x.to_s) }.join(",")
26
+
27
+ # TODO: support options
28
+
29
+ insert <<-SQL
30
+ INSERT INTO #{sqlite3_foreign_key_table}(#{quoted_columns(columns)})
31
+ VALUES (#{quoted_values})
32
+ SQL
33
+ end
34
+
35
+ def remove_foreign_key(table_name, foreign_key_name, options = {})
36
+ return if options[:temporary] == true
37
+ initialize_sqlite3_foreign_key_table
38
+
39
+ rows_deleted = delete <<-SQL
40
+ DELETE FROM #{sqlite3_foreign_key_table}
41
+ WHERE #{quote_column_name("name")} = #{quote(foreign_key_name.to_s)}
42
+ AND #{quote_column_name("from_table_name")} = #{quote(table_name.to_s)}
43
+ SQL
44
+
45
+ if rows_deleted != 1
46
+ raise ActiveRecord::ActiveRecordError, "Foreign-key '#{foreign_key_name}' on table '#{table_name}' not found"
47
+ end
48
+ end
49
+
50
+ def tables_with_redhillonrails_core(name=nil)
51
+ tables_without_redhillonrails_core.reject{ |name| name == sqlite3_foreign_key_table }
52
+ end
53
+
54
+ def foreign_keys(table_name, name = nil)
55
+ load_foreign_keys("from_table_name", table_name, name)
56
+ end
57
+
58
+ def reverse_foreign_keys(table_name, name = nil)
59
+ load_foreign_keys("to_table_name", table_name, name)
60
+ end
61
+
62
+ private
63
+
64
+ def quoted_columns(columns)
65
+ columns.map { |x| quote_column_name(x) }.join(",")
66
+ end
67
+
68
+ def sqlite3_foreign_key_table
69
+ "sqlite3_foreign_keys"
70
+ end
71
+
72
+ def initialize_sqlite3_foreign_key_table
73
+ unless sqlite3_foreign_key_table_exists?
74
+ create_table(sqlite3_foreign_key_table, :id => false) do |t|
75
+ t.string "name", :null => false
76
+ t.string "from_table_name", :null => false
77
+ t.string "from_column_names", :null => false
78
+ t.string "to_table_name", :null => false
79
+ t.string "to_column_names", :null => false
80
+ end
81
+ add_index(sqlite3_foreign_key_table, "name", :unique => true)
82
+ add_index(sqlite3_foreign_key_table, "from_table_name", :unique => false)
83
+ add_index(sqlite3_foreign_key_table, "to_table_name", :unique => false)
84
+ end
85
+ end
86
+
87
+ def sqlite3_foreign_key_table_exists?
88
+ tables_without_redhillonrails_core.detect { |name| name == sqlite3_foreign_key_table }
89
+ end
90
+
91
+ def load_foreign_keys(discriminating_column, table_name, name = nil)
92
+ rows = select_all(<<-SQL, name)
93
+ SELECT *
94
+ FROM #{sqlite3_foreign_key_table}
95
+ WHERE #{quote_column_name(discriminating_column)} = #{quote(table_name.to_s)}
96
+ SQL
97
+
98
+ rows.map do |row|
99
+ ForeignKeyDefinition.new(
100
+ row["name"],
101
+ row["from_table_name"], row["from_column_names"].split(","),
102
+ row["to_table_name"], row["to_column_names"].split(",")
103
+ )
104
+ end
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,31 @@
1
+ module RedhillonrailsCore
2
+ module ActiveRecord
3
+ module ConnectionAdapters
4
+ module TableDefinition
5
+ def self.included(base)
6
+ base.class_eval do
7
+ attr_accessor :name
8
+ alias_method_chain :initialize, :redhillonrails_core
9
+ alias_method_chain :to_sql, :redhillonrails_core
10
+ end
11
+ end
12
+
13
+ def initialize_with_redhillonrails_core(*args)
14
+ initialize_without_redhillonrails_core(*args)
15
+ @foreign_keys = []
16
+ end
17
+
18
+ def foreign_key(column_names, references_table_name, references_column_names, options = {})
19
+ @foreign_keys << ForeignKeyDefinition.new(options[:name], nil, column_names, ::ActiveRecord::Migrator.proper_table_name(references_table_name), references_column_names, options[:on_update], options[:on_delete], options[:deferrable])
20
+ self
21
+ end
22
+
23
+ def to_sql_with_redhillonrails_core
24
+ sql = to_sql_without_redhillonrails_core
25
+ sql << ', ' << @foreign_keys * ', ' unless @foreign_keys.empty? || ::ActiveRecord::Schema.defining?
26
+ sql
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module RedhillonrailsCore
2
+ module ActiveRecord
3
+ module Schema
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def self.extended(base)
10
+ class << base
11
+ attr_accessor :defining
12
+ alias :defining? :defining
13
+
14
+ alias_method_chain :define, :redhillonrails_core
15
+ end
16
+ end
17
+
18
+ def define_with_redhillonrails_core(info={}, &block)
19
+ self.defining = true
20
+ define_without_redhillonrails_core(info, &block)
21
+ ensure
22
+ self.defining = false
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,70 @@
1
+ module RedhillonrailsCore
2
+ module ActiveRecord
3
+ module SchemaDumper
4
+ def self.included(base)
5
+ base.class_eval do
6
+ private
7
+ alias_method_chain :tables, :redhillonrails_core
8
+ alias_method_chain :indexes, :redhillonrails_core
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def tables_with_redhillonrails_core(stream)
15
+ @foreign_keys = StringIO.new
16
+ begin
17
+ tables_without_redhillonrails_core(stream)
18
+ @foreign_keys.rewind
19
+ stream.print @foreign_keys.read
20
+ views(stream)
21
+ ensure
22
+ @foreign_keys = nil
23
+ end
24
+ end
25
+
26
+ def indexes_with_redhillonrails_core(table, stream)
27
+ indexes = @connection.indexes(table)
28
+ indexes.each do |index|
29
+ unless index.columns.blank?
30
+ stream.print " add_index #{index.table.inspect}, #{index.columns.inspect}, :name => #{index.name.inspect}"
31
+ stream.print ", :unique => true" if index.unique
32
+ stream.print ", :kind => \"#{index.kind}\"" unless index.kind.blank?
33
+ stream.print ", :case_sensitive => false" unless index.case_sensitive?
34
+ stream.print ", :conditions => #{index.conditions.inspect}" unless index.conditions.blank?
35
+ else
36
+ stream.print " add_index #{index.table.inspect}"
37
+ stream.print ", :name => #{index.name.inspect}"
38
+ stream.print ", :kind => \"#{index.kind}\"" unless index.kind.blank?
39
+ stream.print ", :expression => #{index.expression.inspect}"
40
+ end
41
+
42
+ stream.puts
43
+ end
44
+ stream.puts unless indexes.empty?
45
+
46
+ foreign_keys(table, @foreign_keys)
47
+ end
48
+
49
+ def foreign_keys(table, stream)
50
+ foreign_keys = @connection.foreign_keys(table)
51
+ foreign_keys.each do |foreign_key|
52
+ stream.print " "
53
+ stream.print foreign_key.to_dump
54
+ stream.puts
55
+ end
56
+ stream.puts unless foreign_keys.empty?
57
+ end
58
+
59
+ def views(stream)
60
+ views = @connection.views
61
+ views.each do |view_name|
62
+ definition = @connection.view_definition(view_name)
63
+ stream.print " create_view #{view_name.inspect}, #{definition.inspect}"
64
+ stream.puts
65
+ end
66
+ stream.puts unless views.empty?
67
+ end
68
+ end
69
+ end
70
+ end
@@ -6,32 +6,26 @@ rescue
6
6
  end
7
7
 
8
8
  # TODO: only needed adapters should be required here
9
- require 'red_hill_consulting/core'
10
- require 'red_hill_consulting/core/active_record'
11
- require 'red_hill_consulting/core/active_record/base'
12
- require 'red_hill_consulting/core/active_record/schema'
13
- require 'red_hill_consulting/core/active_record/schema_dumper'
14
- require 'red_hill_consulting/core/active_record/connection_adapters/abstract_adapter'
15
- require 'red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition'
16
- require 'red_hill_consulting/core/active_record/connection_adapters/column'
17
- require 'red_hill_consulting/core/active_record/connection_adapters/index_definition'
18
- require 'red_hill_consulting/core/active_record/connection_adapters/mysql_column'
19
- require 'red_hill_consulting/core/active_record/connection_adapters/schema_statements'
20
- require 'red_hill_consulting/core/active_record/connection_adapters/table_definition'
9
+ require 'redhillonrails_core/active_record/base'
10
+ require 'redhillonrails_core/active_record/schema'
11
+ require 'redhillonrails_core/active_record/schema_dumper'
12
+ require 'redhillonrails_core/active_record/connection_adapters/abstract_adapter'
13
+ require 'redhillonrails_core/active_record/connection_adapters/foreign_key_definition'
14
+ require 'redhillonrails_core/active_record/connection_adapters/column'
15
+ require 'redhillonrails_core/active_record/connection_adapters/index_definition'
16
+ require 'redhillonrails_core/active_record/connection_adapters/mysql_column'
17
+ require 'redhillonrails_core/active_record/connection_adapters/table_definition'
21
18
 
22
- module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
23
- autoload :MysqlAdapter, 'red_hill_consulting/core/active_record/connection_adapters/mysql_adapter'
24
- autoload :Mysql4Adapter, 'red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter'
25
- autoload :Mysql5Adapter, 'red_hill_consulting/core/active_record/connection_adapters/mysql5_adapter'
26
- autoload :PostgresqlAdapter, 'red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter'
27
- autoload :Sqlite3Adapter, 'red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter'
19
+ module RedhillonrailsCore::ActiveRecord::ConnectionAdapters
20
+ autoload :MysqlAdapter, 'redhillonrails_core/active_record/connection_adapters/mysql_adapter'
21
+ autoload :PostgresqlAdapter, 'redhillonrails_core/active_record/connection_adapters/postgresql_adapter'
22
+ autoload :Sqlite3Adapter, 'redhillonrails_core/active_record/connection_adapters/sqlite3_adapter'
28
23
  end
29
24
 
30
- ActiveRecord::Base.send(:include, RedHillConsulting::Core::ActiveRecord::Base)
31
- ActiveRecord::Schema.send(:include, RedHillConsulting::Core::ActiveRecord::Schema)
32
- ActiveRecord::SchemaDumper.send(:include, RedHillConsulting::Core::ActiveRecord::SchemaDumper)
33
- ActiveRecord::ConnectionAdapters::IndexDefinition.send(:include, RedHillConsulting::Core::ActiveRecord::ConnectionAdapters::IndexDefinition)
34
- ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, RedHillConsulting::Core::ActiveRecord::ConnectionAdapters::TableDefinition)
35
- ActiveRecord::ConnectionAdapters::Column.send(:include, RedHillConsulting::Core::ActiveRecord::ConnectionAdapters::Column)
36
- ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, RedHillConsulting::Core::ActiveRecord::ConnectionAdapters::AbstractAdapter)
37
- ActiveRecord::ConnectionAdapters::SchemaStatements.send(:include, RedHillConsulting::Core::ActiveRecord::ConnectionAdapters::SchemaStatements)
25
+ ActiveRecord::Base.send(:include, RedhillonrailsCore::ActiveRecord::Base)
26
+ ActiveRecord::Schema.send(:include, RedhillonrailsCore::ActiveRecord::Schema)
27
+ ActiveRecord::SchemaDumper.send(:include, RedhillonrailsCore::ActiveRecord::SchemaDumper)
28
+ ActiveRecord::ConnectionAdapters::IndexDefinition.send(:include, RedhillonrailsCore::ActiveRecord::ConnectionAdapters::IndexDefinition)
29
+ ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, RedhillonrailsCore::ActiveRecord::ConnectionAdapters::TableDefinition)
30
+ ActiveRecord::ConnectionAdapters::Column.send(:include, RedhillonrailsCore::ActiveRecord::ConnectionAdapters::Column)
31
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, RedhillonrailsCore::ActiveRecord::ConnectionAdapters::AbstractAdapter)
@@ -5,75 +5,69 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{redhillonrails_core}
8
- s.version = "1.0.9.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Micha\305\202 \305\201omnicki"]
12
- s.date = %q{2010-12-13}
13
- s.description = %q{RedHill on Rails Core is a plugin that features to support other RedHill on Rails plugins. It creates and drops views and foreign-keys or obtains indexes directly from a model class.}
12
+ s.date = %q{2011-01-09}
13
+ s.description = %q{Adds support in ActiveRecord for foreign_keys, complex indexes and other database-related stuff. Easily create foreign_keys, complex indexes and views.}
14
14
  s.email = %q{michal.lomnicki@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
- ".document",
20
- ".gitignore",
21
- ".rvmrc",
22
- "CHANGELOG",
23
- "Gemfile",
24
- "Gemfile.lock",
19
+ "CHANGELOG",
25
20
  "MIT-LICENSE",
26
21
  "README.rdoc",
27
- "Rakefile",
28
- "VERSION",
29
- "examples/example_helper.rb",
30
- "examples/postgresql_index_parser_example.rb",
31
22
  "init.rb",
32
- "lib/red_hill_consulting/core.rb",
33
- "lib/red_hill_consulting/core/active_record.rb",
34
- "lib/red_hill_consulting/core/active_record/base.rb",
35
- "lib/red_hill_consulting/core/active_record/connection_adapters/abstract_adapter.rb",
36
- "lib/red_hill_consulting/core/active_record/connection_adapters/column.rb",
37
- "lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb",
38
- "lib/red_hill_consulting/core/active_record/connection_adapters/index_definition.rb",
39
- "lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb",
40
- "lib/red_hill_consulting/core/active_record/connection_adapters/mysql5_adapter.rb",
41
- "lib/red_hill_consulting/core/active_record/connection_adapters/mysql_adapter.rb",
42
- "lib/red_hill_consulting/core/active_record/connection_adapters/mysql_column.rb",
43
- "lib/red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter.rb",
44
- "lib/red_hill_consulting/core/active_record/connection_adapters/schema_statements.rb",
45
- "lib/red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter.rb",
46
- "lib/red_hill_consulting/core/active_record/connection_adapters/table_definition.rb",
47
- "lib/red_hill_consulting/core/active_record/schema.rb",
48
- "lib/red_hill_consulting/core/active_record/schema_dumper.rb",
49
23
  "lib/redhillonrails_core.rb",
50
- "redhillonrails_core.gemspec",
51
- "tasks/db/comments.rake"
24
+ "lib/redhillonrails_core/active_record/base.rb",
25
+ "lib/redhillonrails_core/active_record/connection_adapters/abstract_adapter.rb",
26
+ "lib/redhillonrails_core/active_record/connection_adapters/column.rb",
27
+ "lib/redhillonrails_core/active_record/connection_adapters/foreign_key_definition.rb",
28
+ "lib/redhillonrails_core/active_record/connection_adapters/index_definition.rb",
29
+ "lib/redhillonrails_core/active_record/connection_adapters/mysql_adapter.rb",
30
+ "lib/redhillonrails_core/active_record/connection_adapters/mysql_column.rb",
31
+ "lib/redhillonrails_core/active_record/connection_adapters/postgresql_adapter.rb",
32
+ "lib/redhillonrails_core/active_record/connection_adapters/sqlite3_adapter.rb",
33
+ "lib/redhillonrails_core/active_record/connection_adapters/table_definition.rb",
34
+ "lib/redhillonrails_core/active_record/schema.rb",
35
+ "lib/redhillonrails_core/active_record/schema_dumper.rb",
36
+ "redhillonrails_core.gemspec"
52
37
  ]
53
38
  s.homepage = %q{http://github.com/mlomnicki/redhillonrails_core}
54
39
  s.rdoc_options = ["--charset=UTF-8"]
55
40
  s.require_paths = ["lib"]
56
- s.rubygems_version = %q{1.3.7}
57
- s.summary = %q{RedHill on Rails Core is a plugin that features to support other RedHill on Rails plugins}
41
+ s.rubygems_version = %q{1.3.6}
42
+ s.summary = %q{Adds support in ActiveRecord for foreign_keys, complex indexes and other database-related stuff}
58
43
  s.test_files = [
59
- "examples/example_helper.rb",
60
- "examples/postgresql_index_parser_example.rb"
44
+ "spec/schema/schema.rb",
45
+ "spec/foreign_key_spec.rb",
46
+ "spec/schema_dumper_spec.rb",
47
+ "spec/connections/mysql2/connection.rb",
48
+ "spec/connections/postgresql/connection.rb",
49
+ "spec/connections/sqlite3/connection.rb",
50
+ "spec/connections/mysql/connection.rb",
51
+ "spec/support/reference.rb",
52
+ "spec/index_definition_spec.rb",
53
+ "spec/spec_helper.rb",
54
+ "spec/index_spec.rb",
55
+ "spec/models/user.rb",
56
+ "spec/models/post.rb",
57
+ "spec/models/comment.rb"
61
58
  ]
62
59
 
63
60
  if s.respond_to? :specification_version then
64
61
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
65
62
  s.specification_version = 3
66
63
 
67
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
64
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
68
65
  s.add_runtime_dependency(%q<activerecord>, [">= 0"])
69
- s.add_development_dependency(%q<micronaut>, [">= 0"])
70
66
  else
71
67
  s.add_dependency(%q<activerecord>, [">= 0"])
72
- s.add_dependency(%q<micronaut>, [">= 0"])
73
68
  end
74
69
  else
75
70
  s.add_dependency(%q<activerecord>, [">= 0"])
76
- s.add_dependency(%q<micronaut>, [">= 0"])
77
71
  end
78
72
  end
79
73
 
@@ -0,0 +1,18 @@
1
+ print "Using MySQL\n"
2
+ require 'logger'
3
+
4
+ ActiveRecord::Base.logger = Logger.new("debug.log")
5
+
6
+ ActiveRecord::Base.configurations = {
7
+ 'redhillonrails' => {
8
+ :adapter => 'mysql',
9
+ :database => 'redhillonrails_core_test',
10
+ :username => 'redhillonrails',
11
+ :encoding => 'utf8',
12
+ :socket => '/var/run/mysqld/mysqld.sock',
13
+ :min_messages => 'warning'
14
+ }
15
+
16
+ }
17
+
18
+ ActiveRecord::Base.establish_connection 'redhillonrails'
@@ -0,0 +1,18 @@
1
+ print "Using MySQL2\n"
2
+ require 'logger'
3
+
4
+ ActiveRecord::Base.logger = Logger.new("debug.log")
5
+
6
+ ActiveRecord::Base.configurations = {
7
+ 'redhillonrails' => {
8
+ :adapter => 'mysql2',
9
+ :database => 'redhillonrails_core_test',
10
+ :username => 'redhillonrails',
11
+ :encoding => 'utf8',
12
+ :socket => '/var/run/mysqld/mysqld.sock',
13
+ :min_messages => 'warning'
14
+ }
15
+
16
+ }
17
+
18
+ ActiveRecord::Base.establish_connection 'redhillonrails'
@@ -0,0 +1,15 @@
1
+ print "Using PostgreSQL\n"
2
+ require 'logger'
3
+
4
+ ActiveRecord::Base.logger = Logger.new("debug.log")
5
+
6
+ ActiveRecord::Base.configurations = {
7
+ 'redhillonrails' => {
8
+ :adapter => 'postgresql',
9
+ :database => 'redhillonrails_core_test',
10
+ :min_messages => 'warning'
11
+ }
12
+
13
+ }
14
+
15
+ ActiveRecord::Base.establish_connection 'redhillonrails'
@@ -0,0 +1,14 @@
1
+ print "Using SQLite3\n"
2
+ require 'logger'
3
+
4
+ ActiveRecord::Base.logger = Logger.new("debug.log")
5
+
6
+ ActiveRecord::Base.configurations = {
7
+ 'redhillonrails' => {
8
+ :adapter => 'sqlite3',
9
+ :database => File.expand_path(File.dirname(__FILE__) + 'redhillonrails_core.db')
10
+ }
11
+
12
+ }
13
+
14
+ ActiveRecord::Base.establish_connection 'redhillonrails'
@@ -0,0 +1,100 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'models/user'
4
+ require 'models/post'
5
+ require 'models/comment'
6
+
7
+ describe "Foreign Key" do
8
+
9
+ let(:migration) { ::ActiveRecord::Migration }
10
+
11
+ context "when is added", "posts(author_id)" do
12
+
13
+ before(:each) do
14
+ add_foreign_key(:posts, :author_id, :users, :id, :on_update => :cascade, :on_delete => :restrict)
15
+ end
16
+
17
+ after(:each) do
18
+ fk = Post.foreign_keys.detect { |fk| fk.column_names == %w[author_id] }
19
+ remove_foreign_key(:posts, fk.name)
20
+ end
21
+
22
+ it "references users(id)" do
23
+ Post.should reference(:users, :id).on(:author_id)
24
+ end
25
+
26
+ it "cascades on update" do
27
+ Post.should reference(:users).on_update(:cascade)
28
+ end
29
+
30
+ it "restricts on delete" do
31
+ Post.should reference(:users).on_delete(:restrict)
32
+ end
33
+
34
+ it "is available in Post.foreign_keys" do
35
+ Post.foreign_keys.collect(&:column_names).should include(%w[author_id])
36
+ end
37
+
38
+ it "is available in User.reverse_foreign_keys" do
39
+ User.reverse_foreign_keys.collect(&:column_names).should include(%w[author_id])
40
+ end
41
+
42
+ end
43
+
44
+ context "when is dropped", "comments(post_id)" do
45
+
46
+ let(:foreign_key_name) { Comment.foreign_keys.detect { |definition| definition.column_names == %w[post_id] }.name }
47
+
48
+ before(:each) do
49
+ remove_foreign_key(:comments, foreign_key_name)
50
+ end
51
+
52
+ after(:each) do
53
+ add_foreign_key(:comments, :post_id, :posts, :id)
54
+ end
55
+
56
+ it "doesn't reference posts(id)" do
57
+ Comment.should_not reference(:posts).on(:post_id)
58
+ end
59
+
60
+ it "is no longer available in Post.foreign_keys" do
61
+ Comment.foreign_keys.collect(&:column_names).should_not include(%w[post_id])
62
+ end
63
+
64
+ it "is no longer available in User.reverse_foreign_keys" do
65
+ Post.reverse_foreign_keys.collect(&:column_names).should_not include(%w[post_id])
66
+ end
67
+
68
+ end
69
+
70
+ context "when referencing column and column is removed" do
71
+
72
+ let(:foreign_key_name) { Comment.foreign_keys.detect { |definition| definition.column_names == %w[post_id] }.name }
73
+
74
+ it "should remove foreign keys" do
75
+ remove_foreign_key(:comments, foreign_key_name)
76
+ Post.reverse_foreign_keys.collect { |fk| fk.column_names == %w[post_id] && fk.table_name == "comments" }.should be_empty
77
+ end
78
+
79
+ end
80
+
81
+ protected
82
+ def add_foreign_key(*args)
83
+ migration.suppress_messages do
84
+ migration.add_foreign_key(*args)
85
+ end
86
+ User.reset_column_information
87
+ Post.reset_column_information
88
+ Comment.reset_column_information
89
+ end
90
+
91
+ def remove_foreign_key(*args)
92
+ migration.suppress_messages do
93
+ migration.remove_foreign_key(*args)
94
+ end
95
+ User.reset_column_information
96
+ Post.reset_column_information
97
+ Comment.reset_column_information
98
+ end
99
+
100
+ end