Passed
Branch master (2f7647)
by Nicolaas
04:56 queued 03:20
created

DatabaseMigrationLegacyYML::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
6
7
/**
8
 * This task adds a legacy branch to the git repo of the original to act as a backup/legacy version for
9
 * holding a version of the module before it was changed
10
 */
11
class DatabaseMigrationLegacyYML extends Task
12
{
13
    protected $taskStep = 's50';
14
15
    public function getTitle()
16
    {
17
        return 'Copy legacy data to database migration file';
18
    }
19
20
    public function getDescription()
21
    {
22
        return '
23
            Take the data from .upgrade.yml and move it to _config/legay.yml with a header.';
24
    }
25
26
    /**
27
     * [runActualTask description]
28
     * @param  array  $params not currently used for this task
29
     * @return [type]         [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
30
     */
31
    public function runActualTask($params = [])
32
    {
33
        foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) {
34
            $oldFile = $moduleDir . '/.upgrade.yml';
35
            $newFile = $moduleDir . '/_config/database.legacy.yml';
36
            $tmpFile = $moduleDir . '/_config/database.legacy.yml.tmp';
37
            $mvStatement = $newFile . ' > ' . $tmpFile . ' && mv ' . $tmpFile . ' ' . $newFile;
38
            if (! file_exists($oldFile)) {
39
                return $oldFile . ' NOT FOUND!!!';
40
            }
41
            $dir = dirname($newFile);
42
            if (! file_exists($dir)) {
43
                mkdir($dir);
44
            }
45
            $this->mu()->execMe(
46
                $moduleDir,
47
                'if test -f ' . $oldFile . '; then cp -vn ' . $oldFile . ' ' . $newFile . '; fi;',
48
                'moving ' . $oldFile . ' to ' . $newFile . ' -v is verbose, -n is only if destination does not exists',
49
                false
50
            );
51
            if (! file_exists($newFile)) {
52
                return 'Could not copy file from ' . $oldFile . ' to ' . $newFile;
53
            }
54
            $this->mu()->execMe(
55
                $moduleDir,
56
                'sed \'1d\' ' . $mvStatement,
57
                'removing the first line and placing into temp file',
58
                false
59
            );
60
            $this->mu()->execMe(
61
                $moduleDir,
62
                "sed -i -e 's/^/  /' " . $newFile,
63
                'adding two additional spaces to the start of each line',
64
                false
65
            );
66
            $this->mu()->execMe(
67
                $moduleDir,
68
                'echo \'  classname_value_remapping:\' | cat - ' . $mvStatement,
69
                'adding `  classname_value_remapping:` to the start of ' . $newFile,
70
                false
71
            );
72
            $this->mu()->execMe(
73
                $moduleDir,
74
                'echo \'SilverStripe\ORM\DatabaseAdmin:\' | cat - ' . $mvStatement,
75
                'adding `SilverStripe\ORM\DatabaseAdmin:` to the start of ' . $newFile,
76
                false
77
            );
78
        }
79
    }
80
81
    protected function hasCommitAndPush()
82
    {
83
        return true;
84
    }
85
}
86