-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started laying groundwork for using Phinx instead of home-grown DB mi…
…grations/seeders
- Loading branch information
1 parent
000621d
commit c349f2b
Showing
19 changed files
with
139 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
25 changes: 25 additions & 0 deletions
25
database/migrations/20230907120000_auth_token_migration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.