|
13 | 13 | before do |
14 | 14 | load File.expand_path('../prepare_testapp.rb', __dir__) |
15 | 15 |
|
16 | | - class IndexSettingsTestModel < ActiveRecord::Base |
| 16 | + class IndexDefinitionTestModel < ActiveRecord::Base |
17 | 17 | fields do |
18 | 18 | name :string, limit: 127, index: true |
19 | 19 |
|
20 | 20 | timestamps |
21 | 21 | end |
22 | 22 | end |
| 23 | + |
| 24 | + class IndexDefinitionCompoundIndexModel < ActiveRecord::Base |
| 25 | + fields do |
| 26 | + fk1_id :integer |
| 27 | + fk2_id :integer |
| 28 | + |
| 29 | + timestamps |
| 30 | + end |
| 31 | + end |
23 | 32 | end |
24 | 33 |
|
25 | | - let(:model_class) { IndexSettingsTestModel } |
| 34 | + let(:model_class) { IndexDefinitionTestModel } |
26 | 35 |
|
27 | 36 | describe 'instance methods' do |
28 | 37 | let(:model) { model_class.new } |
@@ -53,28 +62,59 @@ class IndexSettingsTestModel < ActiveRecord::Base |
53 | 62 | context 'with a migrated database' do |
54 | 63 | before do |
55 | 64 | ActiveRecord::Base.connection.execute <<~EOS |
56 | | - CREATE TABLE index_settings_test_models ( |
| 65 | + CREATE TABLE index_definition_test_models ( |
57 | 66 | id INTEGER NOT NULL PRIMARY KEY, |
58 | 67 | name TEXT NOT NULL |
59 | 68 | ) |
60 | 69 | EOS |
61 | 70 | ActiveRecord::Base.connection.execute <<~EOS |
62 | | - CREATE UNIQUE INDEX index_settings_test_models_on_name ON index_settings_test_models(name) |
| 71 | + CREATE UNIQUE INDEX index_definition_test_models_on_name ON index_definition_test_models(name) |
| 72 | + EOS |
| 73 | + ActiveRecord::Base.connection.execute <<~EOS |
| 74 | + CREATE TABLE index_definition_compound_index_models ( |
| 75 | + fk1_id INTEGER NULL, |
| 76 | + fk2_id INTEGER NULL, |
| 77 | + PRIMARY KEY (fk1_id, fk2_id) |
| 78 | + ) |
63 | 79 | EOS |
64 | 80 | ActiveRecord::Base.connection.schema_cache.clear! |
65 | 81 | end |
66 | 82 |
|
67 | 83 | describe 'for_model' do |
68 | 84 | subject { described_class.for_model(model_class) } |
69 | 85 |
|
70 | | - it 'returns the indexes for the model' do |
71 | | - expect(subject.size).to eq(2), subject.inspect |
72 | | - expect([:name, :columns, :unique].map { |attr| subject[0].send(attr) }).to eq( |
73 | | - ['index_settings_test_models_on_name', ['name'], true] |
74 | | - ) |
75 | | - expect([:name, :columns, :unique].map { |attr| subject[1].send(attr) }).to eq( |
76 | | - ['PRIMARY', ['id'], true] |
77 | | - ) |
| 86 | + context 'with single-column PK' do |
| 87 | + it 'returns the indexes for the model' do |
| 88 | + expect(subject.size).to eq(2), subject.inspect |
| 89 | + expect([:name, :columns, :unique].map { |attr| subject[0].send(attr) }).to eq( |
| 90 | + ['index_definition_test_models_on_name', ['name'], true] |
| 91 | + ) |
| 92 | + expect([:name, :columns, :unique].map { |attr| subject[1].send(attr) }).to eq( |
| 93 | + ['PRIMARY', ['id'], true] |
| 94 | + ) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + context 'with compound-column PK' do |
| 99 | + let(:model_class) { IndexDefinitionCompoundIndexModel } |
| 100 | + |
| 101 | + it 'returns the indexes for the model' do |
| 102 | + # Simulate MySQL for Rails 4 work-around |
| 103 | + if Rails::VERSION::MAJOR < 5 |
| 104 | + expect(model_class.connection).to receive(:primary_key).with('index_definition_compound_index_models').and_return(nil) |
| 105 | + connection_stub = instance_double(ActiveRecord::ConnectionAdapters::SQLite3Adapter, "connection") |
| 106 | + expect(connection_stub).to receive(:indexes). |
| 107 | + with('index_definition_compound_index_models'). |
| 108 | + and_return([DeclareSchema::Model::IndexDefinition.new(model_class, ['fk1_id', 'fk2_id'], name: 'PRIMARY')]) |
| 109 | + |
| 110 | + expect(model_class.connection).to receive(:dup).and_return(connection_stub) |
| 111 | + end |
| 112 | + |
| 113 | + expect(subject.size).to eq(1), subject.inspect |
| 114 | + expect([:name, :columns, :unique].map { |attr| subject[0].send(attr) }).to eq( |
| 115 | + ['PRIMARY', ['fk1_id', 'fk2_id'], true] |
| 116 | + ) |
| 117 | + end |
78 | 118 | end |
79 | 119 | end |
80 | 120 | end |
|
0 commit comments