33
44namespace TheCodingMachine \TDBM ;
55
6+ use BrainDiminished \SchemaVersionControl \SchemaVersionControlService ;
67use Doctrine \Common \Cache \Cache ;
78use Doctrine \DBAL \Connection ;
89use Doctrine \DBAL \Schema \Column ;
1213use Doctrine \DBAL \Types \DateType ;
1314use Doctrine \DBAL \Types \Type ;
1415use Mouf \Database \SchemaAnalyzer \SchemaAnalyzer ;
16+ use TheCodingMachine \TDBM \Utils \ImmutableCaster ;
1517
1618/**
1719 * This class is used to analyze the schema and return valuable information / hints.
1820 */
1921class TDBMSchemaAnalyzer
2022{
23+ private $ lockFilePath ;
2124 private $ connection ;
2225
2326 /**
@@ -41,16 +44,24 @@ class TDBMSchemaAnalyzer
4144 private $ schemaAnalyzer ;
4245
4346 /**
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
4654 * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths...
4755 * Will be automatically created if not passed
56+ * @param string $lockFilePath The path for the lock file which will store the database schema
4857 */
49- public function __construct (Connection $ connection , Cache $ cache , SchemaAnalyzer $ schemaAnalyzer )
58+ public function __construct (Connection $ connection , Cache $ cache , SchemaAnalyzer $ schemaAnalyzer, string $ lockFilePath )
5059 {
5160 $ this ->connection = $ connection ;
5261 $ this ->cache = $ cache ;
5362 $ this ->schemaAnalyzer = $ schemaAnalyzer ;
63+ $ this ->lockFilePath = $ lockFilePath ;
64+ $ this ->schemaVersionControlService = new SchemaVersionControlService ($ this ->connection , $ this ->lockFilePath );
5465 }
5566
5667 /**
@@ -67,53 +78,36 @@ public function getCachePrefix(): string
6778 return $ this ->cachePrefix ;
6879 }
6980
81+ public function getLockFilePath (): string
82+ {
83+ return $ this ->lockFilePath ;
84+ }
85+
7086 /**
7187 * Returns the (cached) schema.
72- *
73- * @return Schema
7488 */
75- public function getSchema (): Schema
89+ public function getSchema (bool $ ignoreCache = false ): Schema
7690 {
7791 if ($ this ->schema === null ) {
7892 $ cacheKey = $ this ->getCachePrefix ().'_immutable_schema ' ;
79- if ($ this ->cache ->contains ($ cacheKey )) {
93+ if (! $ ignoreCache && $ this ->cache ->contains ($ cacheKey )) {
8094 $ 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. ' );
8197 } else {
82- $ this ->schema = $ this ->connection -> getSchemaManager ()-> createSchema ();
83- $ this -> castSchemaToImmutable ($ this ->schema );
98+ $ this ->schema = $ this ->schemaVersionControlService -> loadSchemaFile ();
99+ ImmutableCaster:: castSchemaToImmutable ($ this ->schema );
84100 $ this ->cache ->save ($ cacheKey , $ this ->schema );
85101 }
86102 }
87103
88104 return $ this ->schema ;
89105 }
90106
91- private function castSchemaToImmutable ( Schema $ schema ): void
107+ public function generateLockFile ( ): void
92108 {
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 );
117111 }
118112
119113 /**
0 commit comments