@@ -393,6 +393,87 @@ defmodule AshPostgres.MigrationGeneratorTest do
393
393
end
394
394
end
395
395
396
+ describe "custom_indexes with follow up migrations" 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
+ custom_indexes do
406
+ index ( [ :title ] )
407
+ end
408
+ end
409
+
410
+ attributes do
411
+ uuid_primary_key ( :id )
412
+ attribute ( :title , :string , public?: true )
413
+ end
414
+ end
415
+
416
+ defdomain ( [ Post ] )
417
+
418
+ AshPostgres.MigrationGenerator . generate ( Domain ,
419
+ snapshot_path: "test_snapshots_path" ,
420
+ migration_path: "test_migration_path" ,
421
+ quiet: true ,
422
+ format: false
423
+ )
424
+ end
425
+
426
+ test "it changes attribute and index in the correct order" do
427
+ defposts do
428
+ postgres do
429
+ custom_indexes do
430
+ index ( [ :title_short ] )
431
+ end
432
+ end
433
+
434
+ attributes do
435
+ uuid_primary_key ( :id )
436
+ attribute ( :title_short , :string , public?: true )
437
+ end
438
+ end
439
+
440
+ defdomain ( [ Post ] )
441
+
442
+ send ( self ( ) , { :mix_shell_input , :yes? , true } )
443
+
444
+ AshPostgres.MigrationGenerator . generate ( Domain ,
445
+ snapshot_path: "test_snapshots_path" ,
446
+ migration_path: "test_migration_path" ,
447
+ quiet: true ,
448
+ format: false
449
+ )
450
+
451
+ assert [ _file1 , file2 ] =
452
+ Enum . sort ( Path . wildcard ( "test_migration_path/**/*_migrate_resources*.exs" ) )
453
+ |> Enum . reject ( & String . contains? ( & 1 , "extensions" ) )
454
+
455
+ contents = File . read! ( file2 )
456
+
457
+ [ up_side , down_side ] = String . split ( contents , "def down" , parts: 2 )
458
+
459
+ up_side_parts = String . split ( up_side , "\n " , trim: true )
460
+
461
+ assert Enum . find_index ( up_side_parts , fn x ->
462
+ x == "rename table(:posts), :title, to: :title_short"
463
+ end ) <
464
+ Enum . find_index ( up_side_parts , fn x ->
465
+ x == "create index(:posts, [:title_short])"
466
+ end )
467
+
468
+ down_side_parts = String . split ( down_side , "\n " , trim: true )
469
+
470
+ assert Enum . find_index ( down_side_parts , fn x ->
471
+ x == "rename table(:posts), :title_short, to: :title"
472
+ end ) <
473
+ Enum . find_index ( down_side_parts , fn x -> x == "create index(:posts, [:title])" end )
474
+ end
475
+ end
476
+
396
477
describe "creating follow up migrations with a composite primary key" do
397
478
setup do
398
479
on_exit ( fn ->
0 commit comments