forked from weplay/redhillonrails_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
152 lines (97 loc) · 5.51 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
= RedHill on Rails Core
RedHill on Rails Core is a plugin that features to support other RedHill on Rails plugins. Those features include:
* Creating and dropping views;
* Creating and removing foreign-keys;
* Obtaining indexes directly from a model class; and
* Determining when <code>Schema.define()</code> is running.
=== View Support
The plugin provides a mechanism for creating and dropping views as well as
preserving views when performing a schema dump:
create_view :normal_customers, "SELECT * FROM customers WHERE status = 'normal'"
drop_view :normal_customers
=== Foreign Key Support
The plugin provides two mechanisms for adding foreign keys as well as
preserving foreign keys when performing a schema dump. (Using SQL-92 syntax and
as such should be compatible with most databases that support foreign-key
constraints.)
The first mechanism for creating foreign-keys allows you to add a foreign key
when defining a table. For example:
create_table :orders do |t|
...
t.foreign_key :customer_id, :customers, :id
end
You also have the option of specifying what to do on delete/update using
<code>:on_delete</code>/<code>:on_update</code>, respectively to one of: <code>:cascade</code>; <code>:restrict</code>; and <code>:set_null</code>:
create_table :orders do |t|
...
t.foreign_key :customer_id, :customers, :id, :on_delete => :set_null, :on_update => :cascade
end
The second method allows you to create arbitrary foreign-keys at any time:
add_foreign_key(:orders, :customer_id, :customers, :id, :on_delete => :set_null, :on_update => :cascade)
In either case, if your database supports deferred foreign keys (for example PostgreSQL) you can specify this as well:
t.foreign_key :customer_id, :customers, :id, :deferrable => true
add_foreign_key(:orders, :customer_id, :customers, :id, :deferrable => true)
By default, the foreign key will be assigned a name by the underlying database. However, if this doesn't suit
your needs, you can override the default assignment using the <code>:name</code> option:
add_foreign_key(:orders, :customer_id, :customers, :id, :on_delete => :set_null, :on_update => :cascade, <strong>:name => :orders_customer_id_foreign_key<strong>)
You can also query the foreign keys for a model yourself by calling <code>foreign_keys()</code>:
Order.foreign_keys
Or for an arbitrary table by calling <code>foreign_keys(table_name)</code> on a database adapter.
Either method returns an array of the following meta-data:
* +name+ - The name of the foreign key constraint;
* +table_name+ - The table for which the foreign-key was generated;
* +column_names+ - The column names in the table;
* +references_table_name+ - The table referenced by the foreign-key; and
* +references_column_names+ - The columns names in the referenced table.
If you need to drop a foreign-key, use:
remove_foreign_key :orders, :orders_ordered_by_id_fkey
The plugin also ensures that all foreign keys are output when performing a
schema dump. This happens automatically when running <code>rake migrate</code> or
<code>rake db:schema:dump</code>. This has particular implications when running
unit tests that contain fixtures. To ensure the test data is correctly reset after
each test, you should list your fixtures in order of parent->child. For example:
fixtures :customers, :products, :orders, :order_lines
Rails will then set-up and tear-down the fixtures in the correct sequence.
Some databases (PostgreSQL and MySQL for example) allow you to set a comment for a
table. You can do this for existing tables by using:
set_table_comment :orders, "All pending and processed orders"
or even at the time of creation:
create_table :orders, :comment => "All pending and processed orders" do |t|
...
end
You can clear table comments using:
clear_table_comment :orders
There is also a rake tasks to show all database tables and their comments:
rake db:comments
The plugin fully supports and understands the following active-record
configuration properties:
* <code>config.active_record.pluralize_table_names</code>
* <code>config.active_record.table_name_prefix</code>
* <code>config.active_record.table_name_suffix</code>
=== Model Indexes
ActiveRecord::Base already provides a method on connection for obtaining the
indexes for a given table. This plugin now makes it possible to obtain the
indexes for a given model--<code>ActiveRecord::Base</code>--class. For example:
Invoice.indexes
Would return all the indexes for the +invoices+ table.
=== Schema Defining
The plugin also adds a method--<code>defining?()</code>--to
<code>ActiveRecord::Schema</code> to indicate when <code>define()</code> is running. This is necessary
as some migration plugins must change their behaviour accordingly.
=== Case-insensitive Indexes
For PostgreSQL, you can add an option <code>:case_sensitive => false</code> to <code>add_index</code>
which will generate an expression index of the form:
LOWER(column_name)
This means finder queries of the form:
WHERE LOWER(column_name) = LOWER(?)
are able to use the indexes rather require, in the worst case, full-table scans.
Note also that this ties in well with Rails built-in support for case-insensitive searching:
validates_uniqueness_of :name, :case_sensitive => false
=== See Also
* Foreign Key Associations (foreign_key_associations)
* Foreign Key Migrations (foreign_key_migrations)
* Row Version Migrations (row_version_migrations)
* Schema Validations (schema_validations)
=== License
This plugin is copyright 2006 by RedHill Consulting, Pty. Ltd. and is released
under the MIT license.