trains 0.0.11 → 0.0.13

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: 227d3fc953f2317d57846387bf0c358b0a1bfd6f70f89ddb760b70410616270e
4
- data.tar.gz: edb13b82f62463858971853cd62bb21a7f6223df35625c2a88e651c9da279bbe
3
+ metadata.gz: 1ad58a1dcd643c69b2c278c861647ad0211807edbc2e660f1e09eae127ff6cf7
4
+ data.tar.gz: 3c4a2d0bb7a6604dd3ffe8f1ca80ea8764b72ae1b5e350e18425de3eb7409687
5
5
  SHA512:
6
- metadata.gz: 9550034596435821878df66664809d21e01a0eced82a0f7579bf1f7d84d501d9043b93248527ab52d5c6b77d13d274449132f7f377ac285294361b4472662470
7
- data.tar.gz: ddb3f8f34f5e7940cd98a8a05fae0ecdd600c18bea6a3206d9ef2c4910365f3b673946d8085340cddf4c96f782abe061f8cbfd967a3a4dbb7386a3737a0107ad
6
+ metadata.gz: 476e65ac7eaf350b8e7e15eee1a0d508677f8f5ad3b446503fd890a821fb24935f03de5f6fd7a4bb938a227ca9c1db60b338e9852284e802ac3a585ded01ae82
7
+ data.tar.gz: 726f8983354b206b1f881bea58182b74a0d462bf8c5898435547647b73eac9fe0b845bab206ce1f6da6cc4d9449e8664dadb011e9e76f8194d349d47abdeaf29
data/.deepsource.toml CHANGED
@@ -2,3 +2,6 @@ version = 1
2
2
 
3
3
  [[analyzers]]
4
4
  name = "ruby"
5
+
6
+ [[analyzers]]
7
+ name = "shell"
@@ -44,12 +44,10 @@ module Trains
44
44
 
45
45
  private
46
46
 
47
- # Generate models from either db/schema.rb
48
- # else stitch together migrations to create models
47
+ # Stitch together migrations to create models
49
48
  def generate_models
50
- return get_models if File.exist?(File.join(@dir, 'db', 'schema.rb'))
51
-
52
49
  migrations = get_migrations.flatten.reject(&:nil?)
50
+ migrations = [*migrations, *parse_models.flatten.reject(&:nil?)]
53
51
  Utils::MigrationTailor.stitch(migrations)
54
52
  end
55
53
 
@@ -63,7 +61,7 @@ module Trains
63
61
  .flatten
64
62
  end
65
63
 
66
- def get_models
64
+ def parse_schema
67
65
  result_hash = {}
68
66
  schema_file = [File.join(@dir, 'db', 'schema.rb')]
69
67
  models_results = parse_util(schema_file, Visitor::Schema)
@@ -77,6 +75,15 @@ module Trains
77
75
  result_hash
78
76
  end
79
77
 
78
+ def parse_models
79
+ models = Dir.glob(File.join(@dir, 'app', 'models', '**', '*.rb'))
80
+ model_results = parse_util(models, Visitor::Model)
81
+
82
+ model_results
83
+ .select { |result| result.error.nil? }
84
+ .map(&:data)
85
+ end
86
+
80
87
  def get_helpers; end
81
88
 
82
89
  def get_gemfile; end
@@ -13,7 +13,7 @@ module Trains
13
13
  fields: mig.fields,
14
14
  version: mig.version
15
15
  )
16
- when :add_column, :add_reference
16
+ when :add_column, :add_column_with_default, :add_reference
17
17
  models[mig.table_name].fields.push(*mig.fields)
18
18
  when :remove_column
19
19
  column =
@@ -1,3 +1,3 @@
1
1
  module Trains
2
- VERSION = '0.0.11'.freeze
2
+ VERSION = '0.0.13'.freeze
3
3
  end
@@ -19,6 +19,7 @@ module Trains
19
19
  ].freeze
20
20
  COLUMN_MODIFIERS = %i[
21
21
  add_column
22
+ add_column_with_default
22
23
  change_column
23
24
  add_reference
24
25
  remove_column
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trains
4
+ module Visitor
5
+ # Visit ActiveRecord models and add migrations for reference fields
6
+ class Model < Base
7
+ POSSIBLE_ASSOCIATIONS = %i[
8
+ has_many
9
+ belongs_to
10
+ has_one
11
+ has_and_belongs_to_many
12
+ ]
13
+ attr_reader :result
14
+
15
+ # skipcq: RB-LI1087
16
+ def initialize
17
+ @result = []
18
+ end
19
+
20
+ def on_class(node)
21
+ return unless node.parent_class
22
+ return unless node.parent_class.source == 'ApplicationRecord'
23
+
24
+ @model_class = node.identifier.source
25
+ node.each_descendant(:send) { |send_node| parse_model(send_node) }
26
+ end
27
+
28
+ def parse_model(node)
29
+ return unless POSSIBLE_ASSOCIATIONS.include?(node.method_name)
30
+
31
+ @result << DTO::Migration.new(
32
+ @model_class,
33
+ :add_column,
34
+ [DTO::Field.new(node.arguments.first.value, :bigint)],
35
+ nil
36
+ )
37
+
38
+ return unless node.method_name == :has_and_belongs_to_many
39
+
40
+ @result <<
41
+ DTO::Migration.new(
42
+ node.arguments.first.value.to_s.singularize.camelize,
43
+ :add_column,
44
+ [DTO::Field.new(@model_class.tableize.to_sym, :bigint)],
45
+ nil
46
+ )
47
+ end
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trains
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Syed Faraaz Ahmad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-16 00:00:00.000000000 Z
11
+ date: 2023-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -103,6 +103,7 @@ files:
103
103
  - lib/trains/visitor/controller.rb
104
104
  - lib/trains/visitor/helper.rb
105
105
  - lib/trains/visitor/migration.rb
106
+ - lib/trains/visitor/model.rb
106
107
  - lib/trains/visitor/route.rb
107
108
  - lib/trains/visitor/schema.rb
108
109
  - trains.gemspec