schema_plus 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/README.rdoc +11 -8
- data/gemfiles/Gemfile.rails-2.3.lock +65 -0
- data/gemfiles/Gemfile.rails-3.0.lock +111 -0
- data/gemfiles/Gemfile.rails-3.1 +1 -1
- data/gemfiles/Gemfile.rails-3.1.lock +123 -0
- data/lib/schema_plus.rb +1 -2
- data/lib/schema_plus/active_record/base.rb +3 -10
- data/lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb +11 -1
- data/lib/schema_plus/active_record/connection_adapters/column.rb +3 -2
- data/lib/schema_plus/active_record/connection_adapters/table_definition.rb +1 -1
- data/lib/schema_plus/active_record/foreign_keys.rb +177 -0
- data/lib/schema_plus/version.rb +1 -1
- data/runspecs +35 -16
- data/spec/migration_spec.rb +10 -8
- data/spec/rails3_migration_spec.rb +138 -0
- data/spec/references_spec.rb +1 -1
- data/spec/support/helpers.rb +4 -0
- metadata +102 -129
- data/lib/schema_plus/active_record/migration.rb +0 -220
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -86,17 +86,21 @@ Here are some examples:
|
|
86
86
|
t.integer :author_id # automatically references table 'authors', key id
|
87
87
|
t.integer :parent_id # special name parent_id automatically references its own table (for tree nodes)
|
88
88
|
t.integer :author, :references => :authors # non-conventional column name needs :references for a constraint
|
89
|
-
t.integer :author_id, :
|
90
|
-
t.integer :author_id, :
|
89
|
+
t.integer :author_id, :references => :authors # same as automatic behavior
|
90
|
+
t.integer :author_id, :references => [:authors, :id] # same as automatic behavior
|
91
91
|
t.integer :author_id, :references => :people # override table name
|
92
92
|
t.integer :author_id, :references => [:people, :ssn] # override table name and key
|
93
|
-
t.integer :author_id, :
|
94
|
-
|
93
|
+
t.integer :author_id, :references => nil # don't create a constraint
|
95
94
|
|
96
95
|
You can also modify the behavior using +:on_delete+, +:on_update+, and +:deferrable+
|
97
96
|
|
98
97
|
t.integer :author_id, :on_delete => :cascade
|
99
98
|
|
99
|
+
SchemaPlus also creates foreign keys when used rails' <tt>t.references</tt>
|
100
|
+
or <tt>t.belongs_to</tt>< which take the singular of the referenced table
|
101
|
+
name and implicitly create the column suffixed with +_id+, and it accepts
|
102
|
+
the same arguments.
|
103
|
+
|
100
104
|
The foreign key behavior can be configured globally (see Config) or per-table (see create_table).
|
101
105
|
|
102
106
|
To examine your foreign key constraints, connection.foreign_keys returns a
|
@@ -143,14 +147,13 @@ running. Create database user "schema_plus" with permissions for database
|
|
143
147
|
$ bundle install
|
144
148
|
$ rake postgresql:build_databases
|
145
149
|
$ rake mysql:build_databases
|
146
|
-
$ ./runspecs --install # do this once
|
150
|
+
$ ./runspecs --install # do this once, it runs 'bundle install' for all versions (slow)
|
147
151
|
$ ./runspecs # as many times as you like
|
148
152
|
|
149
153
|
See <tt>./runspecs --help</tt> for more options. You can also manually pick a specific version of rails and ruby to use, such as:
|
150
154
|
$ rvm use 1.9.2
|
151
|
-
$ export
|
152
|
-
$ bundle
|
153
|
-
$ rake spec
|
155
|
+
$ export BUNDLE_GEMFILE=gemfiles/Gemfile.rails-3.1
|
156
|
+
$ bundle exec rake spec
|
154
157
|
|
155
158
|
And you can run the specs for a specific adapter:
|
156
159
|
$ rake postgresql:spec # to run postgresql tests only
|
@@ -0,0 +1,65 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /home/snatcher/projects/schema_plus
|
3
|
+
specs:
|
4
|
+
schema_plus (0.1.3)
|
5
|
+
rails
|
6
|
+
valuable
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionmailer (2.3.14)
|
12
|
+
actionpack (= 2.3.14)
|
13
|
+
actionpack (2.3.14)
|
14
|
+
activesupport (= 2.3.14)
|
15
|
+
rack (~> 1.1.0)
|
16
|
+
activerecord (2.3.14)
|
17
|
+
activesupport (= 2.3.14)
|
18
|
+
activeresource (2.3.14)
|
19
|
+
activesupport (= 2.3.14)
|
20
|
+
activesupport (2.3.14)
|
21
|
+
diff-lcs (1.1.3)
|
22
|
+
multi_json (1.0.3)
|
23
|
+
mysql (2.8.1)
|
24
|
+
mysql2 (0.2.13)
|
25
|
+
pg (0.11.0)
|
26
|
+
rack (1.1.2)
|
27
|
+
rails (2.3.14)
|
28
|
+
actionmailer (= 2.3.14)
|
29
|
+
actionpack (= 2.3.14)
|
30
|
+
activerecord (= 2.3.14)
|
31
|
+
activeresource (= 2.3.14)
|
32
|
+
activesupport (= 2.3.14)
|
33
|
+
rake (>= 0.8.3)
|
34
|
+
rake (0.8.7)
|
35
|
+
rspec (2.6.0)
|
36
|
+
rspec-core (~> 2.6.0)
|
37
|
+
rspec-expectations (~> 2.6.0)
|
38
|
+
rspec-mocks (~> 2.6.0)
|
39
|
+
rspec-core (2.6.4)
|
40
|
+
rspec-expectations (2.6.0)
|
41
|
+
diff-lcs (~> 1.1.2)
|
42
|
+
rspec-mocks (2.6.0)
|
43
|
+
simplecov (0.5.3)
|
44
|
+
multi_json (~> 1.0.3)
|
45
|
+
simplecov-html (~> 0.5.3)
|
46
|
+
simplecov-gem-adapter (1.0.1)
|
47
|
+
simplecov
|
48
|
+
simplecov-html (0.5.3)
|
49
|
+
sqlite3 (1.3.4)
|
50
|
+
valuable (0.9.2)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
mysql
|
57
|
+
mysql2 (~> 0.2.7)
|
58
|
+
pg
|
59
|
+
rails (~> 2.3)
|
60
|
+
rake (~> 0.8.7)
|
61
|
+
rspec
|
62
|
+
schema_plus!
|
63
|
+
simplecov
|
64
|
+
simplecov-gem-adapter
|
65
|
+
sqlite3
|
@@ -0,0 +1,111 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /home/snatcher/projects/schema_plus
|
3
|
+
specs:
|
4
|
+
schema_plus (0.1.3)
|
5
|
+
rails
|
6
|
+
valuable
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
abstract (1.0.0)
|
12
|
+
actionmailer (3.0.10)
|
13
|
+
actionpack (= 3.0.10)
|
14
|
+
mail (~> 2.2.19)
|
15
|
+
actionpack (3.0.10)
|
16
|
+
activemodel (= 3.0.10)
|
17
|
+
activesupport (= 3.0.10)
|
18
|
+
builder (~> 2.1.2)
|
19
|
+
erubis (~> 2.6.6)
|
20
|
+
i18n (~> 0.5.0)
|
21
|
+
rack (~> 1.2.1)
|
22
|
+
rack-mount (~> 0.6.14)
|
23
|
+
rack-test (~> 0.5.7)
|
24
|
+
tzinfo (~> 0.3.23)
|
25
|
+
activemodel (3.0.10)
|
26
|
+
activesupport (= 3.0.10)
|
27
|
+
builder (~> 2.1.2)
|
28
|
+
i18n (~> 0.5.0)
|
29
|
+
activerecord (3.0.10)
|
30
|
+
activemodel (= 3.0.10)
|
31
|
+
activesupport (= 3.0.10)
|
32
|
+
arel (~> 2.0.10)
|
33
|
+
tzinfo (~> 0.3.23)
|
34
|
+
activeresource (3.0.10)
|
35
|
+
activemodel (= 3.0.10)
|
36
|
+
activesupport (= 3.0.10)
|
37
|
+
activesupport (3.0.10)
|
38
|
+
arel (2.0.10)
|
39
|
+
builder (2.1.2)
|
40
|
+
diff-lcs (1.1.3)
|
41
|
+
erubis (2.6.6)
|
42
|
+
abstract (>= 1.0.0)
|
43
|
+
i18n (0.5.0)
|
44
|
+
mail (2.2.19)
|
45
|
+
activesupport (>= 2.3.6)
|
46
|
+
i18n (>= 0.4.0)
|
47
|
+
mime-types (~> 1.16)
|
48
|
+
treetop (~> 1.4.8)
|
49
|
+
mime-types (1.16)
|
50
|
+
multi_json (1.0.3)
|
51
|
+
mysql (2.8.1)
|
52
|
+
mysql2 (0.2.13)
|
53
|
+
pg (0.11.0)
|
54
|
+
polyglot (0.3.2)
|
55
|
+
rack (1.2.4)
|
56
|
+
rack-mount (0.6.14)
|
57
|
+
rack (>= 1.0.0)
|
58
|
+
rack-test (0.5.7)
|
59
|
+
rack (>= 1.0)
|
60
|
+
rails (3.0.10)
|
61
|
+
actionmailer (= 3.0.10)
|
62
|
+
actionpack (= 3.0.10)
|
63
|
+
activerecord (= 3.0.10)
|
64
|
+
activeresource (= 3.0.10)
|
65
|
+
activesupport (= 3.0.10)
|
66
|
+
bundler (~> 1.0)
|
67
|
+
railties (= 3.0.10)
|
68
|
+
railties (3.0.10)
|
69
|
+
actionpack (= 3.0.10)
|
70
|
+
activesupport (= 3.0.10)
|
71
|
+
rake (>= 0.8.7)
|
72
|
+
rdoc (~> 3.4)
|
73
|
+
thor (~> 0.14.4)
|
74
|
+
rake (0.8.7)
|
75
|
+
rdoc (3.9.4)
|
76
|
+
rspec (2.6.0)
|
77
|
+
rspec-core (~> 2.6.0)
|
78
|
+
rspec-expectations (~> 2.6.0)
|
79
|
+
rspec-mocks (~> 2.6.0)
|
80
|
+
rspec-core (2.6.4)
|
81
|
+
rspec-expectations (2.6.0)
|
82
|
+
diff-lcs (~> 1.1.2)
|
83
|
+
rspec-mocks (2.6.0)
|
84
|
+
simplecov (0.5.3)
|
85
|
+
multi_json (~> 1.0.3)
|
86
|
+
simplecov-html (~> 0.5.3)
|
87
|
+
simplecov-gem-adapter (1.0.1)
|
88
|
+
simplecov
|
89
|
+
simplecov-html (0.5.3)
|
90
|
+
sqlite3 (1.3.4)
|
91
|
+
thor (0.14.6)
|
92
|
+
treetop (1.4.10)
|
93
|
+
polyglot
|
94
|
+
polyglot (>= 0.3.1)
|
95
|
+
tzinfo (0.3.29)
|
96
|
+
valuable (0.9.2)
|
97
|
+
|
98
|
+
PLATFORMS
|
99
|
+
ruby
|
100
|
+
|
101
|
+
DEPENDENCIES
|
102
|
+
mysql
|
103
|
+
mysql2 (~> 0.2.7)
|
104
|
+
pg
|
105
|
+
rails (~> 3.0.0)
|
106
|
+
rake (~> 0.8.7)
|
107
|
+
rspec
|
108
|
+
schema_plus!
|
109
|
+
simplecov
|
110
|
+
simplecov-gem-adapter
|
111
|
+
sqlite3
|
data/gemfiles/Gemfile.rails-3.1
CHANGED
@@ -0,0 +1,123 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /home/snatcher/projects/schema_plus
|
3
|
+
specs:
|
4
|
+
schema_plus (0.1.3)
|
5
|
+
rails
|
6
|
+
valuable
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionmailer (3.1.0)
|
12
|
+
actionpack (= 3.1.0)
|
13
|
+
mail (~> 2.3.0)
|
14
|
+
actionpack (3.1.0)
|
15
|
+
activemodel (= 3.1.0)
|
16
|
+
activesupport (= 3.1.0)
|
17
|
+
builder (~> 3.0.0)
|
18
|
+
erubis (~> 2.7.0)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
rack (~> 1.3.2)
|
21
|
+
rack-cache (~> 1.0.3)
|
22
|
+
rack-mount (~> 0.8.2)
|
23
|
+
rack-test (~> 0.6.1)
|
24
|
+
sprockets (~> 2.0.0)
|
25
|
+
activemodel (3.1.0)
|
26
|
+
activesupport (= 3.1.0)
|
27
|
+
bcrypt-ruby (~> 3.0.0)
|
28
|
+
builder (~> 3.0.0)
|
29
|
+
i18n (~> 0.6)
|
30
|
+
activerecord (3.1.0)
|
31
|
+
activemodel (= 3.1.0)
|
32
|
+
activesupport (= 3.1.0)
|
33
|
+
arel (~> 2.2.1)
|
34
|
+
tzinfo (~> 0.3.29)
|
35
|
+
activeresource (3.1.0)
|
36
|
+
activemodel (= 3.1.0)
|
37
|
+
activesupport (= 3.1.0)
|
38
|
+
activesupport (3.1.0)
|
39
|
+
multi_json (~> 1.0)
|
40
|
+
arel (2.2.1)
|
41
|
+
bcrypt-ruby (3.0.1)
|
42
|
+
builder (3.0.0)
|
43
|
+
diff-lcs (1.1.3)
|
44
|
+
erubis (2.7.0)
|
45
|
+
hike (1.2.1)
|
46
|
+
i18n (0.6.0)
|
47
|
+
mail (2.3.0)
|
48
|
+
i18n (>= 0.4.0)
|
49
|
+
mime-types (~> 1.16)
|
50
|
+
treetop (~> 1.4.8)
|
51
|
+
mime-types (1.16)
|
52
|
+
multi_json (1.0.3)
|
53
|
+
mysql (2.8.1)
|
54
|
+
mysql2 (0.3.7)
|
55
|
+
pg (0.11.0)
|
56
|
+
polyglot (0.3.2)
|
57
|
+
rack (1.3.3)
|
58
|
+
rack-cache (1.0.3)
|
59
|
+
rack (>= 0.4)
|
60
|
+
rack-mount (0.8.3)
|
61
|
+
rack (>= 1.0.0)
|
62
|
+
rack-ssl (1.3.2)
|
63
|
+
rack
|
64
|
+
rack-test (0.6.1)
|
65
|
+
rack (>= 1.0)
|
66
|
+
rails (3.1.0)
|
67
|
+
actionmailer (= 3.1.0)
|
68
|
+
actionpack (= 3.1.0)
|
69
|
+
activerecord (= 3.1.0)
|
70
|
+
activeresource (= 3.1.0)
|
71
|
+
activesupport (= 3.1.0)
|
72
|
+
bundler (~> 1.0)
|
73
|
+
railties (= 3.1.0)
|
74
|
+
railties (3.1.0)
|
75
|
+
actionpack (= 3.1.0)
|
76
|
+
activesupport (= 3.1.0)
|
77
|
+
rack-ssl (~> 1.3.2)
|
78
|
+
rake (>= 0.8.7)
|
79
|
+
rdoc (~> 3.4)
|
80
|
+
thor (~> 0.14.6)
|
81
|
+
rake (0.8.7)
|
82
|
+
rdoc (3.9.4)
|
83
|
+
rspec (2.6.0)
|
84
|
+
rspec-core (~> 2.6.0)
|
85
|
+
rspec-expectations (~> 2.6.0)
|
86
|
+
rspec-mocks (~> 2.6.0)
|
87
|
+
rspec-core (2.6.4)
|
88
|
+
rspec-expectations (2.6.0)
|
89
|
+
diff-lcs (~> 1.1.2)
|
90
|
+
rspec-mocks (2.6.0)
|
91
|
+
simplecov (0.5.3)
|
92
|
+
multi_json (~> 1.0.3)
|
93
|
+
simplecov-html (~> 0.5.3)
|
94
|
+
simplecov-gem-adapter (1.0.1)
|
95
|
+
simplecov
|
96
|
+
simplecov-html (0.5.3)
|
97
|
+
sprockets (2.0.0)
|
98
|
+
hike (~> 1.2)
|
99
|
+
rack (~> 1.0)
|
100
|
+
tilt (!= 1.3.0, ~> 1.1)
|
101
|
+
sqlite3 (1.3.4)
|
102
|
+
thor (0.14.6)
|
103
|
+
tilt (1.3.3)
|
104
|
+
treetop (1.4.10)
|
105
|
+
polyglot
|
106
|
+
polyglot (>= 0.3.1)
|
107
|
+
tzinfo (0.3.29)
|
108
|
+
valuable (0.9.2)
|
109
|
+
|
110
|
+
PLATFORMS
|
111
|
+
ruby
|
112
|
+
|
113
|
+
DEPENDENCIES
|
114
|
+
mysql
|
115
|
+
mysql2
|
116
|
+
pg
|
117
|
+
rails (~> 3.1)
|
118
|
+
rake (~> 0.8.7)
|
119
|
+
rspec
|
120
|
+
schema_plus!
|
121
|
+
simplecov
|
122
|
+
simplecov-gem-adapter
|
123
|
+
sqlite3
|
data/lib/schema_plus.rb
CHANGED
@@ -2,7 +2,7 @@ require 'valuable'
|
|
2
2
|
|
3
3
|
require 'schema_plus/version'
|
4
4
|
require 'schema_plus/active_record/base'
|
5
|
-
require 'schema_plus/active_record/
|
5
|
+
require 'schema_plus/active_record/foreign_keys'
|
6
6
|
require 'schema_plus/active_record/connection_adapters/table_definition'
|
7
7
|
require 'schema_plus/active_record/connection_adapters/schema_statements'
|
8
8
|
require 'schema_plus/active_record/schema'
|
@@ -128,7 +128,6 @@ module SchemaPlus
|
|
128
128
|
@inserted = true
|
129
129
|
insert_connection_adapters
|
130
130
|
::ActiveRecord::Base.send(:include, SchemaPlus::ActiveRecord::Base)
|
131
|
-
::ActiveRecord::Migration.send(:include, SchemaPlus::ActiveRecord::Migration)
|
132
131
|
::ActiveRecord::Schema.send(:include, SchemaPlus::ActiveRecord::Schema)
|
133
132
|
::ActiveRecord::SchemaDumper.send(:include, SchemaPlus::ActiveRecord::SchemaDumper)
|
134
133
|
end
|
@@ -20,16 +20,9 @@ module SchemaPlus
|
|
20
20
|
public
|
21
21
|
|
22
22
|
def columns_with_schema_plus #:nodoc:
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
indexes.each do |index|
|
27
|
-
index.columns.each do |name|
|
28
|
-
cols[name].indexes << index
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
columns_without_schema_plus
|
23
|
+
columns = columns_without_schema_plus
|
24
|
+
columns.each do |column| column.connection = self end unless @schema_plus_extended_columns
|
25
|
+
columns
|
33
26
|
end
|
34
27
|
|
35
28
|
def reset_column_information_with_schema_plus #:nodoc:
|
@@ -41,6 +41,7 @@ module SchemaPlus
|
|
41
41
|
# if that constant exists, then include the patches
|
42
42
|
::ActiveRecord::ConnectionAdapters::Mysql2IndexDefinition.send(:include, SchemaPlus::ActiveRecord::ConnectionAdapters::IndexDefinition) if defined? ::ActiveRecord::ConnectionAdapters::Mysql2IndexDefinition
|
43
43
|
end
|
44
|
+
extend(SchemaPlus::ActiveRecord::ForeignKeys)
|
44
45
|
end
|
45
46
|
|
46
47
|
# Create a view given the SQL definition
|
@@ -71,13 +72,22 @@ module SchemaPlus
|
|
71
72
|
|
72
73
|
# Define a foreign key constraint. Valid options are :on_update,
|
73
74
|
# :on_delete, and :deferrable, with values as described at
|
74
|
-
# ForeignKeyDefinition
|
75
|
+
# ConnectionAdapters::ForeignKeyDefinition
|
76
|
+
#
|
77
|
+
# (NOTE: Sqlite3 does not support altering a table to add foreign-key
|
78
|
+
# constraints; they must be included in the table specification when
|
79
|
+
# it's created. If you're using Sqlite3, this method will raise an
|
80
|
+
# error.)
|
75
81
|
def add_foreign_key(table_name, column_names, references_table_name, references_column_names, options = {})
|
76
82
|
foreign_key = ForeignKeyDefinition.new(options[:name], table_name, column_names, ::ActiveRecord::Migrator.proper_table_name(references_table_name), references_column_names, options[:on_update], options[:on_delete], options[:deferrable])
|
77
83
|
execute "ALTER TABLE #{quote_table_name(table_name)} ADD #{foreign_key.to_sql}"
|
78
84
|
end
|
79
85
|
|
80
86
|
# Remove a foreign key constraint
|
87
|
+
#
|
88
|
+
# (NOTE: Sqlite3 does not support altering a table to remove
|
89
|
+
# foreign-key constraints. If you're using Sqlite3, this method will
|
90
|
+
# raise an error.)
|
81
91
|
def remove_foreign_key(table_name, foreign_key_name)
|
82
92
|
execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{foreign_key_name}"
|
83
93
|
end
|
@@ -7,12 +7,13 @@ module SchemaPlus
|
|
7
7
|
#
|
8
8
|
module Column
|
9
9
|
|
10
|
+
attr_writer :connection # connection gets set by SchemaPlus::ActiveRecord::Base::columns_with_schema_plus
|
11
|
+
|
10
12
|
# Returns the list of IndexDefinition instances for each index that
|
11
13
|
# refers to this column. Returns an empty list if there are no
|
12
14
|
# such indexes.
|
13
15
|
def indexes
|
14
|
-
|
15
|
-
@indexes ||= []
|
16
|
+
@indexes ||= @connection.indexes.select{|index| index.columns.include? self.name}
|
16
17
|
end
|
17
18
|
|
18
19
|
# If the column is in a unique index, returns a list of names of other columns in
|