declare_schema 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -64,7 +64,7 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
64
64
  ActiveRecord::Base.connection.execute <<~EOS
65
65
  CREATE TABLE index_definition_test_models (
66
66
  id INTEGER NOT NULL PRIMARY KEY,
67
- name TEXT NOT NULL
67
+ name #{if defined?(Sqlite3) then 'TEXT' else 'VARCHAR(255)' end} NOT NULL
68
68
  )
69
69
  EOS
70
70
  ActiveRecord::Base.connection.execute <<~EOS
@@ -72,8 +72,8 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
72
72
  EOS
73
73
  ActiveRecord::Base.connection.execute <<~EOS
74
74
  CREATE TABLE index_definition_compound_index_models (
75
- fk1_id INTEGER NULL,
76
- fk2_id INTEGER NULL,
75
+ fk1_id INTEGER NOT NULL,
76
+ fk2_id INTEGER NOT NULL,
77
77
  PRIMARY KEY (fk1_id, fk2_id)
78
78
  )
79
79
  EOS
@@ -99,10 +99,9 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
99
99
  let(:model_class) { IndexDefinitionCompoundIndexModel }
100
100
 
101
101
  it 'returns the indexes for the model' do
102
- # Simulate MySQL for Rails 4 work-around
103
102
  if Rails::VERSION::MAJOR < 5
104
103
  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")
104
+ connection_stub = instance_double(ActiveRecord::Base.connection.class, "connection")
106
105
  expect(connection_stub).to receive(:indexes).
107
106
  with('index_definition_compound_index_models').
108
107
  and_return([DeclareSchema::Model::IndexDefinition.new(model_class, ['fk1_id', 'fk2_id'], name: 'PRIMARY')])
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_record/connection_adapters/mysql2_adapter'
3
+ begin
4
+ require 'mysql2'
5
+ require 'active_record/connection_adapters/mysql2_adapter'
6
+ rescue LoadError
7
+ end
4
8
  require_relative '../../../../lib/declare_schema/model/table_options_definition'
5
9
 
6
10
  RSpec.describe DeclareSchema::Model::TableOptionsDefinition do
@@ -22,7 +26,7 @@ RSpec.describe DeclareSchema::Model::TableOptionsDefinition do
22
26
 
23
27
  describe '#to_key' do
24
28
  subject { model.to_key }
25
- it { should eq(["table_options_definition_test_models", "{:charset=>\"utf8\", :collation=>\"utf8_general\"}"]) }
29
+ it { should eq(['table_options_definition_test_models', '{:charset=>"utf8", :collation=>"utf8_general"}']) }
26
30
  end
27
31
 
28
32
  describe '#settings' do
@@ -32,7 +36,7 @@ RSpec.describe DeclareSchema::Model::TableOptionsDefinition do
32
36
 
33
37
  describe '#hash' do
34
38
  subject { model.hash }
35
- it { should eq(["table_options_definition_test_models", "{:charset=>\"utf8\", :collation=>\"utf8_general\"}"].hash) }
39
+ it { should eq(['table_options_definition_test_models', '{:charset=>"utf8", :collation=>"utf8_general"}'].hash) }
36
40
  end
37
41
 
38
42
  describe '#to_s' do
@@ -42,42 +46,28 @@ RSpec.describe DeclareSchema::Model::TableOptionsDefinition do
42
46
 
43
47
  describe '#alter_table_statement' do
44
48
  subject { model.alter_table_statement }
45
- it { should eq('execute "ALTER TABLE \"table_options_definition_test_models\" CHARACTER SET utf8 COLLATE utf8_general;"') }
49
+ it { should match(/execute "ALTER TABLE .*table_options_definition_test_models.* CHARACTER SET utf8 COLLATE utf8_general"/) }
46
50
  end
47
51
  end
48
52
 
49
53
 
50
54
  context 'class << self' do
51
55
  describe '#for_model' do
52
- context 'when using a SQLite connection' do
56
+ context 'when database migrated' do
57
+ let(:options) do
58
+ if defined?(Mysql2)
59
+ { charset: "utf8mb4", collation: "utf8mb4_bin" }
60
+ else
61
+ { }
62
+ end
63
+ end
53
64
  subject { described_class.for_model(model_class) }
54
- it { should eq(described_class.new(model_class.table_name, {})) }
55
- end
56
- # TODO: Convert these tests to run against a MySQL database so that we can
57
- # perform them without mocking out so much
58
- context 'when using a MySQL connection' do
65
+
59
66
  before do
60
- double(ActiveRecord::ConnectionAdapters::Mysql2Adapter).tap do |stub_connection|
61
- expect(stub_connection).to receive(:class).and_return(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
62
- expect(stub_connection).to receive(:current_database).and_return('test_database')
63
- expect(stub_connection).to receive(:quote_string).with('test_database').and_return('test_database')
64
- expect(stub_connection).to receive(:quote_string).with(model_class.table_name).and_return(model_class.table_name)
65
- expect(stub_connection).to(
66
- receive(:select_one).with(<<~EOS)
67
- SELECT CCSA.character_set_name, CCSA.collation_name
68
- FROM information_schema.`TABLES` T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
69
- WHERE CCSA.collation_name = T.table_collation AND
70
- T.table_schema = 'test_database' AND
71
- T.table_name = '#{model_class.table_name}';
72
- EOS
73
- .and_return({ "character_set_name" => "utf8", "collation_name" => "utf8_general" })
74
- )
75
- allow(model_class).to receive(:connection).and_return(stub_connection)
76
- end
67
+ generate_migrations '-n', '-m'
77
68
  end
78
69
 
79
- subject { described_class.for_model(model_class) }
80
- it { should eq(described_class.new(model_class.table_name, { charset: "utf8", collation: "utf8_general" })) }
70
+ it { should eq(described_class.new(model_class.table_name, options)) }
81
71
  end
82
72
  end
83
73
  end
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ begin
4
+ require 'mysql2'
5
+ rescue LoadError
6
+ end
3
7
  require 'rails'
4
8
  require 'rails/generators'
5
9
 
@@ -15,25 +19,16 @@ module Generators
15
19
 
16
20
  describe 'format_options' do
17
21
  let(:mysql_longtext_limit) { 0xffff_ffff }
18
-
19
- context 'MySQL' do
20
- before do
21
- expect(::DeclareSchema::Model::FieldSpec).to receive(:mysql_text_limits?).and_return(true)
22
- end
23
-
24
- it 'returns text limits' do
25
- expect(subject.format_options({ limit: mysql_longtext_limit }, :text)).to eq(["limit: #{mysql_longtext_limit}"])
22
+ let(:limit_option) do
23
+ if defined?(Mysql2)
24
+ ["limit: #{mysql_longtext_limit}"]
25
+ else
26
+ []
26
27
  end
27
28
  end
28
29
 
29
- context 'non-MySQL' do
30
- before do
31
- expect(::DeclareSchema::Model::FieldSpec).to receive(:mysql_text_limits?).and_return(false)
32
- end
33
-
34
- it 'returns text limits' do
35
- expect(subject.format_options({ limit: mysql_longtext_limit }, :text)).to eq([])
36
- end
30
+ it 'returns text limits if supported' do
31
+ expect(subject.format_options({ limit: mysql_longtext_limit }, :text)).to eq(limit_option)
37
32
  end
38
33
  end
39
34
 
@@ -47,13 +42,13 @@ module Generators
47
42
  subject { described_class.default_charset }
48
43
 
49
44
  context 'when not explicitly set' do
50
- it { should eq(:utf8mb4) }
45
+ it { should eq("utf8mb4") }
51
46
  end
52
47
 
53
48
  context 'when explicitly set' do
54
- before { described_class.default_charset = :utf8 }
49
+ before { described_class.default_charset = "utf8" }
55
50
  after { described_class.default_charset = described_class::DEFAULT_CHARSET }
56
- it { should eq(:utf8) }
51
+ it { should eq("utf8") }
57
52
  end
58
53
  end
59
54
 
@@ -61,13 +56,13 @@ module Generators
61
56
  subject { described_class.default_collation }
62
57
 
63
58
  context 'when not explicitly set' do
64
- it { should eq(:utf8mb4_general) }
59
+ it { should eq("utf8mb4_bin") }
65
60
  end
66
61
 
67
62
  context 'when explicitly set' do
68
- before { described_class.default_collation = :utf8mb4_general_ci }
63
+ before { described_class.default_collation = "utf8mb4_general_ci" }
69
64
  after { described_class.default_collation = described_class::DEFAULT_COLLATION }
70
- it { should eq(:utf8mb4_general_ci) }
65
+ it { should eq("utf8mb4_general_ci") }
71
66
  end
72
67
  end
73
68
 
@@ -23,7 +23,7 @@ module AcceptanceSpecHelpers
23
23
 
24
24
  def expect_file_to_eq(file_path, expectation)
25
25
  expect(File.exist?(file_path)).to be_truthy
26
- expect(File.read(file_path)).to eq(expectation)
26
+ expect(File.read(file_path).gsub(/require '([^']*)'/, 'require "\1"')).to eq(expectation)
27
27
  end
28
28
 
29
29
  def clean_up_model(model)
@@ -45,13 +45,13 @@ module AcceptanceSpecHelpers
45
45
 
46
46
  class MigrationUpEquals < RSpec::Matchers::BuiltIn::Eq
47
47
  def matches?(subject)
48
- super(subject[0])
48
+ super(subject[0].gsub(/, +([a-z_]+:)/i, ', \1')) # normalize multiple spaces to one
49
49
  end
50
50
  end
51
51
 
52
52
  class MigrationDownEquals < RSpec::Matchers::BuiltIn::Eq
53
53
  def matches?(subject)
54
- super(subject[1])
54
+ super(subject[1].gsub(/, +([a-z_]+:)/i, ', \1')) # normalize multiple spaces to one
55
55
  end
56
56
  end
57
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declare_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-23 00:00:00.000000000 Z
11
+ date: 2021-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -49,9 +49,12 @@ files:
49
49
  - bin/declare_schema
50
50
  - declare_schema.gemspec
51
51
  - gemfiles/.bundle/config
52
- - gemfiles/rails_4.gemfile
53
- - gemfiles/rails_5.gemfile
54
- - gemfiles/rails_6.gemfile
52
+ - gemfiles/rails_4_mysql.gemfile
53
+ - gemfiles/rails_4_sqlite.gemfile
54
+ - gemfiles/rails_5_mysql.gemfile
55
+ - gemfiles/rails_5_sqlite.gemfile
56
+ - gemfiles/rails_6_mysql.gemfile
57
+ - gemfiles/rails_6_sqlite.gemfile
55
58
  - lib/declare_schema.rb
56
59
  - lib/declare_schema/command.rb
57
60
  - lib/declare_schema/extensions/active_record/fields_declaration.rb
@@ -90,7 +93,7 @@ homepage: https://github.com/Invoca/declare_schema
90
93
  licenses: []
91
94
  metadata:
92
95
  allowed_push_host: https://rubygems.org
93
- post_install_message:
96
+ post_install_message:
94
97
  rdoc_options: []
95
98
  require_paths:
96
99
  - lib
@@ -106,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
109
  version: 1.3.6
107
110
  requirements: []
108
111
  rubygems_version: 3.0.3
109
- signing_key:
112
+ signing_key:
110
113
  specification_version: 4
111
- summary: Database migration generator for Rails
114
+ summary: Database schema declaration and migration generator for Rails
112
115
  test_files: []