Hi Rodrigo,
Thanks for the comments.
Rodrigo Rosenfeld Rosas wrote:
Hi Ronen, I'm definitely +1 for most of them. Here are some exceptions:
t.string :column, index: { with: [:other1, :other2] } # multi-column index
I'd prefer "columns" over "with" in this case, but this is not a big issue for me.
Not a big issue for me either. "columns" might imply that the list is all the columns rather than the additional columns? But i agree that "with" isn't great, aside from being short.
This one is a big issue in my opinion:
create_view :posts_commented_by_staff, Post.joins(comment: user).where(users: {role: 'staff'}).uniq
I don't like the idea of the migrations relying on the models and I'd prefer only the other explicit SQL-based approach to be supported in migrations.
Reasonable point. Anybody who wants to use the model-based version could do it themselves via #to_sql
create_view :posts_commented_by_staff, Post.joins(comment: user).where(users: {role: 'staff'}).uniq.to_sql
Which is all that the current implementation does anyway: send :to_sql if the argument responds to it. So not a big deal either way in terms of coding.
And I didn't understand what this does:
connection.foreign_keys(table) #=> array of ForeignKeyDefinition objects
This looks up all the foreign key constraint that are defined on the table, and returns a list of objects containing the details of each constraint. There's also a connection.reverse_foreign_keys(table) that returns all foreign key constraints that refer to the table.
I've never actually used them in practice in an app, so they're there mostly for completeness. But of course they're used extensively within the gem.
Haven't understood this part either:
AR::Base.columns.first.indexes #=> index definitions that refer to the column
AR::Base.columns.first.unique? #=> whether there's a unique index.
Could you please explain it?
Sorry that wasn't very clear. The Column object has some new methods. So if you're introspecting on the columns of a model or table, you can then query:
column.indexes #=> array of IndexDefinition objects for all indexes that refer to the column
column.unique? #=> true if the column have a unique index on it
and a few others, see http://www.ruby-doc.org/gems/docs/s/schema_plus-1.0.1/SchemaPlus/ActiveRecord/ConnectionAdapters/Column.html
Also, does your gem currently support generic constraints? Or at least is there some DSL to create deferred constraints?
For foreign key constraints it supports
foreign_key: { :deferrable => true }
foreign_key: { :deferrable => :initially_deferred }
It doesn't currently support other constraiints such as CHECK constraints. (just yesterday a call went out for somebody to implement it... https://twitter.com/jonleighton/status/310767523928866816 )
Good luck with getting some of those ideas approved.
Thanks!