Skip to content

Commit

Permalink
Create PSR-2 compliant migration files
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Nov 15, 2024
1 parent ae6a1bf commit a7641b0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
24 changes: 12 additions & 12 deletions src/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,34 +99,34 @@ protected function col( array $def ) : string
foreach( $def as $name => $e )
{
if( in_array( $e['type'], $types ) ) {
$string = '$t->' . $e['type'] . '( \'' . $name . '\' )';
$string = '$t->' . $e['type'] . '(\'' . $name . '\')';
} else {
$string = '$t->col( \'' . $name . '\', \'' . $e['type'] . '\' )';
$string = '$t->col(\'' . $name . '\', \'' . $e['type'] . '\')';
}

foreach( ['seq', 'fixed', 'unsigned', 'null'] as $key )
{
if( $e[$key] ?? false ) {
$string .= '->' . $key . '( true )';
$string .= '->' . $key . '(true)';
}
}

foreach( ['length', 'precision', 'scale'] as $key )
{
if( $e[$key] ?? false ) {
$string .= '->' . $key . '( ' . $e[$key] . ' )';
$string .= '->' . $key . '(' . $e[$key] . ')';
}
}

foreach( ['default', 'comment'] as $key )
{
if( $e[$key] ?? false ) {
$string .= '->' . $key . '( \'' . $e[$key] . '\' )';
$string .= '->' . $key . '(\'' . $e[$key] . '\')';
}
}

foreach( $e['opt'] ?? [] as $key => $value ) {
$string .= '->opt( \'' . $key . '\', \'' . $value . '\' )';
$string .= '->opt(\'' . $key . '\', \'' . $value . '\')';
}

$lines[] = $string . ';';
Expand All @@ -148,12 +148,12 @@ protected function foreign( array $def ) : string

foreach( $def as $name => $e )
{
$string = '$t->foreign( ' . json_encode( $e['localcol'] ) . ', \'' . ( $e['fktable'] ?? '' ) . '\', ' . json_encode( $e['fkcol'] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ' )';
$string = '$t->foreign(' . json_encode( $e['localcol'] ) . ', \'' . ( $e['fktable'] ?? '' ) . '\', ' . json_encode( $e['fkcol'] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ')';

foreach( ['onDelete', 'onUpdate'] as $key )
{
if( $e[$key] ?? false ) {
$string .= '->' . $key . '( \'' . $e[$key] . '\' )';
$string .= '->' . $key . '(\'' . $e[$key] . '\')';
}
}

Expand All @@ -177,11 +177,11 @@ protected function index( array $def ) : string
foreach( $def as $name => $e )
{
if( $e['primary'] ?? false ) {
$lines[] = '$t->primary( ' . json_encode( $e['columns'] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ' );';
$lines[] = '$t->primary(' . json_encode( $e['columns'] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ');';
} elseif( $e['unique'] ?? false ) {
$lines[] = '$t->unique( ' . json_encode( $e['columns'] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ' );';
$lines[] = '$t->unique(' . json_encode( $e['columns'] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ');';
} else {
$lines[] = '$t->index( ' . json_encode( $e['columns'] ?? [] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ', ' . $this->array( $e['flags'] ?? [] ) . ', ' . $this->array( $e['options'] ?? [] ) . ' );';
$lines[] = '$t->index(' . json_encode( $e['columns'] ?? [] ) . ', ' . ( $e['name'] ? '\'' . $e['name'] . '\'' : 'null' ) . ', ' . $this->array( $e['flags'] ?? [] ) . ', ' . $this->array( $e['options'] ?? [] ) . ');';
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ protected function sequence( array $def, string $template ) : string
foreach( ['cache', 'start', 'step'] as $key )
{
if( $def[$key] ?? false ) {
$string .= '->' . $key . '( ' . $def[$key] . ' )';
$string .= '->' . $key . '(' . $def[$key] . ')';
}
}

Expand Down
14 changes: 7 additions & 7 deletions stubs/sequence.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use Aimeos\Upscheme\Schema\Sequence;

return new class( $this ) extends Base
{
public function up()
{
$this->info( 'Create sequence "{{NAME}}"' );
public function up()
{
$this->info('Create sequence "{{NAME}}"');

$this->db( '{{DB}}' )->sequence( '{{NAME}}', function( Sequence $s ) {
$this->db('{{DB}}')->sequence('{{NAME}}', function(Sequence $s) {

{{SEQUENCE}}
} );
}
{{SEQUENCE}}
});
}
};
26 changes: 13 additions & 13 deletions stubs/table.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ use Aimeos\Upscheme\Schema\Table;

return new class( $this ) extends Base
{
public function after() : array
{
return [{{AFTER}}];
}
public function after(): array
{
return [{{AFTER}}];
}


public function up()
{
$this->info( 'Create table "{{NAME}}"' );
public function up()
{
$this->info('Create table "{{NAME}}"');

$this->db( '{{DB}}' )->table( '{{NAME}}', function( Table $t ) {
$this->db('{{DB}}')->table('{{NAME}}', function(Table $t) {

{{COLUMN}}
{{COLUMN}}

{{FOREIGN}}
{{FOREIGN}}

{{INDEX}}
} );
}
{{INDEX}}
});
}
};
4 changes: 2 additions & 2 deletions stubs/view.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ return new class( $this ) extends Base
{
public function up()
{
$this->info( 'Create view "{{NAME}}"' );
$this->info('Create view "{{NAME}}"');

$this->db( '{{DB}}' )->view( '{{NAME}}', '{{SQL}}' );
$this->db('{{DB}}')->view('{{NAME}}', '{{SQL}}');
}
};
7 changes: 4 additions & 3 deletions tests/config.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

return [
'driver' => 'pdo_sqlite',
'driver' => 'pdo_pgsql',
'path' => 'sqlite.test',
'host' => '127.0.0.1',
// 'port' => 5433,
'dbname' => 'upscheme',
'user' => 'upscheme',
'password' => 'upscheme'
'user' => 'aimeos',
'password' => 'aimeos'
];

0 comments on commit a7641b0

Please sign in to comment.