Skip to content

Conversation

@allentje
Copy link

@allentje allentje commented Oct 29, 2019

What does this PR do?

This PR adds timestamps to the question e.g created_at for when a new question is asked, answered_at for when the question is answered and an updated_at every time the question is updated.
This is intended to solve the issues #225

Some checks have been made simpler but in general I have kept everything the same only now it uses a model instead of static queries.

Where should the reviewer start?

The reviewer has to go to the Frag den Himmel page and create a question.
The reviewer should also go to the admin -> Fragen beantworten page to see the list of questions ready to be answered.

How should this be manually tested?

  • run DB migrations
  • go to the Frag den Himmel page and create a question
  • Is the created_at date added?
  • go to the admin -> Fragen beantworten page
  • Is the created_at date visible under the section Unbeantwortete Fragen?
  • Answer one of the newly created questions.
  • Is the question now answered and shows a created_at and answered_at column visible under the section Beantwortete Fragen?

Documents

What should happen on deployment? (check all that apply)

  • The migrations should be run

@allentje allentje changed the title Timestamp questions #225 Timestamp questions Oct 29, 2019
Copy link
Member

@MyIgel MyIgel left a comment

Choose a reason for hiding this comment

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

Hi, thank you for your PR! 🎉

Can you please rebase it on the current master? That makes reviews much easier (esp. because the master has an improved docker setup).

Another thing is that it definitely breaks unit tests as the majority of them runs on a in memory sqlite database which has no "legacy" tables like Questions. To prevent these errors please have a look at the other migrations :)

vendor/bin/phpunit -vvv --testsuite Unit --coverage-text --coverage-html /tmp/coverage/
PHPUnit 8.4.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.11-1+0~20191026.48+debian9~1.gbpf71ca0 with Xdebug 2.7.2
Configuration:~/code/engelsystem/phpunit.xml

.....................E..........EEEEEEEEE.E.EEEEE..............  63 / 313 ( 20%)
......................EEE...................................... 126 / 313 ( 40%)
........................................................EEEE... 189 / 313 ( 60%)
........................E...................................... 252 / 313 ( 80%)
.................EEEEEEEEE...................................   313 / 313 (100%)

Time: 19.69 seconds, Memory: 46.00 MB

There were 33 errors:
[...]

@MyIgel MyIgel added Status: In Progress Fixing this issue is in progress. Type: Refactor Make the code better. labels Oct 30, 2019
@allentje
Copy link
Author

Thanks for the feedback. I will update the branch.
It is a little unclear to me what I exactly have to do with the DB but I think i will be able to figure it out.
I have tried to run the unit tests first on the master branch so I have a good base to compare against but I currently have tests failing on me.

master branch fails on 4 test classes with the error 'Unable to connect to database':

  • DatabaseService
  • EngelsystemLoggerTest
  • LogEntryTest
  • RoomModelTest

and the LegacyDevelopmentTest throws a bunch of errors and then exhausts my memory.

I am running the test through docker, do you have similar issues?

@MyIgel
Copy link
Member

MyIgel commented Oct 30, 2019

If you run vendor/bin/phpunit --testsuite Unit as stated in the readme no mysql/mariadb database is needed as all tests in the tests/Unit folder only run on a in memory database.

After rebasing your branch on the current master and running the dev containers you will be able to just run docker exec engelsystem_dev_es_workspace_1 vendor/bin/phpunit to run all unit tests (including database tests).

@allentje
Copy link
Author

pff, was a lot of work but I think I have everything including a unit test for the questions model.
Coverage is 100% and all tests green :D

@allentje allentje requested a review from MyIgel October 31, 2019 14:20
@MyIgel MyIgel changed the title Timestamp questions Timestamp for questions Nov 2, 2019
Copy link
Member

@MyIgel MyIgel left a comment

Choose a reason for hiding this comment

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

A merge is not a rebase and i see some opportunities to squash some commits together to have a cleaner history ;)

You should really use your IDE's auto formatting feature or use a linter for that as there are some whitespace problems in the code ;)
Another benefit is that a proper IDE can use docblock hints to know which magic method is available for a class which is rally nice if working with models

Following are some comments on the code:

/** @var \stdClass[] $previousQuestionsRecords */
$previousQuestionsRecords = $this->schema->getConnection()->table('Questions')->get();
foreach ($previousQuestionsRecords as $previousQuestions) {
DB::table('questions')->insert([
Copy link
Member

Choose a reason for hiding this comment

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

This is the same as $this->schema->getConnection()->table('questions') so it should be used to make it clearer (and a $connection = $this->schema->getConnection(); to keep the two lines shorter) :)

/**
* Recreates the previous News table, copies back the data and drops the new news table.
*/
public function down(): void
Copy link
Member

Choose a reason for hiding this comment

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

It would be clearer if the up() and down() methods (and thus all public methods) would be at the beginning of the class

Copy link
Author

Choose a reason for hiding this comment

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

I can move it, I actually took the order and based the code on another PR

*/
private function createNewQuestionsTable(): void
{
$this->schema->create('questions', function (Blueprint $table) {
Copy link
Member

Choose a reason for hiding this comment

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

Having two tables with the same name is not the best idea as some dbms are case insensitive wo this migration would fail. A workaround is to rename the old table first (like [TableName]Old).

{
$this->schema->create('questions', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
Copy link
Member

Choose a reason for hiding this comment

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

Using the referencesUser from the \Engelsystem\Migrations\Reference trait would be the clearer option here and on answer_user_id as it also sets cascade options.

Copy link
Author

Choose a reason for hiding this comment

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

That's actually really handy :D

'id' => $previousQuestions->QID,
'user_id' => $previousQuestions->UID,
'answer_user_id' => $previousQuestions->AID,
'question' => $previousQuestions->Question,
Copy link
Member

Choose a reason for hiding this comment

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

Proper indentation would be nice, i highly recommend the auto formatting function of your IDE if you are using one

if (!empty($question)) {
DB::delete('DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id]);
engelsystem_log('Question deleted: ' . $question['Question']);
$questions->delete();
Copy link
Member

Choose a reason for hiding this comment

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

This looks like you want to delete all of them, just using $question->delete() is really better

Copy link
Author

Choose a reason for hiding this comment

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

I agree, as mentioned above this was due to basing the model on the old table.
I will refactor

'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
[$question_id]
);
$questions = Question::where(['id' => $question_id]);
Copy link
Member

Choose a reason for hiding this comment

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

Using Question::find($question_id) directly returns the question or null

Copy link
Author

@allentje allentje Nov 3, 2019

Choose a reason for hiding this comment

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

You are right, I had built the model based on the old table I was forced to do it like this due to the delete not working correctly due to a lack of an id column.

I will refactor this as it should now be possible to delete normally.

DB::delete('DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id]);
engelsystem_log('Question deleted: ' . $question['Question']);
$questions->delete();
engelsystem_log('Question deleted: ' . $question->Question);
Copy link
Member

@MyIgel MyIgel Nov 2, 2019

Choose a reason for hiding this comment

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

Lowercase ->question and ->text ;)

{
foreach ($open_questions as &$question) {
$question['actions'] = form([
$question->actions = form([
Copy link
Member

Choose a reason for hiding this comment

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

Updating the function docblock to contain * @param Question[] $open_questions is really helpful when using an IDE

Copy link
Author

Choose a reason for hiding this comment

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

A merge is not a rebase and i see some opportunities to squash some commits together to have a cleaner history ;)

I am fairly new to rebase as I usually work with branches at work.
My apologies for this.

You should really use your IDE's auto formatting feature or use a linter for that as there are some whitespace problems in the code ;)

Yeah, I'm using phpstorm but I didn't set it up for the engelsystem. My bad, I will go over the changes again with a configured phpstorm.

Another benefit is that a proper IDE can use docblock hints to know which magic method is available for a class which is rally nice if working with models

I use phpstorm but I am strongly against the use of magic methods. I will implement them though to keep the project consistent and to reduce some complexity :)

Following are some comments on the code:

Thank you for taking the time to review, I will try to have the changes done by today

Copy link
Member

Choose a reason for hiding this comment

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

PHPStorm has a nice auto formatter, setup should only be selecting PSR-2 (or 12).

Well as magic methods make it much more clearer they should definitely be preferred ;)

'SELECT * FROM `Questions` WHERE NOT `AID` IS NULL AND `UID`=?',
[$user->id]
);
$open_questions = Question::where('answer_user_id', null)->where('user_id', $user->id)->get();
Copy link
Member

Choose a reason for hiding this comment

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

Same here: Question::whereUserId($user->id)->where('user_id', $user->id)->get()

@MyIgel
Copy link
Member

MyIgel commented Apr 7, 2020

Hi, any updates on this PR?

@allentje
Copy link
Author

allentje commented Apr 8, 2020

Hi, any updates on this PR?

No sorry. I had reinstalled my computer a few weeks ago and will need to get the system back up and running again and tbh.
I have the project backed up so it shouldn't be too much of an issue.

I'll take a look at it on Friday if that's ok with you?

@MyIgel
Copy link
Member

MyIgel commented Apr 8, 2020

Its totally ok, just wanted to know if you want to do something with it someday ;)

@MyIgel
Copy link
Member

MyIgel commented Sep 13, 2020

@allentje any updates on this PR?

@MyIgel MyIgel force-pushed the timestamp-questions branch from f6acd70 to 8e5a67c Compare September 27, 2020 17:08
@MyIgel MyIgel merged commit 6c177d5 into engelsystem:master Sep 27, 2020
@MyIgel MyIgel added Type: Feature An idea for a new feature and removed Status: In Progress Fixing this issue is in progress. labels Sep 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Feature An idea for a new feature Type: Refactor Make the code better.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants