public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Correct PostgreSQL primary key sequence detection.  #2507

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2680 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
jeremy (author)
Tue Oct 18 12:46:11 -0700 2005
commit  311342d8e231f8429e270dce2b1bf4abeb7b4293
tree    a325b6a51da37a0c1b905777dbd3ce1e70ae7ef4
parent  86bdbc3a62ac125f6928d27bf743fec6c0cca81d
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *1.12.1*
0
 
0
+* Correct PostgreSQL primary key sequence detection. #2507 [tmornini@infomania.com]
0
+
0
 * Added support for using limits in eager loads that involve has_many and has_and_belongs_to_many associations
0
 
0
 *1.12.0* (October 16th, 2005)
...
205
206
207
208
209
 
 
210
211
212
213
214
...
214
215
216
217
 
218
219
220
221
222
223
224
225
226
227
228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
 
230
231
232
...
205
206
207
 
 
208
209
210
211
212
213
214
...
214
215
216
 
217
218
 
 
 
 
 
 
 
 
 
 
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
0
@@ -205,8 +205,8 @@
0
 
0
       # Set the sequence to the max value of the table's pk.
0
       def reset_pk_sequence!(table)
0
- sequence, pk = sequence_and_pk_for(table)
0
- if sequence and pk
0
+ pk, sequence = pk_and_sequence_for(table)
0
+ if pk and sequence
0
           select_value <<-end_sql, 'Reset sequence'
0
             SELECT setval('#{sequence}', (SELECT COALESCE(MAX(#{pk})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{table}), false)
0
           end_sql
0
0
0
@@ -214,19 +214,26 @@
0
       end
0
 
0
       # Find a table's primary key and sequence.
0
- def sequence_and_pk_for(table, column = nil)
0
+ def pk_and_sequence_for(table)
0
         execute(<<-end_sql, 'Find pk sequence')[0]
0
- SELECT (name.nspname || '.' || seq.relname) AS sequence, attr.attname AS pk
0
- FROM pg_class seq, pg_attribute attr, pg_depend dep, pg_namespace name, pg_constraint cons
0
- WHERE seq.oid = dep.objid
0
- AND seq.relnamespace = name.oid
0
- AND attr.attrelid = dep.refobjid
0
- AND attr.attnum = dep.refobjsubid
0
- AND attr.attrelid = cons.conrelid
0
- AND attr.attnum = cons.conkey[1]
0
- AND cons.contype = 'p'
0
- AND dep.refobjid = '#{table}'::regclass
0
+ SELECT attr.attname, (name.nspname || '.' || seq.relname)
0
+ FROM pg_class seq,
0
+ pg_attribute attr,
0
+ pg_depend dep,
0
+ pg_namespace name,
0
+ pg_constraint cons
0
+ WHERE seq.oid = dep.objid
0
+ AND seq.relnamespace = name.oid
0
+ AND seq.relkind = 'S'
0
+ AND attr.attrelid = dep.refobjid
0
+ AND attr.attnum = dep.refobjsubid
0
+ AND attr.attrelid = cons.conrelid
0
+ AND attr.attnum = cons.conkey[1]
0
+ AND cons.contype = 'p'
0
+ AND dep.refobjid = '#{table}'::regclass
0
         end_sql
0
+ rescue
0
+ nil
0
       end
0
 
0
       

Comments

    No one has commented yet.