Skip to content

Commit

Permalink
Improved generator for creating migrations from database
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Nov 13, 2024
1 parent 6b50ba4 commit 150c3a0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
42 changes: 32 additions & 10 deletions src/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,56 @@
*/
class Generate
{
private string $path;
private string $seqtpl;
private string $tabletpl;
private string $viewtpl;


/**
* Initializes the object
*
* @param string $path Path where the migration files should be stored
*/
public function __construct( string $path )
{
$dir = dirname( __DIR__ );
$ds = DIRECTORY_SEPARATOR;

$this->seqtpl = $this->read( $dir . $ds . 'stubs' . $ds . 'sequence.stub' );
$this->tabletpl = $this->read( $dir . $ds . 'stubs' . $ds . 'table.stub' );
$this->viewtpl = $this->read( $dir . $ds . 'stubs' . $ds . 'view.stub' );

$this->path = $path;
}


/**
* Generates the migration files
*
* @param array $schema Associative list of schema definitions
* @param string $path Path where the migration files should be stored
* @param string $dbname Name of the database
* @throws \RuntimeException If a file can't be created
*/
public function __invoke( array $schema, string $path, string $dbname = '' ) : void
public function __invoke( array $schema, string $dbname = '' ) : void
{
$dir = dirname( __DIR__ );
$ds = DIRECTORY_SEPARATOR;
$prefix = $dbname ? preg_replace( '/[^A-Za-z0-9]/', '', $dbname ) . '_' : '';

$seqtpl = str_replace( '{{DB}}', $dbname, $this->read( $dir . $ds . 'stubs' . $ds . 'sequence.stub' ) );
$tabletpl = str_replace( '{{DB}}', $dbname, $this->read( $dir . $ds . 'stubs' . $ds . 'table.stub' ) );
$viewtpl = str_replace( '{{DB}}', $dbname, $this->read( $dir . $ds . 'stubs' . $ds . 'view.stub' ) );
$seqtpl = str_replace( '{{DB}}', $dbname, $this->seqtpl );
$tabletpl = str_replace( '{{DB}}', $dbname, $this->tabletpl );
$viewtpl = str_replace( '{{DB}}', $dbname, $this->viewtpl );

foreach( $schema['sequence'] ?? [] as $name => $def ) {
$this->write( $path . $ds . $prefix . 'seq_' . $name . '.php', $this->sequence( $def, $seqtpl ) );
$this->write( $this->path . $ds . $prefix . 'seq_' . $name . '.php', $this->sequence( $def, $seqtpl ) );
}

foreach( $schema['table'] ?? [] as $name => $def ) {
$this->write( $path . $ds . $prefix . 'table_' . $name . '.php', $this->table( $def, $tabletpl ) );
$this->write( $this->path . $ds . $prefix . 'table_' . $name . '.php', $this->table( $def, $tabletpl ) );
}

foreach( $schema['view'] ?? [] as $name => $def ) {
$this->write( $path . $ds . $prefix . 'view_' . $name . '.php', $this->view( $def, $viewtpl ) );
$this->write( $this->path . $ds . $prefix . 'view_' . $name . '.php', $this->view( $def, $viewtpl ) );
}
}

Expand Down Expand Up @@ -236,7 +258,7 @@ protected function table( array $def, string $template ) : string
$this->col( $def['col'] ?? [] ),
$this->index( $def['index'] ?? [] ),
$this->foreign( $def['foreign'] ?? [] ),
"'" . join( "', '", $fktables ) . "'"
$fktables ? "'" . join( "', '", $fktables ) . "'" : ''
], $template );
}

Expand Down
5 changes: 3 additions & 2 deletions src/Up.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ public static function use( array $config, $paths ) : self
*/
public function create( $name = '' ) : self
{
$generator = static::macro( 'generate' ) ?: new Generate();
$path = current( $this->paths );
$generator = ( $fcn = static::macro( 'generate' ) ) ? $fcn( $path ) : new Generate( $path );

foreach( (array) $name as $dbname ) {
$generator( $this->db( $dbname )->toArray(), current( $this->paths ), $dbname );
$generator( $this->db( $dbname )->toArray(), $dbname );
}

return $this;
Expand Down

0 comments on commit 150c3a0

Please sign in to comment.