Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected static function configureSushiConnection()
$states['no-caching-capabilities']();
break;

case file_exists($cachePath) && filemtime($dataPath) <= filemtime($cachePath):
case file_exists($cachePath) && filesize($cachePath) > 0 && filemtime($dataPath) <= filemtime($cachePath):
$states['cache-file-found-and-up-to-date']();
break;

Expand All @@ -116,13 +116,24 @@ protected static function configureSushiConnection()

protected static function cacheFileNotFoundOrStale($cachePath, $dataPath, $instance)
{
file_put_contents($cachePath, '');
$tempPath = $cachePath.'.tmp.'.str_replace('.', '', uniqid('', true));

static::setSqliteConnection($cachePath);
try {
file_put_contents($tempPath, '');

static::setSqliteConnection($tempPath);

$instance->migrate();

$instance->migrate();
touch($tempPath, filemtime($dataPath));
rename($tempPath, $cachePath);

touch($cachePath, filemtime($dataPath));
static::setSqliteConnection($cachePath);
} finally {
if (file_exists($tempPath)) {
@unlink($tempPath);
}
}
}

protected function newRelatedInstance($class)
Expand Down
24 changes: 20 additions & 4 deletions tests/SushiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ function test_caches_sqlite_file_if_storage_cache_folder_is_available()
);
}

function test_rebuilds_empty_cache_file_even_when_its_timestamp_is_fresh()
{
$cachePath = $this->cachePath.'/sushi-tests-foo.sqlite';
$referencePath = (new \ReflectionClass(Foo::class))->getFileName();

file_put_contents($cachePath, '');
touch($cachePath, filemtime($referencePath) + 10);

Foo::resetStatics();

$this->assertEquals(3, Foo::count());
$this->assertGreaterThan(0, filesize($cachePath));
}

function test_avoids_error_when_creating_database_concurrently()
{
$actualFactory = app(ConnectionFactory::class);
Expand All @@ -115,15 +129,17 @@ function test_avoids_error_when_creating_database_concurrently()
]);

$connectionFactory = $this->createMock(ConnectionFactory::class);
$connectionFactory->expects($this->once())
$connectionFactory->expects($this->atLeastOnce())
->method('make')
->willReturnCallback(function () use ($actualConnection) {
// Simulate a concurrent request that creates the table at a point in time
// where our main execution has already determined that it does not exist
// and is about to create it.
$actualConnection->getSchemaBuilder()->create('blanks', function ($table) {
$table->increments('id');
});
if (! $actualConnection->getSchemaBuilder()->hasTable('blanks')) {
$actualConnection->getSchemaBuilder()->create('blanks', function ($table) {
$table->increments('id');
});
}

return $actualConnection;
});
Expand Down
Loading