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

feat: add automatic timestamps for various tables #3435

Merged
merged 6 commits into from
Jun 7, 2022
Merged

feat: add automatic timestamps for various tables #3435

merged 6 commits into from
Jun 7, 2022

Conversation

iPurpl3x
Copy link
Contributor

@iPurpl3x iPurpl3x commented May 20, 2022

Changes proposed in this pull request:
Database migrations that add timestamps to various tables:

  • groups
  • group_user
  • group_permission
  • tags
  • discussion_tag
  • post_mentions_post
  • post_mentions_user

The timestamps are not really used anywhere in Flarum, but can be very handy for analytics purposes.

This PR is kind of a follow-up of flarum/likes#28

Reviewers should focus on:
The migrations run without error and don't cause any unwanted side-effects (including performance aspects).

Necessity

  • Has the problem that is being solved here been clearly explained?
  • If applicable, have various options for solving this problem been considered?
  • For core PRs, does this need to be in core, or could it be in an extension?
  • Are we willing to maintain this for years / potentially forever?

Confirmed

  • Frontend changes: tested on a local Flarum installation.
  • Backend changes: tests are green (run composer test).
  • Core developer confirmed locally this works as intended.
  • Tests have been added, or are not appropriate here.

Required changes:

  • Related documentation PR: (Remove if irrelevant)
  • Related core extension PRs: (Remove if irrelevant)

- groups
- group_user
- group_permission
- tags
- discussion_tag
- post_mentions_post
- post_mentions_user
Copy link
Member

@askvortsov1 askvortsov1 left a comment

Choose a reason for hiding this comment

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

Haven't tested locally, but makes sense!

Copy link
Member

@SychO9 SychO9 left a comment

Choose a reason for hiding this comment

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

Could you please for every timestamp add it to the relevant model's $dates array property for proper casting?

also very minor but there seems to be a space in the filename:
2022_05_20_000005_add_created_at_to_ post_mentions_post_table.php

@davwheat
Copy link
Member

The only issue with this is that it would set created_at to the time the migration is run, which isn't accurate and could pollute statistics.

Would it not be better to set to null initially, then update the default value to be the current time?

@iPurpl3x
Copy link
Contributor Author

@SychO9 Sure, I added the casts (for the tables that have corresponding models) and fixed the file name (good that you saw that one!)

@davwheat I have thought of that. The thing is that:

IF we choose to update the value of the column automatically in the DB (and not use Laravel, which might slightly impact performance) then the DEFAULT is going to be current_timestamp() so I would need to run a second migration that sets all the old values to NULL after the column(s) have been added. Do you want me to do that ?

@davwheat
Copy link
Member

In my opinion, running two migrations to do that (can they be merged into two steps of one migration? I have no idea) would be the best option.

I'd be interested to hear the opinions of other maintainers though.

@iPurpl3x
Copy link
Contributor Author

@davwheat If I don't use the \Flarum\Database\Migration helper, and write the migration by hand, I might be able to merge it all into one file.

I also would like to hear opinions of others...

@SychO9
Copy link
Member

SychO9 commented May 24, 2022

How about creating the columns first, then modifying them to use current (and use current on update where appropriate). That avoids having to modify all rows.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

return [
    'up' => function (Builder $schema) {
        $schema->table('groups', function (Blueprint $table) {
            $table->timestamp('created_at')->nullable();
        });

        // do this manually because dbal doesn't recognize timestamp columns
        $connection = $schema->getConnection();
        $prefix = $connection->getTablePrefix();
        $connection->statement("ALTER TABLE $prefix`groups` MODIFY created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP");
    },

    'down' => function (Builder $schema) {
        $schema->table('groups', function (Blueprint $table) {
            $table->dropColumn('created_at');
        });
    }
];

Also it might be good to have a comment on each migration that mentions the columns were added for third-party analytical purposes.

@iPurpl3x
Copy link
Contributor Author

Ah yes, that's pretty clever @SychO9, I will try that I keep you updated.

Agreed for the comments.

@iPurpl3x iPurpl3x changed the title Add automatic timestamps for various tables WIP: Add automatic timestamps for various tables May 27, 2022
@iPurpl3x iPurpl3x changed the title WIP: Add automatic timestamps for various tables Add automatic timestamps for various tables May 31, 2022
@iPurpl3x
Copy link
Contributor Author

I have now updated all the migrations, using the idea of @SychO9 to makes that all existing rows in the DB stay with NULL on the timestamps and don't get a false value of the time when the migration is done.

Hopefully that makes this PR now mergeable 🤞🏻

@davwheat davwheat requested a review from askvortsov1 June 2, 2022 02:00
Copy link
Member

@davwheat davwheat left a comment

Choose a reason for hiding this comment

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

Tested locally. Seems to work as intended on non-prefixed DBs.

image
image

@davwheat davwheat added this to the 1.4 milestone Jun 2, 2022
@SychO9 SychO9 modified the milestones: 1.3.1, 1.4 Jun 7, 2022
@davwheat davwheat requested a review from SychO9 June 7, 2022 16:58
@davwheat davwheat merged commit 776f9bf into flarum:main Jun 7, 2022
@SychO9 SychO9 changed the title Add automatic timestamps for various tables chore: add automatic timestamps for various tables Jun 7, 2022
@SychO9 SychO9 changed the title chore: add automatic timestamps for various tables feat: add automatic timestamps for various tables Jun 7, 2022
@imorland imorland mentioned this pull request Oct 31, 2022
10 tasks
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