forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSchemaToDatabase.php
379 lines (347 loc) · 15.4 KB
/
SchemaToDatabase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <[email protected]> and contributors
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
*/
namespace cebe\yii2openapi\lib;
use cebe\openapi\exceptions\IOException;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\yii2openapi\generator\ApiGenerator;
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
use cebe\yii2openapi\lib\items\Attribute;
use cebe\yii2openapi\lib\items\AttributeRelation;
use cebe\yii2openapi\lib\items\DbModel;
use cebe\yii2openapi\lib\items\JunctionSchemas;
use cebe\yii2openapi\lib\openapi\ComponentSchema;
use Yii;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\db\ColumnSchema;
use yii\db\Schema;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
use function count;
/**
* Convert OpenAPI description into a database schema.
* There are two options:
* 1. let the generator guess which schemas need a database table
* for storing their data and which do not.
* 2. Explicitly define schemas which represent a database table by adding the
* `x-table` property to the schema.
* The [[]]
* OpenApi Schema definition rules for database conversion:
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#schema-object
* components:
* schemas:
* ModelName: #(table name becomes model_names)
* description: #(optional, become as model class comment)
* required: #(list of required property names that can't be nullable)
* - id
* - some
* x-table: custom_table #(explicit database table name)
* x-indexes: #(list of indexes - property names only, index names will be autogenerated)
* - propertyName
* - propertyName1,propertyName2
* - 'gin:propertyName' #(index type prefix - using("gin") will be added) (For postgres only!)
* - 'unique:propertyName' #(unique attributes)
* Use propertyNames, if property is foreignKey fkcolumn_id will be resolved automatically
* x-pk: pid #(optional, primary key name if it called not "id") (composite keys not supported yet)
* properties: #(table columns and relations)
* prop_name:
* type: #(one of common types string|integer|number|boolean|array)
* format: #(see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#dataTypes)
* readOnly: true/false #(If true, should be skipped from validation rules)
* minimum: #(numeric value, applied for validation rules and faker generation)
* maximum: #(numeric value, applied for integer|number validation rules and faker generation)
* maxLength: #(numeric value, applied for database column size limit!, also can be applied for validation)
* minLength: #(numeric value, can be applied for validation rules)
* default: #(int|string, default value, used for database migration and model rules)
* x-db-type: #(Custom database type like JSON, JSONB, CHAR, VARCHAR, UUID, etc )
* x-faker: #(custom faker generator, for ex '$faker->gender'; PHP code as string)
* description: #(optional, used for comment)
*/
class SchemaToDatabase
{
protected Config $config;
public function __construct(Config $config)
{
$this->config = $config;
}
/**
* @return array|DbModel[]
* @throws IOException
* @throws TypeErrorException
* @throws UnresolvableReferenceException
* @throws InvalidDefinitionException
* @throws Exception
* @throws InvalidConfigException
*/
public function prepareModels(): array
{
/** @var DbModel[] $models */
$models = [];
/** @var AttributeResolver[] $resolvers */
$resolvers = [];
$openApi = $this->config->getOpenApi();
$junctions = $this->findJunctionSchemas();
foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) {
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);
if (!$this->canGenerateModel($schemaName, $schema)) {
continue;
}
if ($junctions->isJunctionSchema($schemaName)) {
$schemaName = $junctions->trimPrefix($schemaName);
}
/** @var AttributeResolver $resolver */
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);
// $models[$schemaName] = $resolver->resolve();
$resolvers[$schemaName] = $resolver;
$models[$schemaName] = $resolvers[$schemaName]->resolve();
}
// handle inverse relation
foreach ($resolvers as $aResolver) {
foreach ($aResolver->inverseRelations as $name => $relations) {
foreach ($relations as $relation) {
/** @var AttributeRelation $relation */
$models[$name]->inverseRelations[] = $relation;
}
}
}
foreach ($models as $model) {
foreach ($model->many2many as $relation) {
if (isset($models[$relation->viaModelName])) {
$relation->hasViaModel = true;
}
$relation->pkAttribute = $model->getPkAttribute();
$relation->relatedPkAttribute = $models[$relation->relatedSchemaName]->getPkAttribute();
}
}
// for drop table/schema https://github.com/cebe/yii2-openapi/issues/132
$modelsToDrop = [];
if (isset($this->config->getOpenApi()->{CustomSpecAttr::DELETED_SCHEMAS})) {
$tablesToDrop = $this->config->getOpenApi()->{CustomSpecAttr::DELETED_SCHEMAS}; // for removed (components) schemas
$modelsToDrop = static::dbModelsForDropTable($tablesToDrop);
}
return ArrayHelper::merge($models, $modelsToDrop);
}
/**
* @return JunctionSchemas
* @throws IOException
* @throws TypeErrorException
* @throws UnresolvableReferenceException
* @throws Exception
* @throws InvalidConfigException|InvalidDefinitionException
*/
public function findJunctionSchemas(): JunctionSchemas
{
$junctions = [];
$openApi = $this->config->getOpenApi();
foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) {
/**@var ComponentSchema $schema */
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);
if ($schema->isNonDb()) {
continue;
}
if (!StringHelper::startsWith($schemaName, JunctionSchemas::PREFIX)) {
continue;
}
if (!$this->canGenerateModel($schemaName, $schema)) {
continue;
}
$propertyMap = [];
$tableName = $schema->resolveTableName($schemaName);
foreach ($schema->getProperties() as $property) {
if (!$property->isReference() || !$property->isRefPointerToSchema()) {
continue;
}
$junkRef = null;
$relatedSchema = $property->getRefSchema();
foreach ($relatedSchema->getProperties() as $prop) {
if (!$prop->hasRefItems()) {
continue;
}
if ($schemaName === $prop->getRefSchemaName()) {
$junkRef = $prop->getName();
break;
}
}
if ($junkRef) {
$relatedTableName = $relatedSchema->resolveTableName($property->getRefClassName());
$foreignPkProperty = $property->getTargetProperty();
if ($foreignPkProperty === null) {
//Non-db
break;
}
$propertyMap[] = [
'property' => $property->getName(),
'targetClass' => $property->getRefClassName(),
'refProperty' => $junkRef,
'junctionSchema' => $schemaName,
'junctionTable' => $tableName,
'relatedClassName' => $property->getRefClassName(),
'relatedTableName' => $relatedTableName,
'foreignPk' => $foreignPkProperty->getName(),
'phpType' => $foreignPkProperty->guessPhpType(),
'dbType' => $foreignPkProperty->guessDbType(true),
];
}
if (count($propertyMap) === 2) {
break;
}
}
if (count($propertyMap) !== 2) {
throw new Exception('Junction table must contains 2 attributes referenced on other schemas');
}
$junkRef0 = $propertyMap[0]['refProperty'];
$junkRef1 = $propertyMap[1]['refProperty'];
$propertyMap[0]['class'] = $propertyMap[1]['targetClass'];
$propertyMap[0]['pairProperty'] = $propertyMap[1]['property'];
$propertyMap[0]['refProperty'] = $junkRef1;
$propertyMap[1]['class'] = $propertyMap[0]['targetClass'];
$propertyMap[1]['refProperty'] = $junkRef0;
$propertyMap[1]['pairProperty'] = $propertyMap[0]['property'];
$junctions[] = $propertyMap[0];
$junctions[] = $propertyMap[1];
unset($junkRef, $junkRef1, $junkRef0, $propertyMap);
}
return Yii::createObject(JunctionSchemas::class, [$junctions]);
}
private function canGenerateModel(string $schemaName, ComponentSchema $schema): bool
{
// only generate tables for schemas of type object and those who have defined properties
if ($schema->isObjectSchema() && !$schema->hasProperties()) {
return false;
}
if (!$schema->isObjectSchema()) {
return false;
}
// do not generate tables for composite schemas
if ($schema->isCompositeSchema()) {
return false;
}
// skip excluded model names
if (in_array($schemaName, $this->config->excludeModels, true)) {
return false;
}
// skip schemas started with underscore
if ($this->config->skipUnderscoredSchemas && StringHelper::startsWith($schemaName, '_')) {
return false;
}
if ($this->config->generateModelsOnlyXTable && !$schema->hasCustomTableName()) {
return false;
}
return true;
}
/**
* @param array $schemasToDrop . Example structure:
* ```
* array(2) {
* [0]=>
* string(5) "Fruit"
* [1]=>
* array(1) {
* ["Mango"]=>
* string(10) "the_mango_custom_table_name"
* }
* }
* ```
* @return DbModel[]
*/
public static function dbModelsForDropTable(array $schemasToDrop): array
{
$dbModelsToDrop = [];
foreach ($schemasToDrop as $key => $value) {
if (is_string($value)) { // schema name
$schemaName = $value;
$tableName = static::resolveTableName($schemaName);
} elseif (is_array($value)) {
$schemaName = array_key_first($value);
$tableName = $value[$schemaName];
} else {
throw new \Exception('Malformed list of schemas to delete');
}
$table = Yii::$app->db->schema->getTableSchema("{{%$tableName}}", true);
if ($table) {
$localDbModel = new DbModel([
'pkName' => $table->primaryKey[0],
'name' => $schemaName,
'tableName' => $tableName,
'attributes' => static::attributesFromColumnSchemas(static::enhanceColumnSchemas($table->columns)),
'drop' => true
]);
$dbModelsToDrop[$key] = $localDbModel;
}
}
return $dbModelsToDrop;
}
public static function resolveTableName(string $schemaName): string
{
return Inflector::camel2id(StringHelper::basename(Inflector::pluralize($schemaName)), '_');
}
/**
* @return Attribute[]
*/
public static function attributesFromColumnSchemas(array $columnSchemas): array
{
$attributes = [];
foreach ($columnSchemas as $columnName => $columnSchema) {
/** @var $columnName string */
/** @var $columnSchema ColumnSchema */
unset($attribute);
$attribute = new Attribute($columnSchema->name, [
'phpType' => $columnSchema->phpType,
'dbType' => $columnSchema->dbType,
'fkColName' => $columnSchema->name,
'required' => !$columnSchema->allowNull && ($columnSchema->defaultValue === null),
'nullable' => $columnSchema->allowNull,
'size' => $columnSchema->size,
'primary' => $columnSchema->isPrimaryKey,
'enumValues' => $columnSchema->enumValues,
'defaultValue' => $columnSchema->defaultValue,
'description' => $columnSchema->comment,
]);
$attributes[] = $attribute;
}
return $attributes;
}
public static function enhanceColumnSchemas(array $columnSchemas)
{
foreach ($columnSchemas as $columnSchema) {
// PgSQL array
if (property_exists($columnSchema, 'dimension') && $columnSchema->dimension !== 0) {
for ($i = 0; $i < $columnSchema->dimension; $i++) {
$columnSchema->dbType .= '[]';
}
}
if (ApiGenerator::isPostgres() && $columnSchema->type === Schema::TYPE_DECIMAL) {
$columnSchema->dbType .= '('.$columnSchema->precision.','.$columnSchema->scale.')';
}
// generate PK using `->primaryKeys()` or similar methods instead of separate SQL statement which sets only PK to a column of table
// https://github.com/cebe/yii2-openapi/issues/132
if (in_array($columnSchema->phpType, [
'integer',
'string' # https://github.com/yiisoft/yii2/issues/14663
])
&& $columnSchema->isPrimaryKey === true && $columnSchema->autoIncrement
) {
str_ireplace(['BIGINT', 'int8', 'bigserial', 'serial8'], 'nothing', $columnSchema->dbType, $count); # can be refactored if https://github.com/yiisoft/yii2/issues/20209 is fixed
if ($count) {
if ($columnSchema->unsigned) {
$columnSchema->dbType = Schema::TYPE_UBIGPK;
} else {
$columnSchema->dbType = Schema::TYPE_BIGPK;
}
} else {
if ($columnSchema->unsigned) {
$columnSchema->dbType = Schema::TYPE_UPK;
} else {
$columnSchema->dbType = Schema::TYPE_PK;
}
}
}
}
return $columnSchemas;
}
}