Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Statamic Beta 45 Fields Migration Helper

Upgrading fields for changes in Beta 45

Instructions:

  • Upgrade to Statamic 3.0.0-beta.45
  • Copy the contents of this gist's console.php into routes/console.php
  • Run php artisan migrate:beta45
  • Delete the command you just copied. It was only temporary.

To confirm it worked:

  • Any fields with type: taxonomy should have been changed to type: terms
  • Those same fields, if they had taxonomy config keys, should have been renamed to taxonomies.
  • Any fields with type: tags should have been changed to type: taggable
<?php // copy everything after this line
use Illuminate\Support\Facades\Artisan;
use Statamic\Facades\File;
use Statamic\Facades\YAML;
class MigrateBetaFortyFive
{
protected $console;
public function __construct($console)
{
$this->console = $console;
}
public function migrate()
{
$this->console->line('<comment>[!]</comment> Any <comment>type: taxonomy</comment> fields will be renamed to <comment>type: terms</comment> and their <comment>taxonomy:</comment> options will be renamed to <comment>taxonomies</comment>.');
$this->console->line('<comment>[!]</comment> Any <comment>type: tags</comment> fields will be renamed to <comment>type: taggable</comment>.');
$blueprints = File::getFilesByTypeRecursively(resource_path('blueprints'), 'yaml');
$fieldsets = File::getFilesByTypeRecursively(resource_path('fieldsets'), 'yaml');
$files = $blueprints->merge($fieldsets);
$converted = $files->reduce(function ($carry, $path) {
$yaml = YAML::file($path)->parse();
$newYaml = $this->updateYaml($yaml);
if ($yaml != $newYaml) {
File::put($path, YAML::dump($newYaml));
$this->console->line("<info>[✓]</info> $path");
return ++$carry;
}
return $carry;
}, 0);
$this->console->info($converted . ' file(s) updated.');
}
protected function updateYaml($array)
{
foreach ($array as $key => &$value) {
if (is_array($value)) {
$value = $this->updateYaml($value);
}
if ($key === 'type' && $value == 'taxonomy') {
$array = $this->replaceTaxonomy($array);
}
if ($key === 'type' && $value == 'tags') {
$array['type'] = 'taggable';
}
}
return $array;
}
protected function replaceTaxonomy($array)
{
if (isset($array['taxonomy'])) {
$array['taxonomies'] = $array['taxonomy'];
unset($array['taxonomy']);
}
$array['type'] = 'terms';
return $array;
}
}
Artisan::command('migrate:beta45', function () {
(new MigrateBetaFortyFive($this))->migrate();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.