Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Statamic Migrator

![Statamic 4.0+](https://img.shields.io/badge/Statamic-4.0+-FF269E?style=for-the-badge&link=https://statamic.com)

🤘 Make migrating from v2 all the moar easier!

📺 See the migrator in action [in this screencast](https://youtu.be/OeXbaeuJrws).
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"mustangostang/spyc": "dev-master#dfd9aadc1f5224065d55b42b712c7e99a50a3f4d"
},
"require-dev": {
"statamic/cms": "^5.41",
"statamic/cms": "^v6.0.0-alpha.20",
"mockery/mockery": "^1.4.4",
"orchestra/testbench": "^8.28 || ^9.6.1 || ^10.0",
"phpunit/phpunit": "^10.5.35 || ^11.0"
"orchestra/testbench": "^10.0",
"phpunit/phpunit": "^11.0"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 17 additions & 0 deletions src/ContentMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Statamic\Migrator;

use Carbon\Carbon;
use Illuminate\Filesystem\Filesystem;
use Statamic\Fieldtypes\Date;
use Statamic\Migrator\Exceptions\EmptyValueException;
use Statamic\Support\Arr;
use Statamic\Support\Str;
Expand Down Expand Up @@ -207,6 +209,21 @@ protected function migrateField($handle, $value, $config = null)
return $value;
}

protected function migrateDateField($handle, $value, $config)
{
if (($originalTimezone = $this->getSetting('system.timezone', 'UTC')) !== 'UTC') {
$value = Carbon::parse($value, $originalTimezone)
->utc()
->format($config['format'] ?? Date::DEFAULT_DATETIME_FORMAT);

if (is_numeric($value)) {
$value = (int) $value;
}
}

return $value;
}

/**
* Migrate assets field.
*
Expand Down
27 changes: 4 additions & 23 deletions src/GlobalSetMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,8 @@ protected function migrateGlobalSetSchema()
->forget('fieldset')
->forget('id');

$meta = $this->set->only($metaKeys);
$data = $this->set->except($metaKeys);

if ($this->isMultisite()) {
$this->set = $meta;
$this->localizedSets = $this->migrateLocalizedSets($data);
} else {
$this->set = $meta->put('data', $this->migrateSetData($data));
}
$this->localizedSets = $this->migrateLocalizedSets($this->set->except($metaKeys));
$this->set = $this->set->only($metaKeys);

return $this;
}
Expand Down Expand Up @@ -125,24 +118,12 @@ protected function migrateSetData($data)
* @return $this
*/
protected function saveMigratedSet()
{
if ($this->isMultisite()) {
$this->saveLocalizedSets();
}

return $this->saveMigratedYaml($this->set);
}

/**
* Save migrated localized sets.
*
* @return $this
*/
protected function saveLocalizedSets()
{
collect($this->localizedSets)->each(function ($set, $locale) {
$this->saveMigratedYaml($set, base_path("content/globals/{$locale}/{$this->handle}.yaml"));
});

return $this->saveMigratedYaml($this->set);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/SettingsMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected function migrateCp()

Configurator::file($configFile = 'statamic/cp.php')
->set('start_page', $this->migrateStartPage($cp['start_page'] ?? false))
->set('date_format', $cp['date_format'] ?? false)
->merge('widgets', $cp['widgets'] ?? [])
->set('pagination_size', $cp['pagination_size'] ?? false)
->ifNoChanges($this->throwNoChangesException($configFile));
Expand Down Expand Up @@ -125,6 +124,7 @@ protected function migrateSystem()

Configurator::file($configFile = 'statamic/system.php')
->set('multisite', count($sites) > 1)
->set('display_timezone', $system['timezone'] ?? null)
->ifNoChanges($this->throwNoChangesException($configFile));

return $this;
Expand Down
90 changes: 90 additions & 0 deletions tests/ContentMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,96 @@ public function it_can_migrate_assets_fields_without_container_url()
$this->assertEquals($expected, $content);
}

#[Test]
public function it_can_migrate_date_field()
{
$content = $this
->setFields([
'date_field' => [
'type' => 'date',
],
'date_field_with_format' => [
'type' => 'date',
'format' => 'Y/m/d',
],
'date_field_with_time' => [
'type' => 'date',
'allow_time' => true,
],
'date_field_with_time_and_format' => [
'type' => 'date',
'allow_time' => true,
'format' => 'Y/m/d H:i',
],
])
->migrateContent([
'date_field' => '2018-01-01',
'date_field_with_format' => '2018/01/01',
'date_field_with_time' => '2018-01-01 12:00',
'date_field_with_time_and_format' => '2018/01/01 12:00',
]);

$expected = [
'date_field' => '2018-01-01',
'date_field_with_format' => '2018/01/01',
'date_field_with_time' => '2018-01-01 12:00',
'date_field_with_time_and_format' => '2018/01/01 12:00',
'blueprint' => 'speaker'
];

$this->assertEquals($expected, $content);

$this->files->delete($this->sitePath('settings/system.yaml'));
}

#[Test]
public function it_can_migrate_date_field_when_previous_timezone_was_not_utc()
{
// -5-hour offset
$this->files->put($this->sitePath('settings/system.yaml'), <<<EOT
timezone: 'America/New_York'
EOT
);

$content = $this
->setFields([
'date_field' => [
'type' => 'date',
],
'date_field_with_format' => [
'type' => 'date',
'format' => 'Y/m/d',
],
'date_field_with_time' => [
'type' => 'date',
'allow_time' => true,
],
'date_field_with_time_and_format' => [
'type' => 'date',
'allow_time' => true,
'format' => 'Y/m/d H:i',
],
])
->migrateContent([
'date_field' => '2018-01-01',
'date_field_with_format' => '2018/01/01',
'date_field_with_time' => '2018-01-01 12:00',
'date_field_with_time_and_format' => '2018/01/01 12:00',
]);

$expected = [
'date_field' => '2018-01-01 05:00',
'date_field_with_format' => '2018/01/01',
'date_field_with_time' => '2018-01-01 17:00',
'date_field_with_time_and_format' => '2018/01/01 17:00',
'blueprint' => 'speaker'
];

$this->assertEquals($expected, $content);

$this->files->delete($this->sitePath('settings/system.yaml'));
}

#[Test]
public function it_can_migrate_term_fields()
{
Expand Down
30 changes: 21 additions & 9 deletions tests/MigrateGlobalSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ protected function newPath()
return Path::tidy(base_path('content/globals/main.yaml'));
}

protected function variablesPath()
{
return Path::tidy(base_path('content/globals/default/main.yaml'));
}

protected function blueprintPath()
{
return Path::tidy(resource_path('blueprints/globals/main.yaml'));
Expand All @@ -34,23 +39,26 @@ protected function blueprintPath()
private function migrateGlobalSet($globalSet)
{
$this->assertFileNotExists($this->newPath());
$this->assertFileNotExists($this->variablesPath());

$this->files->put($this->originalPath(), YAML::dump($globalSet));

$this->artisan('statamic:migrate:global-set', ['handle' => 'main']);

$this->assertFileExists($this->newPath());
$this->assertFileExists($this->variablesPath());

return YAML::parse($this->files->get($this->newPath()));
return [YAML::parse($this->files->get($this->newPath())), YAML::parse($this->files->get($this->variablesPath()))];
}

#[Test]
public function it_can_migrate_a_global_set()
{
$this->assertFileNotExists($this->newPath());
$this->assertFileNotExists($this->variablesPath());
$this->assertFileNotExists($this->blueprintPath());

$set = $this->migrateGlobalSet([
[$set, $variables] = $this->migrateGlobalSet([
'id' => '547c5873-ce9a-4b92-b6b8-a9c785f92fb4',
'title' => 'Global',
'fieldset' => 'globals',
Expand All @@ -61,14 +69,16 @@ public function it_can_migrate_a_global_set()

$this->assertEquals($set, [
'title' => 'Global',
'data' => [
'site_title' => 'Frederick\'s Swap Shop',
'author' => 'Frederick Schwap',
'fav_colour' => 'red',
],
]);

$this->assertEquals($variables, [
'site_title' => 'Frederick\'s Swap Shop',
'author' => 'Frederick Schwap',
'fav_colour' => 'red',
]);

$this->assertFileExists($this->newPath());
$this->assertFileExists($this->variablesPath());
$this->assertFileExists($this->blueprintPath());
}

Expand All @@ -77,7 +87,7 @@ public function it_can_implicitly_migrate_globals_blueprint()
{
$this->assertFileNotExists($this->blueprintPath());

$set = $this->migrateGlobalSet([
$this->migrateGlobalSet([
'id' => '547c5873-ce9a-4b92-b6b8-a9c785f92fb4',
'title' => 'Global',
'site_title' => 'Frederick\'s Swap Shop',
Expand All @@ -94,15 +104,17 @@ public function it_migrates_without_fieldset_when_one_does_not_exist()
$this->files->delete(base_path('site/settings/fieldsets/globals.yaml'));

$this->assertFileNotExists($this->newPath());
$this->assertFileNotExists($this->variablesPath());
$this->assertFileNotExists($this->blueprintPath());

$set = $this->migrateGlobalSet([
$this->migrateGlobalSet([
'title' => 'Global',
'site_title' => 'Frederick\'s Swap Shop',
'author' => 'Frederick Schwap',
]);

$this->assertFileExists($this->newPath());
$this->assertFileExists($this->variablesPath());
$this->assertFileNotExists($this->blueprintPath());
}
}
29 changes: 21 additions & 8 deletions tests/MigrateSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function it_doesnt_touch_assets_config_if_empty()
public function it_migrates_cp_settings()
{
$this->assertConfigFileContains('cp.php', <<<'EOT'
'getting_started',
//
EOT
);

Expand All @@ -84,14 +84,9 @@ public function it_migrates_cp_settings()
EOT
);

$this->assertConfigFileContains('cp.php', <<<'EOT'
'date_format' => 'Y-m-d',
EOT
);

$this->assertConfigFileContains('cp.php', <<<'EOT'
'widgets' => [
'getting_started',
//
[
'type' => 'collection',
'collection' => 'blog',
Expand Down Expand Up @@ -161,6 +156,11 @@ public function it_migrates_routes()
#[Test]
public function it_migrates_system_settings()
{
$this->files->put($this->sitePath('settings/system.yaml'), <<<'EOT'
timezone: 'America/New_York'
EOT
);

$this->artisan('statamic:migrate:settings', ['handle' => 'system']);

$this->assertSameWithNormalizedLineEndings(File::get(resource_path('sites.yaml')),
Expand All @@ -172,6 +172,11 @@ public function it_migrates_system_settings()

$this->assertConfigFileContains('system.php', <<<'EOT'
'multisite' => false,
EOT
);

$this->assertConfigFileContains('system.php', <<<'EOT'
'display_timezone' => 'America/New_York',
EOT
);
}
Expand Down Expand Up @@ -282,11 +287,19 @@ protected function assertConfigFileContains($file, $content)
{
$config = config_path("statamic/{$file}");

$beginning = <<<'EOT'
if ($file === 'cp.php') {
$beginning = <<<'EOT'
<?php

return [
EOT;
} else {
$beginning = <<<'EOT'
<?php

return [
EOT;
}

$end = '];';

Expand Down
4 changes: 2 additions & 2 deletions tests/MigrateSiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ public function it_migrates_groups()
#[Test]
public function it_migrates_settings()
{
$this->assertCount(1, config('statamic.cp.widgets'));
$this->assertCount(0, config('statamic.cp.widgets'));

$this->artisan('statamic:migrate:site');

Configurator::file('statamic/cp.php')->refresh();

$this->assertCount(4, config('statamic.cp.widgets'));
$this->assertCount(3, config('statamic.cp.widgets'));
}

#[Test]
Expand Down