MigrateDataTask   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 21
Bugs 6 Features 0
Metric Value
wmc 6
eloc 21
c 21
b 6
f 0
dl 0
loc 70
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A performMigration() 0 27 6
1
<?php
2
3
namespace Sunnysideup\MigrateData\Tasks;
4
5
use SilverStripe\Core\Config\Config;
6
7
class MigrateDataTask extends MigrateDataTaskBase
8
{
9
    protected $title = 'Migrate Data';
10
11
    protected $description = 'Migrates specific data defined in yml';
12
13
    protected $enabled = true;
14
15
    /**
16
     * an array that is formatted like this:
17
     *     Name => [
18
     *         pre_sql_queries: [
19
     *             - 'SELECT * FROM FOO'
20
     *         ],
21
     *         data => [
22
     *               [
23
     *                   'include_inserts' => true|false, #assumed true if not provided
24
     *                   'old_table' => 'foo',
25
     *                   'new_table' => 'bar' (can be the same!).
26
     *
27
     *                   'simple_move_fields' => ['A', 'B', 'C']
28
     *                       ---  OR ----
29
     *                   'complex_move_fields' => ['A' => 'Anew', 'B' => 'BBew', 'C2' => 'Cnew']
30
     *                ]
31
     *         ]
32
     *         publish_classes => [
33
     *             - MyClassName1
34
     *             - MyClassName2
35
     *         ]
36
     *         post_sql_queries => [
37
     *             - 'SELECT * FROM FOO'
38
     *         ]
39
     *
40
     *     ]
41
     *
42
     * @var array
43
     */
44
    private static $items_to_migrate = [];
45
46
    /**
47
     * Queries the config for Migrate definitions, and runs migrations
48
     * if you extend this task then overwrite it this method.
49
     */
50
    protected function performMigration()
51
    {
52
        $fullList = Config::inst()->get(self::class, 'items_to_migrate');
53
        foreach ($fullList as $item => $details) {
54
            $this->flushNow('<h2>Starting Migration for ' . $item . '</h2>');
55
56
            if (isset($details['pre_sql_queries'])) {
57
                $preSqlQueries = $details['pre_sql_queries'];
58
                $this->runSQLQueries($preSqlQueries, 'PRE');
59
            }
60
61
            if (isset($details['data'])) {
62
                $data = $details['data'];
63
                $this->runMoveData($data);
64
            }
65
66
            if (isset($details['publish_classes'])) {
67
                $publishClasses = $details['publish_classes'];
68
                $this->runPublishClasses($publishClasses);
69
            }
70
71
            if (isset($details['post_sql_queries'])) {
72
                $postSqlQueries = $details['post_sql_queries'];
73
                $this->runSQLQueries($postSqlQueries, 'POST');
74
            }
75
76
            $this->flushNow('<h2>Finish Migration for ' . $item . '</h2>');
77
        }
78
    }
79
}
80