Skip to content

Commit

Permalink
Started laying groundwork for using Phinx instead of home-grown DB mi…
Browse files Browse the repository at this point in the history
…grations/seeders
  • Loading branch information
davidbyoung committed Aug 10, 2023
1 parent 000621d commit c349f2b
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 325 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"ext-pdo": "*",
"ext-sqlite3": "*",
"php": ">=8.2",
"robmorgan/phinx": "1.x-dev",
"symfony/dotenv": "^6.1"
},
"require-dev": {
Expand Down
Empty file removed database/.gitkeep
Empty file.
25 changes: 25 additions & 0 deletions database/migrations/20230907120000_auth_token_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

/**
* Defines the auth token migration
*/
class AuthTokenMigration extends AbstractMigration
{
/**
* Creates the tables necessary for auth tokens
*/
public function change(): void
{
$this->table('auth_tokens')
->addColumn('user_id', 'integer', ['null' => false])
->addColumn('hashed_token', 'text', ['null' => false])
->addColumn('expiration', 'integer', ['null' => false])
->addForeignKey('user_id', 'users', 'id')
->addIndex(['user_id', 'hashed_token'])
->create();
}
}
29 changes: 29 additions & 0 deletions database/migrations/20230907120001_user_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

/**
* Defines the user migration
*/
class UserMigration extends AbstractMigration
{
/**
* Creates the tables necessary for users and roles
*/
public function change(): void
{
$this->table('users')
->addColumn('email', 'text', ['null' => false])
->addColumn('hashed_password', 'text', ['null' => false])
->addIndex('email', ['unique' => true])
->create();

$this->table('user_roles')
->addColumn('user_id', 'integer', ['null' => false])
->addColumn('role', 'text', ['null' => false])
->addForeignKey('user_id', 'users', 'id')
->create();
}
}
42 changes: 42 additions & 0 deletions database/seeds/UserSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use Phinx\Seed\AbstractSeed;

/**
* Defines the user seeder
*/
class UserSeeder extends AbstractSeed
{
/**
* @inheritdoc
*/
public function run(): void
{
// Create the default user if they do not already exist
/** @var PDOStatement $queryForDefaultUser */
$queryForDefaultUser = $this->query(
'SELECT * FROM users WHERE email = :email',
['email' => \getenv('USER_DEFAULT_EMAIL')]
);

if (empty($queryForDefaultUser->fetchAll())) {
$this->insert(
'users',
[
'email' => (string)\getenv('USER_DEFAULT_EMAIL'),
'hashed_password' => \password_hash((string)\getenv('USER_DEFAULT_PASSWORD'), PASSWORD_ARGON2ID)
]
);
$this->insert(
'user_roles',
[
'user_id' => $this->getAdapter()->getConnection()->lastInsertId(),
'role' => 'admin'
]
);
}
echo 'here';
}
}
37 changes: 37 additions & 0 deletions phinx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use Aphiria\Application\Configuration\Bootstrappers\DotEnvBootstrapper;
use Aphiria\DependencyInjection\Container;
use Aphiria\DependencyInjection\IContainer;
use Aphiria\DependencyInjection\IServiceResolver;
use App\Database\Binders\DatabaseBinder;

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

// Create our DI container
$container = new Container();
Container::$globalInstance = $container;
$container->bindInstance([IServiceResolver::class, IContainer::class, Container::class], $container);

// Ensure our environment variables are set
(new DotEnvBootstrapper(__DIR__ . '/.env'))->bootstrap();
// Ensure our database connection is configured
(new DatabaseBinder())->bind($container);

return [
'paths' => [
'migrations' => '%%PHINX_CONFIG_DIR%%/database/migrations',
'seeds' => '%%PHINX_CONFIG_DIR%%/database/seeds'
],
'environments' => [
'default_migration_table' => 'phinxlog',
'default_environment' => 'development',
'development' => [
'name' => 'database',
'connection' => $container->resolve(PDO::class)
]
],
'version_order' => 'creation'
];
4 changes: 0 additions & 4 deletions src/Auth/AuthModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@
use Aphiria\Net\Http\Headers\SameSiteMode;
use Aphiria\Net\Http\HttpStatusCode;
use App\Auth\Binders\AuthServiceBinder;
use App\Database\Components\DatabaseComponents;

/**
* Defines the auth module
*/
final class AuthModule extends AphiriaModule
{
use DatabaseComponents;

/**
* @inheritdoc
*/
public function configure(IApplicationBuilder $appBuilder): void
{
$this->withBinders($appBuilder, new AuthServiceBinder())
->withDatabaseSeeders($appBuilder, SqlTokenSeeder::class)
// Add our default authentication scheme
->withAuthenticationScheme(
$appBuilder,
Expand Down
45 changes: 0 additions & 45 deletions src/Auth/SqlTokenSeeder.php

This file was deleted.

36 changes: 0 additions & 36 deletions src/Database/Components/DatabaseComponents.php

This file was deleted.

54 changes: 0 additions & 54 deletions src/Database/Components/DatabaseSeederComponent.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/Database/Console/DatabaseSeederCommandHandler.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Database/GlobalDatabaseSeeder.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Database/IDatabaseSeeder.php

This file was deleted.

Loading

0 comments on commit c349f2b

Please sign in to comment.