Skip to content

Commit

Permalink
fix migration and env support in develope mode
Browse files Browse the repository at this point in the history
  • Loading branch information
BibaltiK committed Jul 26, 2024
1 parent 53dd054 commit 321b924
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .env.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ENV=dev
APP_ENV=develope
USERMAP_UID=1000
USERMAP_GID=984
MYSQL_USER=dev
Expand Down
14 changes: 10 additions & 4 deletions bin/migrations.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env php
<?php
<?php declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

Expand All @@ -12,28 +12,34 @@
use Doctrine\Migrations\Tools\Console\Command;
use Symfony\Component\Console\Application;

$dbParams = require __DIR__ . '/../config/migrations/migrations.db.php';
$config = require __DIR__ . '/../config/migrations/migrations.setting.php';
$env = getenv('APP_ENV') ?: '';

$dbParams = (require realpath(__DIR__) . sprintf('/../config/autoload/database.%s.php', getenv('APP_ENV') ?: 'global'))['database'];
$dbParams['driver'] = 'pdo_' . $dbParams['driver'];

$config = (require realpath(__DIR__) . sprintf('/../config/autoload/migrations.%s.php', getenv('APP_ENV') ?: 'global'))['migrations'];

$connection = DriverManager::getConnection($dbParams);

$configuration = new Configuration();

$configuration->setCustomTemplate(__DIR__ . '/../config/migrations/migrations.template.tpl');
$configuration->setCustomTemplate(__DIR__ . '/../config/migrations.template.tpl');

$configuration->addMigrationsDirectory('Migrations', $config['migrations_paths']['Migrations']);
$configuration->setAllOrNothing($config['all_or_nothing']);
$configuration->setCheckDatabasePlatform($config['check_database_platform']);
$configuration->setTransactional($config['transactional']);
$configuration->setMigrationOrganization($config['organize_migrations']);


$storageConfiguration = new TableMetadataStorageConfiguration();
$storageConfiguration->setTableName($config['table_storage']['table_name']);
$storageConfiguration->setVersionColumnName($config['table_storage']['version_column_name']);
$storageConfiguration->setVersionColumnLength($config['table_storage']['version_column_length']);
$storageConfiguration->setExecutedAtColumnName($config['table_storage']['executed_at_column_name']);
$storageConfiguration->setExecutionTimeColumnName($config['table_storage']['execution_time_column_name']);


$configuration->setMetadataStorageConfiguration($storageConfiguration);

$container = require_once __DIR__ . '/../config/container.php';
Expand Down
20 changes: 20 additions & 0 deletions config/autoload/database.develope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

return [
'database' => [
'driver' => 'mysql',
'host' => 'stormannsgal-mariadb',
'port' => '3306',
'user' => 'dev',
'password' => 'dev',
'dbname' => 'db',
'charset' => 'utf8mb4',
'defaultTableOptions' => [
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'engine' => 'InnoDB',
],
'error' => PDO::ERRMODE_EXCEPTION,
'emulate_prepares' => false,
]
];
17 changes: 12 additions & 5 deletions config/autoload/database.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

return [
'database' => [
'host' => 'mariadb',
'port' => '3306',
'user' => 'dev',
'password' => 'dev',
'dbname' => 'db',
'driver' => 'driver',
'host' => 'host',
'port' => 'port',
'user' => 'user',
'password' => 'password',
'dbname' => 'dbname',
'charset' => 'utf8mb4',
'defaultTableOptions' => [
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'engine' => 'InnoDB',
],
'error' => PDO::ERRMODE_EXCEPTION,
'emulate_prepares' => false,
]
Expand Down
22 changes: 22 additions & 0 deletions config/autoload/migrations.develope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

return [
'migrations' => [
'table_storage' => [
'table_name' => 'MigrationVersions',
'version_column_name' => 'version',
'version_column_length' => 192,
'executed_at_column_name' => 'executedAt',
'execution_time_column_name' => 'executionTime',
],

'migrations_paths' => [
'Migrations' => __DIR__ . '/../../database/migrations',
],

'all_or_nothing' => true,
'transactional' => true,
'check_database_platform' => true,
'organize_migrations' => 'none',
],
];
22 changes: 22 additions & 0 deletions config/autoload/migrations.global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

return [
'migrations' => [
'table_storage' => [
'table_name' => 'MigrationVersions',
'version_column_name' => 'version',
'version_column_length' => 192,
'executed_at_column_name' => 'executedAt',
'execution_time_column_name' => 'executionTime',
],

'migrations_paths' => [
'Migrations' => __DIR__ . '/../../database/migrations',
],

'all_or_nothing' => true,
'transactional' => true,
'check_database_platform' => true,
'organize_migrations' => 'none',
],
];
10 changes: 7 additions & 3 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
];

$aggregator = new ConfigAggregator([
\Laminas\Log\ConfigProvider::class,
\Mezzio\Tooling\ConfigProvider::class,
\Mezzio\Helper\ConfigProvider::class,
\Mezzio\Router\FastRouteRouter\ConfigProvider::class,
Expand Down Expand Up @@ -41,9 +42,12 @@ class_exists(\Mezzio\Swoole\ConfigProvider::class)
// - `*.global.php`
// - `local.php`
// - `*.local.php`
new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php.dist'),

new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'),
new PhpFileProvider(
realpath(__DIR__) . sprintf(
'/autoload/{,*.}{global,local,%s}.php',
getenv('APP_ENV') ?: 'production'
)
),

// Load development config if it exists
new PhpFileProvider(realpath(__DIR__) . '/development.config.php'),
Expand Down
File renamed without changes.
16 changes: 0 additions & 16 deletions config/migrations/migrations.db.php

This file was deleted.

20 changes: 0 additions & 20 deletions config/migrations/migrations.setting.php

This file was deleted.

3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ services:
container_name: php
user: "${USERMAP_UID:-1000}:${USERMAP_GID:-1000}"
image: "ghcr.io/ownhackathon/hackathon-api-php:latest"
env_file: ".env"
volumes:
- ./:/var/www/html
- ./docker/php/php-ini-overrides.ini:/usr/local/etc/php/conf.d/extra.ini

database:
image: mariadb:${MARIADB_VERSION:-latest}
container_name: mariadb
container_name: stormannsgal-mariadb
ports:
- "${MYSQL_PUBLIC_PORT:-3306}:3306"
environment:
Expand Down
3 changes: 1 addition & 2 deletions src/Core/Factory/DatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class DatabaseFactory
*/
public function __invoke(ContainerInterface $container): PDO
{
$settings = $container->get('config');
$settings = $settings['database'];
$settings = $container->get('config')['database'];

$dsn = $settings['driver'] === 'mysql'
? 'mysql:dbname=' . $settings['dbname'] . ';host=' . $settings['host'] . ';port=' . $settings['port']
Expand Down
1 change: 1 addition & 0 deletions tests/CoreTest/Factory/DatabaseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testThrowPDOException(): void

$config = [
'database' => [
'driver' => 'mysql',
'user' => 'testUser',
'password' => 'testPassword',
'host' => 'https//example.com',
Expand Down

0 comments on commit 321b924

Please sign in to comment.