@@ -1377,6 +1377,84 @@ func TestPgrollSchemaVersionUpgrades(t *testing.T) {
1377
1377
}
1378
1378
}
1379
1379
1380
+ func TestSchemaAfterMigration (t * testing.T ) {
1381
+ t .Parallel ()
1382
+
1383
+ t .Run ("returns error for non-existent migration" , func (t * testing.T ) {
1384
+ testutils .WithStateAndConnectionToContainer (t , func (st * state.State , db * sql.DB ) {
1385
+ ctx := context .Background ()
1386
+
1387
+ _ , err := st .SchemaAfterMigration (ctx , "public" , "non_existent_migration" )
1388
+
1389
+ require .ErrorIs (t , err , sql .ErrNoRows )
1390
+ })
1391
+ })
1392
+
1393
+ t .Run ("returns the schemas after the two most recent migrations" , func (t * testing.T ) {
1394
+ testutils .WithStateAndConnectionToContainer (t , func (st * state.State , db * sql.DB ) {
1395
+ ctx := context .Background ()
1396
+
1397
+ // Run some SQL to generate a first migration
1398
+ _ , err := db .ExecContext (ctx , "CREATE TABLE items (id int NOT NULL)" )
1399
+ require .NoError (t , err )
1400
+
1401
+ // Run some more SQL to generate a second migration
1402
+ _ , err = db .ExecContext (ctx , "ALTER TABLE items ADD COLUMN name text NOT NULL" )
1403
+ require .NoError (t , err )
1404
+
1405
+ // Get the schema history
1406
+ hist , err := st .SchemaHistory (ctx , "public" )
1407
+ require .NoError (t , err )
1408
+
1409
+ // Assert that the schema history has the expected length
1410
+ require .Len (t , hist , 2 )
1411
+
1412
+ // Get the schema after the first migration
1413
+ sc , err := st .SchemaAfterMigration (ctx , "public" , hist [0 ].Migration .Name )
1414
+ require .NoError (t , err )
1415
+
1416
+ // Assert the schema after the first migration
1417
+ expectedTable := & schema.Table {
1418
+ Name : "items" ,
1419
+ Columns : map [string ]* schema.Column {
1420
+ "id" : {
1421
+ Name : "id" ,
1422
+ Type : "integer" ,
1423
+ PostgresType : "base" ,
1424
+ },
1425
+ },
1426
+ }
1427
+ clearOIDS (sc )
1428
+ require .Len (t , sc .Tables , 1 )
1429
+ require .Equal (t , expectedTable , sc .Tables ["items" ])
1430
+
1431
+ // Get the schema after the second migration
1432
+ sc , err = st .SchemaAfterMigration (ctx , "public" , hist [1 ].Migration .Name )
1433
+ require .NoError (t , err )
1434
+
1435
+ // Assert the schema after the second migration
1436
+ expectedTable = & schema.Table {
1437
+ Name : "items" ,
1438
+ Columns : map [string ]* schema.Column {
1439
+ "id" : {
1440
+ Name : "id" ,
1441
+ Type : "integer" ,
1442
+ PostgresType : "base" ,
1443
+ },
1444
+ "name" : {
1445
+ Name : "name" ,
1446
+ Type : "text" ,
1447
+ PostgresType : "base" ,
1448
+ },
1449
+ },
1450
+ }
1451
+ clearOIDS (sc )
1452
+ require .Len (t , sc .Tables , 1 )
1453
+ require .Equal (t , expectedTable , sc .Tables ["items" ])
1454
+ })
1455
+ })
1456
+ }
1457
+
1380
1458
func clearOIDS (s * schema.Schema ) {
1381
1459
for k := range s .Tables {
1382
1460
c := s .Tables [k ]
0 commit comments