types & cleanups for DB initialization

docker:
dockerfile for php7.3 with pgsql
compose file for db, adminer & php
mocked user table
This commit is contained in:
Sebastian Wagner 2021-04-06 21:12:31 +02:00
parent a80da9dab8
commit 0db4b5c61d
5 changed files with 71 additions and 15 deletions

31
docker-compose.yml Normal file
View File

@ -0,0 +1,31 @@
version: '3.3'
# docker-compose -f docker-compose.yml up db adminer app
services:
db:
image: postgres:11-alpine
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: foo
volumes:
- ./mocked-db-table-users.sql:/docker-entrypoint-initdb.d/mocked-db-table-users.sql:ro
ports:
- 5432:5432/tcp
adminer:
image: adminer
ports:
- 8081:8080/tcp
app:
build:
dockerfile: php73.Dockerfile
context: ./
ports:
- 80:80/tcp
volumes:
- ./:/var/www/html:ro
- ./etc/register.ini:/etc/matrix-register/register.ini:ro
#docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php7.3:local

View File

@ -109,5 +109,3 @@ class BaseClass {
}
?>

View File

@ -24,7 +24,7 @@ function testDriver(): bool
return true;
}
function getDatabase(&$config, $log): Database {
function getDatabase(Config $config, Logger $log): Database {
/**
* Erstellt ein Datenbank Objekt und gibt dieses zurück. Im Fehlerfall
@ -49,7 +49,7 @@ class Connection {
private static $conn;
public function connect(&$config)
public function connect(Config $config): PDO
{
$driver = $config->getDbDriver();
@ -79,7 +79,7 @@ class Connection {
return $pdo;
}
public static function get()
public static function get(): self
{
if (null === static::$conn)
{
@ -183,7 +183,8 @@ class Database {
*/
$this->log->n("Try to create table requests");
if ($this->pdo->driver === "PDO_PGSQL") {
$driverName = $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
if ($driverName === "pgsql") {
$stmt = "CREATE TABLE IF NOT EXISTS requests (
id serial PRIMARY KEY,
nick varchar(80) NOT NULL UNIQUE,
@ -191,7 +192,7 @@ class Database {
token char(32) NOT NULL UNIQUE,
ip bytea,
time integer NOT NULL);";
} else if ($this->pdo->driver === "PDO_PSQLITE") {
} else if ($driverName === "sqlite") {
// ungetestet
$stmt = "CREATE TABLE IF NOT EXISTS request (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -200,16 +201,23 @@ class Database {
token TEXT NOT NULL UNIQUE,
ip BLOB,
time INTEGER NOT NULL);";
} else {
$stmt = '';
}
try {
$this->pdo->exec($stmt);
} catch (PDOException $e) {
$this->log->e("Failed to create table requests");
$this->log->e("Error: {$e->getMessage()}");
return false;
if ($stmt) {
try {
$response = $this->pdo->exec($stmt);
} catch (PDOException $e) {
$this->log->e("Failed to create table requests");
$this->log->e("Error: {$e->getMessage()}");
return false;
}
if ($response !== false) {
$this->log->n("Table requests successfull created");
}
$this->log->n("Could not create table requests");
}
$this->log->n("Table requests successfull created");
return true;
return false;
}

View File

@ -0,0 +1,5 @@
CREATE TABLE users (
"name" character varying(30) NOT NULL,
"id" serial NOT NULL,
PRIMARY KEY ("id")
);

14
php73.Dockerfile Normal file
View File

@ -0,0 +1,14 @@
FROM php:7.3-apache
# docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php:7.3-apache
RUN apt-get update && apt-get install -y libpq-dev \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql \
&& apt clean
# docker build --rm --file=php73.Dockerfile -t php7.3:local ./
# docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php:7.3-apache bash
# docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php7.3:local