@@ -1298,6 +1298,12 @@ defmodule AshPostgres.MigrationGenerator do
12981298 defp after? ( % Operation.RemovePrimaryKeyDown { } , _ ) , do: true
12991299 defp after? ( _ , % Operation.RemovePrimaryKeyDown { } ) , do: false
13001300
1301+ defp after? ( % Operation.AddPrimaryKeyDown { } , _ ) , do: false
1302+ defp after? ( _ , % Operation.AddPrimaryKeyDown { } ) , do: true
1303+
1304+ defp after? ( % Operation.AddPrimaryKey { } , _ ) , do: true
1305+ defp after? ( _ , % Operation.AddPrimaryKey { } ) , do: false
1306+
13011307 defp after? (
13021308 % Operation.AddCustomStatement { } ,
13031309 _
@@ -1803,7 +1809,9 @@ defmodule AshPostgres.MigrationGenerator do
18031809
18041810 defp do_fetch_operations ( snapshot , old_snapshot , opts , acc ) do
18051811 attribute_operations = attribute_operations ( snapshot , old_snapshot , opts )
1806- pkey_operations = pkey_operations ( snapshot , old_snapshot , attribute_operations , opts )
1812+
1813+ { pkey_operations , attribute_operations } =
1814+ pkey_operations ( snapshot , old_snapshot , attribute_operations , opts )
18071815
18081816 rewrite_all_identities? = changing_multitenancy_affects_identities? ( snapshot , old_snapshot )
18091817
@@ -2097,7 +2105,7 @@ defmodule AshPostgres.MigrationGenerator do
20972105
20982106 defp pkey_operations ( snapshot , old_snapshot , attribute_operations , opts ) do
20992107 if old_snapshot [ :empty? ] do
2100- [ ]
2108+ { [ ] , attribute_operations }
21012109 else
21022110 must_drop_pkey? =
21032111 Enum . any? (
@@ -2115,11 +2123,38 @@ defmodule AshPostgres.MigrationGenerator do
21152123 } ->
21162124 true
21172125
2126+ % Operation.RemoveAttribute {
2127+ attribute: % { primary_key?: true }
2128+ } ->
2129+ true
2130+
21182131 _ ->
21192132 false
21202133 end
21212134 )
21222135
2136+ must_add_primary_key? =
2137+ must_drop_pkey? &&
2138+ Enum . any? ( snapshot . attributes , fn attribute ->
2139+ attribute . primary_key?
2140+ end )
2141+
2142+ must_add_primary_key_in_down? =
2143+ must_drop_pkey? &&
2144+ Enum . any? ( snapshot . attributes , fn attribute ->
2145+ attribute . primary_key? &&
2146+ ! Enum . any? ( attribute_operations , fn
2147+ % Operation.AlterAttribute { } = operation ->
2148+ operation . new_attribute . source == attribute . source
2149+
2150+ % Operation.AddAttribute { } = operation ->
2151+ operation . attribute . source == attribute . source
2152+
2153+ _ ->
2154+ false
2155+ end )
2156+ end )
2157+
21232158 drop_in_down? =
21242159 Enum . any? ( attribute_operations , fn
21252160 % Operation.AlterAttribute {
@@ -2148,17 +2183,94 @@ defmodule AshPostgres.MigrationGenerator do
21482183 false
21492184 end )
21502185
2151- [
2152- must_drop_pkey? &&
2153- % Operation.RemovePrimaryKey { schema: snapshot . schema , table: snapshot . table } ,
2154- must_drop_pkey? && drop_in_down? &&
2155- % Operation.RemovePrimaryKeyDown {
2156- commented?: opts . dont_drop_columns && drop_in_down_commented? ,
2157- schema: snapshot . schema ,
2158- table: snapshot . table
2159- }
2160- ]
2161- |> Enum . filter ( & & 1 )
2186+ attribute_operations =
2187+ if must_add_primary_key? do
2188+ Enum . map (
2189+ attribute_operations ,
2190+ fn
2191+ % Operation.AlterAttribute { } = operation ->
2192+ % {
2193+ operation
2194+ | new_attribute: % {
2195+ operation . new_attribute
2196+ | primary_key?: operation . old_attribute . primary_key?
2197+ }
2198+ }
2199+
2200+ % Operation.AddAttribute { } = operation ->
2201+ % { operation | attribute: % { operation . attribute | primary_key?: false } }
2202+
2203+ other ->
2204+ other
2205+ end
2206+ )
2207+ else
2208+ attribute_operations
2209+ end
2210+
2211+ attribute_operations =
2212+ if must_add_primary_key_in_down? do
2213+ Enum . map (
2214+ attribute_operations ,
2215+ fn
2216+ % Operation.AlterAttribute { } = operation ->
2217+ % {
2218+ operation
2219+ | old_attribute: % {
2220+ operation . old_attribute
2221+ | primary_key?: operation . new_attribute . primary_key?
2222+ }
2223+ }
2224+
2225+ % Operation.RemoveAttribute { } = operation ->
2226+ % { operation | attribute: % { operation . attribute | primary_key?: false } }
2227+
2228+ other ->
2229+ other
2230+ end
2231+ )
2232+ else
2233+ attribute_operations
2234+ end
2235+
2236+ { [
2237+ must_drop_pkey? &&
2238+ % Operation.RemovePrimaryKey { schema: snapshot . schema , table: snapshot . table } ,
2239+ must_drop_pkey? && drop_in_down? &&
2240+ % Operation.RemovePrimaryKeyDown {
2241+ commented?: opts . dont_drop_columns && drop_in_down_commented? ,
2242+ schema: snapshot . schema ,
2243+ table: snapshot . table
2244+ } ,
2245+ must_add_primary_key? &&
2246+ % Operation.AddPrimaryKey {
2247+ schema: snapshot . schema ,
2248+ table: snapshot . table ,
2249+ keys:
2250+ Enum . flat_map ( snapshot . attributes , fn attribute ->
2251+ if attribute . primary_key? do
2252+ [ attribute . source ]
2253+ else
2254+ [ ]
2255+ end
2256+ end )
2257+ } ,
2258+ must_add_primary_key_in_down? &&
2259+ % Operation.AddPrimaryKeyDown {
2260+ schema: old_snapshot . schema ,
2261+ table: old_snapshot . table ,
2262+ remove_old?: must_add_primary_key? && ! ( must_drop_pkey? && drop_in_down? ) ,
2263+ keys:
2264+ Enum . flat_map ( old_snapshot . attributes , fn attribute ->
2265+ if attribute . primary_key? do
2266+ [ attribute . source ]
2267+ else
2268+ [ ]
2269+ end
2270+ end )
2271+ }
2272+ ]
2273+ |> Enum . filter ( & & 1 ) , attribute_operations }
21622274 end
21632275 end
21642276
0 commit comments