Skip to content

Clean up sizes/sizegroup: deduplicate sizes + cross-reference table#821

Open
Copilot wants to merge 19 commits into
masterfrom
copilot/clean-up-sizes-and-sizegroups
Open

Clean up sizes/sizegroup: deduplicate sizes + cross-reference table#821
Copilot wants to merge 19 commits into
masterfrom
copilot/clean-up-sizes-and-sizegroups

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 21, 2026

Implements the size/sizegroup database cleanup.

Migration 1 – 20260421120000_CreateSizesSizegroupTable

  1. Creates the sizes_sizegroup cross-reference table (many-to-many between sizes and sizegroup).
  2. Populates it from sizes.sizegroup_id for every size currently in the table — including the 75 duplicates that Migration 2 will later remove (so no "extra memberships" block is needed).
  3. Drops the now-redundant sizegroup_id and seq columns from sizes.

The down() method restores sizegroup_id/seq, re-populates them from sizes_sizegroup, and drops the cross-reference table.

Migration 2 – 20260421130000_DeduplicateSizes

Removes 75 duplicate size rows from the sizes table (keeping the entry with the smallest ID for each label). Before deleting the duplicates, all foreign-key references are redirected to the surviving ID:

Table Column(s) updated
stock size_id
shipment_detail source_size_id, target_size_id
history from_int, to_int (for tablename='stock', changes='size_id')
itemsout size_id
distro_events_packing_list_entries size_id
distro_events_tracking_logs size_id
distro_events_unboxed_item_collections size_id

sizes_sizegroup rows for duplicate sizes are then remapped to their canonical ID via INSERT IGNORE (so canonical sizes inherit all sizegroup memberships from their duplicates). The ON DELETE CASCADE on fk_ss_size_id removes the old duplicate rows when the duplicate sizes rows are deleted.

The down() method re-inserts the 75 deleted sizes rows (id + label only, since sizegroup_id/seq are managed by Migration 1) and their corresponding sizes_sizegroup entries, then removes the inherited sizegroup memberships from the canonical sizes (partial rollback; FK remappings cannot be reconstructed).

Migration 3 – 20260421140000_DropAuditColumnsFromSizes

Removes the unused audit columns (created, created_by, modified, modified_by) from the sizes table. These columns are all NULL in every known environment and are not referenced by any application code. FK constraints and indexes on created_by / modified_by are dropped first.

The down() method restores the columns, indexes, and FK constraints referencing cms_users.

Migration 4 – 20260421150000_FixSizesData

Cleans up data inconsistencies in the sizes_sizegroup table:

  1. Sizegroup 1 (XS, S, M, L, XL, XXL): corrects seq values to XS=1, S=2, M=3, L=4, XL=5, XXL=6, Mixed=999.
  2. Sizegroup 23 (Children by year 2-3 to 14-15): corrects seq values to 2-3y=200, 4-5y=201, 6-7y=202, 8-9y=203, 10-11y=204, 12-13y=205, 14-15y=206, All ages=207, Mixed=999.
  3. Sizegroups 17, 18, 22, 25: adds the "All ages" size (id=97) to each sizegroup.

The down() method removes the added "All ages" entries and restores the original seq values.

Code changes

File Change
include/container-stock.php Replaced implicit cross join with explicit INNER JOIN sizes_sizegroup … INNER JOIN sizes
library/ajax/getsizes.php Updated size-lookup query to JOIN via sizes_sizegroup
include/stock_edit.php Updated size-dropdown query to JOIN via sizes_sizegroup

Verification

  • Migrations run cleanly on the dev database
  • vendor/bin/parallel-lint – no errors
  • php-cs-fixer – 0 files to fix (excluding auto-generated templates)
  • No duplicate size labels remain
  • All sizegroup memberships preserved: size labels per sizegroup are identical before and after Migration 2
  • After all four migrations, the sizes table contains only id and label
  • Sizegroups 1 and 23 have correct sequential seq values; sizegroups 17, 18, 22, and 25 include "All ages"

Copilot AI and others added 2 commits April 21, 2026 13:34
Migration 1 (20260421120000): removes 75 duplicate size rows, updating
references in stock, shipment_detail, and history tables.

Migration 2 (20260421130000): creates sizes_sizegroup cross-reference
table with all size-sizegroup relationships (including extra sizegroup
memberships from merged duplicates), then drops sizes.sizegroup_id and
sizes.seq.

Updates include/container-stock.php to JOIN sizes_sizegroup instead of
filtering on sizes.sizegroup_id.

Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/870cef39-acd9-49fe-8c16-4dfd54f3b425

Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@pylipp pylipp left a comment

Choose a reason for hiding this comment

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

@copilot revert any (formatting) changes in files that are NOT in db/migrations and NOT container-stock.php

@pylipp
Copy link
Copy Markdown
Contributor

pylipp commented Apr 21, 2026

…ntainer-stock.php

Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/363f3d93-798a-48aa-b118-9a9a41ecb889

Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 21, 2026

@copilot revert any (formatting) changes in files that are NOT in db/migrations and NOT container-stock.php

Done in b060b78 — reverted formatting changes in all files outside db/migrations/ and include/container-stock.php.

Copilot AI requested a review from pylipp April 21, 2026 14:00
@pylipp pylipp marked this pull request as ready for review April 21, 2026 14:01
@pylipp pylipp requested a review from Copilot April 21, 2026 14:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the app’s size/sizegroup data model by removing duplicate sizes rows and moving from a 1→many (sizes.sizegroup_id) relationship to a many→many relationship via a new sizes_sizegroup cross-reference table, updating stockroom querying accordingly.

Changes:

  • Add a migration to deduplicate sizes labels by remapping foreign keys and deleting duplicate rows.
  • Add a migration to create/populate sizes_sizegroup, then drop sizes.sizegroup_id and sizes.seq.
  • Update the stockroom listing query to join sizes via sizes_sizegroup instead of sizes.sizegroup_id.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
include/container-stock.php Switches sizegroup matching logic to use the new sizes_sizegroup join.
db/migrations/20260421120000_deduplicate_sizes.php Remaps references to removed size IDs and deletes duplicate sizes rows.
db/migrations/20260421130000_create_sizes_sizegroup_table.php Introduces sizes_sizegroup, migrates memberships, and drops legacy sizes columns.

Comment thread include/container-stock.php Outdated
Comment thread db/migrations/20260421130000_create_sizes_sizegroup_table.php Outdated
Comment thread db/migrations/20260421130000_deduplicate_sizes.php
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Philipp Metzner <10617122+pylipp@users.noreply.github.com>
@pylipp
Copy link
Copy Markdown
Contributor

pylipp commented Apr 21, 2026

@copilot apply changes based on the comments in this thread

Copilot AI and others added 2 commits April 21, 2026 16:14
…getsizes.php, stock_edit.php, sizes_edit.php to use sizes_sizegroup

Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/ce308e1f-588a-4185-aa41-13ca1c14601d

Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 21, 2026

@copilot apply changes based on the comments in this thread

Done in 18aab4f. Applied all three review changes:

  1. Migration 1 (deduplicate_sizes): Added remapping of itemsout and all distro_events_* tables that have a size_id column (via INFORMATION_SCHEMA) before the DELETE, preventing FK RESTRICT violations.

  2. include/sizes.php and library/ajax/getsizes.php: Updated all queries to JOIN via sizes_sizegroup and order by ss.seq instead of the removed sizes.sizegroup_id/sizes.seq columns. The re-ordering initialization in sizes.php now updates sizes_sizegroup.seq, and the list display uses GROUP_CONCAT for sizes belonging to multiple sizegroups.

  3. include/stock_edit.php and include/sizes_edit.php: Also updated for the removed columns — stock_edit.php uses a JOIN on sizes_sizegroup for the size dropdown, and sizes_edit.php has sizegroup_id removed from savekeys and the form (the column no longer exists on sizes).

Comment thread db/migrations/20260421120000_deduplicate_sizes.php Outdated
Copilot AI requested a review from pylipp April 21, 2026 16:27
Copilot AI and others added 2 commits April 23, 2026 12:11
Co-authored-by: pylipp <10617122+pylipp@users.noreply.github.com>
Copilot AI requested a review from pylipp April 23, 2026 12:14
@pylipp pylipp requested a review from HaGuesto April 23, 2026 13:34
@pylipp
Copy link
Copy Markdown
Contributor

pylipp commented Apr 23, 2026

@HaGuesto the way I tested this was:

  1. Run test queries
  2. Run first migration
  3. Run test queries again
  4. Run rollback
  5. Run test queries again
  6. Run first and second migration
  7. Run test queries again
  8. Run rollback of second migration
  9. Run test queries again

I could verify

  • after first migration, all size labels grouped by sizegroup are the same as before
  • after the first migration, the number of rows in the new sizes_sizegroup table is the same as the number of rows in the sizes table
  • after the rollback of the first migration, the data in the sizes table is identical to the original state
  • after the second migration, the number of rows in sizes_sizegroup is identical to the number of rows in sizes in the original state (except the duplicated "All ages" size in "Children by year (2-3, 4-5, 6-8, 9-12, 13-15)" (ID 4))
  • after the second migration, the number of rows in sizes is identical to the number of distinct labels in sizes (in the original state)
  • after the second migration, all size labels grouped by sizegroup are the same as before (except "Children by year (2-3, 4-5, 6-8, 9-12, 13-15)" (ID 4) which now misses the duplicated "All ages" size)
  • after the second migration, none of the stock/history/shipment_detail/sizes_sizegroup tables contain any of the duplicated size IDs
  • after rolling back the second migration, the number of rows in the sizes table is the same as in the original state; same for sizes_sizegroup

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Comment thread db/migrations/20260421120000_create_sizes_sizegroup_table.php Outdated
Comment thread db/migrations/20260421120000_create_sizes_sizegroup_table.php Outdated
Comment thread library/ajax/getsizes.php
Comment thread db/migrations/20260421130000_deduplicate_sizes.php
pylipp and others added 2 commits April 23, 2026 16:40
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Philipp Metzner <10617122+pylipp@users.noreply.github.com>
Comment thread db/migrations/20260421130000_deduplicate_sizes.php
Copy link
Copy Markdown
Member

@HaGuesto HaGuesto left a comment

Choose a reason for hiding this comment

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

functional review

Run test queries

  • I queried the 10 last rows from history, stock and shipment_detail and exported them.
  • I exported the sizes and sizegroup table

##Run first migration

  • went through
  • as expected the migration did not change the data in my exports
  • I also exported the sizegroup_sizes table.
  • I found some seq errors and duplication of "All_ages" and "Mixed". Will check again when 2nd migration is applied
    CHANGE REQUEST: remove created, created_by, modified, modified_by columns from sizes table.

will continue review in next comment

@HaGuesto
Copy link
Copy Markdown
Member

@copilot could you add an migration at the end removing the following columns from the sizes table since they are only filled with NULL: created, created_by, modified, modified_by.

…ied, modified_by) from sizes table

Agent-Logs-Url: https://github.com/boxwise/dropapp/sessions/5a267922-fb0f-4a79-9c7d-04cc5362d322

Co-authored-by: HaGuesto <29065125+HaGuesto@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 13, 2026

@copilot could you add an migration at the end removing the following columns from the sizes table since they are only filled with NULL: created, created_by, modified, modified_by.

Done in 37af64e — added migration 20260421140000_DropAuditColumnsFromSizes that drops FK constraints (sizes_ibfk_5, sizes_ibfk_6) and removes created, created_by, modified, modified_by from sizes. After all three migrations run, the table contains only id and label. down() restores the columns, indexes, and FK constraints.

Copilot AI requested a review from HaGuesto May 13, 2026 09:14
Copy link
Copy Markdown
Member

@HaGuesto HaGuesto left a comment

Choose a reason for hiding this comment

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

functional review continued

Run rollback

  • size and sizegroup tables are the same as before

Run first and second migration

  • number of sizes reduced from 179 to 113 as expected.
  • sizes_sizegroup table lost one enty? There seemed to be a duplicate entry of All Ages in one sizegroup.
  • history snapshot of 10 changes was correct
  • stock snapshot of 10 changes was correct
  • shipment_detail snapshot of 10 changes was correct
    @pylipp I found a shipment detail with source_size_id = NULL. does that make sense?

Run rollback of second migration

works

@boxwise boxwise deleted a comment from Copilot AI May 13, 2026
SET from_int = {$fromCaseExpr}
WHERE tablename = 'stock' AND changes = 'size_id'
AND from_int IN ({$ids})");

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.

@pylipp add here a message how many row items should have changed vs have actually changed based on production.

@HaGuesto
Copy link
Copy Markdown
Member

ACTION: @HaGuesto to functional test query updates.
ACTION: @HaGuesto to test if removed "All ages" size was removed correctly.
ACTION: @HaGuesto to send @pylipp table of seq changes and 'All ages' sizes to add
ACTION: @pylipp to ask @aerinsol regarding removing "mixed" size option from "one size"

@HaGuesto
Copy link
Copy Markdown
Member

HaGuesto commented May 13, 2026

ACTION: @HaGuesto to functional test query updates.

DONE. passed

ACTION: @HaGuesto to test if removed "All ages" size was removed correctly.

Done. Yes.

ACTION: @HaGuesto to send @pylipp table of seq changes and 'All ages' sizes to add

Done

@pylipp
Copy link
Copy Markdown
Contributor

pylipp commented May 13, 2026

@copilot I need another migration to clean-up inconsistencies in the sizes data. It should run fourth.

  • update seq for sizes of sizegroup 1. It should be 1-6 for XS, S, ..., XXL and 999 for Mixed
  • update seq for sizes of sizegroup 23. It should be 200-206 for 2-3y, 4-5y, ..., 14-15y, 207 for All ages and 999 for Mixed
  • add "All ages" size for sizegroups 17, 18, 22, 25
  • implement the up() and the corresponding down migration

Copilot AI and others added 2 commits May 13, 2026 16:45
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 13, 2026

@copilot I need another migration to clean-up inconsistencies in the sizes data. It should run fourth.

  • update seq for sizes of sizegroup 1. It should be 1-6 for XS, S, ..., XXL an...

Done in 04d0dfc — added migration 20260421150000_FixSizesData:

  • Sizegroup 1: seq corrected to XS=1, S=2, M=3, L=4, XL=5, XXL=6, Mixed=999
  • Sizegroup 23: seq corrected to 2-3y=200, 4-5y=201, 6-7y=202, 8-9y=203, 10-11y=204, 12-13y=205, 14-15y=206, All ages=207, Mixed=999
  • Sizegroups 17, 18, 22, 25: "All ages" (id=97) added with seq 13, 22, 182, 246 respectively

down() removes the added "All ages" entries and restores the original seq values. Both up() and down() verified on the dev database.

Copilot finished work on behalf of pylipp May 13, 2026 16:49
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.

4 participants