declare_schema 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d4a25e64860b280537a5c131f10537323b6908099a8c58fd72c8da6e8117556
4
- data.tar.gz: 9b376403b635880e2e7bf3a8cd46dbd1bedc2dec030f7be03dc7a671ca956128
3
+ metadata.gz: 8a2f9216c43a3ba2d669d966a4f645edb0faa78a6697cb2a008f017bab2d7534
4
+ data.tar.gz: 3223fad1929f4d75c41e12d8cabd20b6a95775308edf4e4e078b8dcd34ec54cc
5
5
  SHA512:
6
- metadata.gz: 3b8709130944d5eef82e8960a40df117b50c84d29b5ff1ba222bab5f8ff3978a84d2971b022b83c2bd122c2681aa715bd65e67238bc231eb1494d06949e0a297
7
- data.tar.gz: bcf79bbe85dfef64f34a9d56c8f3cb7817a89796ddb086b3ddced1b7e91b1d79fe80f0f1f89de759c81417691cf3315c21655fbd4b93a3ced32495f601c1562f
6
+ metadata.gz: 45318951ec1e963d3eea783def6ee7d9828cdf1826e14ae5977c48cd7f1599bb04712ef95899750ed40a21cccfdd0789b24c993e080d898a10e689c7a9618072
7
+ data.tar.gz: eb1cf73bb3628ea498785b74b4d78029cd2858de1286657ca3f625181100c9daddfd1cb21c7c079f1518ee388e7112e30923ead79dd6e4d6333ee562f2daa2e2
@@ -4,6 +4,16 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.6.3] - UNRELEASED
8
+ ### Added
9
+ - Added `add_foreign_key` native rails call in `DeclareSchema::Model::ForeignKeyDefinition#to_add_statement`.
10
+
11
+ ### Fixed
12
+ - Fixed a bug in migration generation caused by `DeclareSchema::Migration#create_constraints`
13
+ calling `DeclareSchema::Model::ForeignKeyDefinition#to_add_statement` with unused parameters.
14
+
15
+ - Fixed a bug in `DeclareSchema::Migration#remove_foreign_key` where special characters would not be quoted properly.
16
+
7
17
  ## [0.6.2] - 2021-01-06
8
18
  ### Added
9
19
  - Added `sqlite3` as dev dependency for local development
@@ -100,6 +110,7 @@ using the appropriate Rails configuration attributes.
100
110
  ### Added
101
111
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
102
112
 
113
+ [0.6.3]: https://github.com/Invoca/declare_schema/compare/v0.6.2...v0.6.3
103
114
  [0.6.2]: https://github.com/Invoca/declare_schema/compare/v0.6.1...v0.6.2
104
115
  [0.6.1]: https://github.com/Invoca/declare_schema/compare/v0.6.0...v0.6.1
105
116
  [0.6.0]: https://github.com/Invoca/declare_schema/compare/v0.5.0...v0.6.0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.6.2)
4
+ declare_schema (0.6.3)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
@@ -69,7 +69,7 @@ GEM
69
69
  activesupport (>= 4.2.0)
70
70
  i18n (1.8.5)
71
71
  concurrent-ruby (~> 1.0)
72
- listen (3.4.0)
72
+ listen (3.4.1)
73
73
  rb-fsevent (~> 0.10, >= 0.10.3)
74
74
  rb-inotify (~> 0.9, >= 0.9.10)
75
75
  loofah (2.7.0)
@@ -50,9 +50,8 @@ module DeclareSchema
50
50
 
51
51
  attr_writer :parent_table_name
52
52
 
53
- def to_add_statement(_new_table_name = nil, _existing_primary_key = nil)
54
- statement = "ALTER TABLE #{@child_table} ADD CONSTRAINT #{@constraint_name} FOREIGN KEY #{@index_name}(#{@foreign_key_name}) REFERENCES #{parent_table_name}(id) #{'ON DELETE CASCADE' if on_delete_cascade}"
55
- "execute #{statement.inspect}"
53
+ def to_add_statement
54
+ "add_foreign_key(#{@child_table.inspect}, #{parent_table_name.inspect}, name: #{@constraint_name.inspect})"
56
55
  end
57
56
 
58
57
  def key
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.6.2"
4
+ VERSION = "0.6.3"
5
5
  end
@@ -386,7 +386,7 @@ module Generators
386
386
  end
387
387
 
388
388
  def create_constraints(model)
389
- model.constraint_specs.map { |fk| fk.to_add_statement(model.table_name) }
389
+ model.constraint_specs.map { |fk| fk.to_add_statement }
390
390
  end
391
391
 
392
392
  def create_field(field_spec, field_name_width)
@@ -551,20 +551,20 @@ module Generators
551
551
 
552
552
  add_fks.map! do |fk|
553
553
  # next if fk.parent.constantize.abstract_class || fk.parent == fk.model.class_name
554
- undo_add_fks << remove_foreign_key(old_table_name, fk.options[:constraint_name])
554
+ undo_add_fks << remove_foreign_key(old_table_name, fk.constraint_name)
555
555
  fk.to_add_statement
556
556
  end.compact
557
557
 
558
558
  drop_fks.map! do |fk|
559
559
  undo_drop_fks << fk.to_add_statement
560
- remove_foreign_key(new_table_name, fk.options[:constraint_name])
560
+ remove_foreign_key(new_table_name, fk.constraint_name)
561
561
  end
562
562
 
563
563
  [drop_fks + add_fks, undo_add_fks + undo_drop_fks]
564
564
  end
565
565
 
566
566
  def remove_foreign_key(old_table_name, fk_name)
567
- "remove_foreign_key('#{old_table_name}', name: '#{fk_name}')"
567
+ "remove_foreign_key(#{old_table_name.inspect}, name: #{fk_name.to_s.inspect})"
568
568
  end
569
569
 
570
570
  def format_options(options, type, changing: false)
@@ -369,14 +369,14 @@ RSpec.describe 'DeclareSchema Migration Generator' do
369
369
 
370
370
  add_index :adverts, [:category_id], name: 'on_category_id'
371
371
 
372
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" if defined?(Mysql2)}
372
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" if defined?(Mysql2)}
373
373
  EOS
374
374
  .and migrate_down(<<~EOS.strip)
375
375
  remove_column :adverts, :category_id
376
376
 
377
377
  remove_index :adverts, name: :on_category_id rescue ActiveRecord::StatementInvalid
378
378
 
379
- #{"remove_foreign_key('adverts', name: '')\n" if defined?(Mysql2)}
379
+ #{"remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" if defined?(Mysql2)}
380
380
  EOS
381
381
  )
382
382
 
@@ -397,8 +397,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
397
397
 
398
398
  add_index :adverts, [:c_id], name: 'on_c_id'
399
399
 
400
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
401
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
400
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
401
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
402
402
  EOS
403
403
  )
404
404
 
@@ -417,8 +417,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
417
417
  migrate_up(<<~EOS.strip)
418
418
  add_column :adverts, :category_id, :integer, limit: 8, null: false
419
419
 
420
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
421
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
420
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
421
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
422
422
  EOS
423
423
  )
424
424
 
@@ -439,8 +439,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
439
439
 
440
440
  add_index :adverts, [:category_id], name: 'my_index'
441
441
 
442
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
443
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
442
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
443
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
444
444
  EOS
445
445
  )
446
446
 
@@ -465,16 +465,16 @@ RSpec.describe 'DeclareSchema Migration Generator' do
465
465
  add_column :adverts, :updated_at, :datetime
466
466
  add_column :adverts, :lock_version, :integer, null: false, default: 1
467
467
 
468
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
469
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
468
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
469
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
470
470
  EOS
471
471
  .and migrate_down(<<~EOS.strip)
472
472
  remove_column :adverts, :created_at
473
473
  remove_column :adverts, :updated_at
474
474
  remove_column :adverts, :lock_version
475
475
 
476
- #{"remove_foreign_key('adverts', name: '')\n" +
477
- "remove_foreign_key('adverts', name: '')" if defined?(Mysql2)}
476
+ #{"remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" +
477
+ "remove_foreign_key(\"adverts\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
478
478
  EOS
479
479
  )
480
480
 
@@ -498,8 +498,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
498
498
 
499
499
  add_index :adverts, [:title], name: 'on_title'
500
500
 
501
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
502
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
501
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
502
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
503
503
  EOS
504
504
  )
505
505
 
@@ -519,8 +519,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
519
519
 
520
520
  add_index :adverts, [:title], unique: true, name: 'on_title'
521
521
 
522
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
523
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
522
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
523
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
524
524
  EOS
525
525
  )
526
526
 
@@ -540,8 +540,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
540
540
 
541
541
  add_index :adverts, [:title], name: 'my_index'
542
542
 
543
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
544
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
543
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
544
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
545
545
  EOS
546
546
  )
547
547
 
@@ -559,8 +559,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
559
559
 
560
560
  add_index :adverts, [:title], name: 'on_title'
561
561
 
562
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
563
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
562
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
563
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
564
564
  EOS
565
565
  )
566
566
 
@@ -578,8 +578,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
578
578
 
579
579
  add_index :adverts, [:title], unique: true, name: 'my_index'
580
580
 
581
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
582
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
581
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
582
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
583
583
  EOS
584
584
  )
585
585
 
@@ -597,8 +597,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
597
597
 
598
598
  add_index :adverts, [:title, :category_id], name: 'on_title_and_category_id'
599
599
 
600
- #{"execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\n" +
601
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \"" if defined?(Mysql2)}
600
+ #{"add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
601
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")" if defined?(Mysql2)}
602
602
  EOS
603
603
  )
604
604
 
@@ -634,7 +634,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
634
634
  "add_index :ads, [:id], unique: true, name: 'PRIMARY'\n"
635
635
  elsif defined?(Mysql2)
636
636
  "execute \"ALTER TABLE ads DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
637
- "execute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_category_id FOREIGN KEY index_adverts_on_category_id(category_id) REFERENCES categories(id) \"\nexecute \"ALTER TABLE adverts ADD CONSTRAINT index_adverts_on_c_id FOREIGN KEY index_adverts_on_c_id(c_id) REFERENCES categories(id) \""
637
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_category_id\")\n" +
638
+ "add_foreign_key(\"adverts\", \"categories\", name: \"index_adverts_on_c_id\")"
638
639
  end}
639
640
  EOS
640
641
  .and migrate_down(<<~EOS.strip)
@@ -647,8 +648,8 @@ RSpec.describe 'DeclareSchema Migration Generator' do
647
648
  "add_index :adverts, [:id], unique: true, name: 'PRIMARY'\n"
648
649
  elsif defined?(Mysql2)
649
650
  "execute \"ALTER TABLE adverts DROP PRIMARY KEY, ADD PRIMARY KEY (id)\"\n\n" +
650
- "remove_foreign_key('adverts', name: '')\n" +
651
- "remove_foreign_key('adverts', name: '')"
651
+ "remove_foreign_key(\"adverts\", name: \"index_adverts_on_category_id\")\n" +
652
+ "remove_foreign_key(\"adverts\", name: \"index_adverts_on_c_id\")"
652
653
  end}
653
654
  EOS
654
655
  )
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.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-14 00:00:00.000000000 Z
11
+ date: 2021-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails