Skip to content

Commit

Permalink
Update existing transforms' configs
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Feb 4, 2023
1 parent 855a18c commit d81e156
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/migrations/m221027_160703_add_image_transform_fill.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use craft\db\Migration;
use craft\db\Table;
use craft\services\ProjectConfig;

/**
* m221027_160703_add_image_transform_fill migration.
Expand All @@ -18,29 +19,37 @@ public function safeUp(): bool
{
// Place migration code here...
$this->addColumn(Table::IMAGETRANSFORMS, 'fill', $this->string(11)->null()->after('interlace'));
$allowUpscale = Craft::$app->getConfig()->getGeneral()->upscaleImages;
$this->addColumn(Table::IMAGETRANSFORMS, 'upscale', $this->boolean()->notNull()->defaultValue($allowUpscale)->after('fill'));
$this->addColumn(Table::IMAGETRANSFORMS, 'upscale', $this->boolean()->notNull()->defaultValue(true)->after('fill'));

$values = ['stretch', 'fit', 'crop', 'letterbox'];
$modeOptions = ['stretch', 'fit', 'crop', 'letterbox'];
if ($this->db->getIsPgsql()) {
// Manually construct the SQL for Postgres
$check = '[[mode]] in (';
foreach ($values as $i => $value) {
if ($i != 0) {
foreach ($modeOptions as $i => $value) {
if ($i !== 0) {
$check .= ',';
}
$check .= $this->db->quoteValue($value);
}
$check .= ')';
$this->execute("alter table {{%imagetransforms}} drop constraint {{%imagetransforms_mode_check}}, add check ({$check})");
} else {
$this->alterColumn(
Table::IMAGETRANSFORMS,
'mode',
$this->enum('mode', ['stretch', 'fit', 'crop', 'letterbox'])->notNull()->defaultValue('crop'),
);
$this->alterColumn(Table::IMAGETRANSFORMS, 'mode', $this->enum('mode', $modeOptions)->notNull()->defaultValue('crop'));
}

$projectConfig = Craft::$app->getProjectConfig();
$schemaVersion = $projectConfig->get('system.schemaVersion', true);

if (version_compare($schemaVersion, '4.4.0.3', '<')) {
// Hard-code the existing transforms with the current upscaleImages config value
$generalConfig = Craft::$app->getConfig()->getGeneral();
$transforms = $projectConfig->get(ProjectConfig::PATH_IMAGE_TRANSFORMS) ?? [];
foreach ($transforms as $uid => $config) {
$config['upscale'] = $generalConfig->upscaleImages;
$path = sprintf('%s.%s', ProjectConfig::PATH_IMAGE_TRANSFORMS, $uid);
$projectConfig->set($path, $config);
}
}

return true;
}
Expand Down

0 comments on commit d81e156

Please sign in to comment.