activerecord-import 0.27.0 → 1.4.1

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.
Files changed (129) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yaml +107 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +74 -8
  5. data/Brewfile +3 -1
  6. data/CHANGELOG.md +175 -2
  7. data/Gemfile +13 -9
  8. data/LICENSE +21 -56
  9. data/README.markdown +525 -21
  10. data/Rakefile +2 -0
  11. data/activerecord-import.gemspec +6 -5
  12. data/benchmarks/benchmark.rb +7 -1
  13. data/benchmarks/lib/base.rb +2 -0
  14. data/benchmarks/lib/cli_parser.rb +3 -1
  15. data/benchmarks/lib/float.rb +2 -0
  16. data/benchmarks/lib/mysql2_benchmark.rb +2 -0
  17. data/benchmarks/lib/output_to_csv.rb +2 -0
  18. data/benchmarks/lib/output_to_html.rb +4 -2
  19. data/benchmarks/models/test_innodb.rb +2 -0
  20. data/benchmarks/models/test_memory.rb +2 -0
  21. data/benchmarks/models/test_myisam.rb +2 -0
  22. data/benchmarks/schema/{mysql_schema.rb → mysql2_schema.rb} +2 -0
  23. data/gemfiles/4.2.gemfile +2 -0
  24. data/gemfiles/5.0.gemfile +2 -0
  25. data/gemfiles/5.1.gemfile +2 -0
  26. data/gemfiles/5.2.gemfile +2 -0
  27. data/gemfiles/6.0.gemfile +4 -0
  28. data/gemfiles/6.1.gemfile +4 -0
  29. data/gemfiles/7.0.gemfile +4 -0
  30. data/lib/activerecord-import/active_record/adapters/abstract_adapter.rb +2 -0
  31. data/lib/activerecord-import/active_record/adapters/jdbcmysql_adapter.rb +6 -4
  32. data/lib/activerecord-import/active_record/adapters/jdbcpostgresql_adapter.rb +2 -0
  33. data/lib/activerecord-import/active_record/adapters/jdbcsqlite3_adapter.rb +2 -0
  34. data/lib/activerecord-import/active_record/adapters/mysql2_adapter.rb +2 -0
  35. data/lib/activerecord-import/active_record/adapters/postgresql_adapter.rb +2 -0
  36. data/lib/activerecord-import/active_record/adapters/seamless_database_pool_adapter.rb +2 -0
  37. data/lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb +2 -0
  38. data/lib/activerecord-import/adapters/abstract_adapter.rb +9 -1
  39. data/lib/activerecord-import/adapters/em_mysql2_adapter.rb +2 -0
  40. data/lib/activerecord-import/adapters/mysql2_adapter.rb +2 -0
  41. data/lib/activerecord-import/adapters/mysql_adapter.rb +10 -11
  42. data/lib/activerecord-import/adapters/postgresql_adapter.rb +49 -38
  43. data/lib/activerecord-import/adapters/sqlite3_adapter.rb +21 -25
  44. data/lib/activerecord-import/base.rb +11 -2
  45. data/lib/activerecord-import/import.rb +180 -78
  46. data/lib/activerecord-import/mysql2.rb +2 -0
  47. data/lib/activerecord-import/postgresql.rb +2 -0
  48. data/lib/activerecord-import/sqlite3.rb +2 -0
  49. data/lib/activerecord-import/synchronize.rb +4 -2
  50. data/lib/activerecord-import/value_sets_parser.rb +4 -0
  51. data/lib/activerecord-import/version.rb +3 -1
  52. data/lib/activerecord-import.rb +3 -1
  53. data/test/adapters/jdbcmysql.rb +2 -0
  54. data/test/adapters/jdbcpostgresql.rb +2 -0
  55. data/test/adapters/jdbcsqlite3.rb +2 -0
  56. data/test/adapters/makara_postgis.rb +2 -0
  57. data/test/adapters/mysql2.rb +2 -0
  58. data/test/adapters/mysql2_makara.rb +2 -0
  59. data/test/adapters/mysql2spatial.rb +2 -0
  60. data/test/adapters/postgis.rb +2 -0
  61. data/test/adapters/postgresql.rb +2 -0
  62. data/test/adapters/postgresql_makara.rb +2 -0
  63. data/test/adapters/seamless_database_pool.rb +2 -0
  64. data/test/adapters/spatialite.rb +2 -0
  65. data/test/adapters/sqlite3.rb +2 -0
  66. data/test/{travis → github}/database.yml +3 -1
  67. data/test/import_test.rb +138 -4
  68. data/test/jdbcmysql/import_test.rb +2 -0
  69. data/test/jdbcpostgresql/import_test.rb +2 -0
  70. data/test/jdbcsqlite3/import_test.rb +2 -0
  71. data/test/makara_postgis/import_test.rb +2 -0
  72. data/test/models/account.rb +2 -0
  73. data/test/models/alarm.rb +2 -0
  74. data/test/models/animal.rb +8 -0
  75. data/test/models/bike_maker.rb +2 -0
  76. data/test/models/book.rb +2 -0
  77. data/test/models/car.rb +2 -0
  78. data/test/models/card.rb +5 -0
  79. data/test/models/chapter.rb +2 -0
  80. data/test/models/customer.rb +8 -0
  81. data/test/models/deck.rb +8 -0
  82. data/test/models/dictionary.rb +2 -0
  83. data/test/models/discount.rb +2 -0
  84. data/test/models/end_note.rb +2 -0
  85. data/test/models/group.rb +2 -0
  86. data/test/models/order.rb +8 -0
  87. data/test/models/playing_card.rb +4 -0
  88. data/test/models/promotion.rb +2 -0
  89. data/test/models/question.rb +2 -0
  90. data/test/models/rule.rb +2 -0
  91. data/test/models/tag.rb +3 -0
  92. data/test/models/tag_alias.rb +5 -0
  93. data/test/models/topic.rb +2 -0
  94. data/test/models/user.rb +2 -0
  95. data/test/models/user_token.rb +3 -0
  96. data/test/models/vendor.rb +2 -0
  97. data/test/models/widget.rb +2 -0
  98. data/test/mysql2/import_test.rb +2 -0
  99. data/test/mysql2_makara/import_test.rb +2 -0
  100. data/test/mysqlspatial2/import_test.rb +2 -0
  101. data/test/postgis/import_test.rb +2 -0
  102. data/test/postgresql/import_test.rb +2 -0
  103. data/test/schema/generic_schema.rb +33 -0
  104. data/test/schema/jdbcpostgresql_schema.rb +2 -0
  105. data/test/schema/mysql2_schema.rb +2 -0
  106. data/test/schema/postgis_schema.rb +2 -0
  107. data/test/schema/postgresql_schema.rb +18 -0
  108. data/test/schema/sqlite3_schema.rb +2 -0
  109. data/test/schema/version.rb +2 -0
  110. data/test/sqlite3/import_test.rb +2 -0
  111. data/test/support/active_support/test_case_extensions.rb +2 -0
  112. data/test/support/assertions.rb +2 -0
  113. data/test/support/factories.rb +2 -0
  114. data/test/support/generate.rb +4 -2
  115. data/test/support/mysql/import_examples.rb +2 -1
  116. data/test/support/postgresql/import_examples.rb +115 -2
  117. data/test/support/shared_examples/on_duplicate_key_ignore.rb +2 -0
  118. data/test/support/shared_examples/on_duplicate_key_update.rb +32 -0
  119. data/test/support/shared_examples/recursive_import.rb +61 -1
  120. data/test/support/sqlite3/import_examples.rb +4 -16
  121. data/test/synchronize_test.rb +2 -0
  122. data/test/test_helper.rb +27 -2
  123. data/test/value_sets_bytes_parser_test.rb +2 -0
  124. data/test/value_sets_records_parser_test.rb +2 -0
  125. metadata +29 -16
  126. data/.travis.yml +0 -71
  127. data/gemfiles/3.2.gemfile +0 -2
  128. data/gemfiles/4.0.gemfile +0 -2
  129. data/gemfiles/4.1.gemfile +0 -2
data/test/import_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('../test_helper', __FILE__)
2
4
 
3
5
  describe "#import" do
@@ -17,6 +19,11 @@ describe "#import" do
17
19
  assert_equal error.message, "Last argument should be a two dimensional array '[[]]'. First element in array was a String"
18
20
  end
19
21
 
22
+ it "warns you that you're passing more data than you ought to" do
23
+ error = assert_raise(ArgumentError) { Topic.import %w(title author_name), [['Author #1', 'Book #1', 0]] }
24
+ assert_equal error.message, "Number of values (8) exceeds number of columns (7)"
25
+ end
26
+
20
27
  it "should not produce an error when importing empty arrays" do
21
28
  assert_nothing_raised do
22
29
  Topic.import []
@@ -154,6 +161,25 @@ describe "#import" do
154
161
  Tag.import columns, values, validate: false
155
162
  end
156
163
  end
164
+
165
+ it "should import models that are required to belong to models with composite primary keys" do
166
+ tag = Tag.create!(tag_id: 1, publisher_id: 1, tag: 'Mystery')
167
+ valid_tag_alias = TagAlias.new(tag_id: tag.tag_id, parent_id: tag.publisher_id, alias: 'Detective')
168
+ invalid_tag_aliases = [
169
+ TagAlias.new(tag_id: nil, parent_id: nil, alias: 'Detective'),
170
+ TagAlias.new(tag_id: tag.tag_id, parent_id: nil, alias: 'Detective'),
171
+ TagAlias.new(tag_id: nil, parent_id: tag.publisher_id, alias: 'Detective'),
172
+ ]
173
+
174
+ assert_difference "TagAlias.count", +1 do
175
+ TagAlias.import [valid_tag_alias]
176
+ end
177
+ invalid_tag_aliases.each do |invalid_tag_alias|
178
+ assert_no_difference "TagAlias.count" do
179
+ TagAlias.import [invalid_tag_alias]
180
+ end
181
+ end
182
+ end
157
183
  end
158
184
  end
159
185
 
@@ -164,7 +190,17 @@ describe "#import" do
164
190
  assert_difference "Dictionary.count", +1 do
165
191
  Dictionary.import dictionaries
166
192
  end
167
- assert_equal "Dictionary", Dictionary.first.type
193
+ assert_equal "Dictionary", Dictionary.last.type
194
+ end
195
+
196
+ it "should import arrays successfully" do
197
+ columns = [:author_name, :title]
198
+ values = [["Noah Webster", "Webster's Dictionary"]]
199
+
200
+ assert_difference "Dictionary.count", +1 do
201
+ Dictionary.import columns, values
202
+ end
203
+ assert_equal "Dictionary", Dictionary.last.type
168
204
  end
169
205
  end
170
206
 
@@ -247,6 +283,16 @@ describe "#import" do
247
283
  end
248
284
  end
249
285
 
286
+ it "should index the failed instances by their poistion in the set if `track_failures` is true" do
287
+ index_offset = valid_values.length
288
+ results = Topic.import columns, valid_values + invalid_values, validate: true, track_validation_failures: true
289
+ assert_equal invalid_values.size, results.failed_instances.size
290
+ invalid_values.each_with_index do |value_set, index|
291
+ assert_equal index + index_offset, results.failed_instances[index].first
292
+ assert_equal value_set.first, results.failed_instances[index].last.title
293
+ end
294
+ end
295
+
250
296
  it "should set ids in valid models if adapter supports setting primary key of imported objects" do
251
297
  if ActiveRecord::Base.supports_setting_primary_key_of_imported_objects?
252
298
  Topic.import (invalid_models + valid_models), validate: true
@@ -303,6 +349,27 @@ describe "#import" do
303
349
  end
304
350
  end
305
351
  end
352
+
353
+ context "when validatoring presence of belongs_to association" do
354
+ it "should not import records without foreign key" do
355
+ assert_no_difference "UserToken.count" do
356
+ UserToken.import [:token], [['12345abcdef67890']]
357
+ end
358
+ end
359
+
360
+ it "should import records with foreign key" do
361
+ assert_difference "UserToken.count", +1 do
362
+ UserToken.import [:user_name, :token], [%w("Bob", "12345abcdef67890")]
363
+ end
364
+ end
365
+
366
+ it "should not mutate the defined validations" do
367
+ UserToken.import [:user_name, :token], [%w("Bob", "12345abcdef67890")]
368
+ ut = UserToken.new
369
+ ut.valid?
370
+ assert_includes ut.errors.messages, :user
371
+ end
372
+ end
306
373
  end
307
374
 
308
375
  context "without :validation option" do
@@ -369,6 +436,15 @@ describe "#import" do
369
436
  assert_equal 3, result.num_inserts if Topic.supports_import?
370
437
  end
371
438
  end
439
+
440
+ it "should accept and call an optional callable to run after each batch" do
441
+ lambda_called = 0
442
+
443
+ my_proc = ->(_row_count, _batches, _batch, _duration) { lambda_called += 1 }
444
+ Topic.import Build(10, :topics), batch_size: 4, batch_progress: my_proc
445
+
446
+ assert_equal 3, lambda_called
447
+ end
372
448
  end
373
449
 
374
450
  context "with :synchronize option" do
@@ -499,11 +575,15 @@ describe "#import" do
499
575
 
500
576
  context "when the timestamps columns are present" do
501
577
  setup do
502
- @existing_book = Book.create(title: "Fell", author_name: "Curry", publisher: "Bayer", created_at: 2.years.ago.utc, created_on: 2.years.ago.utc)
503
- ActiveRecord::Base.default_timezone = :utc
578
+ @existing_book = Book.create(title: "Fell", author_name: "Curry", publisher: "Bayer", created_at: 2.years.ago.utc, created_on: 2.years.ago.utc, updated_at: 2.years.ago.utc, updated_on: 2.years.ago.utc)
579
+ if ActiveRecord.respond_to?(:default_timezone)
580
+ ActiveRecord.default_timezone = :utc
581
+ else
582
+ ActiveRecord::Base.default_timezone = :utc
583
+ end
504
584
  Timecop.freeze(time) do
505
585
  assert_difference "Book.count", +2 do
506
- Book.import %w(title author_name publisher created_at created_on), [["LDAP", "Big Bird", "Del Rey", nil, nil], [@existing_book.title, @existing_book.author_name, @existing_book.publisher, @existing_book.created_at, @existing_book.created_on]]
586
+ Book.import %w(title author_name publisher created_at created_on updated_at updated_on), [["LDAP", "Big Bird", "Del Rey", nil, nil, nil, nil], [@existing_book.title, @existing_book.author_name, @existing_book.publisher, @existing_book.created_at, @existing_book.created_on, @existing_book.updated_at, @existing_book.updated_on]]
507
587
  end
508
588
  end
509
589
  @new_book, @existing_book = Book.last 2
@@ -532,6 +612,23 @@ describe "#import" do
532
612
  it "should set the updated_on column for new records" do
533
613
  assert_in_delta time.to_i, @new_book.updated_on.to_i, 1.second
534
614
  end
615
+
616
+ it "should not set the updated_at column for existing records" do
617
+ assert_equal 2.years.ago.utc.strftime("%Y:%d"), @existing_book.updated_at.strftime("%Y:%d")
618
+ end
619
+
620
+ it "should not set the updated_on column for existing records" do
621
+ assert_equal 2.years.ago.utc.strftime("%Y:%d"), @existing_book.updated_on.strftime("%Y:%d")
622
+ end
623
+
624
+ it "should not set the updated_at column on models if changed" do
625
+ timestamp = Time.now.utc
626
+ books = [
627
+ Book.new(author_name: "Foo", title: "Baz", created_at: timestamp, updated_at: timestamp)
628
+ ]
629
+ Book.import books
630
+ assert_equal timestamp.strftime("%Y:%d"), Book.last.updated_at.strftime("%Y:%d")
631
+ end
535
632
  end
536
633
 
537
634
  context "when a custom time zone is set" do
@@ -599,6 +696,14 @@ describe "#import" do
599
696
  assert_equal [val1, val2], scope.map(&column).sort
600
697
  end
601
698
 
699
+ context "for cards and decks" do
700
+ it "works when the polymorphic name is different than base class name" do
701
+ deck = Deck.create(id: 1, name: 'test')
702
+ deck.cards.import [:id, :deck_type], [[1, 'PlayingCard']]
703
+ assert_equal deck.cards.first.deck_type, "PlayingCard"
704
+ end
705
+ end
706
+
602
707
  it "works importing array of hashes" do
603
708
  scope.import [{ column => val1 }, { column => val2 }]
604
709
 
@@ -857,4 +962,33 @@ describe "#import" do
857
962
  end
858
963
  end
859
964
  end
965
+ describe "importing model with after_initialize callback" do
966
+ let(:columns) { %w(name size) }
967
+ let(:valid_values) { [%w("Deer", "Small"), %w("Monkey", "Medium")] }
968
+ let(:invalid_values) do
969
+ [
970
+ { name: "giraffe", size: "Large" },
971
+ { size: "Medium" } # name is missing
972
+ ]
973
+ end
974
+ context "with validation checks turned off" do
975
+ it "should import valid data" do
976
+ Animal.import(columns, valid_values, validate: false)
977
+ assert_equal 2, Animal.count
978
+ end
979
+ it "should raise ArgumentError" do
980
+ assert_raise(ArgumentError) { Animal.import(invalid_values, validate: false) }
981
+ end
982
+ end
983
+
984
+ context "with validation checks turned on" do
985
+ it "should import valid data" do
986
+ Animal.import(columns, valid_values, validate: true)
987
+ assert_equal 2, Animal.count
988
+ end
989
+ it "should raise ArgumentError" do
990
+ assert_raise(ArgumentError) { Animal.import(invalid_values, validate: true) }
991
+ end
992
+ end
993
+ end
860
994
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/assertions')
3
5
  require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/import_examples')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/sqlite3/import_examples')
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Account < ActiveRecord::Base
2
4
  self.locking_column = :lock
3
5
  end
data/test/models/alarm.rb CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Alarm < ActiveRecord::Base
2
4
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Animal < ActiveRecord::Base
4
+ after_initialize :validate_name_presence, if: :new_record?
5
+ def validate_name_presence
6
+ raise ArgumentError if name.nil?
7
+ end
8
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bike
2
4
  def self.table_name_prefix
3
5
  'bike_'
data/test/models/book.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Book < ActiveRecord::Base
2
4
  belongs_to :topic, inverse_of: :books
3
5
  belongs_to :tag, foreign_key: [:tag_id, :parent_id]
data/test/models/car.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Car < ActiveRecord::Base
2
4
  self.primary_key = :Name
3
5
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Card < ActiveRecord::Base
4
+ belongs_to :deck, polymorphic: true
5
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Chapter < ActiveRecord::Base
2
4
  belongs_to :book, inverse_of: :chapters
3
5
  validates :title, presence: true
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Customer < ActiveRecord::Base
4
+ has_many :orders,
5
+ inverse_of: :customer,
6
+ primary_key: %i(account_id id),
7
+ foreign_key: %i(account_id customer_id)
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Deck < ActiveRecord::Base
4
+ has_many :cards
5
+ def self.polymorphic_name
6
+ "PlayingCard"
7
+ end
8
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'book'
2
4
 
3
5
  class Dictionary < Book
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Discount < ActiveRecord::Base
2
4
  belongs_to :discountable, polymorphic: true
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class EndNote < ActiveRecord::Base
2
4
  belongs_to :book, inverse_of: :end_notes
3
5
  validates :note, presence: true
data/test/models/group.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Group < ActiveRecord::Base
2
4
  self.table_name = 'group'
3
5
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Order < ActiveRecord::Base
4
+ belongs_to :customer,
5
+ inverse_of: :orders,
6
+ primary_key: %i(account_id id),
7
+ foreign_key: %i(account_id customer_id)
8
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PlayingCard < ActiveRecord::Base
4
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Promotion < ActiveRecord::Base
2
4
  self.primary_key = :promotion_id
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Question < ActiveRecord::Base
2
4
  has_one :rule
3
5
  end
data/test/models/rule.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Rule < ActiveRecord::Base
2
4
  belongs_to :question
3
5
  end
data/test/models/tag.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tag < ActiveRecord::Base
2
4
  self.primary_keys = :tag_id, :publisher_id unless ENV["SKIP_COMPOSITE_PK"]
3
5
  has_many :books, inverse_of: :tag
6
+ has_many :tag_aliases, inverse_of: :tag
4
7
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TagAlias < ActiveRecord::Base
4
+ belongs_to :tag, foreign_key: [:tag_id, :parent_id], required: true
5
+ end
data/test/models/topic.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Topic < ActiveRecord::Base
2
4
  validates_presence_of :author_name
3
5
  validates :title, numericality: { only_integer: true }, on: :context_test
data/test/models/user.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class User < ActiveRecord::Base
2
4
  has_many :user_tokens, primary_key: :name, foreign_key: :user_name
3
5
  end
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class UserToken < ActiveRecord::Base
2
4
  belongs_to :user, primary_key: :name, foreign_key: :user_name
5
+ validates :user, presence: true
3
6
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Vendor < ActiveRecord::Base
2
4
  store :preferences, accessors: [:color], coder: JSON
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CustomCoder
2
4
  def load(value)
3
5
  if value.nil?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/assertions')
3
5
  require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/import_examples')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
 
3
5
  require File.expand_path(File.dirname(__FILE__) + '/../support/assertions')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
 
3
5
  require File.expand_path(File.dirname(__FILE__) + '/../support/assertions')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  create_table :schema_info, force: :cascade do |t|
3
5
  t.integer :version, unique: true
@@ -52,6 +54,20 @@ ActiveRecord::Schema.define do
52
54
  t.string :name
53
55
  end
54
56
 
57
+ create_table :cards, force: :cascade do |t|
58
+ t.string :name
59
+ t.string :deck_type
60
+ t.integer :deck_id
61
+ end
62
+
63
+ create_table :decks, force: :cascade do |t|
64
+ t.string :name
65
+ end
66
+
67
+ create_table :playing_cards, force: :cascade do |t|
68
+ t.string :name
69
+ end
70
+
55
71
  create_table :books, force: :cascade do |t|
56
72
  t.string :title, null: false
57
73
  t.string :publisher, null: false, default: 'Default Publisher'
@@ -190,5 +206,22 @@ ActiveRecord::Schema.define do
190
206
  PRIMARY KEY (tag_id, publisher_id)
191
207
  );
192
208
  ).split.join(' ').strip
209
+
210
+ create_table :tag_aliases, force: :cascade do |t|
211
+ t.integer :tag_id, null: false
212
+ t.integer :parent_id, null: false
213
+ t.string :alias, null: false
214
+ end
215
+ end
216
+
217
+ create_table :customers, force: :cascade do |t|
218
+ t.integer :account_id
219
+ t.string :name
220
+ end
221
+
222
+ create_table :orders, force: :cascade do |t|
223
+ t.integer :account_id
224
+ t.integer :customer_id
225
+ t.integer :amount
193
226
  end
194
227
  end
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/postgresql_schema')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  create_table :books, force: :cascade do |t|
3
5
  t.string :title, null: false
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/postgresql_schema')
@@ -1,13 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  execute('CREATE extension IF NOT EXISTS "hstore";')
3
5
  execute('CREATE extension IF NOT EXISTS "pgcrypto";')
4
6
  execute('CREATE extension IF NOT EXISTS "uuid-ossp";')
5
7
 
8
+ # create ENUM if it does not exist yet
9
+ begin
10
+ execute('CREATE TYPE vendor_type AS ENUM (\'wholesaler\', \'retailer\');')
11
+ rescue ActiveRecord::StatementInvalid => e
12
+ # since PostgreSQL does not support IF NOT EXISTS when creating a TYPE,
13
+ # rescue the error and check the error class
14
+ raise unless e.cause.is_a? PG::DuplicateObject
15
+ execute('ALTER TYPE vendor_type ADD VALUE IF NOT EXISTS \'wholesaler\';')
16
+ execute('ALTER TYPE vendor_type ADD VALUE IF NOT EXISTS \'retailer\';')
17
+ end
18
+
6
19
  create_table :vendors, id: :uuid, force: :cascade do |t|
7
20
  t.string :name, null: true
21
+ t.text :hours
8
22
  t.text :preferences
9
23
 
10
24
  if t.respond_to?(:json)
25
+ t.json :pure_json_data
11
26
  t.json :data
12
27
  else
13
28
  t.text :data
@@ -20,6 +35,7 @@ ActiveRecord::Schema.define do
20
35
  end
21
36
 
22
37
  if t.respond_to?(:jsonb)
38
+ t.jsonb :pure_jsonb_data
23
39
  t.jsonb :settings
24
40
  t.jsonb :json_data, null: false, default: {}
25
41
  else
@@ -27,6 +43,8 @@ ActiveRecord::Schema.define do
27
43
  t.text :json_data
28
44
  end
29
45
 
46
+ t.column :vendor_type, :vendor_type
47
+
30
48
  t.datetime :created_at
31
49
  t.datetime :updated_at
32
50
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  create_table :alarms, force: true do |t|
3
5
  t.column :device_id, :integer, null: false
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class SchemaInfo < ActiveRecord::Base
2
4
  if respond_to?(:table_name=)
3
5
  self.table_name = 'schema_info'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
4
  require File.expand_path(File.dirname(__FILE__) + '/../support/sqlite3/import_examples')
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ActiveSupport::TestCase
2
4
  include ActiveRecord::TestFixtures
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ActiveSupport::TestCase
2
4
  module ImportAssertions
3
5
  def self.extended(klass)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  sequence(:book_title) { |n| "Book #{n}" }
3
5
  sequence(:chapter_title) { |n| "Chapter #{n}" }
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ActiveSupport::TestCase
2
- def Build(*args) # rubocop:disable Style/MethodName
4
+ def Build(*args) # rubocop:disable Naming/MethodName
3
5
  n = args.shift if args.first.is_a?(Numeric)
4
6
  factory = args.shift
5
7
  factory_bot_args = args.shift || {}
@@ -13,7 +15,7 @@ class ActiveSupport::TestCase
13
15
  end
14
16
  end
15
17
 
16
- def Generate(*args) # rubocop:disable Style/MethodName
18
+ def Generate(*args) # rubocop:disable Naming/MethodName
17
19
  n = args.shift if args.first.is_a?(Numeric)
18
20
  factory = args.shift
19
21
  factory_bot_args = args.shift || {}
@@ -1,4 +1,5 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  def should_support_mysql_import_functionality
3
4
  # Forcefully disable strict mode for this session.
4
5
  ActiveRecord::Base.connection.execute "set sql_mode='STRICT_ALL_TABLES'"