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

Order attributes #170

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

Order attributes #170

wants to merge 4 commits into from

Conversation

bohdan-vorona
Copy link
Contributor

#165

Listing:

attrs-listing

View:

attrs-view

Add/Update:

attrs-add-update

API:

attrs-api

@bohdan-vorona bohdan-vorona added the enhancement New feature or request label Mar 15, 2023
@bohdan-vorona bohdan-vorona self-assigned this Mar 15, 2023
@bohdan-vorona bohdan-vorona linked an issue Mar 15, 2023 that may be closed by this pull request
@@ -343,4 +357,18 @@ public function afterSave($insert, $changedAttributes)
}
}
}

protected function executeAfterFind(): void
Copy link
Collaborator

Choose a reason for hiding this comment

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

{
$this->addColumn('{{%orders}}', 'order_attributes',
$this
->text()
Copy link
Collaborator

Choose a reason for hiding this comment

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

@bohdan-vorona
Copy link
Contributor Author

@samdark

  • I converted the order_attributes field to JSON format.
  • Also, I added the JsonAttributeBehavior - it would be useful for future models with JSON field(s) for data like tags/attributes.

I needed the method executeAfterFind since I have the field order_attributes_array I use for quick accessing the attributes in array format like this:

return implode(', ', array_map(function ($attr) {
                                return Html::a(Html::encode($attr), Url::to(['index', 'OrderSearch[order_attributes]' => Html::encode($attr)]));
                            }, $model->order_attributes_array));

But anyway I moved it to the JsonAttributeBehavior and changed the name.

@bohdan-vorona bohdan-vorona requested a review from samdark March 16, 2023 12:37
@cgsmith
Copy link
Contributor

cgsmith commented Mar 22, 2023

Working on checking this but my local machine is running slow. Trying to reduce the dataset.

@samdark
Copy link
Collaborator

samdark commented Mar 22, 2023

Won't the real dataset be even larger?

@cgsmith
Copy link
Contributor

cgsmith commented Mar 22, 2023

Yes. And I'm not sure why it is running slow. I'm going to copy production to another database on Digital Oceans server and run the migration against it.

@bohdan-vorona
Copy link
Contributor Author

@cgsmith

Working on checking this but my local machine is running slow. Trying to reduce the dataset.

After adding the field?

@cgsmith
Copy link
Contributor

cgsmith commented Mar 22, 2023

Yes. And I'm not sure why it is running slow. I'm going to copy production to another database on Digital Oceans server and run the migration against it.

It just sits there while trying to add the field. I tried from the mysql cli too.

@samdark
Copy link
Collaborator

samdark commented Mar 23, 2023

A good idea is to give @bohdan-vorona a DB dump so he reproduce/fix it.

@cgsmith
Copy link
Contributor

cgsmith commented Mar 23, 2023

Good idea. I'll get a sql dump today for you @bohdan-vorona

@bohdan-vorona
Copy link
Contributor Author

@cgsmith @samdark It turned out a pretty tricky thing...

On my local machine it took almost 300 seconds (5 minutes) to execute the query:

ALTER TABLE orders ADD order_attributes JSON NULL DEFAULT NULL AFTER notes;


To be short, it will take lots of time to add a new field to the table orders due to lots of records. I tried all the variants I've found on the internet like setting a different mode, locking/unlocking, creating a tmp table and then moving data between tables, etc. I even tried to remove FULLTEXT due to restrictions for one of the modes presented here - https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html

The "faster" way: (except the direct query execution)

  1. Remove FULLTEXT.
  2. Add the field using one of the existing algorithms (the link above). Time: ~4 mins. Only 1 minute difference.
  3. Return back FULLTEXT. Time: ~4.5 mins.

Total time approximately: ~9-10 minutes (on my local machine).


So:

Option A. Just apply the migration as is. Probably for this purpose, it would be better to have a column, not a separate table. In addition, as a separate task, I'd propose to:

  • create schema.sql file with only the DB structure
  • remove all existing migrations
  • create something like test_data.sql with test data

Option B. Create a new table - order_attribute(s).

Option C. Create a new table but with keeping in mind then potentially we can extend the table with other columns in future. Let's name it order_extra or order_param(s) with the first field attributes needed for this task.

BTW it takes lots of time also removing the column 😁

@samdark
Copy link
Collaborator

samdark commented Mar 24, 2023

That sounds alright. @cgsmith let's apply that during "maintenance window"?

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.

Order attributes [FEATURE]
3 participants