Compare commits

...

2 commits

Author SHA1 Message Date
Sebastian Wagner 0db4b5c61d types & cleanups for DB initialization
docker:
dockerfile for php7.3 with pgsql
compose file for db, adminer & php
mocked user table
2021-04-06 21:28:32 +02:00
bernd a80da9dab8 fix typo, db-weiche wieder entfernt 2021-03-20 00:40:34 +01:00
5 changed files with 76 additions and 26 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)
{
@ -158,15 +158,9 @@ class Database {
*/
$this->log->d("Check if table requests exists");
if ($this->pdo->driver === "PDO_PGSQL") {
$stmt = "SELECT * FROM information_schema.tables WHERE
table_type = 'BASE TABLE' and
table_name = 'requests'";
} else if ($this->pdo->driver === "PDO_PSQLITE") {
// ungetestet
$stmt = "SELECT 1 FROM sqlite_master WHERE type='table' and
name='requests'";
}
$stmt = "SELECT * FROM information_schema.tables WHERE
table_type = 'BASE TABLE' and
table_name = 'requests'";
try {
$response = $this->pdo->query($stmt);
} catch (PDOException $e) {
@ -189,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,
@ -197,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,
@ -206,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;
}
@ -234,7 +236,7 @@ class Database {
$userFound = false;
$users = [];
$this->log->d("Search for localpart {$nick} in users");
$query = "SELECT `name` FROM users WHERE `name` = :nick";
$query = "SELECT name FROM users WHERE name = :nick";
$name = "@" . $nick . ":matrix.kraut.space";
$users = $this->searchUser($query, $name);
$count = count($users);
@ -321,7 +323,7 @@ class Database {
$this->log->i("Search for IP: {$_SERVER['REMOTE_ADDR']}");
$stmt = $this->pdo->prepare(
"SELECT `time` FROM requests WHERE ip = :ip"
"SELECT time FROM requests WHERE ip = :ip"
);
try {
$stmt->BindValue(':ip', $ip, PDO::PARAM_LOB);

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