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 support for SQLite #3984

Merged
merged 17 commits into from
Jun 21, 2024
Merged
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ indent_size = 2

[*.neon]
indent_style = tab

[{install,update}.php]
indent_size = 2
35 changes: 33 additions & 2 deletions .github/workflows/REUSABLE_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ on:
description: Versions of databases to test with. Should be array of strings encoded as JSON array
type: string
required: false
default: '["mysql:5.7", "mysql:8.0.30", "mysql:8.1.0", "mariadb"]'
default: '["mysql:5.7", "mysql:8.0.30", "mysql:8.1.0", "mariadb", "sqlite:3"]'

php_ini_values:
description: PHP ini values
Expand Down Expand Up @@ -79,43 +79,72 @@ jobs:
# Expands the matrix by naming DBs.
- service: 'mysql:5.7'
db: MySQL 5.7
driver: mysql
- service: 'mysql:8.0.30'
db: MySQL 8.0
driver: mysql
- service: mariadb
db: MariaDB
driver: mysql
- service: 'mysql:8.1.0'
db: MySQL 8.1
driver: mysql
- service: 'sqlite:3'
db: SQLite
driver: sqlite

# Include Database prefix tests with only one PHP version.
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: 'mysql:5.7'
db: MySQL 5.7
driver: mysql
prefix: flarum_
prefixStr: (prefix)
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: 'mysql:8.0.30'
db: MySQL 8.0
driver: mysql
prefix: flarum_
prefixStr: (prefix)
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: mariadb
db: MariaDB
driver: mysql
prefix: flarum_
prefixStr: (prefix)
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: 'mysql:8.1.0'
db: MySQL 8.1
driver: mysql
prefix: flarum_
prefixStr: (prefix)
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: 'sqlite:3'
db: SQLite
driver: sqlite
prefix: flarum_
prefixStr: (prefix)

# To reduce number of actions, we exclude some PHP versions from running with some DB versions.
exclude:
- php: ${{ fromJSON(inputs.php_versions)[1] }}
service: 'mysql:8.0.30'
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: mariadb
- php: ${{ fromJSON(inputs.php_versions)[1] }}
service: mariadb
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: 'mysql:8.1.0'
- php: ${{ fromJSON(inputs.php_versions)[1] }}
service: 'mysql:8.1.0'
- php: ${{ fromJSON(inputs.php_versions)[0] }}
service: 'sqlite:3'
- php: ${{ fromJSON(inputs.php_versions)[1] }}
service: 'sqlite:3'

services:
mysql:
image: ${{ matrix.service }}
image: ${{ matrix.service != 'sqlite:3' && matrix.service || '' }}
ports:
- 13306:3306

Expand All @@ -138,6 +167,7 @@ jobs:
ini-values: ${{ matrix.php_ini_values }}

- name: Create MySQL Database
if: ${{ matrix.service != 'sqlite:3' }}
run: |
sudo systemctl start mysql
mysql -uroot -proot -e 'CREATE DATABASE flarum_test;' --port 13306
Expand Down Expand Up @@ -167,6 +197,7 @@ jobs:
DB_PORT: 13306
DB_PASSWORD: root
DB_PREFIX: ${{ matrix.prefix }}
DB_DRIVER: ${{ matrix.driver }}
COMPOSER_PROCESS_TIMEOUT: 600

phpstan:
Expand Down
2 changes: 2 additions & 0 deletions extensions/flags/src/Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Database\ScopeVisibilityTrait;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
Expand All @@ -30,6 +31,7 @@
class Flag extends AbstractModel
{
use ScopeVisibilityTrait;
use HasFactory;

protected $casts = ['created_at' => 'datetime'];

Expand Down
30 changes: 30 additions & 0 deletions extensions/flags/src/FlagFactory.php
Original file line number Diff line number Diff line change
@@ -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(),
];
}
}
3 changes: 2 additions & 1 deletion extensions/flags/tests/integration/api/flags/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,7 +56,7 @@ protected function setUp(): void
['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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +84,7 @@ protected function setUp(): void
['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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function mutateRelation(BelongsToMany $query, ServerRequestInterfa
$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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ private function getTimedCounts(Builder $query, string $column, ?DateTime $start
$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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function can_request_timed_stats()

$body = json_decode($response->getBody()->getContents(), true);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(200, $response->getStatusCode(), $body['errors'][0]['detail'] ?? '');

$this->assertEquals(
$data,
Expand Down
31 changes: 18 additions & 13 deletions extensions/sticky/src/PinStickiedDiscussionsToTop.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\SearchCriteria;
use Flarum\Tags\Search\Filter\TagFilter;
use Illuminate\Database\Query\Builder;

class PinStickiedDiscussionsToTop
{
Expand Down Expand Up @@ -45,22 +46,26 @@ public function __invoke(DatabaseSearchState $state, SearchCriteria $criteria):
$sticky->where('is_sticky', true);
unset($sticky->orders);

$query->union($sticky);
/** @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]));
}

$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->union($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->orderByDesc('is_unread_sticky');

$query->unionOrders = array_merge($query->unionOrders, $query->orders);
$query->unionOrders = array_merge($query->unionOrders ?? [], $query->orders ?? []);
$query->unionLimit = $query->limit;
$query->unionOffset = $query->offset;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
'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();
Expand All @@ -26,11 +31,17 @@
'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');
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
$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');
});
}
];
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
$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) {
Expand Down
2 changes: 1 addition & 1 deletion framework/core/js/src/admin/components/StatusWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class StatusWidget extends DashboardWidget {

items.add('version-flarum', [<strong>Flarum</strong>, <br />, app.forum.attribute('version')], 100);
items.add('version-php', [<strong>PHP</strong>, <br />, app.data.phpVersion], 90);
items.add('version-mysql', [<strong>MySQL</strong>, <br />, app.data.mysqlVersion], 80);
items.add('version-db', [<strong>{app.data.dbDriver}</strong>, <br />, app.data.dbVersion], 80);
if (app.data.schedulerStatus) {
items.add(
'schedule-status',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@

$connection = $schema->getConnection();
$prefix = $connection->getTablePrefix();
$connection->statement('ALTER TABLE '.$prefix.'posts ADD FULLTEXT content (content)');

if ($connection->getDriverName() !== 'sqlite') {
$connection->statement('ALTER TABLE '.$prefix.'posts ADD FULLTEXT content (content)');
}
},

'down' => function (Builder $schema) {
Expand Down
Loading
Loading