Skip to content

Commit

Permalink
Fixed General error: 1 no such table: sqlite_sequence when running te…
Browse files Browse the repository at this point in the history
…sts and table does not exist
  • Loading branch information
jamielsharief committed Jun 17, 2020
1 parent 0756d83 commit 1c69a8e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## [unreleased]

## [2.7.4] - 2020-06-17

### Fixed
- Fixed General error: 1 no such table: sqlite_sequence when running tests and table does not exist

## [2.7.3] - 2020-06-14

### Added
Expand Down
20 changes: 19 additions & 1 deletion src/Model/Schema/SqliteSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Origin\Model\Schema;

use InvalidArgumentException;
use Origin\Model\ConnectionManager;
use Origin\Core\Exception\Exception;

/**
Expand Down Expand Up @@ -520,13 +521,30 @@ public function renameIndex(string $table, string $oldName, string $newName, arr
public function truncateTableSql(string $table): array
{
$out = [];

if ($this->hasSquences()) {
$out[] = sprintf('DELETE from sqlite_sequence WHERE name = %s', $this->quoteIdentifier($table));
}

$out[] = sprintf('DELETE from sqlite_sequence WHERE name = %s', $this->quoteIdentifier($table));
$out[] = sprintf('DELETE FROM %s', $this->quoteIdentifier($table));

return $out;
}

/**
* Check that the squences table exists, if not it will generate an error
* e.g. General error: 1 no such table: sqlite_sequence
*
* @return boolean
*/
private function hasSquences(): bool
{
$connection = ConnectionManager::get($this->datasource);
$connection->execute('SELECT * FROM sqlite_master WHERE name = "sqlite_sequence"');

return ! empty($connection->fetchAll());
}

/**
* Changes the autoincrement number. Due to how sqlite works, you can't set the number to start with, unless
* you create a record, set the number then delete the record.
Expand Down
5 changes: 4 additions & 1 deletion src/TestSuite/FixtureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ public function loadFixture(string $fixture): void
}
} catch (DataSourceException $e) {
ConnectionManager::get('test')->rollback(); # Cancel Transaction
throw new Exception(sprintf('Error creating fixture %s for test case %s : %s', $fixture, $this->testCaseName, $e->getMessage()));
// run db:test:prepare
throw new Exception(
sprintf('Error creating fixture %s for test case %s : %s', $fixture, $this->testCaseName, $e->getMessage())
);
}
}

Expand Down

0 comments on commit 1c69a8e

Please sign in to comment.