Skip to content

Commit 5332f59

Browse files
Jacob AtanasioJacob Atanasio
authored andcommitted
Override Type
1 parent b9b0e60 commit 5332f59

File tree

2 files changed

+73
-93
lines changed

2 files changed

+73
-93
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -792,18 +792,12 @@ defmodule AshPostgres.MigrationGenerator do
792792
snapshots
793793
|> Enum.count(& &1.has_create_action)
794794

795-
identity_column_defaults =
796-
snapshots
797-
|> Enum.flat_map(&Map.get(&1, :identity_column_defaults, []))
798-
|> Enum.uniq()
799-
800795
new_snapshot = %{
801796
snapshot
802797
| attributes: merge_attributes(attributes, snapshot.table, count_with_create),
803798
identities: snapshots |> Enum.flat_map(& &1.identities) |> Enum.uniq(),
804799
custom_indexes: snapshots |> Enum.flat_map(& &1.custom_indexes) |> Enum.uniq(),
805-
custom_statements: snapshots |> Enum.flat_map(& &1.custom_statements) |> Enum.uniq(),
806-
identity_column_defaults: identity_column_defaults
800+
custom_statements: snapshots |> Enum.flat_map(& &1.custom_statements) |> Enum.uniq()
807801
}
808802

809803
all_identities =
@@ -939,13 +933,16 @@ defmodule AshPostgres.MigrationGenerator do
939933
end
940934

941935
defp merge_types(types, name, table) do
942-
types
943-
|> Enum.uniq()
944-
|> case do
945-
[type] ->
946-
type
936+
types = Enum.uniq(types)
947937

948-
types ->
938+
cond do
939+
length(types) == 1 ->
940+
hd(types)
941+
942+
:identity in types ->
943+
:identity
944+
945+
true ->
949946
raise "Conflicting types for table `#{table}.#{name}`: #{inspect(types)}"
950947
end
951948
end
@@ -2621,15 +2618,13 @@ defmodule AshPostgres.MigrationGenerator do
26212618
%Operation.AddAttribute{
26222619
attribute: Map.delete(attribute, :references),
26232620
schema: snapshot.schema,
2624-
table: snapshot.table,
2625-
identity_column_defaults: Map.get(snapshot, :identity_column_defaults, [])
2621+
table: snapshot.table
26262622
},
26272623
%Operation.AlterAttribute{
26282624
old_attribute: Map.delete(attribute, :references),
26292625
new_attribute: attribute,
26302626
schema: snapshot.schema,
2631-
table: snapshot.table,
2632-
identity_column_defaults: Map.get(snapshot, :identity_column_defaults, [])
2627+
table: snapshot.table
26332628
},
26342629
%Operation.DropForeignKey{
26352630
attribute: attribute,
@@ -2644,8 +2639,7 @@ defmodule AshPostgres.MigrationGenerator do
26442639
%Operation.AddAttribute{
26452640
attribute: attribute,
26462641
table: snapshot.table,
2647-
schema: snapshot.schema,
2648-
identity_column_defaults: Map.get(snapshot, :identity_column_defaults, [])
2642+
schema: snapshot.schema
26492643
}
26502644
]
26512645
end
@@ -2704,8 +2698,7 @@ defmodule AshPostgres.MigrationGenerator do
27042698
new_attribute: new_attribute,
27052699
old_attribute: old_attribute,
27062700
schema: snapshot.schema,
2707-
table: snapshot.table,
2708-
identity_column_defaults: Map.get(snapshot, :identity_column_defaults, [])
2701+
table: snapshot.table
27092702
}
27102703
]
27112704
else
@@ -2714,8 +2707,7 @@ defmodule AshPostgres.MigrationGenerator do
27142707
new_attribute: new_attribute,
27152708
old_attribute: old_attribute,
27162709
schema: snapshot.schema,
2717-
table: snapshot.table,
2718-
identity_column_defaults: Map.get(snapshot, :identity_column_defaults, [])
2710+
table: snapshot.table
27192711
}
27202712
]
27212713
end ++
@@ -2743,8 +2735,7 @@ defmodule AshPostgres.MigrationGenerator do
27432735
new_attribute: Map.delete(new_attribute, :references),
27442736
old_attribute: Map.delete(old_attribute, :references),
27452737
schema: snapshot.schema,
2746-
table: snapshot.table,
2747-
identity_column_defaults: Map.get(snapshot, :identity_column_defaults, [])
2738+
table: snapshot.table
27482739
}
27492740
]
27502741
end
@@ -3172,8 +3163,7 @@ defmodule AshPostgres.MigrationGenerator do
31723163
multitenancy: multitenancy(resource),
31733164
base_filter: AshPostgres.DataLayer.Info.base_filter_sql(resource),
31743165
has_create_action: has_create_action?(resource),
3175-
create_table_options: AshPostgres.DataLayer.Info.create_table_options(resource),
3176-
identity_column_defaults: AshPostgres.DataLayer.Info.identity_column_defaults(resource)
3166+
create_table_options: AshPostgres.DataLayer.Info.create_table_options(resource)
31773167
}
31783168

31793169
hash =
@@ -3293,6 +3283,16 @@ defmodule AshPostgres.MigrationGenerator do
32933283
type
32943284
end
32953285

3286+
identity_column_defaults = AshPostgres.DataLayer.Info.identity_column_defaults(resource)
3287+
source = attribute.source || attribute.name
3288+
3289+
type =
3290+
if source in identity_column_defaults and attribute.generated? and type in [:bigint, :integer] do
3291+
:identity
3292+
else
3293+
type
3294+
end
3295+
32963296
{type, size, precision, scale} =
32973297
migration_type_to_type(type)
32983298

0 commit comments

Comments
 (0)