Guest User

Untitled

a guest
Sep 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. ## hackery for tables in the database that do not have standard auto-incrementing PK's or no PK at all
  2. def tables_with_no_auto_inc_pk_hackery(stream)
  3. tables_without_pk = {
  4. 'os_names' => [
  5. 'os_name',
  6. "\tt.string \"os_name\", :limit => 15, :null => false"
  7. ],
  8. 'os_families' => [
  9. 'os_family_name',
  10. "\tt.string \"os_family_name\", :limit => 31, :null => false"
  11. ]
  12. }
  13. fake_stream = StringIO.new
  14. tables_without_no_auto_inc_pk_hackery(fake_stream)
  15. fake_stream.rewind
  16. fake_stream.lines.each do |line|
  17. if ( line =~ /create_table "(\w*)".*do \|\w+\|/ )
  18. table_name = $1
  19. if(tables_without_pk.key? table_name)
  20. incorrect_pk = tables_without_pk[table_name][0]
  21. columns_to_inject = tables_without_pk[table_name][1]
  22. if(line.gsub!(/:primary_key => "#{incorrect_pk}"/,':id => false').nil?)
  23. raise "Table #{table_name} failed tables_without_no_auto_inc_pk_hackery. Could not match incorrect PK #{incorrect_pk}"
  24. end
  25. stream.puts(line)
  26. stream.puts(columns_to_inject)
  27. end
  28. end
  29. end
  30. stream
  31. end
  32.  
  33. alias_method_chain :header, :details
  34. alias_method_chain :tables, :rvn_hackery
  35. alias_method_chain :tables, :no_auto_inc_pk_hackery
Add Comment
Please, Sign In to add comment