Skip to content

Commit

Permalink
Tests run on sqlite, mysql & pgsql db engines
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi committed Feb 25, 2023
1 parent 0006218 commit 8e07a37
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 80 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@ Also make sure the username you specified (for non-sqlite DBs) has permission
to create and drop tables and view.

Because of the way the test-suite is designed, in-memory sqlite does not work.
The sqlite db must be stored in a file. This is already setup in the default pdo config.
The sqlite db must be stored in a file. This is already setup in the default pdo config.

The package should work with MS Sqlserver, but the tests will only run with sqlite, mysql & postgres databases.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"phpunit/phpunit": "^9.0",
"php-coveralls/php-coveralls": "^2.0",
"vimeo/psalm": "^5.4.0",
"rector/rector": "^0.15.0",
"opis/database": "^4.2"
"rector/rector": "^0.15.0"
},
"autoload": {
"classmap": ["src/"]
Expand Down
5 changes: 5 additions & 0 deletions pdo-dist.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
// NOTE: Because of the way the test-suite is designed, in-memory sqlite does not work.
// This means that you should not use the DSN below for sqlite:
// sqlite::memory:
// The sqlite db must be stored in a file. A default file is already configured below.

// Args for the PDO constructor
//public PDO::__construct(
// string $dsn,
Expand Down
180 changes: 103 additions & 77 deletions tests/SchemaManagementTrait.php
Original file line number Diff line number Diff line change
@@ -1,89 +1,115 @@
<?php
use Opis\Database\Database;
use \Opis\Database\Connection;
use \Opis\Database\Schema\CreateTable;

trait SchemaManagementTrait {

public function doSetUp(\PDO $pdo): void {

// Now using opis/database to create tables so that it generates the
// appropriate SQL create statements for the current pdo driver.
// This makes the tests run with sqlite, mysql & postgres.
// Haven't tried sqlserver yet.
$connection = Connection::fromPDO($pdo);
$db = new Database($connection);
$schema = $db->schema();
$driver_name = strtolower(\LeanOrmCli\SchemaUtils::getPdoDriverName($pdo));

$schema->create('authors', function(CreateTable $table) {

//add table authors
$table->integer('author_id')->autoincrement();
$table->primary('author_id', 'author_id');

$table->text('name');

$table->text('m_timestamp')->notNull();
$table->text('date_created')->notNull();
});

// Table creation method below is sqlite specific,
// won't work with mysql & the others
// $this->pdo->exec("
// CREATE TABLE authors (
// author_id INTEGER PRIMARY KEY,
// name TEXT,
// m_timestamp TEXT NOT NULL,
// date_created TEXT NOT NULL
// )
// ");

// The veiw creation sql below works on sqlite, mysql & postgres
// haven't tested with sqlserver though
$this->pdo->exec("
CREATE VIEW v_authors
AS
SELECT
author_id,
name,
m_timestamp,
date_created
FROM authors
");
$create_queries = [
'sqlite' => [
"
CREATE TABLE authors (
author_id INTEGER PRIMARY KEY,
name TEXT,
m_timestamp TEXT NOT NULL,
date_created TEXT NOT NULL
)
",
"
CREATE VIEW v_authors
AS
SELECT
author_id,
name,
m_timestamp,
date_created
FROM
authors
",
"
CREATE TABLE posts (
post_id INTEGER PRIMARY KEY,
author_id INTEGER NOT NULL,
datetime TEXT,
title TEXT,
body TEXT,
m_timestamp TEXT NOT NULL,
date_created TEXT NOT NULL,
FOREIGN KEY(author_id) REFERENCES authors(author_id)
)
",
],
'mysql' => [
"
CREATE TABLE `authors` (
`author_id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`m_timestamp` datetime NOT NULL,
`date_created` datetime NOT NULL,
PRIMARY KEY (`author_id`)
)
",
"
CREATE VIEW `v_authors` AS
SELECT
`authors`.`author_id` AS `author_id`,
`authors`.`name` AS `name`,
`authors`.`m_timestamp` AS `m_timestamp`,
`authors`.`date_created` AS `date_created`
FROM `authors`
",
"
CREATE TABLE `posts` (
`post_id` int unsigned NOT NULL AUTO_INCREMENT,
`author_id` int unsigned NOT NULL,
`datetime` datetime DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`body` text,
`m_timestamp` datetime NOT NULL,
`date_created` datetime NOT NULL,
PRIMARY KEY (`post_id`),
KEY `fk_posts_belong_to_an_author` (`author_id`),
CONSTRAINT `fk_posts_belong_to_an_author` FOREIGN KEY (`author_id`) REFERENCES `authors` (`author_id`) ON DELETE CASCADE ON UPDATE CASCADE
)
",
],
'pgsql' => [
"
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
name varchar(255) DEFAULT NULL,
m_timestamp TIMESTAMP NOT NULL,
date_created TIMESTAMP NOT NULL
)
",
"
CREATE VIEW v_authors AS
SELECT
authors.author_id AS author_id,
authors.name AS name,
authors.m_timestamp AS m_timestamp,
authors.date_created AS date_created
FROM authors
",
"
CREATE TABLE posts (
post_id SERIAL PRIMARY KEY,
author_id int NOT NULL,
datetime TIMESTAMP DEFAULT NULL,
title varchar(255) DEFAULT NULL,
body text,
m_timestamp TIMESTAMP NOT NULL,
date_created TIMESTAMP NOT NULL
)
",
],
];

// Table creation method below is sqlite specific,
// won't work with mysql & the others
// $this->pdo->exec("
// CREATE TABLE posts (
// post_id INTEGER PRIMARY KEY,
// author_id INTEGER NOT NULL,
// datetime TEXT,
// title TEXT,
// body TEXT,
// m_timestamp TEXT NOT NULL,
// date_created TEXT NOT NULL,
// FOREIGN KEY(author_id) REFERENCES authors(author_id)
// )
// ");

$schema->create('posts', function(CreateTable $table) {

//add table authors
$table->integer('post_id')->autoincrement();
$table->primary('post_id', 'post_id');

$table->integer('author_id')->notNull();

$table->text('datetime');
$table->text('title');
$table->text('body');

$table->text('m_timestamp')->notNull();
$table->text('date_created')->notNull();
foreach ($create_queries[$driver_name] as $query) {

$table->foreign('author_id')
->references('authors', 'author_id');
});
$pdo->exec($query);
}
}

protected function doTearDown(\PDO $pdo): void {
Expand Down

0 comments on commit 8e07a37

Please sign in to comment.