Skip to content

Commit

Permalink
Schema by default (#45)
Browse files Browse the repository at this point in the history
* ci: Update .deepsource.toml

* pass in models from schema

* version bump

---------

Co-authored-by: deepsource-totalanarchy[bot] <107136100+deepsource-totalanarchy[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 5f52ece commit dc575b7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
5 changes: 4 additions & 1 deletion lib/trains/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ def scan

# Stitch together migrations to create models
def generate_models
# Parse models from schema.rb (used when brownfield project)
schema_models = parse_schema

migrations = get_migrations.flatten.reject(&:nil?)
migrations = [*migrations, *parse_models.flatten.reject(&:nil?)]
Utils::MigrationTailor.stitch(migrations)
Utils::MigrationTailor.stitch(schema_models, migrations)
end

def get_routes
Expand Down
19 changes: 10 additions & 9 deletions lib/trains/utils/migration_tailor.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module Trains
module Utils
module MigrationTailor
def self.stitch(migrations)
models = {}

def self.stitch(models = {}, migrations)
migrations.each do |mig|
case mig.modifier
when :create_table, :create_join_table
models[mig.table_name] = {}
models[mig.table_name] = Trains::DTO::Model.new(
name: mig.table_name,
fields: mig.fields,
version: mig.version
)
if models.key?(mig.table_name)
models[mig.table_name].fields.push(*mig.fields)
else
models[mig.table_name] = Trains::DTO::Model.new(
name: mig.table_name,
fields: mig.fields,
version: mig.version
)
end
when :add_column, :add_column_with_default, :add_reference
models[mig.table_name].fields.push(*mig.fields)
when :remove_column
Expand Down
2 changes: 1 addition & 1 deletion lib/trains/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Trains
VERSION = '0.0.16'.freeze
VERSION = '0.0.17'.freeze
end
8 changes: 4 additions & 4 deletions lib/trains/visitor/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Schema < Base
PATTERN

def_node_matcher :unversioned_schema?, <<~PATTERN
(block
(send (const (const nil? :ActiveRecord) :Schema) :define ...)
...)
(block
(send (const (const nil? :ActiveRecord) :Schema) :define ...)
...)
PATTERN

def initialize
Expand Down Expand Up @@ -64,7 +64,7 @@ def build_columns(node)
next if child.method?(:index)

DTO::Field.new(
name: child.first_argument.str_content,
name: child.first_argument.str_content.to_sym,
type: child.method_name
)
end
Expand Down
16 changes: 14 additions & 2 deletions spec/lib/trains/utils/migration_tailor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

describe Trains::Utils::MigrationTailor do
context 'when migrations create_table, add_column' do
let(:models_from_schema) do
{
'Post' => Trains::DTO::Model.new(
name: 'Post',
fields: [
Trains::DTO::Field.new(:name, :string),
],
version: 6.3
)
}
end
let(:create_add_migs) do
[
Trains::DTO::Migration.new(
Expand All @@ -25,18 +36,19 @@
end

it 'creates a model and adds a column to it' do
models = Trains::Utils::MigrationTailor.stitch(create_add_migs)
models = Trains::Utils::MigrationTailor.stitch(models_from_schema, create_add_migs)
expect(models).to eq(
{
'Post' =>
Trains::DTO::Model.new(
name: 'Post',
fields: [
Trains::DTO::Field.new(:name, :string),
Trains::DTO::Field.new(:title, :string),
Trains::DTO::Field.new(:more, :text),
Trains::DTO::Field.new(:user_name, :string)
],
version: 5.2
version: 6.3
)
}
)
Expand Down

0 comments on commit dc575b7

Please sign in to comment.