Skip to content

Commit

Permalink
Migrate from type IDs to type codes
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Dec 18, 2018
1 parent bb9fc3b commit b1ee36d
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 103 deletions.
20 changes: 10 additions & 10 deletions lib/custom/config/mshop/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@
FROM "fos_user_list"
WHERE "siteid" = ?
AND "parentid" = ?
AND "typeid" = ?
AND "type" = ?
AND "domain" = ?
',
),
'insert' => array(
'ansi' => '
INSERT INTO "fos_user_list"(
"parentid", "typeid", "domain", "refid", "start", "end",
"parentid", "type", "domain", "refid", "start", "end",
"config", "pos", "status", "mtime", "editor", "siteid", "ctime"
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
Expand All @@ -199,7 +199,7 @@
'update' => array(
'ansi' => '
UPDATE "fos_user_list"
SET "parentid"=?, "typeid" = ?, "domain" = ?, "refid" = ?, "start" = ?, "end" = ?,
SET "parentid"=?, "type" = ?, "domain" = ?, "refid" = ?, "start" = ?, "end" = ?,
"config" = ?, "pos" = ?, "status" = ?, "mtime" = ?, "editor" = ?
WHERE "siteid" = ? AND "id" = ?
',
Expand All @@ -223,15 +223,15 @@
SET "pos" = "pos" + ?, "mtime" = ?, "editor" = ?
WHERE "siteid" = ?
AND "parentid" = ?
AND "typeid" = ?
AND "type" = ?
AND "domain" = ?
AND "pos" >= ?
',
),
'search' => array(
'ansi' => '
SELECT fosli."id" AS "customer.lists.id", fosli."siteid" AS "customer.lists.siteid",
fosli."parentid" AS "customer.lists.parentid", fosli."typeid" AS "customer.lists.typeid",
fosli."parentid" AS "customer.lists.parentid", fosli."type" AS "customer.lists.type",
fosli."domain" AS "customer.lists.domain", fosli."refid" AS "customer.lists.refid",
fosli."start" AS "customer.lists.datestart", fosli."end" AS "customer.lists.dateend",
fosli."config" AS "customer.lists.config", fosli."pos" AS "customer.lists.position",
Expand All @@ -240,7 +240,7 @@
FROM "fos_user_list" AS fosli
:joins
WHERE :cond
GROUP BY fosli."id", fosli."parentid", fosli."siteid", fosli."typeid",
GROUP BY fosli."id", fosli."parentid", fosli."siteid", fosli."type",
fosli."domain", fosli."refid", fosli."start", fosli."end",
fosli."config", fosli."pos", fosli."status", fosli."mtime",
fosli."editor", fosli."ctime" /*-columns*/ , :columns /*columns-*/
Expand Down Expand Up @@ -348,7 +348,7 @@
'insert' => array(
'ansi' => '
INSERT INTO "fos_user_property" (
"parentid", "typeid", "langid", "value",
"parentid", "type", "langid", "value",
"mtime", "editor", "siteid", "ctime"
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?
Expand All @@ -358,22 +358,22 @@
'update' => array(
'ansi' => '
UPDATE "fos_user_property"
SET "parentid" = ?, "typeid" = ?, "langid" = ?,
SET "parentid" = ?, "type" = ?, "langid" = ?,
"value" = ?, "mtime" = ?, "editor" = ?
WHERE "siteid" = ? AND "id" = ?
'
),
'search' => array(
'ansi' => '
SELECT fospr."id" AS "customer.property.id", fospr."parentid" AS "customer.property.parentid",
fospr."siteid" AS "customer.property.siteid", fospr."typeid" AS "customer.property.typeid",
fospr."siteid" AS "customer.property.siteid", fospr."type" AS "customer.property.type",
fospr."langid" AS "customer.property.languageid", fospr."value" AS "customer.property.value",
fospr."mtime" AS "customer.property.mtime", fospr."editor" AS "customer.property.editor",
fospr."ctime" AS "customer.property.ctime"
FROM "fos_user_property" AS fospr
:joins
WHERE :cond
GROUP BY fospr."id", fospr."parentid", fospr."siteid", fospr."typeid",
GROUP BY fospr."id", fospr."parentid", fospr."siteid", fospr."type",
fospr."langid", fospr."value", fospr."mtime", fospr."editor",
fospr."ctime" /*-columns*/ , :columns /*columns-*/
/*-orderby*/ ORDER BY :order /*orderby-*/
Expand Down
81 changes: 81 additions & 0 deletions lib/custom/setup/TypesMigrateColumnsFosuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2018
*/


namespace Aimeos\MW\Setup\Task;


/**
* Adds the new type columns
*/
class TypesMigrateColumnsFosuser extends \Aimeos\MW\Setup\Task\TypesMigrateColumns
{
private $tables = [
'db-customer' => ['fos_user_list', 'fos_user_property'],
];

private $constraints = [
'db-customer' => ['fos_user_list' => 'unq_fosli_sid_dm_rid_tid_pid', 'fos_user_property' => 'unq_fospr_sid_tid_lid_value'],
];

private $migrations = [
'db-customer' => [
'fos_user_list' => 'UPDATE "fos_user_list" SET "type" = ( SELECT "code" FROM "fos_user_list_type" AS t WHERE t."id" = "typeid" AND t."domain" = "domain" LIMIT 1 ) WHERE "type" = IS NULL',
'fos_user_property' => 'UPDATE "fos_user_property" SET "type" = ( SELECT "code" FROM "fos_user_property_type" AS t WHERE t."id" = "typeid" AND t."domain" = "domain" LIMIT 1 ) WHERE "type" IS NULL',
],
];


/**
* Returns the list of task names which this task depends on.
*
* @return string[] List of task names
*/
public function getPreDependencies()
{
return [];
}


/**
* Returns the list of task names which depends on this task.
*
* @return array List of task names
*/
public function getPostDependencies()
{
return ['TablesCreateMShop'];
}


/**
* Executes the task
*/
public function migrate()
{
$this->msg( sprintf( 'Add new type columns for FosUser' ), 0 );
$this->status( '' );

foreach( $this->tables as $rname => $list ) {
$this->addColumn( $rname, $list );
}

$this->msg( sprintf( 'Drop old unique indexes for FosUser' ), 0 );
$this->status( '' );

foreach( $this->constraints as $rname => $list ) {
$this->dropIndex( $rname, $list );
}

$this->msg( sprintf( 'Migrate typeid to type for FosUser' ), 0 );
$this->status( '' );

foreach( $this->migrations as $rname => $list ) {
$this->migrateData( $rname, $list );
}
}
}
23 changes: 5 additions & 18 deletions lib/custom/setup/default/schema/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
$table->addColumn( 'id', 'integer', array( 'autoincrement' => true ) );
$table->addColumn( 'parentid', 'integer', [] );
$table->addColumn( 'siteid', 'integer', [] );
$table->addColumn( 'typeid', 'integer', [] );
$table->addColumn( 'type', 'string', array( 'length' => 32 ) );
$table->addColumn( 'domain', 'string', array( 'length' => 32 ) );
$table->addColumn( 'refid', 'string', array( 'length' => 32 ) );
$table->addColumn( 'start', 'datetime', array( 'notnull' => false ) );
Expand All @@ -156,19 +156,12 @@
$table->addColumn( 'editor', 'string', array( 'length' => 255 ) );

$table->setPrimaryKey( array( 'id' ), 'pk_fosli_id' );
$table->addUniqueIndex( array( 'siteid', 'domain', 'refid', 'typeid', 'parentid' ), 'unq_fosli_sid_dm_rid_tid_pid' );
$table->addIndex( array( 'siteid', 'status', 'start', 'end' ), 'idx_fosli_sid_stat_start_end' );
$table->addIndex( array( 'parentid', 'siteid', 'domain', 'refid', 'typeid' ), 'idx_fosli_pid_sid_dom_rid_tid' );
$table->addIndex( array( 'parentid', 'siteid', 'start' ), 'idx_fosli_pid_sid_start' );
$table->addIndex( array( 'parentid', 'siteid', 'end' ), 'idx_fosli_pid_sid_end' );
$table->addIndex( array( 'parentid', 'siteid', 'pos' ), 'idx_fosli_pid_sid_pos' );
$table->addUniqueIndex( array( 'parentid', 'siteid', 'domain', 'type', 'refid' ), 'unq_fosli_pid_sid_dm_ty_rid' );
$table->addIndex( array( 'parentid' ), 'fk_fosli_pid' );

$table->addForeignKeyConstraint( 'fos_user', array( 'parentid' ), array( 'id' ),
array( 'onUpdate' => 'CASCADE', 'onDelete' => 'CASCADE' ), 'fk_fosli_pid' );

$table->addForeignKeyConstraint( 'fos_user_list_type', array( 'typeid' ), array( 'id' ),
array( 'onUpdate' => 'CASCADE', 'onDelete' => 'CASCADE' ), 'fk_fosli_typeid' );

return $schema;
},

Expand Down Expand Up @@ -203,26 +196,20 @@
$table->addColumn( 'id', 'integer', array( 'autoincrement' => true ) );
$table->addColumn( 'siteid', 'integer', [] );
$table->addColumn( 'parentid', 'integer', [] );
$table->addColumn( 'typeid', 'integer', [] );
$table->addColumn( 'type', 'string', array( 'length' => 32 ) );
$table->addColumn( 'langid', 'string', array( 'length' => 5, 'notnull' => false ) );
$table->addColumn( 'value', 'string', array( 'length' => 255 ) );
$table->addColumn( 'mtime', 'datetime', [] );
$table->addColumn( 'ctime', 'datetime', [] );
$table->addColumn( 'editor', 'string', array( 'length' => 255 ) );

$table->setPrimaryKey( array( 'id' ), 'pk_fospr_id' );
$table->addUniqueIndex( array( 'parentid', 'siteid', 'typeid', 'langid', 'value' ), 'unq_fospr_sid_tid_lid_value' );
$table->addIndex( array( 'siteid', 'langid' ), 'idx_fospr_sid_langid' );
$table->addIndex( array( 'siteid', 'value' ), 'idx_fospr_sid_value' );
$table->addIndex( array( 'typeid' ), 'fk_fospr_typeid' );
$table->addUniqueIndex( array( 'parentid', 'siteid', 'type', 'langid', 'value' ), 'unq_fospr_sid_ty_lid_value' );
$table->addIndex( array( 'parentid' ), 'fk_fospr_pid' );

$table->addForeignKeyConstraint( 'fos_user', array( 'parentid' ), array( 'id' ),
array( 'onUpdate' => 'CASCADE', 'onDelete' => 'CASCADE' ), 'fk_fospr_pid' );

$table->addForeignKeyConstraint( 'fos_user_property_type', array( 'typeid' ), array( 'id' ),
array( 'onUpdate' => 'CASCADE', 'onDelete' => 'CASCADE' ), 'fk_fospr_typeid' );

return $schema;
},
),
Expand Down
8 changes: 4 additions & 4 deletions lib/custom/setup/unittest/data/customer-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
),

'customer/lists' => array (
array( 'parentid' => 'customer/UTC003', 'typeid' => 'text/default', 'domain' => 'text', 'refid' => 'text/customer/information', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 1, 'status' => 1 ),
array( 'parentid' => 'customer/UTC003', 'typeid' => 'text/default', 'domain' => 'text', 'refid' => 'text/customer/notify', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 2, 'status' => 1 ),
array( 'parentid' => 'customer/UTC003', 'typeid' => 'text/default', 'domain' => 'text', 'refid' => 'text/customer/newsletter', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 3, 'status' => 1 ),
array( 'parentid' => 'customer/UTC001', 'typeid' => 'text/default', 'domain' => 'text', 'refid' => 'text/customer/information', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 1, 'status' => 1 ),
array( 'parentid' => 'customer/UTC003', 'type' => 'default', 'domain' => 'text', 'refid' => 'text/customer/information', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 1, 'status' => 1 ),
array( 'parentid' => 'customer/UTC003', 'type' => 'default', 'domain' => 'text', 'refid' => 'text/customer/notify', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 2, 'status' => 1 ),
array( 'parentid' => 'customer/UTC003', 'type' => 'default', 'domain' => 'text', 'refid' => 'text/customer/newsletter', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 3, 'status' => 1 ),
array( 'parentid' => 'customer/UTC001', 'type' => 'default', 'domain' => 'text', 'refid' => 'text/customer/information', 'start' => '2010-01-01 00:00:00', 'end' => '2100-01-01 00:00:00', 'config' => [], 'pos' => 1, 'status' => 1 ),
),
);
2 changes: 1 addition & 1 deletion lib/custom/setup/unittest/data/customer-property.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
),

'customer/property' => array(
'customer/property/UTC001/newsletter' => array( 'parentid' => 'customer/UTC001', 'typeid' => 'customer/property/type/newsletter', 'langid' => null, 'value' => '1' ),
'customer/property/UTC001/newsletter' => array( 'parentid' => 'customer/UTC001', 'type' => 'newsletter', 'langid' => null, 'value' => '1' ),
),
);
17 changes: 8 additions & 9 deletions lib/custom/src/MShop/Customer/Manager/Lists/FosUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ class FosUser
'type'=> 'string',
'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
),
'customer.lists.typeid' => array(
'code'=>'customer.lists.typeid',
'internalcode'=>'fosli."typeid"',
'label'=>'Customer list type ID',
'type'=> 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
'customer.lists.type' => array(
'code'=>'customer.lists.type',
'internalcode'=>'fosli."type"',
'label'=>'Customer list type',
'type'=> 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
),
'customer.lists.refid'=> array(
'code'=>'customer.lists.refid',
Expand Down Expand Up @@ -135,7 +134,7 @@ class FosUser
public function cleanup( array $siteids )
{
$path = 'mshop/customer/manager/lists/submanagers';
foreach( $this->getContext()->getConfig()->get( $path, array( 'type' ) ) as $domain ) {
foreach( $this->getContext()->getConfig()->get( $path, [] ) as $domain ) {
$this->getObject()->getSubManager( $domain )->cleanup( $siteids );
}

Expand All @@ -153,7 +152,7 @@ public function getSearchAttributes( $withsub = true )
{
$path = 'mshop/customer/manager/lists/submanagers';

return $this->getSearchAttributesBase( $this->searchConfig, $path, array( 'type' ), $withsub );
return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class FosUser
'customer.lists.type.id' => array(
'code'=>'customer.lists.type.id',
'internalcode'=>'foslity."id"',
'internaldeps'=>array( 'LEFT JOIN "fos_user_list_type" AS foslity ON ( fosli."typeid" = foslity."id" )' ),
'label'=>'Customer list type ID',
'type'=> 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
Expand Down
15 changes: 7 additions & 8 deletions lib/custom/src/MShop/Customer/Manager/Property/FosUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ class FosUser
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
),
'customer.property.typeid' => array(
'code' => 'customer.property.typeid',
'internalcode' => 'fospr."typeid"',
'label' => 'Property type ID',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
'customer.property.type' => array(
'code' => 'customer.property.type',
'internalcode' => 'fospr."type"',
'label' => 'Property type',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
),
'customer.property.value' => array(
'code' => 'customer.property.value',
Expand Down Expand Up @@ -121,7 +120,7 @@ public function getSearchAttributes( $withsub = true )
{
$path = 'mshop/customer/manager/property/submanagers';

return $this->getSearchAttributesBase( $this->searchConfig, $path, ['type'], $withsub );
return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class FosUser
'customer.property.type.id' => array(
'code' => 'customer.property.type.id',
'internalcode' => 'fosprty."id"',
'internaldeps' => array( 'LEFT JOIN "fos_user_property_type" AS fosprty ON ( fospr."typeid" = fosprty."id" )' ),
'label' => 'Property type ID',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
Expand Down
22 changes: 5 additions & 17 deletions lib/custom/tests/MShop/Customer/Manager/Lists/FosUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,10 @@ public function testSaveUpdateDeleteItem()


$this->assertTrue( $item->getId() !== null );
$this->assertTrue( $itemSaved->getType() !== null );
$this->assertEquals( $item->getId(), $itemSaved->getId() );
$this->assertEquals( $item->getSiteId(), $itemSaved->getSiteId() );
$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
$this->assertEquals( $item->getTypeId(), $itemSaved->getTypeId() );
$this->assertEquals( $item->getType(), $itemSaved->getType() );
$this->assertEquals( $item->getRefId(), $itemSaved->getRefId() );
$this->assertEquals( $item->getDomain(), $itemSaved->getDomain() );
$this->assertEquals( $item->getDateStart(), $itemSaved->getDateStart() );
Expand All @@ -114,11 +113,10 @@ public function testSaveUpdateDeleteItem()
$this->assertRegExp( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
$this->assertRegExp('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );

$this->assertTrue( $itemUpd->getType() !== null );
$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
$this->assertEquals( $itemExp->getSiteId(), $itemUpd->getSiteId() );
$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
$this->assertEquals( $itemExp->getTypeId(), $itemUpd->getTypeId() );
$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
$this->assertEquals( $itemExp->getRefId(), $itemUpd->getRefId() );
$this->assertEquals( $itemExp->getDomain(), $itemUpd->getDomain() );
$this->assertEquals( $itemExp->getDateStart(), $itemUpd->getDateStart() );
Expand Down Expand Up @@ -230,8 +228,8 @@ public function testSearchItems()
$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
$expr[] = $search->compare( '>', 'customer.lists.parentid', 0 );
$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
$expr[] = $search->compare( '>', 'customer.lists.typeid', 0 );
$expr[] = $search->compare( '>', 'customer.lists.refid', 0 );
$expr[] = $search->compare( '==', 'customer.lists.type', 'default' );
$expr[] = $search->compare( '>', 'customer.lists.refid', '' );
$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
$expr[] = $search->compare( '==', 'customer.lists.dateend', '2100-01-01 00:00:00' );
$expr[] = $search->compare( '!=', 'customer.lists.config', null );
Expand All @@ -241,16 +239,6 @@ public function testSearchItems()
$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor );

$expr[] = $search->compare( '!=', 'customer.lists.type.id', 0 );
$expr[] = $search->compare( '!=', 'customer.lists.type.siteid', null );
$expr[] = $search->compare( '==', 'customer.lists.type.code', 'default' );
$expr[] = $search->compare( '==', 'customer.lists.type.domain', 'text' );
$expr[] = $search->compare( '==', 'customer.lists.type.label', 'Standard' );
$expr[] = $search->compare( '==', 'customer.lists.type.status', 1 );
$expr[] = $search->compare( '>=', 'customer.lists.type.mtime', '1970-01-01 00:00:00' );
$expr[] = $search->compare( '>=', 'customer.lists.type.ctime', '1970-01-01 00:00:00' );
$expr[] = $search->compare( '==', 'customer.lists.type.editor', $this->editor );

$search->setConditions( $search->combine( '&&', $expr ) );
$search->setSlice(0, 1);
$results = $this->object->searchItems( $search, [], $total );
Expand Down Expand Up @@ -312,7 +300,7 @@ protected function getListItems()
$search->compare( '==', 'customer.lists.parentid', $item->getId() ),
$search->compare( '==', 'customer.lists.domain', 'text' ),
$search->compare( '==', 'customer.lists.editor', $this->editor ),
$search->compare( '==', 'customer.lists.type.code', 'default' ),
$search->compare( '==', 'customer.lists.type', 'default' ),
);
$search->setConditions( $search->combine( '&&', $expr ) );
$search->setSortations( array( $search->sort( '+', 'customer.lists.position' ) ) );
Expand Down

0 comments on commit b1ee36d

Please sign in to comment.