diff --git a/CHANGELOG.md b/CHANGELOG.md index d094215..7b0a34b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ CHANGELOG - **Unreleased** * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.3.1...master) - * Nothing yet + * [#43](https://github.com/westonganger/active_snapshot/pull/43) - Fix unique index error in generated DB migration - **v0.3.1** - August 4, 2023 * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.3.0...v0.3.1) diff --git a/lib/generators/active_snapshot/install/templates/create_snapshots_tables.rb.erb b/lib/generators/active_snapshot/install/templates/create_snapshots_tables.rb.erb index c934ee2..1055921 100644 --- a/lib/generators/active_snapshot/install/templates/create_snapshots_tables.rb.erb +++ b/lib/generators/active_snapshot/install/templates/create_snapshots_tables.rb.erb @@ -3,16 +3,22 @@ class <%= migration_name %> < ActiveRecord::Migration::Current def change create_table :snapshots<%= table_options %> do |t| t.belongs_to :item, polymorphic: true, null: false, index: true - t.string :identifier, unique: [:item_id, :item_type], index: true t.belongs_to :user, polymorphic: true + + t.string :identifier, index: true + t.index [:identifier, :item_id, :item_type], unique: true + t.<%= ActiveSnapshot.config.storage_method == 'native_json' ? 'json' : 'text' %> :metadata + t.datetime :created_at, null: false end create_table :snapshot_items<%= table_options %> do |t| t.belongs_to :snapshot, null: false, index: true t.belongs_to :item, polymorphic: true, null: false, unique: [:snapshot_id], index: true + t.<%= ActiveSnapshot.config.storage_method == 'native_json' ? 'json' : 'text' %> :object, null: false + t.datetime :created_at, null: false t.string :child_group_name end diff --git a/test/dummy_app/db/migrate/20210306100122_create_snapshots_tables.rb b/test/dummy_app/db/migrate/20210306100122_create_snapshots_tables.rb index e7e5aa3..4d1a5d0 100644 --- a/test/dummy_app/db/migrate/20210306100122_create_snapshots_tables.rb +++ b/test/dummy_app/db/migrate/20210306100122_create_snapshots_tables.rb @@ -3,14 +3,12 @@ class CreateSnapshotsTables < ActiveRecord::Migration::Current def change create_table :snapshots do |t| t.belongs_to :item, polymorphic: true, null: false, index: true - t.string :identifier, unique: true, index: true t.belongs_to :user, polymorphic: true - if ActiveSnapshot.config.storage_method_native_json? - t.json :metadata - else - t.text :metadata - end + t.string :identifier, index: true + t.index [:identifier, :item_id, :item_type], unique: true + + t.<%= ActiveSnapshot.config.storage_method == 'native_json' ? 'json' : 'text' %> :metadata, null: false t.datetime :created_at, null: false end @@ -19,11 +17,7 @@ def change t.belongs_to :snapshot, null: false, index: true t.belongs_to :item, polymorphic: true, null: false, unique: [:snapshot_id], index: true - if ActiveSnapshot.config.storage_method_native_json? - t.json :object, null: false - else - t.text :object, null: false - end + t.<%= ActiveSnapshot.config.storage_method == 'native_json' ? 'json' : 'text' %> :object, null: false t.datetime :created_at, null: false t.string :child_group_name