Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace gorm.Migrator calls with sql #2846

Merged
merged 5 commits into from Aug 9, 2022

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented Aug 5, 2022

Summary

gorm.Migrator (not to be confused with gormigrate which we already replaced) provides helper functions for inspecting and changing the schema.

This PR replaces calls to those helpers with regular SQL. In a couple cases (HasTable, HasColumn) I added a helper function to the data/migrator package, because these two require inspecting the informational schema and are a bit more involved.

This PR keeps sqlite support by writing the equivalent sqlite queries (which are not much different), but it is important to note that the sqlite versions of a couple of these are not idempotent.

The only remaining use of gorm.Migrator is AutoMigrate, which will be replaced next.

Related Issues

Related to #2768

Comment on lines +48 to +50
AND sql LIKE ?
`
column = "%`" + column + "`%"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I based this on the gorm migrator implementation we're using now: https://github.com/go-gorm/sqlite/blob/v1.3.6/migrator.go#L70-L71

The sqlite_master table stores the CREATE TABLE statement as sql, so we have to use substring matching to find the column.

The gorm implementation has a lot more sql LIKE ? parts of the query. My guess is that different sqlite implementations might use different quoting for column names. This works for our existing use cases, and since we won't support sqlite in production, this seems sufficient for our use case.

Copy link
Contributor

@ssoroka ssoroka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm


// DefaultOptions used for tests
var DefaultOptions = Options{
UseTransaction: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably start using transactions when we drop sqlite

return nil
}

if err := tx.Migrator().AddColumn(&models.Destination{}, "last_seen_at"); err != nil {
stmt := `ALTER TABLE destinations ADD COLUMN last_seen_at timestamp with time zone`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, I prefer the timezone in the db, but do you know if other tables in postgres have been getting timestamp with time zone? might cause subtle problems somewhere if they're different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, this matches what gorm.AutoMigrate produces, you can see that here: https://github.com/infrahq/infra/pull/2843/files#r940287138

And move DefaultOptions to test code, since it is only used by tests.

Also format the SQL statements with consistent casing.
Also replace our tableExists test helper.

These are replaced with a new HasTable function that should be easy to change to stdlib.
This one doesn't really need a helper, we can write the DROP TABLE statement
in place.
Replace with regular sql statements.
@dnephin dnephin force-pushed the dnephin/data-reaplce-gorm-migrator branch from c8f16f8 to f1b3126 Compare August 9, 2022 16:30
@dnephin dnephin merged commit b7a5bd1 into main Aug 9, 2022
@dnephin dnephin deleted the dnephin/data-reaplce-gorm-migrator branch August 9, 2022 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants