@@ -393,6 +393,105 @@ defmodule AshPostgres.MigrationGeneratorTest do
393393 end
394394 end
395395
396+ describe "creating follow up migrations with a composite primary key" do
397+ setup do
398+ on_exit ( fn ->
399+ File . rm_rf! ( "test_snapshots_path" )
400+ File . rm_rf! ( "test_migration_path" )
401+ end )
402+
403+ defposts do
404+ postgres do
405+ schema ( "example" )
406+ end
407+
408+ attributes do
409+ uuid_primary_key ( :id )
410+ attribute ( :title , :string , public?: true , primary_key?: true , allow_nil?: false )
411+ end
412+ end
413+
414+ defdomain ( [ Post ] )
415+
416+ AshPostgres.MigrationGenerator . generate ( Domain ,
417+ snapshot_path: "test_snapshots_path" ,
418+ migration_path: "test_migration_path" ,
419+ quiet: true ,
420+ format: false
421+ )
422+
423+ :ok
424+ end
425+
426+ test "when removing an element, it recreates the primary key" do
427+ defposts do
428+ postgres do
429+ schema ( "example" )
430+ end
431+
432+ attributes do
433+ uuid_primary_key ( :id )
434+ end
435+ end
436+
437+ defdomain ( [ Post ] )
438+
439+ send ( self ( ) , { :mix_shell_input , :yes? , true } )
440+
441+ AshPostgres.MigrationGenerator . generate ( Domain ,
442+ snapshot_path: "test_snapshots_path" ,
443+ migration_path: "test_migration_path" ,
444+ quiet: true ,
445+ format: false
446+ )
447+
448+ assert [ _file1 , file2 ] =
449+ Enum . sort ( Path . wildcard ( "test_migration_path/**/*_migrate_resources*.exs" ) )
450+ |> Enum . reject ( & String . contains? ( & 1 , "extensions" ) )
451+
452+ contents = File . read! ( file2 )
453+
454+ [ up_side , down_side ] = String . split ( contents , "def down" , parts: 2 )
455+
456+ assert up_side =~ ~S[ execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id)")]
457+ assert down_side =~ ~S[ execute("ALTER TABLE \"example.posts\" DROP constraint posts_pkey")]
458+ assert down_side =~ ~S[ execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id, title)")]
459+
460+ defposts do
461+ postgres do
462+ schema ( "example" )
463+ end
464+
465+ attributes do
466+ uuid_primary_key ( :id )
467+ attribute ( :title , :string , public?: true , primary_key?: true , allow_nil?: false )
468+ end
469+ end
470+
471+ defdomain ( [ Post ] )
472+
473+ send ( self ( ) , { :mix_shell_input , :yes? , true } )
474+
475+ AshPostgres.MigrationGenerator . generate ( Domain ,
476+ snapshot_path: "test_snapshots_path" ,
477+ migration_path: "test_migration_path" ,
478+ quiet: true ,
479+ format: false
480+ )
481+
482+ assert [ _file1 , _file2 , file3 ] =
483+ Enum . sort ( Path . wildcard ( "test_migration_path/**/*_migrate_resources*.exs" ) )
484+ |> Enum . reject ( & String . contains? ( & 1 , "extensions" ) )
485+
486+ contents = File . read! ( file3 )
487+
488+ [ up_side , down_side ] = String . split ( contents , "def down" , parts: 2 )
489+
490+ assert up_side =~ ~S[ execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id, title)")]
491+ assert down_side =~ ~S[ execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id)")]
492+ end
493+ end
494+
396495 describe "creating follow up migrations with a schema" do
397496 setup do
398497 on_exit ( fn ->
0 commit comments