schema_plus 1.0.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.
data/spec/schema_spec.rb CHANGED
@@ -36,7 +36,6 @@ describe ActiveRecord::Schema do
36
36
  protected
37
37
  def do_schema
38
38
  define_schema do
39
- connection.tables.each do |table| drop_table table end
40
39
 
41
40
  create_table :users, :force => true do
42
41
  end
data/spec/spec_helper.rb CHANGED
@@ -38,7 +38,7 @@ def define_schema(config={}, &block)
38
38
  ActiveRecord::Migration.suppress_messages do
39
39
  ActiveRecord::Schema.define do
40
40
  connection.tables.each do |table|
41
- drop_table table
41
+ drop_table table, :cascade => true
42
42
  end
43
43
  instance_eval &block
44
44
  end
@@ -22,6 +22,7 @@ module SchemaPlusMatchers
22
22
  @result.keep_if {|fk| fk.column_names == @column_names } if @column_names
23
23
  @result.keep_if {|fk| fk.on_update == @on_update } if @on_update
24
24
  @result.keep_if {|fk| fk.on_delete == @on_delete } if @on_delete
25
+ @result.keep_if {|fk| fk.deferrable == @deferrable } if @deferrable
25
26
  @result.keep_if {|fk| fk.name == @name } if @name
26
27
  !@result.empty?
27
28
  end
@@ -34,6 +35,7 @@ module SchemaPlusMatchers
34
35
  with = []
35
36
  with << "on_update=#{@on_update.inspect}" if @on_update
36
37
  with << "on_delete=#{@on_delete.inspect}" if @on_delete
38
+ with << "deferrable=#{@deferrable.inspect}" if @deferrable
37
39
  with << "name=#{@name.inspect}" if @name
38
40
  msg += " with #{with.join(" and ")}" if with.any?
39
41
  msg
@@ -53,6 +55,11 @@ module SchemaPlusMatchers
53
55
  self
54
56
  end
55
57
 
58
+ def deferrable(action)
59
+ @deferrable = action
60
+ self
61
+ end
62
+
56
63
  def on_delete(action)
57
64
  @on_delete = action
58
65
  self
data/spec/views_spec.rb CHANGED
@@ -130,7 +130,7 @@ describe ActiveRecord do
130
130
  def define_schema_and_data
131
131
  migration.suppress_messages do
132
132
  connection.views.each do |view| connection.drop_view view end
133
- connection.tables.each do |table| connection.drop_table table end
133
+ connection.tables.each do |table| connection.drop_table table, cascade: true end
134
134
 
135
135
  schema.define do
136
136
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-04 00:00:00.000000000 Z
13
+ date: 2013-03-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -122,7 +122,7 @@ files:
122
122
  - .travis.yml
123
123
  - Gemfile
124
124
  - MIT-LICENSE
125
- - README.rdoc
125
+ - README.md
126
126
  - Rakefile
127
127
  - gemfiles/rails-3.2/Gemfile.base
128
128
  - gemfiles/rails-3.2/Gemfile.mysql
@@ -163,7 +163,7 @@ files:
163
163
  - spec/index_definition_spec.rb
164
164
  - spec/index_spec.rb
165
165
  - spec/migration_spec.rb
166
- - spec/multiple_schemas_spec.rb
166
+ - spec/named_schemas_spec.rb
167
167
  - spec/schema/auto_schema.rb
168
168
  - spec/schema/core_schema.rb
169
169
  - spec/schema_dumper_spec.rb
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  version: '0'
195
195
  segments:
196
196
  - 0
197
- hash: 1266061864634900724
197
+ hash: 1918846716163571609
198
198
  requirements: []
199
199
  rubyforge_project: schema_plus
200
200
  rubygems_version: 1.8.24
@@ -215,7 +215,7 @@ test_files:
215
215
  - spec/index_definition_spec.rb
216
216
  - spec/index_spec.rb
217
217
  - spec/migration_spec.rb
218
- - spec/multiple_schemas_spec.rb
218
+ - spec/named_schemas_spec.rb
219
219
  - spec/schema/auto_schema.rb
220
220
  - spec/schema/core_schema.rb
221
221
  - spec/schema_dumper_spec.rb
data/README.rdoc DELETED
@@ -1,347 +0,0 @@
1
- = SchemaPlus
2
-
3
-
4
- SchemaPlus is an ActiveRecord extension that provides enhanced capabilities for schema definition and querying, including: enhanced and more DRY index capabilities, support and automation for foreign key constraints, and support for views.
5
-
6
- For added rails DRYness see also the gems
7
- {+schema_associations+}[http://rubygems.org/gems/schema_associations] and
8
- {+schema_validations+}[http://rubygems.org/gems/schema_validations]
9
-
10
- {<img src="https://secure.travis-ci.org/lomba/schema_plus.png"/>}[http://travis-ci.org/lomba/schema_plus]
11
- {<img src="https://gemnasium.com/lomba/schema_plus.png" alt="Dependency Status" />}[https://gemnasium.com/lomba/schema_plus]
12
-
13
- == Compatibility
14
-
15
- SchemaPlus supports all combinations of:
16
- * rails 3.2
17
- * PostgreSQL, MySQL (using mysql or mysql2 gem), or SQLite3 (using sqlite3 3.7.7 which has foreign key support)
18
- * MRI ruby 1.9.2 or 1.9.3
19
-
20
- Note: As of version 1.0.0, SchemaPlus no longer supports rails 2.3, 3.0 and 3.1 and ruby 1.8.7. The last version
21
- to support them was 0.4.1.
22
-
23
- == Installation
24
-
25
- Install from http://rubygems.org via
26
-
27
- $ gem install "schema_plus"
28
-
29
- or in a Gemfile
30
-
31
- gem "schema_plus"
32
-
33
- == Features
34
-
35
- Here some examples that show off the high points. For full details see the
36
- {RDoc documentation}[http://rubydoc.info/gems/schema_plus].
37
-
38
- === Indexes
39
-
40
- With standard rails migrations, you specify indexes separately from the table definition:
41
-
42
- # Standard Rails approach...
43
- create_table :parts do |t|
44
- t.string :name
45
- t.string :product_code
46
- end
47
-
48
- add_index :parts, :name # index repeats table and column names and is defined separately
49
- add_index :parts, :product_code, unique: true
50
-
51
- But with SchemaPlus you can specify your indexes when you define each column, with options as desired
52
-
53
- # More DRY way...
54
- create_table :parts do |t|
55
- t.string :name, index: true
56
- t.string :product_code, index: { unique: true }
57
- end
58
-
59
- The options hash can include an index name:
60
-
61
- t.string :product_code, index: { unique: true, name: "my_index_name" }
62
-
63
- You can also create multi-column indexes, for example:
64
-
65
- t.string :first_name
66
- t.string :last_name, index: { with: :first_name }
67
-
68
- t.string :country_code
69
- t.string :area_code
70
- t.string :local_number index: { with: [:country_code, :area_code], unique: true }
71
-
72
- And you can specify index orders:
73
-
74
- t.string :first_name
75
- t.string :last_name, index: { with: :first_name, order: { first_name: :desc, last_name: :asc}}
76
-
77
- As a convenient shorthand, the :unique option can be specified as
78
-
79
- t.string :product_code, index: :unique
80
-
81
- which is equivalent to
82
-
83
- t.string :product_code, index: { unique: true }
84
-
85
- If you're using Postgresql, SchemaPlus provides support for conditions, expressions, index methods, and case-insensitive indexes:
86
-
87
- t.string :last_name, index: { conditions: 'deleted_at IS NULL' }
88
- t.string :last_name, index: { expression: 'upper(last_name)' }
89
- t.string :last_name, index: { kind: 'hash' }
90
- t.string :last_name, index: { case_sensitive: false } # shorthand for expression: 'lower(last_name)'
91
-
92
- These features are available also in ActiveRecord::Migration.add_index. See
93
- doc at SchemaPlus::ActiveRecord::ConnectionAdapters::PostgresqlAdapter and
94
- SchemaPlus::ActiveRecord::ConnectionAdapters::IndexDefinition
95
-
96
- When you query column information using ActiveRecord::Base#columns, SchemaPlus
97
- analogously provides index information relevant to each column: which indexes
98
- reference the column, whether the column must be unique, etc. See doc at
99
- SchemaPlus::ActiveRecord::ConnectionAdapters::Column
100
-
101
- SchemaPlus also tidies some index-related behavior:
102
-
103
- * Rails' various db adapters have inconsistent behavior regarding an
104
- attempt to create a duplicate index: some quietly ignore the attempt,
105
- some raise an error. SchemaPlus regularizes the behavor to ignore the
106
- attempt for all db adapters.
107
-
108
- * If you rename a table, indexes named using rails' automatic naming
109
- convention will be renamed correspondingly.
110
-
111
-
112
- === Foreign Key Constraints
113
-
114
- SchemaPlus adds support for foreign key constraints. In fact, for the
115
- common convention that you name a column with suffix +_id+ to indicate that
116
- it's a foreign key, SchemaPlus automatically defines the appropriate
117
- constraint.
118
-
119
- SchemaPlus also creates foreign key constraints for rails' <tt>t.references</tt>
120
- or <tt>t.belongs_to</tt>, which take the singular of the referenced table
121
- name and implicitly create the column suffixed with +_id+.
122
-
123
- You can explicitly specify whether or not to generate a foreign key
124
- constraint, and specify or override automatic options, using the
125
- +:foreign_key+ keyword
126
-
127
- Here are some examples:
128
-
129
- t.integer :author_id # automatically references table 'authors', key id
130
- t.integer :parent_id # special name parent_id automatically references its own table (for tree nodes)
131
- t.integer :author_id, foreign_key: true # same as default automatic behavior
132
- t.integer :author, foreign_key: true # non-conventional column name needs to force creation, table name is assumed to be 'authors'
133
- t.integer :author_id, foreign_key: false # don't create a constraint
134
-
135
- t.integer :author_id, foreign_key: { references: :authors } # same as automatic behavior
136
- t.integer :author, foreign_key: { reference: :authors} # same default name
137
- t.integer :author_id, foreign_key: { references: [:authors, :id] } # same as automatic behavior
138
- t.integer :author_id, foreign_key: { references: :people } # override table name
139
- t.integer :author_id, foreign_key: { references: [:people, :ssn] } # override table name and key
140
- t.integer :author_id, foreign_key: { references: nil } # don't create a constraint
141
- t.integer :author_id, foreign_key: { name: "my_fk" } # override default generated constraint name
142
- t.integer :author_id, foreign_key: { on_delete: :cascade }
143
- t.integer :author_id, foreign_key: { on_update: :set_null }
144
- t.integer :author_id, foreign_key: { deferrable: true }
145
-
146
- Of course the options can be combined, e.g.
147
-
148
- t.integer :author_id, foreign_key: { name: "my_fk", on_delete: :no_action }
149
-
150
- As a shorthand, all options except +:name+ can be specified without placing them in a hash, e.g.
151
-
152
- t.integer :author_id, on_delete: :cascade
153
- t.integer :author_id, references: nil
154
-
155
- The foreign key behavior can be configured globally (see Config) or per-table (see create_table).
156
-
157
- To examine your foreign key constraints, <tt>connection.foreign_keys</tt> returns a
158
- list of foreign key constraints defined for a given table, and
159
- <tt>connection.reverse_foreign_keys</tt> returns a list of foreign key constraints
160
- that reference a given table. See SchemaPlus::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.
161
-
162
- === Views
163
-
164
- SchemaPlus provides support for creating and dropping views. In a migration, a view can be created using a rails relation or literal sql:
165
-
166
- create_view :posts_commented_by_staff, Post.joins(comment: user).where(users: {role: 'staff'}).uniq
167
- create_view :uncommented_posts, "SELECT * FROM posts LEFT OUTER JOIN comments ON comments.post_id = posts.id WHERE comments.id IS NULL"
168
-
169
- And can be dropped:
170
-
171
- drop_view :posts_commented_by_staff
172
- drop_view :uncommented_posts
173
-
174
- ActiveRecord works with views the same as with ordinary tables. That is, for the above views you can define
175
-
176
- class PostCommentedByStaff < ActiveRecord::Base
177
- table_name = "posts_commented_by_staff"
178
- end
179
-
180
- class UncommentedPost < ActiveRecord::Base
181
- end
182
-
183
- === Column Defaults: Expressions
184
-
185
- SchemaPlus allows defaults to be set using expressions or constant values:
186
-
187
- t.datetime :seen_at, default: { expr: 'NOW()' }
188
- t.datetime :seen_at, default: { value: "2011-12-11 00:00:00" }
189
-
190
- Note that in MySQL only the TIMESTAMP column data type accepts SQL column
191
- defaults and Rails uses DATETIME, so expressions can't be used with MySQL.
192
-
193
- The standard syntax will still work as usual:
194
-
195
- t.datetime :seen_at, default: "2011-12-11 00:00:00"
196
-
197
- Also, as a convenience
198
-
199
- t.datetime :seen_at, default: :now
200
-
201
- resolves to:
202
-
203
- NOW() # PostgreSQL
204
- (DATETIME('now')) # SQLite3
205
- invalid # MySQL
206
-
207
- === Column Defaults: Using
208
-
209
- SchemaPlus introduces a constant <tt>ActiveRecord::DB_DEFAULT</tt> that you can use to explicitly instruct the database to use the column default value (or expression). For example:
210
-
211
- Post.create(category: ActiveRecord::DB_DEFAULT)
212
- post.update_attributes(category: ActiveRecord::DB_DEFAULT)
213
-
214
- (Without <tt>ActiveRecord::DB_DEFAULT</tt>, you can update a value to <tt>NULL</tt> but not to its default value.)
215
-
216
- Note that after updating, you would need to reload a record to replace <tt>ActiveRecord::DB_DEFAULT</tt> with the value assigned by the database.
217
-
218
- Note also that Sqlite3 does not support <tt>ActiveRecord::DB_DEFAULT</tt>; attempting to use it will raise <tt>ActiveRecord::StatementInvalid</tt>
219
-
220
-
221
- === Schema Dump and Load (schema.rb)
222
-
223
- When dumping <tt>schema.rb</tt>, SchemaPlus orders the views and tables in
224
- the schema dump alphabetically, but subject to the requirement that each
225
- table or view be defined before those that depend on it. This allows all
226
- foreign key constraints to be defined within the scope of the table
227
- definition. (Unless there are cyclical dependencies, in which case some
228
- foreign keys constraints must be defined later.)
229
-
230
- Also, when dumping <tt>schema.rb</tt>, SchemaPlus dumps explicit foreign key
231
- definition statements rather than relying on the auto-creation behavior,
232
- for maximum clarity and for independence from global config. And
233
- correspondingly, when loading a schema, i.e. with the context of
234
- <tt>ActiveRecord::Schema.define</tt>, SchemaPlus ensures that auto creation of
235
- foreign key constraints is turned off regardless of the global setting. But if for
236
- some reason you are creating your schema.rb file by hand, and would like to
237
- take advantage of auto-creation of foreign key constraints, you can re-enable it:
238
-
239
- ActiveRecord::Schema.define do
240
- SchemaPlus.config.foreign_keys.auto_create = true
241
- SchemaPlus.config.foreign_keys.auto_index = true
242
-
243
- create_table ...etc...
244
- end
245
-
246
-
247
- == Release notes:
248
-
249
-
250
- === 1.0.1
251
-
252
- * README cleanups (thanks to https://github.com/denispeplin)
253
-
254
- * Now raises ArgumentError if index has both :case_sensitive => false and an :expression
255
-
256
- * Now creates consistent default name for foreign key constraints
257
-
258
- * Bug fix: respect :length keyword for index (thanks to https://github.com/teleological )
259
-
260
- * Bug fix: renaming table with multiple foreign key constraints (thanks to https://github.com/teleological )
261
-
262
- * Bug fix: don't dump :case_sensitive => false for index with an expression that includes "lower(name)".
263
-
264
- * Bug fix: Properly dump multi-column case-insensitive indexes
265
-
266
- === 1.0.0
267
-
268
- * No longer support rails < 3.2 and ruby < 1.9
269
-
270
- * New feature: specify foreign key constraints using :foreign_key => { ... }, motivated in particular to support :name (suggested by https://github.com/daniele-m)
271
-
272
- * New feature: create view using ActiveRecord relation
273
-
274
- * New feature: <tt>ActiveRecord::DB_DEFAULT</tt> (suggested by https://github.com/zaadjis)
275
-
276
- * New feature: renaming a table renames its indexes and constraints correspondingly.
277
-
278
- * Bug fix for postgres :kind index attribute (thanks to https://github.com/eugenebolshakov)
279
-
280
- * Sort fks in dump for stability (thanks to https://github.com/zephyr-dev)
281
-
282
- * Bug fix: change_column should maintain foreign key constraints even when config.foreign_keys.auto_create is false
283
-
284
- * Bug fix: quote default expressions in schema dump (thanks to https://github.com/jonleighton)
285
-
286
- * Bug fix: when removing a foreign key constraint, remove its auto-generated index.
287
-
288
- * Bug fix: SchemaDumper.ignore_tables needs to support regexps (suggested by https://github.com/mtalcott)
289
-
290
- * Bug fix: More robust handling of Postgresql schema_search path (suggested by https://github.com/mtalcott)
291
-
292
- * Bug fix: Only get index, view, and foreign key information from current schema (thanks to https://github.com/bhavinkamani)
293
-
294
- === Earlier releases
295
- * 0.4.1 - Bug fix: don't attempt foreign key creation for t.belongs_to ... :polymorphic => true
296
- * 0.4.0 - Add :force for create_view (suggested by greglazarev[https://github.com/greglazarev]). cleanups by betelgeuse[https://github.com/betelgeuse]
297
- * 0.3.4 - Bug fix: regression causing :default => false to be ignored
298
- * 0.3.3 - Bug fix: properly handle boolean defaults in mysql
299
- * 0.3.2 - Bug fix: make sure rake db:schema:load initializes schema_plus
300
- * 0.3.1 - Bug fix for PostgreSQL schema dump after change_column_default(... nil)
301
- * 0.3.0 - Add :default => expressions (Thanks to Luke Saunders). support rails 3.2 and ruby 1.9.3
302
- * 0.2.1 - Suppress duplicate add_indexes. compatibility with rails 3.2.0.rc2
303
-
304
- == History
305
-
306
- * SchemaPlus is derived from several "Red Hill On Rails" plugins
307
- originally created by harukizaemon (https://github.com/harukizaemon)
308
- with later contributions from
309
- * Michał Łomnicki (https://github.com/mlomnicki)
310
- * Ronen Barzel (https://github.com/ronen)
311
- * François Beausoleil (https://github.com/francois)
312
- * Greg Barnett (https://github.com/greg-barnett)
313
- * Luke Saunders (https://github.com/lukesaunders)
314
-
315
-
316
- * SchemaPlus was created in 2011 by Michał Łomnicki and Ronen Barzel
317
-
318
-
319
-
320
- == Testing
321
-
322
- SchemaPlus is tested using rspec and rvm, with some hackery to test against
323
- multiple versions of rails and ruby and db adapters. To run the tests,
324
- after you've forked & cloned: Make sure you have Postgresql and MySQL
325
- running. You'll need db users with permissions to create and access
326
- databases (schema_plus_test and schema_plus_test2). Default user is
327
- 'postgres' for Postgresql and 'schema_plus' for MySQL; you can change them via
328
- $ export POSTGRES_DB_USER = pgusername
329
- $ export MYSQL_DB_USER = mysqlusername
330
- Then:
331
- $ cd schema_plus
332
- $ bundle install
333
- $ rake create_databases
334
- $ ./runspecs --install # do this once, it runs 'bundle install' for all versions
335
- $ ./runspecs # as many times as you like
336
-
337
- See <tt>./runspecs --help</tt> for more options. In particular, to run
338
- rspec on a specific file or example (rather than running the full suite)
339
- you can do, e.g.
340
-
341
- $ ./runspecs [other options] --rspec -- spec/migration_spec.rb -e 'default name'
342
-
343
- Code coverage results will be in coverage/index.html -- it should be at 100% coverage if you're running against all databases
344
-
345
- == License
346
-
347
- This gem is released under the MIT license.