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

Add field tag to ignore migration #4028

Merged
merged 3 commits into from
Feb 9, 2021
Merged

Add field tag to ignore migration #4028

merged 3 commits into from
Feb 9, 2021

Conversation

yrong
Copy link
Contributor

@yrong yrong commented Jan 31, 2021

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

User Case Description

import (
	"github.com/shopspring/decimal"
       ...
)

type ChainExtrinsic struct {
Fee                decimal.Decimal `json:"fee" sql:"type:decimal(30,0);"`//v1
...
}
type ChainExtrinsic struct {
Fee                decimal.Decimal `json:"fee" gorm:"type:decimal(30,0);"`//v2
...
}
db.AutoMigrate()

/*Actually there is no field definition change between v1 and v2.
for mysql migration from gorm v1 to v2 the unnessary alter column ddl as following will be generated
the worst thing is after migrating to v2, will still generate ddl each time when run automigrate
---ALTER TABLE `chain_extrinsics` MODIFY COLUMN `fee` decimal(30,0)
*/

we can avoid by add field tag as following:

type ChainExtrinsic struct {
Fee                decimal.Decimal `json:"fee" gorm:"type:decimal(30,0);ignoreMigration"`
...
}

@jinzhu
Copy link
Member

jinzhu commented Feb 2, 2021

Hello @yrong

For you example, I would suggest you add a precision tag like:

type ChainExtrinsic struct {
  Fee                decimal.Decimal `json:"fee" gorm:"type:decimal(30,0);precision:30"`
  ...
}

but the skip migration feature sounds interesting, maybe we could support it like following, would you mind to make the change?

type ChainExtrinsic struct {
  Fee                decimal.Decimal `json:"fee" gorm:"type:decimal(30,0);-:migration"`
  ...
}

@yrong
Copy link
Contributor Author

yrong commented Feb 6, 2021

@jinzhu
Thanks for the clarifaction about precision tag and will take a try. For the skip migration I think maybe there is some confliction with the exist Field-Level Permission Definition which is essentially different and better not mixed together

Name string gorm:"-" // ignore this field when write and read with struct

@yrong
Copy link
Contributor Author

yrong commented Feb 6, 2021

@jinzhu
Btw, I add one minor fix 4a1096b for model definition as following which works in gorm v1 but failed in v2, maybe something related to #3363

Address string gorm:"default: null;size:100" json:"address" //there is a leading space before null

@jinzhu
Copy link
Member

jinzhu commented Feb 7, 2021

@jinzhu
Thanks for the clarifaction about precision tag and will take a try. For the skip migration I think maybe there is some confliction with the exist Field-Level Permission Definition which is essentially different and better not mixed together

Name string gorm:"-" // ignore this field when write and read with struct

I think it would be good to group all field permission related things into similar tags, so users can get them in one place.

Maybe we could have different permission level like the ->, <- one, - means disable read/write by default, -:all means disable read/write & migration, -:migration means only disable the migration?

@yrong
Copy link
Contributor Author

yrong commented Feb 9, 2021

@jinzhu
some refactor per your request, pls check if anything missing

@jinzhu jinzhu merged commit 2ba612e into go-gorm:master Feb 9, 2021
@jinzhu
Copy link
Member

jinzhu commented Feb 9, 2021

Thank you for your PR

@birdycn
Copy link

birdycn commented Feb 21, 2021

I want to Add field tag to ignore migration,too

What should I do now

yrong added a commit to yrong/gorm that referenced this pull request Apr 11, 2021
jinzhu pushed a commit that referenced this pull request Apr 13, 2021
jinzhu pushed a commit that referenced this pull request Apr 14, 2021
* Respect ignore migration when add column (#4276)

continue #4028

* feat: Optimal value type acquisition for v (#4278)

* feat: optimize relect value length and value

* feat: optimize ConvertSliceOfMapToValuesForCreate method

Co-authored-by: yrong1997 <[email protected]>
mittwillson pushed a commit to itering/gorm that referenced this pull request Sep 27, 2021
mittwillson pushed a commit to itering/gorm that referenced this pull request Sep 27, 2021
* Respect ignore migration when add column (go-gorm#4276)

continue go-gorm#4028

* feat: Optimal value type acquisition for v (go-gorm#4278)

* feat: optimize relect value length and value

* feat: optimize ConvertSliceOfMapToValuesForCreate method

Co-authored-by: yrong1997 <[email protected]>
cgxxv pushed a commit to cgxxv/gorm that referenced this pull request Mar 25, 2022
* Add field tag to ignore migration

* Fix null value with space

* refactor migration tag
cgxxv pushed a commit to cgxxv/gorm that referenced this pull request Mar 25, 2022
cgxxv pushed a commit to cgxxv/gorm that referenced this pull request Mar 25, 2022
* Respect ignore migration when add column (go-gorm#4276)

continue go-gorm#4028

* feat: Optimal value type acquisition for v (go-gorm#4278)

* feat: optimize relect value length and value

* feat: optimize ConvertSliceOfMapToValuesForCreate method

Co-authored-by: yrong1997 <[email protected]>
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.

3 participants