Skip to content

Commit 8e07a37

Browse files
author
rotimi
committed
Tests run on sqlite, mysql & pgsql db engines
1 parent 0006218 commit 8e07a37

File tree

4 files changed

+112
-80
lines changed

4 files changed

+112
-80
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,6 @@ Also make sure the username you specified (for non-sqlite DBs) has permission
166166
to create and drop tables and view.
167167

168168
Because of the way the test-suite is designed, in-memory sqlite does not work.
169-
The sqlite db must be stored in a file. This is already setup in the default pdo config.
169+
The sqlite db must be stored in a file. This is already setup in the default pdo config.
170+
171+
The package should work with MS Sqlserver, but the tests will only run with sqlite, mysql & postgres databases.

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
"phpunit/phpunit": "^9.0",
2727
"php-coveralls/php-coveralls": "^2.0",
2828
"vimeo/psalm": "^5.4.0",
29-
"rector/rector": "^0.15.0",
30-
"opis/database": "^4.2"
29+
"rector/rector": "^0.15.0"
3130
},
3231
"autoload": {
3332
"classmap": ["src/"]

pdo-dist.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
2+
// NOTE: Because of the way the test-suite is designed, in-memory sqlite does not work.
3+
// This means that you should not use the DSN below for sqlite:
4+
// sqlite::memory:
5+
// The sqlite db must be stored in a file. A default file is already configured below.
6+
27
// Args for the PDO constructor
38
//public PDO::__construct(
49
// string $dsn,

tests/SchemaManagementTrait.php

Lines changed: 103 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,115 @@
11
<?php
2-
use Opis\Database\Database;
3-
use \Opis\Database\Connection;
4-
use \Opis\Database\Schema\CreateTable;
52

63
trait SchemaManagementTrait {
74

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

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

18-
$schema->create('authors', function(CreateTable $table) {
19-
20-
//add table authors
21-
$table->integer('author_id')->autoincrement();
22-
$table->primary('author_id', 'author_id');
23-
24-
$table->text('name');
25-
26-
$table->text('m_timestamp')->notNull();
27-
$table->text('date_created')->notNull();
28-
});
29-
30-
// Table creation method below is sqlite specific,
31-
// won't work with mysql & the others
32-
// $this->pdo->exec("
33-
// CREATE TABLE authors (
34-
// author_id INTEGER PRIMARY KEY,
35-
// name TEXT,
36-
// m_timestamp TEXT NOT NULL,
37-
// date_created TEXT NOT NULL
38-
// )
39-
// ");
40-
41-
// The veiw creation sql below works on sqlite, mysql & postgres
42-
// haven't tested with sqlserver though
43-
$this->pdo->exec("
44-
CREATE VIEW v_authors
45-
AS
46-
SELECT
47-
author_id,
48-
name,
49-
m_timestamp,
50-
date_created
51-
FROM authors
52-
");
9+
$create_queries = [
10+
'sqlite' => [
11+
"
12+
CREATE TABLE authors (
13+
author_id INTEGER PRIMARY KEY,
14+
name TEXT,
15+
m_timestamp TEXT NOT NULL,
16+
date_created TEXT NOT NULL
17+
)
18+
",
19+
"
20+
CREATE VIEW v_authors
21+
AS
22+
SELECT
23+
author_id,
24+
name,
25+
m_timestamp,
26+
date_created
27+
FROM
28+
authors
29+
",
30+
"
31+
CREATE TABLE posts (
32+
post_id INTEGER PRIMARY KEY,
33+
author_id INTEGER NOT NULL,
34+
datetime TEXT,
35+
title TEXT,
36+
body TEXT,
37+
m_timestamp TEXT NOT NULL,
38+
date_created TEXT NOT NULL,
39+
FOREIGN KEY(author_id) REFERENCES authors(author_id)
40+
)
41+
",
42+
],
43+
'mysql' => [
44+
"
45+
CREATE TABLE `authors` (
46+
`author_id` int unsigned NOT NULL AUTO_INCREMENT,
47+
`name` varchar(255) DEFAULT NULL,
48+
`m_timestamp` datetime NOT NULL,
49+
`date_created` datetime NOT NULL,
50+
PRIMARY KEY (`author_id`)
51+
)
52+
",
53+
"
54+
CREATE VIEW `v_authors` AS
55+
SELECT
56+
`authors`.`author_id` AS `author_id`,
57+
`authors`.`name` AS `name`,
58+
`authors`.`m_timestamp` AS `m_timestamp`,
59+
`authors`.`date_created` AS `date_created`
60+
FROM `authors`
61+
",
62+
"
63+
CREATE TABLE `posts` (
64+
`post_id` int unsigned NOT NULL AUTO_INCREMENT,
65+
`author_id` int unsigned NOT NULL,
66+
`datetime` datetime DEFAULT NULL,
67+
`title` varchar(255) DEFAULT NULL,
68+
`body` text,
69+
`m_timestamp` datetime NOT NULL,
70+
`date_created` datetime NOT NULL,
71+
PRIMARY KEY (`post_id`),
72+
KEY `fk_posts_belong_to_an_author` (`author_id`),
73+
CONSTRAINT `fk_posts_belong_to_an_author` FOREIGN KEY (`author_id`) REFERENCES `authors` (`author_id`) ON DELETE CASCADE ON UPDATE CASCADE
74+
)
75+
",
76+
],
77+
'pgsql' => [
78+
"
79+
CREATE TABLE authors (
80+
author_id SERIAL PRIMARY KEY,
81+
name varchar(255) DEFAULT NULL,
82+
m_timestamp TIMESTAMP NOT NULL,
83+
date_created TIMESTAMP NOT NULL
84+
)
85+
",
86+
"
87+
CREATE VIEW v_authors AS
88+
SELECT
89+
authors.author_id AS author_id,
90+
authors.name AS name,
91+
authors.m_timestamp AS m_timestamp,
92+
authors.date_created AS date_created
93+
FROM authors
94+
",
95+
"
96+
CREATE TABLE posts (
97+
post_id SERIAL PRIMARY KEY,
98+
author_id int NOT NULL,
99+
datetime TIMESTAMP DEFAULT NULL,
100+
title varchar(255) DEFAULT NULL,
101+
body text,
102+
m_timestamp TIMESTAMP NOT NULL,
103+
date_created TIMESTAMP NOT NULL
104+
)
105+
",
106+
],
107+
];
53108

54-
// Table creation method below is sqlite specific,
55-
// won't work with mysql & the others
56-
// $this->pdo->exec("
57-
// CREATE TABLE posts (
58-
// post_id INTEGER PRIMARY KEY,
59-
// author_id INTEGER NOT NULL,
60-
// datetime TEXT,
61-
// title TEXT,
62-
// body TEXT,
63-
// m_timestamp TEXT NOT NULL,
64-
// date_created TEXT NOT NULL,
65-
// FOREIGN KEY(author_id) REFERENCES authors(author_id)
66-
// )
67-
// ");
68-
69-
$schema->create('posts', function(CreateTable $table) {
70-
71-
//add table authors
72-
$table->integer('post_id')->autoincrement();
73-
$table->primary('post_id', 'post_id');
74-
75-
$table->integer('author_id')->notNull();
76-
77-
$table->text('datetime');
78-
$table->text('title');
79-
$table->text('body');
80-
81-
$table->text('m_timestamp')->notNull();
82-
$table->text('date_created')->notNull();
109+
foreach ($create_queries[$driver_name] as $query) {
83110

84-
$table->foreign('author_id')
85-
->references('authors', 'author_id');
86-
});
111+
$pdo->exec($query);
112+
}
87113
}
88114

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

0 commit comments

Comments
 (0)