Skip to content

Commit

Permalink
Change table (#48)
Browse files Browse the repository at this point in the history
* ci: Update .deepsource.toml

* create migrations for renaming and removing

* handle renaming columns

* fix

* convert to sym

* update version

* remove pp

---------

Co-authored-by: deepsource-totalanarchy[bot] <107136100+deepsource-totalanarchy[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 996e695 commit 94c2393
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 6 deletions.
45 changes: 44 additions & 1 deletion lib/trains/utils/migration_tailor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

module Trains
module Utils
# Combine multiple migrations into models
module MigrationTailor
def self.stitch(models = {}, migrations)
migrations.each do |mig|
Expand All @@ -22,8 +25,46 @@ def self.stitch(models = {}, migrations)
field.name == mig.fields.first.name
end
models[mig.table_name].fields.delete(column)
when :rename_column
column =
models[mig.table_name].fields.find do |field|
field.name == mig.fields.first.name
end
models[mig.table_name].fields.push(
Trains::DTO::Field.new(
name: mig.fields.first.type.to_sym,
type: column.type
)
)
models[mig.table_name].fields.delete(column)
when :change_table
# TODO: handle renaming columns
mig.fields.each do |field|
case field.type
when :remove
column =
models[mig.table_name].fields.find do |mod_field|
mod_field.name == field.name
end
models[mig.table_name].fields.delete(column)
when :rename
# find the field and store temporarily
column =
models[mig.table_name].fields.find do |mod_field|
mod_field.name == field.name[0]
end
# Create new field from temp with new name
models[mig.table_name].fields.push(
Trains::DTO::Field.new(
name: field.name[1],
type: column.type
)
)
# Delete the field
models[mig.table_name].fields.delete(column)
else
models[mig.table_name].fields.push(field)
end
end
when :change_column
# get column
column =
Expand All @@ -34,6 +75,8 @@ def self.stitch(models = {}, migrations)
models[mig.table_name].fields.delete(column)

models[mig.table_name].fields << mig.fields.first
else
next
end

rescue NoMethodError
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.18'.freeze
VERSION = '0.0.19'.freeze
end
7 changes: 6 additions & 1 deletion lib/trains/visitor/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Migration < Base
add_column_with_default
change_column
add_reference
rename_column
remove_column
].freeze

Expand Down Expand Up @@ -181,7 +182,11 @@ def parse_migration_field(node)
# t.references
type = :bigint
value = "#{node.children[2].value}_id".to_sym
fields << DTO::Field.new(value.to_sym, type)
fields << DTO::Field.new(value, type)
when :rename
type = :rename
value = node.children[2..3].map { |field| field.value.to_sym }
fields << DTO::Field.new(value, type)
when :index
else
# t.string, t.integer etc.
Expand Down
2 changes: 1 addition & 1 deletion lib/trains/visitor/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def parse_model(node)
@result << DTO::Migration.new(
@model_class,
:add_column,
[DTO::Field.new(node.arguments.first.value, :bigint)],
[DTO::Field.new(node.arguments.first.value.to_sym, :bigint)],
nil
)

Expand Down
19 changes: 19 additions & 0 deletions spec/fixtures/change_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ChangeTable < ActiveRecord::Migration[7.0]
def change
create_table :groups do |t|
t.string :title

t.timestamps
end
add_column :groups, :name, :string
add_index :groups, :title, unique: true

change_table :groups do |t|
t.remove :title
t.rename :name, :whatup
end

rename_column :groups, :whatup, :name
remove_column :groups, :name
end
end
76 changes: 74 additions & 2 deletions spec/lib/trains/utils/migration_tailor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'Post' => Trains::DTO::Model.new(
name: 'Post',
fields: [
Trains::DTO::Field.new(:name, :string),
Trains::DTO::Field.new(:name, :string)
],
version: 6.3
)
Expand Down Expand Up @@ -36,7 +36,8 @@
end

it 'creates a model and adds a column to it' do
models = Trains::Utils::MigrationTailor.stitch(models_from_schema, create_add_migs)
models = Trains::Utils::MigrationTailor.stitch(models_from_schema,
create_add_migs)
expect(models).to eq(
{
'Post' =>
Expand Down Expand Up @@ -135,4 +136,75 @@
)
end
end

context 'Given migrations with rename and remove column' do
let(:migs_with_rename) do
[
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :create_table,
fields: [
Trains::DTO::Field.new(:id, :bigint),
Trains::DTO::Field.new(:title, :string),
Trains::DTO::Field.new(:age, :integer),
Trains::DTO::Field.new(:created_at, :datetime),
Trains::DTO::Field.new(:updated_at, :datetime)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :add_column,
fields: [
Trains::DTO::Field.new(:name, :string)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :change_table,
fields: [
Trains::DTO::Field.new(:title, :remove),
Trains::DTO::Field.new(%i[name whatup], :rename)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :rename_column,
fields: [
Trains::DTO::Field.new(:age, :alive_since)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :rename_column,
fields: [
Trains::DTO::Field.new(:whatup, :name)
],
version: 7.0
)
]
end

it 'generates models with renamed fields' do
models = Trains::Utils::MigrationTailor.stitch(migs_with_rename)
expect(models).to eq(
{
'Group' => Trains::DTO::Model.new(
name: 'Group',
version: 7.0,
fields: [
Trains::DTO::Field.new(:id, :bigint),
Trains::DTO::Field.new(:created_at, :datetime),
Trains::DTO::Field.new(:updated_at, :datetime),
Trains::DTO::Field.new(:alive_since, :integer),
Trains::DTO::Field.new(:name, :string)
]
)
}
)
end
end
end
65 changes: 65 additions & 0 deletions spec/lib/trains/visitor/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
File.expand_path "#{__FILE__}/../../../../fixtures/safety_assured.rb"
end

let(:change_table) do
File.expand_path "#{__FILE__}/../../../../fixtures/change_table.rb"
end

context 'Given a valid DB migration file path' do
it 'returns an object with its metadata' do
parser = described_class.new
Expand Down Expand Up @@ -298,4 +302,65 @@
end
end
end

context 'Give a migration containing change_table migration' do
it 'create the appropriate migration objects' do
parser = described_class.new
file_ast =
RuboCop::AST::ProcessedSource.from_file(
change_table,
RUBY_VERSION.to_f
).ast
file_ast.each_node { |node| parser.process(node) }

expect(parser.result).to eq(
[
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :create_table,
fields: [
Trains::DTO::Field.new(:id, :bigint),
Trains::DTO::Field.new(:title, :string),
Trains::DTO::Field.new(:created_at, :datetime),
Trains::DTO::Field.new(:updated_at, :datetime)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :add_column,
fields: [
Trains::DTO::Field.new(:name, :string)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :change_table,
fields: [
Trains::DTO::Field.new(:title, :remove),
Trains::DTO::Field.new(%i[name whatup], :rename)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :rename_column,
fields: [
Trains::DTO::Field.new(:whatup, :name)
],
version: 7.0
),
Trains::DTO::Migration.new(
table_name: 'Group',
modifier: :remove_column,
fields: [
Trains::DTO::Field.new(:name, nil)
],
version: 7.0
)
]
)
end
end
end

0 comments on commit 94c2393

Please sign in to comment.