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 ;
17
+ use TheCodingMachine \TDBM \Utils \RootProjectLocator ;
15
18
16
19
/**
17
20
* This class is used to analyze the schema and return valuable information / hints.
18
21
*/
19
22
class TDBMSchemaAnalyzer
20
23
{
24
+ const schemaFileName = 'tdbm.lock.yml ' ;
25
+
21
26
private $ connection ;
22
27
23
28
/**
@@ -40,6 +45,11 @@ class TDBMSchemaAnalyzer
40
45
*/
41
46
private $ schemaAnalyzer ;
42
47
48
+ /**
49
+ * @var SchemaVersionControlService
50
+ */
51
+ private $ schemaVersionControlService ;
52
+
43
53
/**
44
54
* @param Connection $connection The DBAL DB connection to use
45
55
* @param Cache $cache A cache service to be used
@@ -51,6 +61,7 @@ public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer
51
61
$ this ->connection = $ connection ;
52
62
$ this ->cache = $ cache ;
53
63
$ this ->schemaAnalyzer = $ schemaAnalyzer ;
64
+ $ this ->schemaVersionControlService = new SchemaVersionControlService ($ this ->connection , self ::getLockFilePath ());
54
65
}
55
66
56
67
/**
@@ -67,53 +78,35 @@ public function getCachePrefix(): string
67
78
return $ this ->cachePrefix ;
68
79
}
69
80
81
+ public static function getLockFilePath (): string
82
+ {
83
+ return RootProjectLocator::getRootLocationPath ().self ::schemaFileName;
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 (self ::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 ();
117
110
}
118
111
119
112
/**
0 commit comments