1
|
|
|
<?php namespace Xethron\MigrationsGenerator\Generators; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\DB; |
4
|
|
|
|
5
|
|
|
class SchemaGenerator { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager |
9
|
|
|
*/ |
10
|
|
|
protected $schema; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var FieldGenerator |
14
|
|
|
*/ |
15
|
|
|
protected $fieldGenerator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ForeignKeyGenerator |
19
|
|
|
*/ |
20
|
|
|
protected $foreignKeyGenerator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $database; |
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $ignoreIndexNames; |
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $ignoreForeignKeyNames; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $database |
37
|
|
|
* @param bool $ignoreIndexNames |
38
|
|
|
* @param bool $ignoreForeignKeyNames |
39
|
|
|
*/ |
40
|
|
|
public function __construct($database, $ignoreIndexNames, $ignoreForeignKeyNames) |
41
|
|
|
{ |
42
|
|
|
$connection = DB::connection($database)->getDoctrineConnection(); |
43
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('json', 'text'); |
44
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('jsonb', 'text'); |
45
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
46
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('bit', 'boolean'); |
47
|
|
|
|
48
|
|
|
// Postgres types |
49
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('_text', 'text'); |
50
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('_int4', 'integer'); |
51
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('_numeric', 'float'); |
52
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('cidr', 'string'); |
53
|
|
|
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('inet', 'string'); |
54
|
|
|
|
55
|
|
|
$this->database = $connection->getDatabase(); |
56
|
|
|
|
57
|
|
|
$this->schema = $connection->getSchemaManager(); |
58
|
|
|
$this->fieldGenerator = new FieldGenerator(); |
59
|
|
|
$this->foreignKeyGenerator = new ForeignKeyGenerator(); |
60
|
|
|
|
61
|
|
|
$this->ignoreIndexNames = $ignoreIndexNames; |
62
|
|
|
$this->ignoreForeignKeyNames = $ignoreForeignKeyNames; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function getTables() |
69
|
|
|
{ |
70
|
|
|
return $this->schema->listTableNames(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getFields($table) |
74
|
|
|
{ |
75
|
|
|
return $this->fieldGenerator->generate($table, $this->schema, $this->database, $this->ignoreIndexNames); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getForeignKeyConstraints($table) |
79
|
|
|
{ |
80
|
|
|
return $this->foreignKeyGenerator->generate($table, $this->schema, $this->ignoreForeignKeyNames); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|