Skip to content

Commit

Permalink
implement database upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Oct 22, 2018
1 parent 0ff9ec9 commit 5f1b7b3
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 115 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ composer.lock
# Ignore configuration files
/config/*
!/config/migrations.php
!/config/migrations.upgrades.php
!/config/api_sample.php

# PHPUnit
Expand Down
1 change: 0 additions & 1 deletion config/migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@

'environments' => [
'default_migration_table' => 'directus_migrations',
'default_database' => 'development'
]
];
14 changes: 14 additions & 0 deletions config/migrations.upgrades.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'paths' => [
'migrations' => '%%PHINX_CONFIG_DIR%%/../migrations/upgrades/schemas',
'seeds' => '%%PHINX_CONFIG_DIR%%/../migrations/upgrades/seeds'
],

'version_order' => 'creation',

'environments' => [
'default_migration_table' => 'directus_migrations',
]
];
159 changes: 159 additions & 0 deletions migrations/upgrades/schemas/20181022175715_upgrade_070003.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

use Phinx\Migration\AbstractMigration;

class Upgrade070003 extends AbstractMigration
{
public function up()
{
// ----------------------------------------------------------------------------
// Fix directus_users.timezone placeholder text
// ----------------------------------------------------------------------------
$options = json_encode([
'choices' => [
'America/Puerto_Rico' => 'Puerto Rico (Atlantic)',
'America/New_York' => 'New York (Eastern)',
'America/Chicago' => 'Chicago (Central)',
'America/Denver' => 'Denver (Mountain)',
'America/Phoenix' => 'Phoenix (MST)',
'America/Los_Angeles' => 'Los Angeles (Pacific)',
'America/Anchorage' => 'Anchorage (Alaska)',
'Pacific/Honolulu' => 'Honolulu (Hawaii)'
],
'placeholder' => 'Choose a timezone...'
]);

$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
['options' => $options],
['collection' => 'directus_users', 'field' => 'timezone']
));

// ----------------------------------------------------------------------------
// Update directus_users.roles interface from "many-to-many" to "user-roles"
// Set directus_users.roles hidden_detail and hidden_browse attribute to "false"
// Update directus_users.roles width to 2
// Update directus_users.roes sort to 8
// ----------------------------------------------------------------------------
$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
[
'interface' => 'user-roles',
'hidden_detail' => 0,
'hidden_browse' => 0,
'width' => 2,
'sort' => 8,
],
[
'collection' => 'directus_users',
'field' => 'roles'
]
));

// ----------------------------------------------------------------------------
// Update directus_users.locale_options hidden_detail attribute to "true"
// ----------------------------------------------------------------------------
$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
[
'hidden_detail' => 1,
],
[
'collection' => 'directus_users',
'field' => 'locale_options'
]
));

// ----------------------------------------------------------------------------
// Update directus_settings "logo" interface from "single_file" to "file"
// ----------------------------------------------------------------------------
$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
[
'interface' => 'file',
],
[
'collection' => 'directus_settings',
'field' => 'logo'
]
));

// ----------------------------------------------------------------------------
// Update directus_user_roles user_id field to user in directus_fields
// ----------------------------------------------------------------------------
$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
[
'field' => 'user',
],
[
'collection' => 'directus_user_roles',
'field' => 'user_id'
]
));

// ----------------------------------------------------------------------------
// Update directus_user_roles role_id field to role in directus_fields
// ----------------------------------------------------------------------------
$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
[
'field' => 'role',
],
[
'collection' => 'directus_user_roles',
'field' => 'role_id'
]
));

// ----------------------------------------------------------------------------
// Update directus_users.password width attribute to 2
// ----------------------------------------------------------------------------
$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
[
'width' => 2,
],
[
'collection' => 'directus_users',
'field' => 'password'
]
));

// ----------------------------------------------------------------------------
// Update directus_users fields sort value
// ----------------------------------------------------------------------------
$fields = [
'company' => 9,
'title' => 10,
'timezone' => 11,
'locale' => 12,
'locale_options' => 13,
'token' => 14,
'last_login' => 15,
'last_access_on' => 16,
'avatar' => 18,
];

foreach ($fields as $field => $sort) {
$this->execute(\Directus\phinx_update(
$this->getAdapter(),
'directus_fields',
[
'sort' => $sort,
],
[
'collection' => 'directus_users',
'field' => $field
]
));
}
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use Directus\Application\Http\Request;
use Directus\Application\Http\Response;
use Directus\Authentication\Exception\UserNotAuthenticatedException;
use Directus\Exception\UnauthorizedException;
use Directus\Permissions\Acl;

class AdminMiddleware extends AbstractMiddleware
class AdminOnlyMiddleware extends AbstractMiddleware
{
public function __invoke(Request $request, Response $response, callable $next)
{
Expand All @@ -18,6 +19,6 @@ public function __invoke(Request $request, Response $response, callable $next)
return $next($request, $response);
}

throw new UserNotAuthenticatedException();
throw new UnauthorizedException('Permission denied: Only Admins');
}
}
13 changes: 12 additions & 1 deletion src/core/Directus/Console/Modules/DatabaseModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Directus\Console\Modules;

use Directus\Util\ArrayUtils;
use Directus\Util\Installation\InstallerUtils;
use Phinx\Config\Config;
use Phinx\Migration\Manager;
use Symfony\Component\Console\Input\StringInput;
Expand Down Expand Up @@ -40,7 +41,17 @@ public function cmdInstall($args, $extra)

public function cmdUpgrade($args, $extra)
{
$this->runMigration('upgrades');
$project = null;

foreach ($args as $key => $value) {
switch ($key) {
case 'N':
$project = $value;
break;
}
}

InstallerUtils::updateTables($this->getBasePath(), $project);
}

protected function runMigration($name)
Expand Down
39 changes: 36 additions & 3 deletions src/core/Directus/Util/Installation/InstallerUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ public static function createTables($basePath, $env = null, $force = false)
static::dropTables($basePath, $env);
}

static::runMigrationAndSeeder($config);
}

/**
* Update Directus Tables from Migrations
*
* @param string $basePath
* @param string $env
*
* @throws \Exception
*/
public static function updateTables($basePath, $env = null)
{
$config = static::getMigrationConfig($basePath, $env, 'upgrades');

static::runMigrationAndSeeder($config);
}

/**
* Create Directus Tables from Migrations
*
* @param Config $config
*
* @throws \Exception
*/
public static function runMigrationAndSeeder(Config $config)
{
$manager = new Manager($config, new StringInput(''), new NullOutput());
$manager->migrate('development');
$manager->seed('development');
Expand Down Expand Up @@ -540,20 +567,26 @@ private static function createConfigFile($path, array $data, $force = false)
/**
* @param string $basePath
* @param string $projectName
* @param string|null $migrationName
*
* @return Config
*/
private static function getMigrationConfig($basePath, $projectName = null)
private static function getMigrationConfig($basePath, $projectName = null, $migrationName = null)
{
static::ensureConfigFileExists($basePath, $projectName);
static::ensureMigrationFileExists($basePath);

if ($migrationName === null) {
$migrationName = 'db';
}

$configPath = static::createConfigPath($basePath, $projectName);
$migrationPath = $basePath . '/migrations/' . $migrationName;

$apiConfig = require $configPath;
$configArray = require $basePath . '/config/migrations.php';
$configArray['paths']['migrations'] = $basePath . '/migrations/db/schemas';
$configArray['paths']['seeds'] = $basePath . '/migrations/db/seeds';
$configArray['paths']['migrations'] = $migrationPath . '/schemas';
$configArray['paths']['seeds'] = $migrationPath . '/seeds';
$configArray['environments']['development'] = [
'adapter' => ArrayUtils::get($apiConfig, 'database.type'),
'host' => ArrayUtils::get($apiConfig, 'database.host'),
Expand Down
20 changes: 20 additions & 0 deletions src/endpoints/ProjectUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Directus\Api\Routes;

use Directus\Application\Http\Request;
use Directus\Application\Http\Response;
use Directus\Application\Route;
use function Directus\get_api_project_from_request;
use Directus\Util\Installation\InstallerUtils;

class ProjectUpdate extends Route
{
public function __invoke(Request $request, Response $response)
{
InstallerUtils::updateTables($this->container->get('path_base'), get_api_project_from_request());

$response = $response->withStatus(204);
return $this->responseWithData($request, $response, []);
}
}
32 changes: 32 additions & 0 deletions src/helpers/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Directus\Hook\Emitter;
use Directus\Util\ArrayUtils;
use Directus\Util\StringUtils;
use Phinx\Db\Adapter\AdapterInterface;

require __DIR__ . '/constants.php';
require __DIR__ . '/app.php';
Expand Down Expand Up @@ -1521,3 +1522,34 @@ function env($key, $default = null)
return $value;
}
}

if (!function_exists('phinx_update')) {
/**
* @param AdapterInterface $adapter
* @param string $table
* @param array $data
* @param array $conditions
*
* @return string
*/
function phinx_update(AdapterInterface $adapter, $table, array $data, array $conditions)
{
$processColumns = function ($data, $glue = ',') use ($adapter) {
$list = [];
foreach ($data as $column => $value) {
if (is_string($value)) {
$value = sprintf('%s', $adapter->getConnection()->quote($value));
}

$list[] = sprintf('%s = %s', $adapter->quoteColumnName($column), $value);
}

return implode($glue, $list);
};

$set = $processColumns($data);
$where = $processColumns($conditions, ' AND ');

return sprintf('UPDATE %s SET %s WHERE %s;', $adapter->quoteTableName($table), $set, $where);
}
}
Loading

0 comments on commit 5f1b7b3

Please sign in to comment.