-
-
Notifications
You must be signed in to change notification settings - Fork 835
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
Conversation
- groups - group_user - group_permission - tags - discussion_tag - post_mentions_post - post_mentions_user
There was a problem hiding this 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!
There was a problem hiding this 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
The only issue with this is that it would set Would it not be better to set to null initially, then update the default value to be the current time? |
@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 |
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. |
@davwheat If I don't use the I also would like to hear opinions of others... |
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. |
Ah yes, that's pretty clever @SychO9, I will try that I keep you updated. Agreed for the comments. |
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 🤞🏻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Confirmed
composer test
).Required changes: