Does GORM have a way to do a migration dry run / check if DB needs any changes?
Should be answered on migration docs page
Expected answer
I would like to be able to run a dry run to see if the database needs any migration. Something like:
func (m Migrator) NeedsMigration(dst ...interface{}) (bool, error)
For example, to check the whole DB:
m := db.Migrator()
needsMigration, _ := m.NeedsMigration()
And maybe to check a single table:
needsMigration, _ := m.NeedsMigration(&models.User{})
I looked into altering the code a bit this morning, but thought I should ask to see if anyone else has encountered this issue.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: jinzhu
AutoMigrate will migrate if necessary, but there is not dry run mode.
Comment From: molind
@jinzhu there should be IsMigrationRequired() or something. Just to make sure it won't AutoMigrate something in production and let developer know about that migration is required. May be even print queued SQL commands.
I plan to create own Dialector, to provide custom Migrator, that will fail on any modification function. Then call AutoMigrate and catch any thrown panic().
All migrations in production servers should be explicit and launched manually with ./service migrate.
Comment From: molind
Minimal working sample. https://gist.github.com/molind/a67100448b886b7257e30799e06a0718
Comment From: defool
Minimal working sample. https://gist.github.com/molind/a67100448b886b7257e30799e06a0718
I think it is nice to translate the output to SQL format.
Comment From: molind
@defool SQL generation is DB dependent. May be there is a simple solution, but I don't know how to do it. You may try to do it yourself.
Comment From: PalanQu
@defool I write an example to generate mysql sql with gorm, https://github.com/PalanQu/easycoding#topic1-database-migrate