spanner-translator 0.3.0 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54175adb11828290cc2c5f7993973130dd9b870b66fcf90748af2de11a3a3ea1
4
- data.tar.gz: 5e2a822a94c976fa76f20e86122cbd95961a5616e008e2cfba53510d38e2594c
3
+ metadata.gz: 926b9a0a9e9c07d185627a742e55d5b46361e060de681317fc9af9a78342fdcd
4
+ data.tar.gz: c9a93b243c46c5238fbbad3c479ab346c1204138a679730a5afc322a085d5063
5
5
  SHA512:
6
- metadata.gz: c8169d285df624c76f63ad577d588b19b85606cd393abdd5f0ebbad229de8c5409e4c2ed0d3fa461df8d18db9b75dc97dabf6169ddea0bc61c43376c1b751551
7
- data.tar.gz: 6013cd95da080222aca143982461624971c1c3a22ab71c3deddc791bc85a22f1b00b947fa402aa66a3c66ffa1de139b5f79fec7568431fe2aa654d46e96d5012
6
+ metadata.gz: 4dbc32f2c941211c6f6c57f85737319dec4d0926a7c145cc23ad700f966d72241792360c1dd4a3b51478127012859f1bc6babb9952f3e92d6774ca4587de9e72
7
+ data.tar.gz: 24002f577195c9f02e2de88c94261be22807d7201774a0f2268097e304715d8c1b9ac85df4c6378ec15b02662945c0ce9a6e3ac85a6cb52f0fa8c566ef418bfe
data/.rubocop.yml CHANGED
@@ -1,3 +1,6 @@
1
+ require:
2
+ - rubocop-rspec
3
+
1
4
  AllCops:
2
5
  TargetRubyVersion: 3.2.2
3
6
  NewCops: enable
@@ -25,3 +28,12 @@ Metrics/BlockLength:
25
28
  Max: 120
26
29
  Exclude:
27
30
  - 'spec/**/*'
31
+
32
+ RSpec/ExampleLength:
33
+ Max: 120
34
+
35
+ RSpec/ContextWording:
36
+ Enabled: false
37
+
38
+ RSpec/MultipleMemoizedHelpers:
39
+ Enabled: false
data/Gemfile CHANGED
@@ -10,3 +10,4 @@ gem "debug", ">= 1.0.0"
10
10
  gem "rake", "~> 13.0"
11
11
  gem "rspec", "~> 3.0"
12
12
  gem "rubocop", "~> 1.59.0"
13
+ gem "rubocop-rspec"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spanner-translator (0.3.0)
4
+ spanner-translator (0.3.3)
5
5
  rubocop-ast (~> 1.30.0)
6
6
 
7
7
  GEM
@@ -59,6 +59,14 @@ GEM
59
59
  unicode-display_width (>= 2.4.0, < 3.0)
60
60
  rubocop-ast (1.30.0)
61
61
  parser (>= 3.2.1.0)
62
+ rubocop-capybara (2.20.0)
63
+ rubocop (~> 1.41)
64
+ rubocop-factory_bot (2.25.1)
65
+ rubocop (~> 1.41)
66
+ rubocop-rspec (2.26.1)
67
+ rubocop (~> 1.40)
68
+ rubocop-capybara (~> 2.17)
69
+ rubocop-factory_bot (~> 2.22)
62
70
  ruby-progressbar (1.13.0)
63
71
  stringio (3.0.8)
64
72
  unicode-display_width (2.5.0)
@@ -73,6 +81,7 @@ DEPENDENCIES
73
81
  rake (~> 13.0)
74
82
  rspec (~> 3.0)
75
83
  rubocop (~> 1.59.0)
84
+ rubocop-rspec
76
85
  spanner-translator!
77
86
 
78
87
  BUNDLED WITH
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module AST
5
5
  # Spanner-specific extensions for RuboCop::AST::SendNode objects
6
6
  class SendNode
7
- %i[index column bigint datetime timestamp].each do |t_method_name|
7
+ %i[index column bigint datetime timestamp timestamps].each do |t_method_name|
8
8
  define_method(:"t_#{t_method_name}?") do
9
9
  sending_t? && method_name == t_method_name
10
10
  end
@@ -10,17 +10,17 @@ module Spanner
10
10
  class << self
11
11
  # process code translation, raising errors if a CheckRule fails.
12
12
  def process_code!(code,
13
- check_classes = Rules::Check.constants,
14
- rule_classes = Rules::Translation::ORDERED_SCHEMA_RULES, options: {})
13
+ check_classes: Rules::Check::ORDERED_CHECK_RULES,
14
+ rule_classes: Rules::Translation::ORDERED_SCHEMA_RULES, options: {})
15
15
  check_classes.each do |rule_class|
16
16
  process_check_rule(Spanner::Translator::Rules::Check.const_get(rule_class), code)
17
17
  end
18
18
 
19
- process_code(code, rule_classes, options:)
19
+ process_code(code, rule_classes:, options:)
20
20
  end
21
21
 
22
22
  # process code translation for only the schema related rules
23
- def process_code(code, rule_classes = Rules::Translation::ORDERED_SCHEMA_RULES, options: {})
23
+ def process_code(code, rule_classes: Rules::Translation::ORDERED_SCHEMA_RULES, options: {})
24
24
  rule_classes.each do |rule_class|
25
25
  code = process_rule(Spanner::Translator::Rules::Translation.const_get(rule_class), code, options:)
26
26
  end
@@ -48,13 +48,15 @@ module Spanner
48
48
  private
49
49
 
50
50
  # dumb create_table block code formatting
51
- def formatted_create_table(code)
51
+ def formatted_create_table(code) # rubocop:disable Metrics/MethodLength
52
52
  code.lines.map do |l|
53
53
  case l
54
54
  when /(create_table|end)/
55
55
  l.strip
56
56
  when /t\.index/
57
57
  " #{l.strip}" unless Spanner::Translator.configuration.skip_indexes
58
+ when /^\s*$/
59
+ nil # remove blank lines
58
60
  else
59
61
  " #{l.strip}"
60
62
  end
@@ -6,6 +6,8 @@ module Spanner
6
6
  module Translator
7
7
  module Rules
8
8
  module Check # rubocop:disable Style/Documentation
9
+ ORDERED_CHECK_RULES = [].freeze
10
+
9
11
  autoload :HasAnyIndex, "spanner/translator/rules/check/has_any_index"
10
12
  end
11
13
  end
@@ -4,18 +4,25 @@ module Spanner
4
4
  module Translator
5
5
  module Rules
6
6
  module Translation
7
- # Add t.time :committed_at with allow_commit_timestamp: true before the first t.index
8
- #
9
- # FIXME (@abachman): if table has no indexes, this rule will not be applied
10
- #
7
+ # In the create_table block, add t.time :committed_at with
8
+ # allow_commit_timestamp: true before the first t.index or at the end of
9
+ # the block if no t.index is found.
11
10
  class AddCommittedAtTimestamp < BaseRule
12
- def on_send(node)
13
- return if @seen["committed_at"]
14
- return unless node.t_index?
11
+ COMMITTED_AT_COLUMN = 't.time "committed_at", null: false, allow_commit_timestamp: true'
15
12
 
16
- committed_at_column = 't.time "committed_at", null: false, allow_commit_timestamp: true'
17
- @rewriter.insert_before(node.loc.expression, "#{committed_at_column}\n ")
18
- @seen["committed_at"] = true
13
+ def on_block(node)
14
+ # only pay attention to create_table blocks
15
+ return unless node.send_node.method_name == :create_table
16
+
17
+ # try all body children to see if any are t.index, if so, insert before
18
+ node.body.children.each do |child|
19
+ next unless child.is_a?(RuboCop::AST::SendNode) && child.t_index?
20
+
21
+ return @rewriter.insert_before(child.loc.expression, "#{COMMITTED_AT_COLUMN}\n ")
22
+ end
23
+
24
+ # otherwise, insert at end of block
25
+ @rewriter.insert_before(node.loc.end, " #{COMMITTED_AT_COLUMN}\n")
19
26
  end
20
27
  end
21
28
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spanner
4
+ module Translator
5
+ module Rules
6
+ module Translation
7
+ # In the create_table block, add t.time :committed_at with
8
+ # allow_commit_timestamp: true before the first t.index or at the end of
9
+ # the block if no t.index is found.
10
+ class AddTimestamps < BaseRule
11
+ COLUMN_DEF = "t.timestamps"
12
+
13
+ def on_send(node)
14
+ return unless node.t_datetime? && %w[created_at updated_at].include?(node.first_argument.value)
15
+
16
+ full_line = Parser::Source::Range.new(@rewriter.source_buffer,
17
+ node.loc.expression.begin_pos,
18
+ node.loc.expression.end_pos)
19
+ @rewriter.remove(full_line)
20
+ end
21
+
22
+ def on_block(node) # rubocop:disable Metrics/AbcSize
23
+ # only pay attention to create_table blocks
24
+ return unless node.send_node.method_name == :create_table
25
+
26
+ # check all already has `t.timestamps` bail out
27
+ return if timestamp_child?(node.body.children)
28
+
29
+ # try all body children to see if any are t.index, if so, insert before
30
+ t_index = node.body.children.find { |child| child.is_a?(RuboCop::AST::SendNode) && child.t_index? }
31
+ return @rewriter.insert_before(t_index.loc.expression, "#{COLUMN_DEF}\n ") if t_index
32
+
33
+ # otherwise, insert at end of block
34
+ @rewriter.insert_before(node.loc.end, " #{COLUMN_DEF}\n")
35
+ end
36
+
37
+ private
38
+
39
+ def timestamp_child?(children)
40
+ children.any? do |child|
41
+ child.is_a?(RuboCop::AST::SendNode) && child.t_timestamps?
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -10,7 +10,9 @@ module Spanner
10
10
  return unless node.create_table?
11
11
 
12
12
  # keep table name argument, replace the rest with sensible spanner options
13
- primary_keys = Spanner::Translator.configuration.default_primary_keys.inspect
13
+ primary_keys = translation_options
14
+ .fetch(:primary_keys, Spanner::Translator.configuration.default_primary_keys)
15
+ .inspect
14
16
  @rewriter.replace(node.arguments[1].source_range, "primary_key: #{primary_keys}, force: :cascade")
15
17
  end
16
18
  end
@@ -10,6 +10,7 @@ module Spanner
10
10
  ORDERED_SCHEMA_RULES = %w[
11
11
  AddExplicitPrimaryKey
12
12
  AddCommittedAtTimestamp
13
+ AddTimestamps
13
14
  AddIndexOptions
14
15
  EnumColumnToStringWithConstraint
15
16
  TimeColumnsToTime
@@ -21,6 +22,7 @@ module Spanner
21
22
  autoload :AddCommittedAtTimestamp, "spanner/translator/rules/translation/add_committed_at_timestamp"
22
23
  autoload :AddIndexOptions, "spanner/translator/rules/translation/add_index_options"
23
24
  autoload :AddTestCase, "spanner/translator/rules/translation/add_test_case"
25
+ autoload :AddTimestamps, "spanner/translator/rules/translation/add_timestamps"
24
26
  autoload :BigintToInteger, "spanner/translator/rules/translation/bigint_to_integer"
25
27
  autoload :EnumColumnToStringWithConstraint,
26
28
  "spanner/translator/rules/translation/enum_column_to_string_with_constraint"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Spanner
4
4
  module Translator
5
- VERSION = "0.3.0"
5
+ VERSION = "0.3.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spanner-translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Bachman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-10 00:00:00.000000000 Z
11
+ date: 2024-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop-ast
@@ -57,6 +57,7 @@ files:
57
57
  - lib/spanner/translator/rules/translation/add_explicit_primary_key.rb
58
58
  - lib/spanner/translator/rules/translation/add_index_options.rb
59
59
  - lib/spanner/translator/rules/translation/add_test_case.rb
60
+ - lib/spanner/translator/rules/translation/add_timestamps.rb
60
61
  - lib/spanner/translator/rules/translation/base_rule.rb
61
62
  - lib/spanner/translator/rules/translation/bigint_to_integer.rb
62
63
  - lib/spanner/translator/rules/translation/enum_column_to_string_with_constraint.rb