spanner-translator 0.3.3 → 0.3.4

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: 926b9a0a9e9c07d185627a742e55d5b46361e060de681317fc9af9a78342fdcd
4
- data.tar.gz: c9a93b243c46c5238fbbad3c479ab346c1204138a679730a5afc322a085d5063
3
+ metadata.gz: 5245a782dcce416f03a31c38710e8e5a3acb18a64d0082c44ebb004a1e8121ec
4
+ data.tar.gz: 264532835fce24060de4579d46cf95a09b777fb05b7d0c9e6f3c16aa02360ce1
5
5
  SHA512:
6
- metadata.gz: 4dbc32f2c941211c6f6c57f85737319dec4d0926a7c145cc23ad700f966d72241792360c1dd4a3b51478127012859f1bc6babb9952f3e92d6774ca4587de9e72
7
- data.tar.gz: 24002f577195c9f02e2de88c94261be22807d7201774a0f2268097e304715d8c1b9ac85df4c6378ec15b02662945c0ce9a6e3ac85a6cb52f0fa8c566ef418bfe
6
+ metadata.gz: 06645ad45c11413b73284d32b5fed8dc6dbb9ba1f2c2d67b78c29dae3f188dac3bace1220fbe7342990777c2c608f6f8849deabc66ef92146e5187a81594cdf9
7
+ data.tar.gz: 846b937be1eb9b5e829216567b0070790d99cef0f1c2b1b889364de6c5bc93717970c023a1658002c6c7a3f36f44cb0cee4705b3cb7dcc2a7167eb815e7e3aa7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spanner-translator (0.3.3)
4
+ spanner-translator (0.3.4)
5
5
  rubocop-ast (~> 1.30.0)
6
6
 
7
7
  GEM
@@ -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 timestamps].each do |t_method_name|
7
+ %i[index column bigint integer 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
@@ -31,7 +31,7 @@ module RuboCop
31
31
  find_hash_option(option_name.to_sym)&.source
32
32
  end
33
33
 
34
- %i[null name unique limit default].each do |option_name|
34
+ %i[null name unique limit default primary_key].each do |option_name|
35
35
  define_method(:"#{option_name}_option") do
36
36
  find_hash_option(option_name)
37
37
  end
@@ -48,18 +48,11 @@ module Spanner
48
48
  private
49
49
 
50
50
  # dumb create_table block code formatting
51
- def formatted_create_table(code) # rubocop:disable Metrics/MethodLength
51
+ def formatted_create_table(code)
52
52
  code.lines.map do |l|
53
- case l
54
- when /(create_table|end)/
55
- l.strip
56
- when /t\.index/
57
- " #{l.strip}" unless Spanner::Translator.configuration.skip_indexes
58
- when /^\s*$/
59
- nil # remove blank lines
60
- else
61
- " #{l.strip}"
62
- end
53
+ next if /^\s*$/ =~ l
54
+
55
+ /(create_table|end)/ =~ l ? l.strip : " #{l.strip}"
63
56
  end.compact.join("\n")
64
57
  end
65
58
  end
@@ -4,7 +4,7 @@ module Spanner
4
4
  module Translator
5
5
  # Configuration
6
6
  class Configuration
7
- attr_accessor :rules, :checks, :db_schema, :skip_indexes
7
+ attr_accessor :rules, :checks, :db_schema, :skip_indexes, :primary_key_type
8
8
  attr_reader :default_primary_keys
9
9
 
10
10
  def initialize
@@ -17,6 +17,7 @@ module Spanner
17
17
  @db_schema = "db/schema.rb"
18
18
  @default_primary_keys = ["id"]
19
19
  @skip_indexes = false
20
+ @primary_key_type = :integer
20
21
  end
21
22
 
22
23
  # Set primary key column(s), make sure it is an array of symbols
@@ -6,8 +6,9 @@ module Spanner
6
6
  module Translator
7
7
  module Rules
8
8
  module Translation
9
- # Based on the `create_table` statement, add an explicit column
10
- # creation for the default primary key.
9
+ # Based on the `create_table` statement, add an explicit column creation
10
+ # for the default primary key when the default is "id" and not already
11
+ # defined.
11
12
  #
12
13
  # Scenarios:
13
14
  # create_table "users" do |t|
@@ -25,7 +26,8 @@ module Spanner
25
26
 
26
27
  primary_key_name = "id"
27
28
 
28
- @rewriter.insert_before(node.loc.expression, "t.integer \"#{primary_key_name}\", limit: 8, null: false\n ")
29
+ @rewriter.insert_before(node.loc.expression,
30
+ "t.integer \"#{primary_key_name}\", null: false # generated\n ")
29
31
  @seen["primary_key"] = true
30
32
  end
31
33
  end
@@ -14,6 +14,7 @@ module Spanner
14
14
  def on_send(node)
15
15
  capture_column_nullity(node)
16
16
  return unless node.t_index?
17
+ return @rewriter.remove(node.loc.expression) if Spanner::Translator.configuration.skip_indexes
17
18
 
18
19
  @rewriter.replace(node.loc.expression, "t.index #{new_arguments_for_column_definition(node)}")
19
20
  end
@@ -8,7 +8,7 @@ module Spanner
8
8
  # allow_commit_timestamp: true before the first t.index or at the end of
9
9
  # the block if no t.index is found.
10
10
  class AddTimestamps < BaseRule
11
- COLUMN_DEF = "t.timestamps"
11
+ COLUMN_DEF = "t.timestamps # generated"
12
12
 
13
13
  def on_send(node)
14
14
  return unless node.t_datetime? && %w[created_at updated_at].include?(node.first_argument.value)
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Spanner
6
+ module Translator
7
+ module Rules
8
+ module Translation
9
+ # In the create_table block, add t.time :committed_at with
10
+ # allow_commit_timestamp: true before the first t.index or at the end of
11
+ # the block if no t.index is found.
12
+ class EnsurePrimaryKeyColumnsExist < BaseRule
13
+ ID_COLUMN_TEMPLATE = %(t.%<type>s "%<colname>s", null: false # generated\n )
14
+
15
+ def on_block(node)
16
+ # only pay attention to create_table blocks
17
+ return unless node.send_node.method_name == :create_table
18
+
19
+ # get primary_key hash argument value
20
+ specified_primary_keys = value_of(node.send_node.primary_key_option.value)
21
+ return unless specified_primary_keys
22
+
23
+ # get primary_key column name[s]
24
+ undefined_columns = undefined_primary_key_columns node.body.children, specified_primary_keys
25
+
26
+ # otherwise, insert at beginning of block
27
+ undefined_columns.each do |colname|
28
+ @rewriter.insert_before(node.body.loc.expression, id_column_source(colname))
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def value_of(node)
35
+ case node
36
+ when RuboCop::AST::ArrayNode
37
+ node.children.map { |c| value_of(c) }
38
+ when RuboCop::AST::StrNode, RuboCop::AST::SymbolNode
39
+ node.value.to_s
40
+ else
41
+ raise "Unexpected node type in argument slot: #{node.class}"
42
+ end
43
+ end
44
+
45
+ def undefined_primary_key_columns(children, columns)
46
+ columns = Array(columns)
47
+
48
+ children.each do |child|
49
+ next unless string_argument_haver?(child)
50
+
51
+ colname = child.arguments.first.value.to_s
52
+ columns.delete(colname)
53
+ end
54
+
55
+ columns
56
+ end
57
+
58
+ def string_argument_haver?(child)
59
+ child.is_a?(RuboCop::AST::SendNode) &&
60
+ child.sending_t? &&
61
+ child.arguments.first.is_a?(RuboCop::AST::StrNode)
62
+ end
63
+
64
+ def id_column_source(colname)
65
+ format(ID_COLUMN_TEMPLATE, colname: colname, type: Spanner::Translator.configuration.primary_key_type)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -14,10 +14,11 @@ module Spanner
14
14
 
15
15
  constraint_name = generate_constraint_name(node)
16
16
  unless_seen(constraint_name) do
17
- @rewriter.replace(node.loc.expression, "t.string #{new_arguments_for_column_definition(node)}")
17
+ @rewriter.replace(node.loc.expression,
18
+ "t.string #{new_arguments_for_column_definition(node)} # generated")
18
19
  constraint = %["#{column_name(node)} IN (#{choices_for_enum_string(node)})"]
19
20
  @rewriter.insert_after(node.last_sibling.source_range,
20
- "\n t.check_constraint #{constraint}, name: \"#{constraint_name}\"")
21
+ "\n t.check_constraint #{constraint}, name: \"#{constraint_name}\" # generated")
21
22
  end
22
23
  end
23
24
 
@@ -13,7 +13,7 @@ module Spanner
13
13
  conditionally_propagate_argument new_arguments, node, "null"
14
14
  conditionally_propagate_argument new_arguments, node, "default"
15
15
 
16
- @rewriter.replace(node.loc.expression, "t.time #{new_arguments.join(", ")}")
16
+ @rewriter.replace(node.loc.expression, "t.time #{new_arguments.join(", ")} # generated")
17
17
  end
18
18
  end
19
19
  end
@@ -16,6 +16,7 @@ module Spanner
16
16
  TimeColumnsToTime
17
17
  BigintToInteger
18
18
  ReplaceCreateTableArgs
19
+ EnsurePrimaryKeyColumnsExist
19
20
  ].freeze
20
21
 
21
22
  autoload :AddExplicitPrimaryKey, "spanner/translator/rules/translation/add_explicit_primary_key"
@@ -26,6 +27,7 @@ module Spanner
26
27
  autoload :BigintToInteger, "spanner/translator/rules/translation/bigint_to_integer"
27
28
  autoload :EnumColumnToStringWithConstraint,
28
29
  "spanner/translator/rules/translation/enum_column_to_string_with_constraint"
30
+ autoload :EnsurePrimaryKeyColumnsExist, "spanner/translator/rules/translation/ensure_primary_key_columns_exist"
29
31
  autoload :ReplaceCreateTableArgs, "spanner/translator/rules/translation/replace_create_table_args"
30
32
  autoload :ReplaceApplicationRecord, "spanner/translator/rules/translation/replace_application_record"
31
33
  autoload :TimeColumnsToTime, "spanner/translator/rules/translation/time_columns_to_time"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Spanner
4
4
  module Translator
5
- VERSION = "0.3.3"
5
+ VERSION = "0.3.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spanner-translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Bachman
@@ -60,6 +60,7 @@ files:
60
60
  - lib/spanner/translator/rules/translation/add_timestamps.rb
61
61
  - lib/spanner/translator/rules/translation/base_rule.rb
62
62
  - lib/spanner/translator/rules/translation/bigint_to_integer.rb
63
+ - lib/spanner/translator/rules/translation/ensure_primary_key_columns_exist.rb
63
64
  - lib/spanner/translator/rules/translation/enum_column_to_string_with_constraint.rb
64
65
  - lib/spanner/translator/rules/translation/replace_application_record.rb
65
66
  - lib/spanner/translator/rules/translation/replace_create_table_args.rb