Skip to content

Commit

Permalink
Merge pull request #263 from uasoft-indonesia/feature/v1/foreign-key
Browse files Browse the repository at this point in the history
Add foreign key and soft delete
  • Loading branch information
rizkiheryandi authored Jul 9, 2021
2 parents 66bfd67 + 680cc90 commit d508875
Show file tree
Hide file tree
Showing 12 changed files with 761 additions and 162 deletions.
1 change: 0 additions & 1 deletion src/Badaso.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class Badaso
'password_resets',
'menus',
'menu_items',
'users',
'roles',
'permissions',
'configurations',
Expand Down
27 changes: 21 additions & 6 deletions src/ContentManager/FileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function repackSeedData($data): array
/**
* Generate Badaso Migration File.
*/
public function generateBDOMigrationFile(string $table_name, string $prefix, array $rows): string
public function generateBDOMigrationFile(string $table_name, string $prefix, array $rows, array $relations = []): string
{
$migration_class_name = $this->file_system->generateMigrationClassName($table_name, $prefix);

Expand All @@ -301,8 +301,15 @@ public function generateBDOMigrationFile(string $table_name, string $prefix, arr

$migration_file = $this->file_system->getMigrationFile($migration_file_name, $migration_folder_path);

$schema_up = $this->migration_parser->getMigrationSchemaUp($table_name, $rows, $prefix);
$schema_down = $this->migration_parser->getMigrationSchemaDown($table_name, $rows, $prefix);
$schema_up = '';
$schema_down = '';

$schema_up .= $this->migration_parser->getMigrationSchemaUp($table_name, $rows, $prefix);
if (! empty($relations)) {
$schema_up .= PHP_EOL.PHP_EOL.$this->migration_parser->getMigrationRelationshipSchemaUp($table_name, $relations);
$schema_down .= $this->migration_parser->getMigrationRelationshipSchemaDown($table_name, $relations).PHP_EOL.PHP_EOL;
}
$schema_down .= $this->migration_parser->getMigrationSchemaDown($table_name, $rows, $prefix);

$stub = $this->content_manager->replaceString('{{class}}', $migration_class_name, $stub);
$stub = $this->content_manager->replaceString('{{schema_up}}', $schema_up, $stub);
Expand All @@ -324,7 +331,7 @@ public function deleteMigrationFiles(string $file_name)
/**
* Generate Badaso Alter Migration File.
*/
public function generateBDOAlterMigrationFile(array $table, array $rows = null, string $prefix): string
public function generateBDOAlterMigrationFile(array $table, array $rows = null, string $prefix, array $relations = []): string
{
$migration_class_name = $this->file_system->generateAlterMigrationClassName($table, $prefix);

Expand All @@ -337,8 +344,16 @@ public function generateBDOAlterMigrationFile(array $table, array $rows = null,
$migration_folder_path = $this->file_system->getMigrationFolderPath();

$migration_file = $this->file_system->getMigrationFile($migration_file_name, $migration_folder_path);
$schema_up = $this->migration_parser->getAlterMigrationSchemaUp($table, $rows, $prefix);
$schema_down = $this->migration_parser->getAlterMigrationSchemaDown($table, $rows, $prefix);

$schema_up = '';
$schema_down = '';

$schema_up .= $this->migration_parser->getAlterMigrationSchemaUp($table, $rows, $prefix, $relations);
if (array_key_exists('current_relations', $relations) && count($relations['current_relations']) > 0) {
$schema_up .= $this->migration_parser->getAlterMigrationRelationshipSchemaUp($table, $relations);
$schema_down .= $this->migration_parser->getAlterMigrationRelationshipSchemaDown($table, $relations).PHP_EOL;
}
$schema_down .= $this->migration_parser->getAlterMigrationSchemaDown($table, $rows, $prefix, $relations);

$stub = $this->content_manager->replaceString('{{class}}', $migration_class_name, $stub);
$stub = $this->content_manager->replaceString('{{schema_up}}', $schema_up, $stub);
Expand Down
22 changes: 17 additions & 5 deletions src/Controllers/BadasoDatabaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function ($attribute, $value, $fail) {
'rows.*.field_type' => 'required|string',
]);

$this->file_name = $this->file_generator->generateBDOMigrationFile($request->table, 'create', $request->rows);
$this->file_name = $this->file_generator->generateBDOMigrationFile($request->table, 'create', $request->rows, $request->relations);

$exitCode = Artisan::call('migrate', [
'--path' => 'database/migrations/badaso/',
Expand Down Expand Up @@ -89,8 +89,19 @@ function ($attribute, $value, $fail) {
]);

$columns = SchemaManager::describeTable($request->table)->toArray();
$columnsFK = SchemaManager::getDoctrineForeignKeys($request->table);
$fKConstraints = [];
foreach ($columnsFK as $columnFK) {
$fKConstraints[$columnFK->getUnquotedLocalColumns()[0]] = [
'source_field' => $columnFK->getUnquotedLocalColumns()[0],
'target_table' => $columnFK->getForeignTableName(),
'target_field' => $columnFK->getForeignColumns()[0],
'on_delete' => strtolower($columnFK->getOption('onDelete')),
'on_update' => strtolower($columnFK->getOption('onUpdate')),
];
}

return ApiResponse::success(['columns' => $columns]);
return ApiResponse::success(['columns' => $columns, 'columnsFK' => $fKConstraints]);
} catch (Exception $e) {
return APIResponse::failed($e);
}
Expand All @@ -114,15 +125,16 @@ function ($attribute, $value, $fail) {
Rule::notIn(Badaso::getProtectedTables()),
],
'fields.current_fields' => 'required|array',
'fields.modified_fields' => 'nullable|array',
'fields.modified_fields' => 'required|array',
]);

$data = $request->all();
$fields = $data['fields'];
$table = $data['table'];
$relations = $data['relations'];

if (count($fields['current_fields']) > 0) {
$this->file_name[] = $this->file_generator->generateBDOAlterMigrationFile($table, $fields, 'alter');
if (count($fields['modified_fields']) > 0) {
$this->file_name[] = $this->file_generator->generateBDOAlterMigrationFile($table, $fields, 'alter', $relations);
}

if ($table['current_name'] !== $table['modified_name']) {
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Schema/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ public static function getDoctrineColumn($table, $column)
{
return static::getDoctrineTable($table)->getColumn($column);
}

public static function getDoctrineForeignKeys($table)
{
return static::manager()->listTableForeignKeys($table);
}
}
Loading

0 comments on commit d508875

Please sign in to comment.