Skip to content

Once the InitSchema function is called, the contents of []*gormigrate.Migration are not executed as expected. #212

@shi-yang

Description

@shi-yang

My database is clean, without any tables.

I want to InitSchema and do some other migrations, just like this:

	m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
		&gormigrate.Migration{
			ID: "20230716",
			Migrate: func(tx *gorm.DB) error {
				return tx.Exec("INSERT INTO user (username) VALUES ('admin')").Error
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Exec("DELETE FROM user WHERE username='admin'").Error
			},
		},
	})
	m.InitSchema(func(tx *gorm.DB) error {
		err := tx.AutoMigrate(
			&User{},
			// all other tables of you app
		)
		return err
	})
	if err := m.Migrate(); err != nil {
		log.Fatalf("Migration failed: %v", err)
	}
	log.Println("Migration did run successfully")

However, the sql INSERT INTO user (username) VALUES ('admin') was not executed as expected.

I then checked the source code of this library. There is only insertMigration in this place:

gormigrate/gormigrate.go

Lines 347 to 351 in 293e5ee

for _, migration := range g.migrations {
if err := g.insertMigration(migration.ID); err != nil {
return err
}
}

Is migration.Migrate(g.tx) missing. Or should it be call g.runMigration(migration.ID) instead of g.insertMigration(migration.ID)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions