From 88740e4de5307417a8932867e56411d3e4e60729 Mon Sep 17 00:00:00 2001 From: Morgan Aubert Date: Thu, 3 Nov 2022 22:12:58 -0400 Subject: [PATCH] Fix missing multicolumns indexes in migrations for newly created models --- .../db/management/migrations/diff_spec.cr | 48 +++++++++++++++++++ src/marten/db/management/migrations/diff.cr | 7 +-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/spec/marten/db/management/migrations/diff_spec.cr b/spec/marten/db/management/migrations/diff_spec.cr index 92c918f66..0261c3eed 100644 --- a/spec/marten/db/management/migrations/diff_spec.cr +++ b/spec/marten/db/management/migrations/diff_spec.cr @@ -48,6 +48,54 @@ describe Marten::DB::Management::Migrations::Diff do operation.unique_constraints[0].column_names.should eq ["foo", "bar"] end + it "is able to detect the addition of a new table with indexes" do + from_project_state = Marten::DB::Management::ProjectState.new + + new_table_state = Marten::DB::Management::TableState.new( + app_label: "app", + name: "new_table", + columns: [ + Marten::DB::Management::Column::BigInt.new("id", primary_key: true, auto: true), + Marten::DB::Management::Column::BigInt.new("foo"), + Marten::DB::Management::Column::BigInt.new("bar"), + ] of Marten::DB::Management::Column::Base, + indexes: [ + Marten::DB::Management::Index.new("test_index", ["foo", "bar"]), + ] + ) + to_project_state = Marten::DB::Management::ProjectState.new(tables: [new_table_state]) + + diff = Marten::DB::Management::Migrations::Diff.new(from_project_state, to_project_state) + changes = diff.detect + + changes.size.should eq 1 + changes["app"].size.should eq 1 + + changes["app"][0].name.ends_with?("create_new_table_table").should be_true + + changes["app"][0].operations.size.should eq 1 + changes["app"][0].operations[0].should be_a Marten::DB::Migration::Operation::CreateTable + + operation = changes["app"][0].operations[0].as(Marten::DB::Migration::Operation::CreateTable) + operation.name.should eq "new_table" + + operation.columns.size.should eq 3 + operation.columns[0].should be_a Marten::DB::Management::Column::BigInt + operation.columns[0].name.should eq "id" + operation.columns[0].as(Marten::DB::Management::Column::BigInt).primary_key?.should be_true + operation.columns[0].as(Marten::DB::Management::Column::BigInt).auto?.should be_true + operation.columns[1].should be_a Marten::DB::Management::Column::BigInt + operation.columns[1].name.should eq "foo" + operation.columns[2].should be_a Marten::DB::Management::Column::BigInt + operation.columns[2].name.should eq "bar" + + operation.unique_constraints.size.should eq 0 + + operation.indexes.size.should eq 1 + operation.indexes[0].name.should eq "test_index" + operation.indexes[0].column_names.should eq ["foo", "bar"] + end + it "is able to detect the addition of new columns to existing tables" do from_project_state = Marten::DB::Management::ProjectState.new( tables: [ diff --git a/src/marten/db/management/migrations/diff.cr b/src/marten/db/management/migrations/diff.cr index 6c80d54dd..72aeabe34 100644 --- a/src/marten/db/management/migrations/diff.cr +++ b/src/marten/db/management/migrations/diff.cr @@ -359,9 +359,10 @@ module Marten insert_operation( created_table.app_label, DB::Migration::Operation::CreateTable.new( - created_table.name, - created_table.columns.dup, - created_table.unique_constraints.dup + name: created_table.name, + columns: created_table.columns.dup, + unique_constraints: created_table.unique_constraints.dup, + indexes: created_table.indexes.dup ), dependencies, beginning: true