Skip to content

Migration fix - Remove check for field, just try to get rid of it - #4979

Closed
miggland wants to merge 6 commits into
inventree:masterfrom
miggland:migration-bug-fix-3
Closed

miggland wants to merge 6 commits into
inventree:masterfrom
miggland:migration-bug-fix-3

Conversation

@miggland

@miggland miggland commented Jun 6, 2023

Copy link
Copy Markdown
Contributor

See #4977

@miggland
miggland requested a review from SchrodingersGat as a code owner June 6, 2023 11:39
@miggland miggland mentioned this pull request Jun 6, 2023
@miggland
miggland marked this pull request as draft June 6, 2023 11:47
@SchrodingersGat

SchrodingersGat commented Jun 6, 2023 •

Copy link
Copy Markdown
Member

@miggland the current approach seems to be working ok - what's the rationale here?

Never mind, just saw your original comment

@SchrodingersGat

Copy link
Copy Markdown
Member

Looks like it doesn't work for all db types

@miggland

miggland commented Jun 6, 2023

Copy link
Copy Markdown
Contributor Author

Yes, that syntax unfortunately only works for PG. I have some more ideas, more is coming

@SchrodingersGat SchrodingersGat added bug Identifies a bug which needs to be addressed setup Relates to the InvenTree setup / installation process labels Jun 6, 2023
@SchrodingersGat SchrodingersGat added this to the 0.12.0 milestone Jun 6, 2023
@miggland

miggland commented Jun 6, 2023

Copy link
Copy Markdown
Contributor Author

Both checks together, then it works - at least with Postgres and SQLite.

@miggland
miggland marked this pull request as ready for review June 6, 2023 14:51

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@miggland is there reason for using raw SQL or can we use at least the schema_editor? Pure Mysql can not to "IF EXISTS"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

schema_editor looks appropriate, I was not aware of this, should have done that to start with

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did not try it, and kept it from @SchrodingersGat original implementation.

However, since Django seems to not realise that the field is there (the reason #4977 doesn't quite work), I suspected using RemoveField wouldn't work either.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems I suspected correctly - since the fields do not show up in the list, RemoveField does not do anything - just like checking for the field in the list.

@matmair

matmair commented Jun 7, 2023

Copy link
Copy Markdown
Member

The suggested changes fix the myqsl migrations on mysql and mariadb; on-prem and on github (docker running on ubuntu 20.04)

@SchrodingersGat

Copy link
Copy Markdown
Member

Another option:

The original PR which broke this renamed part.migrations.0112_auto_20230531_1205.py to part.migrations.0113_auto_20230531_1205.py

So, for anyone who ran 0112_auto_20230531_1205, there should be a log in their migration history.

Anyone who did not run this (now deleted) migration is fine.

So, can we check if there is an entry in the migration log for this file?

@matmair

matmair commented Jun 7, 2023

Copy link
Copy Markdown
Member

@SchrodingersGat that should also be possible

@miggland
miggland marked this pull request as draft June 7, 2023 07:59
@miggland

miggland commented Jun 7, 2023 •

Copy link
Copy Markdown
Contributor Author

The suggested changes fix the myqsl migrations on mysql and mariadb; on-prem and on github (docker running on ubuntu 20.04)

Did you actually test this out, by going from a state before the botched migration to this? When I do that (see #4977 for details) I get this:

Checking for column 'choices' in table 'part_partparametertemplate'
Traceback (most recent call last):
  File "/home/inventree/data/env/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/inventree/data/env/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.9/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/usr/local/lib/python3.9/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1060, "Duplicate column name 'checkbox'")

@SchrodingersGat

Copy link
Copy Markdown
Member

Interestingly the duplicate migration doesn't even error out in sqlite:

image

@miggland

miggland commented Jun 7, 2023

Copy link
Copy Markdown
Contributor Author

I've added another suggestion, which should work for MySQL even on an existing DB.

@matmair Could you double-check that this doesn't break the test in mysql?

@wolflu05

wolflu05 commented Jun 7, 2023 •

Copy link
Copy Markdown
Member

Why do we want to put such a potentially unsafe operation in a migration that runs on every fresh install? I would propose to not include such doctoring in the migration chain. Instead I would consider starting a doctor management command that can be used to fix the database whenever such an migration issue occur. E.g. gitea has such a doctor command.

@SchrodingersGat

Copy link
Copy Markdown
Member

@wolflu05 I agree it is far from ideal but we have to make it seamless for the user.

@SchrodingersGat

Copy link
Copy Markdown
Member

@miggland your latest patch works for me on postgresql. I can successfully:

  • Start with a fresh db from before your original commit (with the original migration applied)
  • Migrate forwards (fields get deleted first)
  • Migrate backwards
  • Migrate forwards again (fields get skipped)

@miggland

miggland commented Jun 7, 2023

Copy link
Copy Markdown
Contributor Author

@miggland your latest patch works for me on postgresql. I can successfully:

* Start with a fresh db from _before_ your original commit (with the original migration applied)

* Migrate forwards (fields get deleted first)

* Migrate backwards

* Migrate forwards again (fields get skipped)

Awesome. Had not tried migrating backwards, but I did try it on postgres, sqlite and mysql in docker, going from before to after the broken migration.

@SchrodingersGat

Copy link
Copy Markdown
Member

@miggland @matmair FYI I have an alternative approach which does not require any custom SQL

@SchrodingersGat SchrodingersGat added migration Data or schema migrations and removed setup Relates to the InvenTree setup / installation process labels Jun 7, 2023
@SchrodingersGat

Copy link
Copy Markdown
Member

@miggland other PR is merged, feel free to close this out now

@miggland miggland closed this Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Identifies a bug which needs to be addressed migration Data or schema migrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants