How to fix the «Wrong attribute set ID» while executing the «setup:upgrade» console command (2016-02-19)

The failure is a result of a reqular low-quality Magento 2 core team Friday’s commit.

The failure point

The defect code

The defect code wrongly assumes that the default attribute set is named «Default».
If you have a different name for the default attribute set then you will get the failure while upgrading.

How to fix it

The failure’s author should learn:

 /** @var int $defaultProductsAttributeSetId */
$defaultProductsAttributeSetId = 
    $categorySetup->getDefaultAttributeSetId(ProductAttributeInterface::ENTITY_TYPE_CODE)
;

Then you should replace the 'Default' string with the actual default attribute set id:

$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Product Details',
    'visibility',
    80
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Product Details',
    'news_from_date',
    90
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Product Details',
    'news_to_date',
    100
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Product Details',
    'country_of_manufacture',
    110
);

//Content tab
$categorySetup->addAttributeGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Content',
    15
);
$categorySetup->updateAttributeGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Content',
    'tab_group_code',
    'basic'
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Content',
    'description'
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Content',
    'short_description',
    100
);

//Images tab
$groupId = (int)$categorySetup->getAttributeGroupByCode(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'image-management',
    'attribute_group_id'
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    $groupId,
    'image',
    1
);
$categorySetup->updateAttributeGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    $groupId,
    'attribute_group_name',
    'Images'
);
//Schedule Design Update tab
$categorySetup->addAttributeGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Schedule Design Update',
    55
);
$categorySetup->updateAttributeGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Schedule Design Update',
    'tab_group_code',
    'advanced'
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Schedule Design Update',
    'custom_design_from',
    20
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Schedule Design Update',
    'custom_design_to',
    30
);
$categorySetup->updateAttribute(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    'custom_design',
    'frontend_label',
    'New Theme',
    40
);
$categorySetup->addAttributeToGroup(
    ProductAttributeInterface::ENTITY_TYPE_CODE,
    $defaultProductsAttributeSetId,
    'Schedule Design Update',
    'custom_design'
);

Replace it to:

/** @var int $defaultProductsAttributeSetId */
$defaultProductsAttributeSetId = $categorySetup->getDefaultAttributeSetId(Product::ENTITY);

if (!$categorySetup->getAttributeGroup(Product::ENTITY, $defaultProductsAttributeSetId, $groupName)) {
	$categorySetup->addAttributeGroup(Product::ENTITY, $defaultProductsAttributeSetId, $groupName, 60);
}

$entityTypeId = $categorySetup->getEntityTypeId(Product::ENTITY);
$attributeSetId = $categorySetup->getAttributeSetId($entityTypeId, $defaultProductsAttributeSetId);

Replace it to:

/** @var int $defaultProductsAttributeSetId */
$defaultProductsAttributeSetId = $eavSetup->getDefaultAttributeSetId(Product::ENTITY);
$groupId = (int)$eavSetup->getAttributeGroupByCode(
	Product::ENTITY,
	$defaultProductsAttributeSetId,
	'image-management',
	'attribute_group_id'
);
$eavSetup->addAttributeToGroup(Product::ENTITY, $defaultProductsAttributeSetId, $groupId, 'swatch_image');