Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

test: add tests for sqlite

peaklabs-devb429c3d88e67
diff --git a/tests/Feature/AllResourceRestartLimitsTest.php b/tests/Feature/AllResourceRestartLimitsTest.php
index d1ff39d8ee..54efe447aa 100644
--- a/tests/Feature/AllResourceRestartLimitsTest.php
+++ b/tests/Feature/AllResourceRestartLimitsTest.php
@@ -15,6 +15,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Traits\HasRestartLimit;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
@@ -175,6 +176,7 @@ it('limits restarts only for applications', function () {
StandaloneKeydb::class,
StandaloneDragonfly::class,
StandaloneClickhouse::class,
+ StandaloneSqlite::class,
];
foreach ($databaseModels as $databaseModel) {
@@ -191,6 +193,7 @@ it('limits restarts only for applications', function () {
'standalone_keydbs',
'standalone_dragonflies',
'standalone_clickhouses',
+ 'standalone_sqlites',
] as $databaseTable) {
expect(Schema::hasColumn($databaseTable, 'max_restart_count'))->toBeFalse()
->and(Schema::hasColumn($databaseTable, 'restart_limit_reached'))->toBeFalse();
diff --git a/tests/Feature/Api/DatabaseBackupCreationApiTest.php b/tests/Feature/Api/DatabaseBackupCreationApiTest.php
index cb0c5cf737..57f151f15e 100644
--- a/tests/Feature/Api/DatabaseBackupCreationApiTest.php
+++ b/tests/Feature/Api/DatabaseBackupCreationApiTest.php
@@ -9,6 +9,7 @@ use App\Models\StandaloneClickhouse;
use App\Models\StandaloneDocker;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -129,6 +130,22 @@ describe('POST /api/v1/databases/{uuid}/backups', function () {
->and($backup->database_type)->toBe(StandaloneClickhouse::class);
});
+ test('defaults sqlite backups to every configured database file', function () {
+ $database = create_standalone_sqlite($this->environment->id, $this->destination, ['sqlite_databases' => 'app.db,jobs.db']);
+
+ $response = $this->withHeaders(backupHeaders())
+ ->postJson("/api/v1/databases/{$database->uuid}/backups", [
+ 'frequency' => 'daily',
+ ]);
+
+ $response->assertCreated();
+
+ $backup = ScheduledDatabaseBackup::where('uuid', $response->json('uuid'))->firstOrFail();
+
+ expect($backup->databases_to_backup)->toBe('app.db,jobs.db')
+ ->and($backup->database_type)->toBe(StandaloneSqlite::class);
+ });
+
test('creates backup configuration with valid frequency', function () {
$response = $this->withHeaders(backupHeaders())
->postJson("/api/v1/databases/{$this->database->uuid}/backups", [
diff --git a/tests/Feature/ApiDeploymentPermissionsTest.php b/tests/Feature/ApiDeploymentPermissionsTest.php
index fd1c1bb632..4b57ec381c 100644
--- a/tests/Feature/ApiDeploymentPermissionsTest.php
+++ b/tests/Feature/ApiDeploymentPermissionsTest.php
@@ -272,4 +272,5 @@ test('all other database create variants reject write-only instant deployment',
'/api/v1/databases/clickhouse',
'/api/v1/databases/dragonfly',
'/api/v1/databases/keydb',
+ '/api/v1/databases/sqlite',
]);
diff --git a/tests/Feature/AuditEventsTest.php b/tests/Feature/AuditEventsTest.php
index a82b04b609..21771ba5f6 100644
--- a/tests/Feature/AuditEventsTest.php
+++ b/tests/Feature/AuditEventsTest.php
@@ -27,6 +27,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Team;
use App\Models\User;
use App\Traits\Auditable;
@@ -486,6 +487,7 @@ test('team resource models opt in to automatic auditing', function (string $mode
StandaloneKeydb::class,
StandaloneDragonfly::class,
StandaloneClickhouse::class,
+ StandaloneSqlite::class,
]);
test('withoutAuditLogging suppresses mutations until the outer callback ends', function () {
diff --git a/tests/Feature/CreateScheduledBackupValidationTest.php b/tests/Feature/CreateScheduledBackupValidationTest.php
index 507aad1dc3..23a212b3bc 100644
--- a/tests/Feature/CreateScheduledBackupValidationTest.php
+++ b/tests/Feature/CreateScheduledBackupValidationTest.php
@@ -12,6 +12,7 @@ use App\Models\StandaloneClickhouse;
use App\Models\StandaloneDocker;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -190,6 +191,19 @@ it('creates a clickhouse backup for its configured database', function () {
->and($backup->databases_to_backup)->toBe('analytics');
});
+it('creates a sqlite backup for its configured database files', function () {
+ $database = create_standalone_sqlite($this->environment->id, $this->destination, ['sqlite_databases' => 'app.db,jobs.db']);
+
+ Livewire::test(CreateScheduledBackup::class, ['database' => $database])
+ ->set('frequency', 'daily')
+ ->call('submit');
+
+ $backup = ScheduledDatabaseBackup::firstOrFail();
+
+ expect($backup->database_type)->toBe(StandaloneSqlite::class)
+ ->and($backup->databases_to_backup)->toBe('app.db,jobs.db');
+});
+
it('rejects scheduled backups for unsupported database types', function () {
$server = Server::factory()->create(['team_id' => $this->team->id]);
$destination = StandaloneDocker::where('server_id', $server->id)->firstOrFail();
diff --git a/tests/Feature/MassAssignmentProtectionTest.php b/tests/Feature/MassAssignmentProtectionTest.php
index 436d0736b0..0bd0e40a04 100644
--- a/tests/Feature/MassAssignmentProtectionTest.php
+++ b/tests/Feature/MassAssignmentProtectionTest.php
@@ -11,6 +11,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Team;
use App\Models\User;
@@ -31,6 +32,7 @@ describe('mass assignment protection', function () {
StandaloneKeydb::class,
StandaloneDragonfly::class,
StandaloneClickhouse::class,
+ StandaloneSqlite::class,
];
foreach ($models as $modelClass) {
@@ -137,6 +139,7 @@ describe('mass assignment protection', function () {
StandaloneKeydb::class,
StandaloneDragonfly::class,
StandaloneClickhouse::class,
+ StandaloneSqlite::class,
];
foreach ($models as $modelClass) {
diff --git a/tests/Feature/ModelFillableCreationTest.php b/tests/Feature/ModelFillableCreationTest.php
index 1e06ea22d3..54870d3f8c 100644
--- a/tests/Feature/ModelFillableCreationTest.php
+++ b/tests/Feature/ModelFillableCreationTest.php
@@ -26,6 +26,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Subscription;
use App\Models\SwarmDocker;
use App\Models\Tag;
@@ -837,6 +838,40 @@ it('creates StandaloneDragonfly with all fillable attributes', function () {
expect($db->environment_id)->toBe($this->environment->id);
});
+it('creates StandaloneSqlite with all fillable attributes', function () {
+ $db = StandaloneSqlite::create([
+ 'uuid' => 'custom-sqlite-uuid',
+ 'name' => 'Full Fillable SQLite',
+ 'description' => 'SQLite with all attrs',
+ 'sqlite_databases' => 'app.db,jobs.db',
+ 'is_log_drain_enabled' => false,
+ 'is_include_timestamps' => false,
+ 'status' => 'running',
+ 'image' => 'peakimages/sqlite:3.53.4-v0.1.0',
+ 'limits_memory' => '1g',
+ 'limits_memory_swap' => '2g',
+ 'limits_memory_swappiness' => 30,
+ 'limits_memory_reservation' => '512m',
+ 'limits_cpus' => '2',
+ 'limits_cpuset' => '0-1',
+ 'limits_cpu_shares' => 512,
+ 'started_at' => now()->subDay()->toISOString(),
+ 'restart_count' => 0,
+ 'last_restart_at' => null,
+ 'last_restart_type' => null,
+ 'last_online_at' => now()->toISOString(),
+ 'custom_docker_run_options' => '',
+ 'destination_type' => $this->destination->getMorphClass(),
+ 'destination_id' => $this->destination->id,
+ 'environment_id' => $this->environment->id,
+ ]);
+
+ expect($db->exists)->toBeTrue();
+ expect($db->uuid)->toBe('custom-sqlite-uuid');
+ expect($db->sqlite_databases)->toBe('app.db,jobs.db');
+ expect($db->environment_id)->toBe($this->environment->id);
+});
+
it('creates StandaloneClickhouse with all fillable attributes', function () {
$db = StandaloneClickhouse::create([
'uuid' => 'custom-ch-uuid',
diff --git a/tests/Feature/SecretManagers/SecretManagerLinkTest.php b/tests/Feature/SecretManagers/SecretManagerLinkTest.php
index bbf1924dfe..524fa272c2 100644
--- a/tests/Feature/SecretManagers/SecretManagerLinkTest.php
+++ b/tests/Feature/SecretManagers/SecretManagerLinkTest.php
@@ -22,6 +22,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Team;
use App\Models\User;
use App\Traits\HasSecretManager;
@@ -259,6 +260,7 @@ test('all deployable environment-variable resources support secret managers', fu
StandaloneKeydb::class,
StandaloneDragonfly::class,
StandaloneClickhouse::class,
+ StandaloneSqlite::class,
]);
test('an application has at most one secret manager source', function () {
diff --git a/tests/Feature/StandaloneDockerDatabasesTest.php b/tests/Feature/StandaloneDockerDatabasesTest.php
index 8d78891493..97741affe2 100644
--- a/tests/Feature/StandaloneDockerDatabasesTest.php
+++ b/tests/Feature/StandaloneDockerDatabasesTest.php
@@ -12,6 +12,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -56,7 +57,14 @@ test('StandaloneDocker::databases() includes attached clickhouse', function () {
expect($this->destination->attachedTo())->toBeTrue();
});
-test('StandaloneDocker::databases() includes all 8 standalone database types', function () {
+test('StandaloneDocker::databases() includes attached sqlite', function () {
+ attachDb(StandaloneSqlite::class, [], $this->destination, $this->environment);
+
+ expect($this->destination->databases()->count())->toBe(1);
+ expect($this->destination->attachedTo())->toBeTrue();
+});
+
+test('StandaloneDocker::databases() includes all 9 standalone database types', function () {
attachDb(StandalonePostgresql::class, ['postgres_password' => 'pw'], $this->destination, $this->environment);
attachDb(StandaloneRedis::class, ['redis_password' => 'pw'], $this->destination, $this->environment);
attachDb(StandaloneMongodb::class, ['mongo_initdb_root_password' => 'pw'], $this->destination, $this->environment);
@@ -65,7 +73,8 @@ test('StandaloneDocker::databases() includes all 8 standalone database types', f
attachDb(StandaloneKeydb::class, ['keydb_password' => 'pw'], $this->destination, $this->environment);
attachDb(StandaloneDragonfly::class, ['dragonfly_password' => 'pw'], $this->destination, $this->environment);
attachDb(StandaloneClickhouse::class, ['clickhouse_admin_password' => 'pw'], $this->destination, $this->environment);
+ attachDb(StandaloneSqlite::class, [], $this->destination, $this->environment);
- expect($this->destination->databases()->count())->toBe(8);
+ expect($this->destination->databases()->count())->toBe(9);
expect($this->destination->attachedTo())->toBeTrue();
});
diff --git a/tests/Feature/StandaloneSqliteTest.php b/tests/Feature/StandaloneSqliteTest.php
new file mode 100644
index 0000000000..31b1d6735b
--- /dev/null
+++ b/tests/Feature/StandaloneSqliteTest.php
@@ -0,0 +1,133 @@
+<?php
+
+use App\Actions\Database\StartSqlite;
+use App\Livewire\Project\Database\Sqlite\General;
+use App\Models\Environment;
+use App\Models\InstanceSettings;
+use App\Models\Project;
+use App\Models\Server;
+use App\Models\StandaloneDocker;
+use App\Models\StandaloneSqlite;
+use App\Models\Team;
+use App\Models\User;
+use App\Services\DatabaseStartCommandExecutor;
+use App\Support\DatabaseImport\DatabaseImportCommandBuilder;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Livewire\Livewire;
+use Spatie\Activitylog\Models\Activity;
+use Symfony\Component\Yaml\Yaml;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ $this->team = Team::factory()->create();
+ $this->user = User::factory()->create();
+ $this->team->members()->attach($this->user->id, ['role' => 'owner']);
+ session(['currentTeam' => $this->team]);
+ $this->server = Server::factory()->create(['team_id' => $this->team->id]);
+ $project = Project::factory()->create(['team_id' => $this->team->id]);
+ $this->environment = Environment::factory()->create(['project_id' => $project->id]);
+ $destination = StandaloneDocker::where('server_id', $this->server->id)->first();
+
+ $this->database = create_standalone_sqlite($this->environment->id, $destination, ['sqlite_databases' => 'app.db,jobs.db']);
+});
+
+function sqliteApiHeaders(): array
+{
+ InstanceSettings::forceCreate(['id' => 0, 'is_api_enabled' => true]);
+
+ return ['Authorization' => 'Bearer '.test()->user->createToken('test-token', ['*'])->plainTextToken];
+}
+
+it('creates the data volume and restores into the first configured file', function () {
+ expect($this->database->image)->toBe('peakimages/sqlite:3.53.4-v0.1.0')
+ ->and($this->database->databaseFilePath())->toBe('/var/lib/sqlite/app.db')
+ ->and($this->database->persistentStorages()->first()->mount_path)->toBe('/var/lib/sqlite');
+});
+
+it('only accepts plain comma-separated file names', function (string $value, bool $valid) {
+ expect((bool) preg_match(StandaloneSqlite::DATABASES_PATTERN, $value))->toBe($valid);
+})->with([
+ 'single file' => ['app.db', true],
+ 'several files' => ['app.db,jobs.sqlite', true],
+ 'path traversal' => ['../escape.db', false],
+ 'hidden file' => ['.app.db', false],
+ 'shell metacharacters' => ['app.db;id', false],
+ 'trailing comma' => ['app.db,', false],
+]);
+
+it('generates a compose file that passes the database files to the image', function () {
+ $commands = [];
+ $executor = Mockery::mock(DatabaseStartCommandExecutor::class);
+ $executor->shouldReceive('execute')->once()->withArgs(function (array $captured) use (&$commands) {
+ $commands = $captured;
+
+ return true;
+ })->andReturn(new Activity);
+ app()->instance(DatabaseStartCommandExecutor::class, $executor);
+
+ (new StartSqlite)->handle($this->database, new Activity);
+
+ preg_match("/^echo '([^']+)' \| base64 -d/", collect($commands)->first(fn (string $command) => str_contains($command, 'docker-compose.yml')), $matches);
+ $service = Yaml::parse(base64_decode($matches[1]))['services'][$this->database->uuid];
+
+ expect($service)->not->toHaveKey('command')
+ ->and($service['environment'])->toBe(['SQLITE_DATABASES=app.db,jobs.db'])
+ ->and($service['healthcheck']['test'])->toBe(['CMD', 'healthcheck.sh'])
+ ->and($service['volumes'])->toBe(['sqlite-data-'.$this->database->uuid.':/var/lib/sqlite']);
+});
+
+it('backs up with a read-only VACUUM INTO snapshot instead of a SQL dump', function () {
+ expect(file_get_contents(app_path('Jobs/DatabaseBackupJob.php')))
+ ->toContain('sqlite3 -readonly')
+ ->toContain('VACUUM INTO')
+ ->not->toContain('.dump');
+});
+
+it('restores a gzipped backup with .restore into the first database file', function () {
+ $command = app(DatabaseImportCommandBuilder::class)->buildRestoreCommand($this->database, '/tmp/restore_1', false);
+
+ expect($command)->toBe('backup=\'/tmp/restore_1\'; gunzip -c "$backup" > "$backup.db" && sqlite3 -bail \'/var/lib/sqlite/app.db\' \'.timeout 10000\' ".restore $backup.db"; status=$?; rm -f "$backup.db"; exit $status');
+});
+
+it('normalises the file list when saved from the general page', function () {
+ Livewire::actingAs($this->user)
+ ->test(General::class, ['database' => $this->database])
+ ->set('sqliteDatabases', 'app.db, cache.db')
+ ->call('submit')
+ ->assertHasNoErrors();
+
+ expect($this->database->refresh()->sqlite_databases)->toBe('app.db,cache.db');
+});
+
+it('creates a sqlite database through the API without public access fields', function () {
+ $headers = sqliteApiHeaders();
+ $payload = [
+ 'server_uuid' => $this->server->uuid,
+ 'project_uuid' => $this->environment->project->uuid,
+ 'environment_uuid' => $this->environment->uuid,
+ 'sqlite_databases' => 'main.db',
+ ];
+
+ $response = $this->withHeaders($headers)->postJson('/api/v1/databases/sqlite', $payload)->assertStatus(201);
+
+ expect(StandaloneSqlite::where('uuid', $response->json('uuid'))->value('sqlite_databases'))->toBe('main.db');
+
+ $this->withHeaders($headers)
+ ->postJson('/api/v1/databases/sqlite', $payload + ['is_public' => true, 'public_port' => 5432])
+ ->assertStatus(422);
+});
+
+it('updates the database files through the API and rejects unsafe names', function () {
+ $headers = sqliteApiHeaders();
+
+ $this->withHeaders($headers)
+ ->patchJson("/api/v1/databases/{$this->database->uuid}", ['sqlite_databases' => 'jobs.sqlite'])
+ ->assertStatus(200);
+
+ expect($this->database->refresh()->sqlite_databases)->toBe('jobs.sqlite');
+
+ $this->withHeaders($headers)
+ ->patchJson("/api/v1/databases/{$this->database->uuid}", ['sqlite_databases' => '../escape.db'])
+ ->assertStatus(422);
+});
diff --git a/tests/Unit/DatabaseImport/DatabaseImportCommandBuilderTest.php b/tests/Unit/DatabaseImport/DatabaseImportCommandBuilderTest.php
index 87b37b6668..3de55c8912 100644
--- a/tests/Unit/DatabaseImport/DatabaseImportCommandBuilderTest.php
+++ b/tests/Unit/DatabaseImport/DatabaseImportCommandBuilderTest.php
@@ -6,6 +6,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Support\DatabaseImport\DatabaseImportCommandBuilder;
function importResource(string $class, ?string $databaseType = null): object
@@ -15,6 +16,9 @@ function importResource(string $class, ?string $databaseType = null): object
if ($class === ServiceDatabase::class) {
$resource->shouldReceive('databaseType')->andReturn($databaseType);
}
+ if ($class === StandaloneSqlite::class) {
+ $resource->shouldReceive('databaseFilePath')->andReturn('/var/lib/sqlite/database.sqlite');
+ }
return $resource;
}
@@ -30,6 +34,7 @@ test('builds database-specific restore commands', function (string $class, ?stri
'mysql' => [StandaloneMysql::class, null, 'mysql -u $MYSQL_USER'],
'mariadb' => [StandaloneMariadb::class, null, 'mariadb -u $MARIADB_USER'],
'mongodb' => [StandaloneMongodb::class, null, 'mongorestore'],
+ 'sqlite' => [StandaloneSqlite::class, null, '.restore'],
'service postgres' => [ServiceDatabase::class, 'postgresql', 'pg_restore'],
'service mysql' => [ServiceDatabase::class, 'mysql', 'mysql -u $MYSQL_USER'],
'service mariadb' => [ServiceDatabase::class, 'mariadb', 'mariadb -u $MARIADB_USER'],
diff --git a/tests/Unit/DatabaseStartActionResolvedCredentialsTest.php b/tests/Unit/DatabaseStartActionResolvedCredentialsTest.php
index 7ee295e34f..2f8205a357 100644
--- a/tests/Unit/DatabaseStartActionResolvedCredentialsTest.php
+++ b/tests/Unit/DatabaseStartActionResolvedCredentialsTest.php
@@ -54,6 +54,7 @@ it('runs database start commands without persisting them through remote process'
'StartMysql',
'StartPostgresql',
'StartRedis',
+ 'StartSqlite',
]);
it('queues database starts with identifiers instead of generated commands', function () {
diff --git a/tests/Unit/DatabaseStartupOutputTest.php b/tests/Unit/DatabaseStartupOutputTest.php
index 6b20ec9ae9..cd6262b2c1 100644
--- a/tests/Unit/DatabaseStartupOutputTest.php
+++ b/tests/Unit/DatabaseStartupOutputTest.php
@@ -13,6 +13,7 @@ it('formats image progress output consistently', function (string $action) {
'mongodb' => 'StartMongodb',
'mysql' => 'StartMysql',
'redis' => 'StartRedis',
+ 'sqlite' => 'StartSqlite',
]);
it('prints image names as text', function (string $image) {
diff --git a/tests/Unit/ModelFillableRegressionTest.php b/tests/Unit/ModelFillableRegressionTest.php
index eff477c5a5..8d180ba92e 100644
--- a/tests/Unit/ModelFillableRegressionTest.php
+++ b/tests/Unit/ModelFillableRegressionTest.php
@@ -27,6 +27,7 @@ use App\Models\StandaloneMongodb;
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
+use App\Models\StandaloneSqlite;
use App\Models\Subscription;
use App\Models\SwarmDocker;
use App\Models\Tag;
@@ -73,4 +74,5 @@ it('keeps required mass-assignment attributes fillable for internal create flows
[StandaloneKeydb::class, ['uuid', 'destination_type', 'destination_id', 'environment_id']],
[StandaloneDragonfly::class, ['uuid', 'destination_type', 'destination_id', 'environment_id']],
[StandaloneClickhouse::class, ['uuid', 'destination_type', 'destination_id', 'environment_id']],
+ [StandaloneSqlite::class, ['uuid', 'destination_type', 'destination_id', 'environment_id']],
]);