1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Migrations; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Migration as MigrationRecord; |
6
|
|
|
use Illuminate\Database\Capsule\Manager as DatabaseManager; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Migration. Basic features for migration |
10
|
|
|
* @package Ffcms\Console\Migrations |
11
|
|
|
*/ |
12
|
|
|
class Migration |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
public $now; |
16
|
|
|
/** @var string|null */ |
17
|
|
|
private $connection; |
18
|
|
|
/** @var string */ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Migration constructor. Pass connection name inside if exist |
23
|
|
|
* @param null $connectionName |
24
|
|
|
*/ |
25
|
|
|
public function __construct($migrationName, $connectionName = null) |
26
|
|
|
{ |
27
|
|
|
$this->name = $migrationName; |
28
|
|
|
$this->connection = $connectionName; |
29
|
|
|
$this->now = date('Y-m-d H:i:s', time()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Insert data into migration table |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function up() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$record = new MigrationRecord(); |
38
|
|
|
if ($this->connection !== null) { |
39
|
|
|
$record->setConnection($this->connection); |
40
|
|
|
} |
41
|
|
|
$record->migration = $this->name; |
42
|
|
|
$record->save(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Remove data from migration table |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function down() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$record = new MigrationRecord(); |
51
|
|
|
if ($this->connection !== null) { |
52
|
|
|
$record->setConnection($this->connection); |
53
|
|
|
} |
54
|
|
|
$record->where('migration', $this->name)->delete(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set connection name |
59
|
|
|
* @param string|null $name |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function setConnection($name = null) |
63
|
|
|
{ |
64
|
|
|
$this->connection = $name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get database connection instance |
69
|
|
|
* @return \Illuminate\Database\Connection |
70
|
|
|
*/ |
71
|
|
|
public function getConnection() |
72
|
|
|
{ |
73
|
|
|
return DatabaseManager::connection($this->connection); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get database connection schema builder for current instance of connection |
78
|
|
|
* @return \Illuminate\Database\Schema\Builder |
79
|
|
|
*/ |
80
|
|
|
public function getSchema() |
81
|
|
|
{ |
82
|
|
|
return DatabaseManager::schema($this->connection); |
83
|
|
|
} |
84
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.