Skip to content

Add Metadata to more models - #4898

Merged
SchrodingersGat merged 34 commits into
inventree:masterfrom
miggland:metadata3
Jun 2, 2023
Merged

SchrodingersGat merged 34 commits into
inventree:masterfrom
miggland:metadata3

Conversation

@miggland

@miggland miggland commented May 25, 2023 •

Copy link
Copy Markdown
Contributor

Closes #4202

Adds metadata to:

  • Part test templates
  • Part parameters
  • Part category parameter templates
  • BOM item substitute
  • Part relateds
  • Stock item test result
  • Return order
  • Return order lines
  • Return order extra lines
  • Company contacts

Contains one bug fix

Todo:

  • Tests

@miggland miggland self-assigned this May 25, 2023
@miggland miggland added this to the 0.11.3 milestone May 25, 2023
@miggland miggland added bug Identifies a bug which needs to be addressed enhancement This is an suggested enhancement or new feature order Related to purchase orders / sales orders part Related to Part models stock Related to Stock models api Relates to the API labels May 25, 2023
@wolflu05

Copy link
Copy Markdown
Member

I'm not sure if the 0.11.3 milestone is correct here as this is getting merged in master? Also it probably makes sense to not backport this change as this is no bugfix.

@miggland miggland removed this from the 0.11.3 milestone May 25, 2023
@miggland

Copy link
Copy Markdown
Contributor Author

There is a small bugfix in there - and the rest is not really new, just an extension of existing functionality to more models

@miggland

miggland commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

Update: this test runs without problems if I run only the Class on it's own..

@miggland

miggland commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

An explanation:
The failing test is in the class ContactTest, and used a fixed PK of 1 in test_edit and 6 in test_delete for testing the edit/delete functions.
However, these PKs were not defined through fixtures but from creating the contacts through the API, without a defined PK.
To be able to test metadata, each model needs an object in the database. Thus, I added a fixture used in class CompanyMetadataAPITest, in which the contact has PK=1.
Since ContactTest was run after CompanyMetadataAPITest, but without fixtures, there was no contact with PK=1, but with auto-incremented, higher values. Thus a 404 error.

I have changed the failing tests to get the first/last contact item from the database, then use that PK in the API tests. Now it works (at least on my dev machine).
I don't quite understand why the PK is incremented even when the data is not present, but maybe the database is not truncated properly after each test class, just all rows dropped?

@miggland

miggland commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

Could you cherry-pick the bugfix to a separate branch and add a PR against 0.11.x? I think it might be a while till the next release based on the current milestone set.

OK, see: #4947

Assuming the test passes, this is ready for review/merge.

@miggland
miggland marked this pull request as ready for review June 1, 2023 17:20
@miggland
miggland requested a review from SchrodingersGat as a code owner June 1, 2023 17:20
@matmair

matmair commented Jun 1, 2023

Copy link
Copy Markdown
Member

@miggland there was a new migration upstream

@SchrodingersGat

Copy link
Copy Markdown
Member

@miggland thanks for the additions :)

@SchrodingersGat
SchrodingersGat merged commit 1d85b70 into inventree:master Jun 2, 2023
@SchrodingersGat SchrodingersGat added this to the 0.12.0 milestone Jun 2, 2023
@SchrodingersGat

Copy link
Copy Markdown
Member

@miggland @matmair I think this PR has broken database migrations for live data (demo instance is down currently). Will have to look into this ASAP

@SchrodingersGat

Copy link
Copy Markdown
Member

image

@SchrodingersGat

Copy link
Copy Markdown
Member

image

@SchrodingersGat

Copy link
Copy Markdown
Member

@miggland OK I can see what happened here:

  • You renamed part migration 0012 to 0013
  • You inserted your new migration as "0012"

In systems where old 0012 had already been run, the new fields had been applied to the partparameter model.

Basically, if you have to mess around with the order of migrations, you should never change one which has already been run! I should have checked this more carefully before merging in. The CI checks passed because they operate on a clean database, and the tables here had not already been created.

I'll make a PR to address this.

SchrodingersGat added a commit to SchrodingersGat/InvenTree that referenced this pull request Jun 3, 2023
- Order of migrations had been changed
- Resulted in conflicting database state
- Never ever do this!
- Ref: inventree#4898
@SchrodingersGat

Copy link
Copy Markdown
Member

Bottom line, we should never adjust the ordering of previously applied migrations - any new migrations should be applied after old ones.

If there are ever conflicts of migrations this needs to be carefully checked (I dropped the ball on this one!)

@matmair

matmair commented Jun 3, 2023

Copy link
Copy Markdown
Member

I also didn't notice. Is this something we could check for in CI?

@miggland

miggland commented Jun 3, 2023

Copy link
Copy Markdown
Contributor Author

Sorry about that. I have no problem with your explanation, and that wasn't what I intended to do either, but I must have messed up and moved the wrong one. Classical case of wanting to get things finished and not checking properly.

Being more aware of the problem helps though - I'll be more careful in future.

@SchrodingersGat

Copy link
Copy Markdown
Member

@matmair unsure. The issue is subtle:

  1. Previous migration file 0012 added new fields to the PartParameter model
  2. Database migrations were updated, such that 0012 was applied
  3. Previous migration file 0012 was renamed to 0013
  4. New migration file inserted as 0012 which added new metadata fields
  5. Database migrations run again (i.e. python manage.py migrate)
  6. 0012 is recorded as already having been run (and is not run again) - thus metadata fields are not added
  7. 0013 is a new migration, and so DB tries to apply it. However it is actually the old 0012 so causes a "field already exists" error
  8. At this point we cannot roll back either, because we can't apply 0013 to then un-apply it

To test this in CI, maybe we maintain an active database, separate from the repo (or in a separate branch), and the data migrations have to successfully apply to that database, in addition to an empty database (which is the current check).

  1. New PR is created
  2. As part of CI, old database is fetched which is running on top of current master
  3. If database migrations apply, all is OK
  4. Once PR is merged, triggers a workflow to update the database with the new migrations

This is probably the closest we can replicate an "active" database in CI

SchrodingersGat added a commit that referenced this pull request Jun 3, 2023
- Order of migrations had been changed
- Resulted in conflicting database state
- Never ever do this!
- Ref: #4898
This was referenced Jun 6, 2023
@miggland
miggland deleted the metadata3 branch January 12, 2024 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Relates to the API bug Identifies a bug which needs to be addressed enhancement This is an suggested enhancement or new feature order Related to purchase orders / sales orders part Related to Part models stock Related to Stock models

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FR] All Models should have Metadata attached

4 participants