@@ -55,6 +55,7 @@ func TestMigrate(t *testing.T) {
5555 tests := []Test {
5656 {run : testMigrateUpAndDown },
5757 {run : testMigrateUpError },
58+ {run : testRunMigration },
5859 }
5960
6061 testEachDB (t , func (t * testing.T , dbName string , db * bun.DB ) {
@@ -180,6 +181,64 @@ func testMigrateUpError(t *testing.T, db *bun.DB) {
180181 require .Equal (t , []string {"down2" , "down1" }, history )
181182}
182183
184+ func testRunMigration (t * testing.T , db * bun.DB ) {
185+ ctx := context .Background ()
186+ cleanupMigrations (t , ctx , db )
187+
188+ var history []string
189+
190+ migrations := migrate .NewMigrations ()
191+ migrations .Add (migrate.Migration {
192+ Name : "20060102150405" ,
193+ Up : func (ctx context.Context , migrator * migrate.Migrator , migration * migrate.Migration ) error {
194+ history = append (history , "up1" )
195+ return nil
196+ },
197+ })
198+ migrations .Add (migrate.Migration {
199+ Name : "20060102160405" ,
200+ Up : func (ctx context.Context , migrator * migrate.Migrator , migration * migrate.Migration ) error {
201+ history = append (history , "up2" )
202+ return nil
203+ },
204+ })
205+
206+ m := migrate .NewMigrator (db , migrations ,
207+ migrate .WithTableName (migrationsTable ),
208+ migrate .WithLocksTableName (migrationLocksTable ),
209+ )
210+ require .NoError (t , m .Reset (ctx ))
211+
212+ _ , err := m .Migrate (ctx )
213+ require .NoError (t , err )
214+ require .Equal (t , []string {"up1" , "up2" }, history )
215+
216+ appliedBefore , err := m .AppliedMigrations (ctx )
217+ require .NoError (t , err )
218+ migrationIDs := func (ms migrate.MigrationSlice ) []int64 {
219+ ids := make ([]int64 , len (ms ))
220+ for i := range ms {
221+ ids [i ] = ms [i ].ID
222+ }
223+ return ids
224+ }
225+ migrationsWithStatus , err := m .MigrationsWithStatus (ctx )
226+ require .NoError (t , err )
227+ require .Len (t , migrationsWithStatus , 2 )
228+
229+ targetID := migrationsWithStatus [0 ].ID
230+ require .NotZero (t , targetID )
231+
232+ history = nil
233+ err = m .RunMigration (ctx , targetID )
234+ require .NoError (t , err )
235+ require .Equal (t , []string {"up1" }, history )
236+
237+ appliedAfter , err := m .AppliedMigrations (ctx )
238+ require .NoError (t , err )
239+ require .ElementsMatch (t , migrationIDs (appliedBefore ), migrationIDs (appliedAfter ))
240+ }
241+
183242// newAutoMigratorOrSkip creates an AutoMigrator configured to use test migratins/locks
184243// tables and dedicated migrations directory. If an AutoMigrator cannob be created because
185244// the dialect doesn't support either schema inspections or migrations, the test will be *skipped*
0 commit comments