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 method to alter table column #324

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

lafriks
Copy link
Contributor

@lafriks lafriks commented Nov 1, 2022

TODO:

  • Way to detect what constraints are applied

For discussion:

  • How to detect that type have changed?
  • How to detect what constraints need to be changed?
  • Should SQLite just return error as it does not support alter column?

@Fs02
Copy link
Member

Fs02 commented Nov 2, 2022

Should SQLite just return error as it does not support alter column?

agree

@Fs02
Copy link
Member

Fs02 commented Nov 6, 2022

How to detect that type have changed?
How to detect what constraints need to be changed?

I haven't look at postgres, but after looking at mysql doc, how about we define one method for each alter type? ex:

  • AlterColumnType(...)
  • AlterColumnConstraint(...)

@lafriks
Copy link
Contributor Author

lafriks commented Nov 7, 2022

* `AlterColumnType(...)`

* `AlterColumnConstraint(...)`

This could probably work but still question is how to pass on to what constraint exactly needs to be changed? Create bitwise enum?

@lafriks
Copy link
Contributor Author

lafriks commented Nov 7, 2022

One option to create something like:

type AlterColumnOption int

const (
  AlterColumnType AlterColumnOption = 1 << iota
  AlterColumnDefault
  AlterColumnNull
  ...
)

@Fs02
Copy link
Member

Fs02 commented Nov 8, 2022

This could probably work but still question is how to pass on to what constraint exactly needs to be changed? Create bitwise enum?

oh, do you mean to pass it to adapter?

@lafriks
Copy link
Contributor Author

lafriks commented Nov 8, 2022

yes so that it will know what SQL to generate as currently adapter will receive not constraint options but already filled constraint struct rel.Column that has constraints as fields but for alter it's unknown what fields need to be changed so adding such bitwise enum to rel.Column type as field would allow to specify that

@lafriks lafriks requested a review from Fs02 November 8, 2022 15:10
@codecov
Copy link

codecov bot commented Nov 8, 2022

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (8750e2c) 100.00% compared to head (2a1fe42) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #324   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           35        35           
  Lines         2826      2859   +33     
=========================================
+ Hits          2826      2859   +33     
Files Changed Coverage Δ
column.go 100.00% <100.00%> (ø)
query.go 100.00% <100.00%> (ø)
schema.go 100.00% <100.00%> (ø)
schema_options.go 100.00% <100.00%> (ø)
table.go 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lafriks lafriks marked this pull request as ready for review November 8, 2022 19:05
@lafriks
Copy link
Contributor Author

lafriks commented Nov 8, 2022

codeclimate test fail is not related to this code changes

@lafriks lafriks added the enhancement New feature or request label Nov 8, 2022
column.go Outdated Show resolved Hide resolved
schema_test.go Outdated Show resolved Hide resolved
column.go Show resolved Hide resolved
@lafriks
Copy link
Contributor Author

lafriks commented Nov 9, 2022

For MySQL to change not null it would still require to use AlterColumType would it need to panic if not used otherwise or just do not generate SQL as for SQLite code currently does it?

@lafriks
Copy link
Contributor Author

lafriks commented Nov 9, 2022

Suggestions implemented

@Fs02
Copy link
Member

Fs02 commented Nov 10, 2022

For MySQL to change not null it would still require to use AlterColumType would it need to panic if not used otherwise or just do not generate SQL as for SQLite code currently does it?

do you mean if other than AlterColumType is used? I think panic is better here so no surprise (if possible provide the suggestion in the error message)

edit: I guess just log is okay, since it's MySQL specific, panic will cause the same migration code not compatible with other db 🤔

Copy link
Member

@Fs02 Fs02 left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks

column.go Outdated Show resolved Hide resolved
@lafriks
Copy link
Contributor Author

lafriks commented Nov 10, 2022

for example:
For example for Postgres you can use (but can not be supported for mysql as column full definition needs to be known):

s.AlterColumn("table", "col", rel.Require(false))

that wold generate:

alter table "table" alter column "col" drop not null;

But for mysql only default can be used this way but for not null/null change you need to use:

s.AlterColumnType("table", "col", rel.String, rel.Limit(100) rel.Require(false))

that would generate:

alter table `table` modify `col` varchar(100) null;

@Fs02
Copy link
Member

Fs02 commented Nov 10, 2022

I see, hmm maybe let's go for panic now? and adds some comment on the alter function?

@lafriks
Copy link
Contributor Author

lafriks commented Nov 10, 2022

panic is not very practical and should be avoided but currently there is inconsistencies between how sqlite3 is handled (by just ignoring unsupported features) and others. Imho in the future this should be dealt with by adding error return value where it's appropriate and user should decide whether to ignore it or panic

@Fs02
Copy link
Member

Fs02 commented Nov 10, 2022

okay, then let's align with sqlite3

@lafriks
Copy link
Contributor Author

lafriks commented Nov 10, 2022

updated documentation about exceptions

@Fs02
Copy link
Member

Fs02 commented Nov 15, 2022

@lafriks is this ready to be merged?

@lafriks
Copy link
Contributor Author

lafriks commented Nov 15, 2022

I think yes but that depends on how you want me to proceed. Should this be merged only when all related repo changed are done? Or should this be merged and than I can create PR to other repos with correct go mod dependency update

@Fs02
Copy link
Member

Fs02 commented Nov 16, 2022

Should this be merged only when all related repo changed are done?

I prefer this, after all merged, we can update go.mod on separate PRs to point to tagged version before release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants