1
0
mirror of https://github.com/flarum/core.git synced 2025-08-07 00:47:00 +02:00

feat: add support for SQLite (#3984)

* feat: add support for sqlite

* chore: add warning on install

* fix: ignore constraints before transaction begins

* chore: update workflow

* Apply fixes from StyleCI

* chore: generate sqlite dump and manually add foreign keys

* chore: fix actions

* chore: fix actions

* chore: fix actions

* chore: fix actions

* chore: fix actions

* chore: fix actions

* test: fix

* Apply fixes from StyleCI

* fix: sqlite with db prefix

* Apply fixes from StyleCI

* fix: statistics sqlite
This commit is contained in:
Sami Mazouz
2024-06-21 07:25:11 +01:00
committed by GitHub
parent 5ce1aeab47
commit eb6e599df1
61 changed files with 801 additions and 263 deletions

View File

@@ -14,6 +14,7 @@ use Flarum\Database\AbstractModel;
use Flarum\Database\ScopeVisibilityTrait;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
@@ -30,6 +31,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Flag extends AbstractModel
{
use ScopeVisibilityTrait;
use HasFactory;
protected $casts = ['created_at' => 'datetime'];

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Flags;
use Carbon\Carbon;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class FlagFactory extends Factory
{
public function definition(): array
{
return [
'type' => 'user',
'post_id' => Post::factory(),
'user_id' => User::factory(),
'reason' => $this->faker->sentence,
'reason_detail' => $this->faker->sentence,
'created_at' => Carbon::now(),
];
}
}

View File

@@ -10,6 +10,7 @@
namespace Flarum\Flags\Tests\integration\api\flags;
use Flarum\Discussion\Discussion;
use Flarum\Flags\Flag;
use Flarum\Group\Group;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
@@ -55,7 +56,7 @@ class ListTest extends TestCase
['id' => 2, 'discussion_id' => 1, 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p></p></t>'],
['id' => 3, 'discussion_id' => 1, 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p></p></t>'],
],
'flags' => [
Flag::class => [
['id' => 1, 'post_id' => 1, 'user_id' => 1],
['id' => 2, 'post_id' => 1, 'user_id' => 2],
['id' => 3, 'post_id' => 1, 'user_id' => 3],

View File

@@ -10,6 +10,7 @@
namespace Flarum\Flags\Tests\integration\api\flags;
use Flarum\Discussion\Discussion;
use Flarum\Flags\Flag;
use Flarum\Group\Group;
use Flarum\Post\Post;
use Flarum\Tags\Tag;
@@ -83,7 +84,7 @@ class ListWithTagsTest extends TestCase
['id' => 6, 'discussion_id' => 4, 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p></p></t>'],
['id' => 7, 'discussion_id' => 5, 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p></p></t>'],
],
'flags' => [
Flag::class => [
// From regular ListTest
['id' => 1, 'post_id' => 1, 'user_id' => 1],
['id' => 2, 'post_id' => 1, 'user_id' => 2],

View File

@@ -16,10 +16,9 @@ return [
$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}post_mentions_post` MODIFY created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");
$schema->table('post_mentions_post', function (Blueprint $table) {
$table->timestamp('created_at')->nullable()->useCurrent()->change();
});
},
'down' => function (Builder $schema) {

View File

@@ -16,10 +16,9 @@ return [
$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}post_mentions_user` MODIFY created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");
$schema->table('post_mentions_user', function (Blueprint $table) {
$table->timestamp('created_at')->nullable()->useCurrent()->change();
});
},
'down' => function (Builder $schema) {

View File

@@ -32,7 +32,7 @@ class LoadMentionedByRelationship
$query
->with(['mentionsPosts', 'mentionsPosts.user', 'mentionsPosts.discussion', 'mentionsUsers'])
->whereVisibleTo($actor)
->oldest()
->oldest('posts.created_at')
// Limiting a relationship results is only possible because
// the Post model uses the \Staudenmeir\EloquentEagerLimit\HasEagerLimit
// trait.

View File

@@ -130,12 +130,16 @@ class ShowStatisticsData implements RequestHandlerInterface
$endDate = new DateTime();
}
// if within the last 24 hours, group by hour
$format = 'CASE WHEN '.$column.' > ? THEN \'%Y-%m-%d %H:00:00\' ELSE \'%Y-%m-%d\' END';
$dbFormattedDatetime = match ($query->getConnection()->getDriverName()) {
'sqlite' => 'strftime('.$format.', '.$column.')',
default => 'DATE_FORMAT('.$column.', '.$format.')',
};
$results = $query
->selectRaw(
'DATE_FORMAT(
@date := '.$column.',
IF(@date > ?, \'%Y-%m-%d %H:00:00\', \'%Y-%m-%d\') -- if within the last 24 hours, group by hour
) as time_group',
$dbFormattedDatetime.' as time_group',
[new DateTime('-25 hours')]
)
->selectRaw('COUNT(id) as count')

View File

@@ -102,7 +102,7 @@ class CanRequestCustomTimedStatisticsTest extends TestCase
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(200, $response->getStatusCode(), $body['errors'][0]['detail'] ?? '');
$this->assertEquals(
$data,

View File

@@ -12,6 +12,7 @@ namespace Flarum\Sticky;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\SearchCriteria;
use Flarum\Tags\Search\Filter\TagFilter;
use Illuminate\Database\Query\Builder;
class PinStickiedDiscussionsToTop
{
@@ -45,22 +46,26 @@ class PinStickiedDiscussionsToTop
$sticky->where('is_sticky', true);
unset($sticky->orders);
/** @var Builder $q */
foreach ([$sticky, $query] as $q) {
$read = $q->newQuery()
->selectRaw('1')
->from('discussion_user as sticky')
->whereColumn('sticky.discussion_id', 'id')
->where('sticky.user_id', '=', $state->getActor()->id)
->whereColumn('sticky.last_read_post_number', '>=', 'last_post_number');
// Add the bindings manually (rather than as the second
// argument in orderByRaw) for now due to a bug in Laravel which
// would add the bindings in the wrong order.
$q->selectRaw('(is_sticky and not exists ('.$read->toSql().') and last_posted_at > ?) as is_unread_sticky', array_merge($read->getBindings(), [$state->getActor()->marked_all_as_read_at ?: 0]));
}
$query->union($sticky);
$read = $query->newQuery()
->selectRaw('1')
->from('discussion_user as sticky')
->whereColumn('sticky.discussion_id', 'id')
->where('sticky.user_id', '=', $state->getActor()->id)
->whereColumn('sticky.last_read_post_number', '>=', 'last_post_number');
$query->orderByDesc('is_unread_sticky');
// Add the bindings manually (rather than as the second
// argument in orderByRaw) for now due to a bug in Laravel which
// would add the bindings in the wrong order.
$query->orderByRaw('is_sticky and not exists ('.$read->toSql().') and last_posted_at > ? desc')
->addBinding(array_merge($read->getBindings(), [$state->getActor()->marked_all_as_read_at ?: 0]), 'union');
$query->unionOrders = array_merge($query->unionOrders, $query->orders);
$query->unionOrders = array_merge($query->unionOrders ?? [], $query->orders ?? []);
$query->unionLimit = $query->limit;
$query->unionOffset = $query->offset;

View File

@@ -14,9 +14,14 @@ return [
'up' => function (Builder $schema) {
$schema->table('tags', function (Blueprint $table) {
$table->renameColumn('discussions_count', 'discussion_count');
});
$schema->table('tags', function (Blueprint $table) {
$table->renameColumn('last_time', 'last_posted_at');
});
$schema->table('tags', function (Blueprint $table) {
$table->renameColumn('last_discussion_id', 'last_posted_discussion_id');
});
$schema->table('tags', function (Blueprint $table) {
$table->integer('parent_id')->unsigned()->nullable()->change();
$table->integer('last_posted_user_id')->unsigned()->nullable();
@@ -26,11 +31,17 @@ return [
'down' => function (Builder $schema) {
$schema->table('tags', function (Blueprint $table) {
$table->dropColumn('last_posted_user_id');
});
$schema->table('tags', function (Blueprint $table) {
$table->integer('parent_id')->nullable()->change();
});
$schema->table('tags', function (Blueprint $table) {
$table->renameColumn('discussion_count', 'discussions_count');
});
$schema->table('tags', function (Blueprint $table) {
$table->renameColumn('last_posted_at', 'last_time');
});
$schema->table('tags', function (Blueprint $table) {
$table->renameColumn('last_posted_discussion_id', 'last_discussion_id');
});
}

View File

@@ -17,17 +17,15 @@ return [
$table->timestamp('updated_at')->nullable();
});
// do this manually because dbal doesn't recognize timestamp columns
$connection = $schema->getConnection();
$prefix = $connection->getTablePrefix();
$connection->statement("ALTER TABLE `{$prefix}tags` MODIFY created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");
$connection->statement("ALTER TABLE `{$prefix}tags` MODIFY updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
$schema->table('tags', function (Blueprint $table) {
$table->timestamp('created_at')->nullable()->useCurrent()->change();
$table->timestamp('updated_at')->nullable()->useCurrent()->useCurrentOnUpdate()->change();
});
},
'down' => function (Builder $schema) {
$schema->table('tags', function (Blueprint $table) {
$table->dropColumn('created_at');
$table->dropColumn('updated_at');
$table->dropColumn('created_at', 'updated_at');
});
}
];

View File

@@ -16,10 +16,9 @@ return [
$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}discussion_tag` MODIFY created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");
$schema->table('discussion_tag', function (Blueprint $table) {
$table->timestamp('created_at')->nullable()->useCurrent()->change();
});
},
'down' => function (Builder $schema) {