-
Notifications
You must be signed in to change notification settings - Fork 12
fix: isolate statement cache and fix SQLite inserts #20
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace App\Model; | ||
|
|
||
| /** | ||
| * @property int|null $id | ||
| * @property string|null $name | ||
| */ | ||
| class IsolatedConnectionCategoryA extends \Freshsauce\Model\Model | ||
| { | ||
| public static $_db; | ||
|
|
||
| protected static $_tableName = 'items'; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace App\Model; | ||
|
|
||
| /** | ||
| * @property int|null $id | ||
| * @property string|null $name | ||
| */ | ||
| class IsolatedConnectionCategoryB extends \Freshsauce\Model\Model | ||
| { | ||
| public static $_db; | ||
|
|
||
| protected static $_tableName = 'items'; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace App\Model; | ||
|
|
||
| /** | ||
| * @property int|null $id | ||
| * @property string|null $name | ||
| * @property string|null $updated_at | ||
| * @property string|null $created_at | ||
| */ | ||
| class SqliteCategory extends \Freshsauce\Model\Model | ||
| { | ||
| public static $_db; | ||
|
|
||
| protected static $_tableName = 'categories'; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||||
| class CategoryTest extends TestCase | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| private const TEST_DB_NAME = 'categorytest'; | ||||||||||||||||||||||||||
| private const SQLITE_SEQUENCE_TABLE = 'sqlite_sequence'; | ||||||||||||||||||||||||||
| private static ?string $driverName = null; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
|
|
@@ -53,6 +54,16 @@ public static function setUpBeforeClass(): void | |||||||||||||||||||||||||
| "created_at" TIMESTAMP NULL | ||||||||||||||||||||||||||
| )', | ||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||
| } elseif (self::$driverName === 'sqlite') { | ||||||||||||||||||||||||||
| $sql_setup = [ | ||||||||||||||||||||||||||
| 'DROP TABLE IF EXISTS `categories`', | ||||||||||||||||||||||||||
| 'CREATE TABLE `categories` ( | ||||||||||||||||||||||||||
| `id` INTEGER PRIMARY KEY AUTOINCREMENT, | ||||||||||||||||||||||||||
| `name` VARCHAR(120) NULL, | ||||||||||||||||||||||||||
| `updated_at` TEXT NULL, | ||||||||||||||||||||||||||
| `created_at` TEXT NULL | ||||||||||||||||||||||||||
| )', | ||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| throw new RuntimeException('Unsupported PDO driver for tests: ' . self::$driverName); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
@@ -81,6 +92,12 @@ public function setUp(): void | |||||||||||||||||||||||||
| Freshsauce\Model\Model::execute('TRUNCATE TABLE `categories`'); | ||||||||||||||||||||||||||
| } elseif (self::$driverName === 'pgsql') { | ||||||||||||||||||||||||||
| Freshsauce\Model\Model::execute('TRUNCATE TABLE "categories" RESTART IDENTITY'); | ||||||||||||||||||||||||||
| } elseif (self::$driverName === 'sqlite') { | ||||||||||||||||||||||||||
| Freshsauce\Model\Model::execute('DELETE FROM `categories`'); | ||||||||||||||||||||||||||
| Freshsauce\Model\Model::execute( | ||||||||||||||||||||||||||
| 'DELETE FROM `' . self::SQLITE_SEQUENCE_TABLE . '` WHERE `name` = ?', | ||||||||||||||||||||||||||
| ['categories'] | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
Comment on lines
+97
to
+100
|
||||||||||||||||||||||||||
| Freshsauce\Model\Model::execute( | |
| 'DELETE FROM `' . self::SQLITE_SEQUENCE_TABLE . '` WHERE `name` = ?', | |
| ['categories'] | |
| ); | |
| try { | |
| Freshsauce\Model\Model::execute( | |
| 'DELETE FROM `' . self::SQLITE_SEQUENCE_TABLE . '` WHERE `name` = ?', | |
| ['categories'] | |
| ); | |
| } catch (\Throwable $e) { | |
| // Ignore if sqlite_sequence does not exist (e.g. on a brand-new SQLite database) | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||||||||||||||
| <?php | ||||||||||||||||||
|
|
||||||||||||||||||
| use PHPUnit\Framework\TestCase; | ||||||||||||||||||
| use PHPUnit\Framework\SkippedTestSuiteError; | ||||||||||||||||||
|
|
||||||||||||||||||
| class SqliteModelTest extends TestCase | ||||||||||||||||||
| { | ||||||||||||||||||
| public static function setUpBeforeClass(): void | ||||||||||||||||||
| { | ||||||||||||||||||
| if (!in_array('sqlite', \PDO::getAvailableDrivers(), true)) { | ||||||||||||||||||
| throw new SkippedTestSuiteError('The pdo_sqlite extension is required to run SQLite-specific tests.'); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| App\Model\SqliteCategory::connectDb('sqlite::memory:', '', ''); | ||||||||||||||||||
| App\Model\SqliteCategory::execute( | ||||||||||||||||||
| 'CREATE TABLE categories ( | ||||||||||||||||||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||||||||||||||||||
| name TEXT NULL, | ||||||||||||||||||
| updated_at TEXT NULL, | ||||||||||||||||||
| created_at TEXT NULL | ||||||||||||||||||
| )' | ||||||||||||||||||
| ); | ||||||||||||||||||
davebarnwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| protected function setUp(): void | ||||||||||||||||||
| { | ||||||||||||||||||
| App\Model\SqliteCategory::execute('DELETE FROM `categories`'); | ||||||||||||||||||
| App\Model\SqliteCategory::execute('DELETE FROM sqlite_sequence WHERE name = ?', ['categories']); | ||||||||||||||||||
|
||||||||||||||||||
| App\Model\SqliteCategory::execute('DELETE FROM sqlite_sequence WHERE name = ?', ['categories']); | |
| try { | |
| App\Model\SqliteCategory::execute('DELETE FROM sqlite_sequence WHERE name = ?', ['categories']); | |
| } catch (\PDOException $e) { | |
| if (strpos($e->getMessage(), 'no such table: sqlite_sequence') === false) { | |
| throw $e; | |
| } | |
| } |
Uh oh!
There was an error while loading. Please reload this page.