44 if(isset($dbOffsetConfig[
'wrapper'])){
45 $wrapperClass=$dbOffsetConfig[
'wrapper'];
46 if(\class_exists($wrapperClass)){
47 return new $wrapperClass($dbOffsetConfig[
'type']??
null);
49 throw new DBException(
"Wrapper class $wrapperClass does not exist!");
51 throw new DBException(
"Wrapper class is not set for database at offset $databaseOffset!");
55 $this->generator->setMigrationMode(true);
57 $this->generator->createDatabase($name);
58 $this->generator->selectDatabase($name);
60 $config = Startup::getConfig();
61 $models = $this->models ?? CacheManager::getModels($config,
true, $this->database);
64 $tableReversor->initFromClass();
65 if ($this->generator->hasToCreateTable($tableReversor->getTable())) {
66 $tableReversor->generateSQL($this->generator);
68 $tableReversor->scanManyToManys($this->generator);
71 $this->generator->generateManyToManys();
96 $this->generator->setMigrationMode(true);
98 $dbName=$this->getDbName();
99 if (! $checker->checkDatabase()) {
100 $this->createDatabase($dbName);
103 $tablesToCreate = $checker->getNonExistingTables();
104 if (\count($tablesToCreate)>0) {
105 $this->generator->setTablesToCreate($tablesToCreate);
106 $this->createDatabase($dbName,
false);
109 $config = Startup::getConfig();
110 $models = $this->models ?? CacheManager::getModels($config,
true, $this->database);
113 foreach ($models as $model){
114 $tablereversor=
new TableReversor($model);
115 $tablereversor->initFromClass();
117 $uFields=$checker->getUpdatedFields($model);
118 $missingFields=$uFields[
'missing'][$model]??[];
119 foreach ($missingFields as $missingField){
120 $this->generator->addField($missingField[
'table'],$missingField[
'name'],$missingField[
'attributes']);
122 $updatedFields=$uFields[
'updated'][$model]??[];
123 foreach ($updatedFields as $updatedField){
124 $this->generator->modifyField($updatedField[
'table'],$updatedField[
'name'],$updatedField[
'attributes']);
126 $missingPks=$checker->checkPrimaryKeys($model);
127 if (\count($missingPks)>0) {
128 $pks=$missingPks[
'primaryKeys'];
129 $tablereversor->addPrimaryKeys($this->generator,$pks);
131 $missingFks=$checker->checkManyToOne($model);
132 if (\count($missingFks)>0){
133 foreach ($missingFks as $fk){
134 $this->generator->addForeignKey($fk[
'table'], $fk[
'column'], $fk[
'fkTable'], $fk[
'fkId']);
138 $missingFks=$checker->checkManyToMany($model);
139 if (\count($missingFks)>0){
140 foreach ($missingFks as $fk){
141 if (!$this->generator->hasToCreateTable($fk[
'table'])) {
142 $this->checkManyToManyFields($checker, $fk[
'table'], $fk[
'column'],$newMissingPks);
143 $this->generator->addForeignKey($fk[
'table'], $fk[
'column'], $fk[
'fkTable'], $fk[
'fkId']);
148 foreach ($newMissingPks as $table=>$pks){
149 $this->generator->addKey($table,$pks);
154 $originalFieldInfos = $checker->getDb()->getFieldsInfos($table);
155 $pks=$checker->
getDb()->getPrimaryKeys($table);
156 if (!isset($originalFieldInfos[$field])) {
157 $this->generator->addField($table, $field, [
'type' =>
'int']);
159 if(\array_search($field,$pks)===
false){
160 $newMissingPks[$table][]=$field;