Skip to content

fix: Normalize PKs to not null#13557

Merged
kodiakhq[bot] merged 2 commits intocloudquery:mainfrom
erezrokah:fix/set_pks_not_null
Sep 1, 2023
Merged

fix: Normalize PKs to not null#13557
kodiakhq[bot] merged 2 commits intocloudquery:mainfrom
erezrokah:fix/set_pks_not_null

Conversation

@erezrokah
Copy link
Copy Markdown
Member

@erezrokah erezrokah commented Aug 31, 2023

Summary

Saw this while working on #13486. If you run migration twice on the same spec it fails. The reason is that MySQL Implicitly sets PKs to non null in the DB, so we have to account for that.

We had this in the code and it seems it got lost in the migration to v3 or v4.
I also cleaned up the tests as those are passing without that loop on test options. Tests cleanup moved to #13565

Not sure why the double migration tests didn't catch this

@erezrokah erezrokah requested review from a team and candiduslynx and removed request for a team August 31, 2023 15:57
@erezrokah erezrokah added the automerge Automatically merge once required checks pass label Aug 31, 2023
@cq-bot cq-bot added the mysql label Aug 31, 2023
@erezrokah erezrokah requested review from a team and bbernays August 31, 2023 15:57
@erezrokah
Copy link
Copy Markdown
Member Author

Ah, for some reason those tests are passing locally for me

@erezrokah
Copy link
Copy Markdown
Member Author

Tests fixed in a429640

@cq-bot cq-bot added the ci label Aug 31, 2023
plugin.WriterTestSuiteTests{
SafeMigrations: plugin.SafeMigrations{
AddColumn: true,
AddColumnNotNull: false,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it just needs the true options, the false is the default.

func (c *Client) normalizeTable(table *schema.Table) *schema.Table {
columns := make([]schema.Column, len(table.Columns))
for i, col := range table.Columns {
if col.PrimaryKey {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe just col.NotNull = col.NotNull || col.PrimaryKey ?

@erezrokah erezrokah requested a review from candiduslynx August 31, 2023 16:24
columns := make([]schema.Column, len(table.Columns))
for i, col := range table.Columns {
// In MySQL primary keys are implicitly not null
col.NotNull = col.NotNull || col.PrimaryKey
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm a bit worried that we modify column here instead of working on the copy.
Could we move it to

normalizedCol := schema.NewColumnFromArrowField(*normalized)
normalizedCol.NotNull = normalizedCol.NotNull || normalizedCol.PrimaryKey
columns[i] = normalizedCol

or something similar?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Cleaned up the PR. Also thought about removing the conversion to arrow and back to column, but probably safer to keep it so we remove any non relevant column fields.

Also moved the tests change to #13565

@erezrokah erezrokah force-pushed the fix/set_pks_not_null branch from 8bf0482 to a43b486 Compare September 1, 2023 07:43
@cq-bot cq-bot removed the ci label Sep 1, 2023
Name: field.Name,
Type: normalizedType,
Nullable: field.Nullable,
Nullable: !notNull,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
Nullable: !notNull,
Nullable: !(col.NotNull || col.PrimaryKey), // In MySQL primary keys are implicitly not null

Comment on lines +33 to +34
// In MySQL primary keys are implicitly not null
notNull := col.NotNull || col.PrimaryKey
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// In MySQL primary keys are implicitly not null
notNull := col.NotNull || col.PrimaryKey

@kodiakhq kodiakhq bot merged commit 2230538 into cloudquery:main Sep 1, 2023
kodiakhq bot pushed a commit that referenced this pull request Sep 1, 2023
🤖 I have created a release *beep* *boop*
---


## [3.0.5](plugins-destination-mysql-v3.0.4...plugins-destination-mysql-v3.0.5) (2023-09-01)


### Bug Fixes

* **deps:** Update module github.com/cloudquery/plugin-sdk/v4 to v4.6.0 ([#13492](#13492)) ([c305876](c305876))
* Normalize PKs to not null ([#13557](#13557)) ([2230538](2230538))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Automatically merge once required checks pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants