3
3
4
4
namespace TheCodingMachine \TDBM ;
5
5
6
+ use BrainDiminished \SchemaVersionControl \SchemaVersionControlService ;
6
7
use Doctrine \Common \Cache \Cache ;
7
8
use Doctrine \DBAL \Connection ;
8
9
use Doctrine \DBAL \Schema \Column ;
12
13
use Doctrine \DBAL \Types \DateType ;
13
14
use Doctrine \DBAL \Types \Type ;
14
15
use Mouf \Database \SchemaAnalyzer \SchemaAnalyzer ;
16
+ use TheCodingMachine \TDBM \Utils \ImmutableCaster ;
15
17
16
18
/**
17
19
* This class is used to analyze the schema and return valuable information / hints.
18
20
*/
19
21
class TDBMSchemaAnalyzer
20
22
{
23
+ private $ lockFilePath ;
21
24
private $ connection ;
22
25
23
26
/**
@@ -41,16 +44,24 @@ class TDBMSchemaAnalyzer
41
44
private $ schemaAnalyzer ;
42
45
43
46
/**
44
- * @param Connection $connection The DBAL DB connection to use
45
- * @param Cache $cache A cache service to be used
47
+ * @var SchemaVersionControlService
48
+ */
49
+ private $ schemaVersionControlService ;
50
+
51
+ /**
52
+ * @param Connection $connection The DBAL DB connection to use
53
+ * @param Cache $cache A cache service to be used
46
54
* @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths...
47
55
* Will be automatically created if not passed
56
+ * @param string $lockFilePath The path for the lock file which will store the database schema
48
57
*/
49
- public function __construct (Connection $ connection , Cache $ cache , SchemaAnalyzer $ schemaAnalyzer )
58
+ public function __construct (Connection $ connection , Cache $ cache , SchemaAnalyzer $ schemaAnalyzer, string $ lockFilePath )
50
59
{
51
60
$ this ->connection = $ connection ;
52
61
$ this ->cache = $ cache ;
53
62
$ this ->schemaAnalyzer = $ schemaAnalyzer ;
63
+ $ this ->lockFilePath = $ lockFilePath ;
64
+ $ this ->schemaVersionControlService = new SchemaVersionControlService ($ this ->connection , $ this ->lockFilePath );
54
65
}
55
66
56
67
/**
@@ -67,53 +78,36 @@ public function getCachePrefix(): string
67
78
return $ this ->cachePrefix ;
68
79
}
69
80
81
+ public function getLockFilePath (): string
82
+ {
83
+ return $ this ->lockFilePath ;
84
+ }
85
+
70
86
/**
71
87
* Returns the (cached) schema.
72
- *
73
- * @return Schema
74
88
*/
75
- public function getSchema (): Schema
89
+ public function getSchema (bool $ ignoreCache = false ): Schema
76
90
{
77
91
if ($ this ->schema === null ) {
78
92
$ cacheKey = $ this ->getCachePrefix ().'_immutable_schema ' ;
79
- if ($ this ->cache ->contains ($ cacheKey )) {
93
+ if (! $ ignoreCache && $ this ->cache ->contains ($ cacheKey )) {
80
94
$ this ->schema = $ this ->cache ->fetch ($ cacheKey );
95
+ } elseif (!file_exists ($ this ->getLockFilePath ())) {
96
+ throw new TDBMException ('No tdbm lock file found. Please regenerate DAOs and Beans. ' );
81
97
} else {
82
- $ this ->schema = $ this ->connection -> getSchemaManager ()-> createSchema ();
83
- $ this -> castSchemaToImmutable ($ this ->schema );
98
+ $ this ->schema = $ this ->schemaVersionControlService -> loadSchemaFile ();
99
+ ImmutableCaster:: castSchemaToImmutable ($ this ->schema );
84
100
$ this ->cache ->save ($ cacheKey , $ this ->schema );
85
101
}
86
102
}
87
103
88
104
return $ this ->schema ;
89
105
}
90
106
91
- private function castSchemaToImmutable ( Schema $ schema ): void
107
+ public function generateLockFile ( ): void
92
108
{
93
- foreach ($ schema ->getTables () as $ table ) {
94
- foreach ($ table ->getColumns () as $ column ) {
95
- $ this ->toImmutableType ($ column );
96
- }
97
- }
98
- }
99
-
100
- /**
101
- * Changes the type of a column to an immutable date type if the type is a date.
102
- * This is needed because by default, when reading a Schema, Doctrine assumes a mutable datetime.
103
- */
104
- private function toImmutableType (Column $ column ): void
105
- {
106
- $ mapping = [
107
- Type::DATE => Type::DATE_IMMUTABLE ,
108
- Type::DATETIME => Type::DATETIME_IMMUTABLE ,
109
- Type::DATETIMETZ => Type::DATETIMETZ_IMMUTABLE ,
110
- Type::TIME => Type::TIME_IMMUTABLE
111
- ];
112
-
113
- $ typeName = $ column ->getType ()->getName ();
114
- if (isset ($ mapping [$ typeName ])) {
115
- $ column ->setType (Type::getType ($ mapping [$ typeName ]));
116
- }
109
+ $ this ->schemaVersionControlService ->dumpSchema ();
110
+ \chmod ($ this ->getLockFilePath (), 0664 );
117
111
}
118
112
119
113
/**
0 commit comments