20251017205859_create_active_storage_tables.active_storage.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # This migration comes from active_storage (originally 20170806125915)
  2. class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
  3. def change
  4. # Use Active Record's configured type for primary and foreign keys
  5. primary_key_type, foreign_key_type = primary_and_foreign_key_types
  6. create_table :active_storage_blobs, id: primary_key_type do |t|
  7. t.string :key, null: false
  8. t.string :filename, null: false
  9. t.string :content_type
  10. t.text :metadata
  11. t.string :service_name, null: false
  12. t.bigint :byte_size, null: false
  13. t.string :checksum
  14. if connection.supports_datetime_with_precision?
  15. t.datetime :created_at, precision: 6, null: false
  16. else
  17. t.datetime :created_at, null: false
  18. end
  19. t.index [ :key ], unique: true
  20. end
  21. create_table :active_storage_attachments, id: primary_key_type do |t|
  22. t.string :name, null: false
  23. t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
  24. t.references :blob, null: false, type: foreign_key_type
  25. if connection.supports_datetime_with_precision?
  26. t.datetime :created_at, precision: 6, null: false
  27. else
  28. t.datetime :created_at, null: false
  29. end
  30. t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
  31. t.foreign_key :active_storage_blobs, column: :blob_id
  32. end
  33. create_table :active_storage_variant_records, id: primary_key_type do |t|
  34. t.belongs_to :blob, null: false, index: false, type: foreign_key_type
  35. t.string :variation_digest, null: false
  36. t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
  37. t.foreign_key :active_storage_blobs, column: :blob_id
  38. end
  39. end
  40. private
  41. def primary_and_foreign_key_types
  42. config = Rails.configuration.generators
  43. setting = config.options[config.orm][:primary_key_type]
  44. primary_key_type = setting || :primary_key
  45. foreign_key_type = setting || :bigint
  46. [ primary_key_type, foreign_key_type ]
  47. end
  48. end