Skip to content

Commit

Permalink
MAGETWO-85326: Tool for generating schema
Browse files Browse the repository at this point in the history
--add staging schema
  • Loading branch information
Sergii Kovalenko committed Jan 4, 2018
1 parent 86fb388 commit 9c4aa72
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 55 deletions.
3 changes: 3 additions & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,9 @@
<item name="date" xsi:type="object">Magento\Setup\Model\SchemaListenerDefinition\DateDefinition</item>
<item name="boolean" xsi:type="object">Magento\Setup\Model\SchemaListenerDefinition\BooleanDefinition</item>
</argument>
<argument name="handlers" xsi:type="array">
<item name="staging" xsi:type="object">Magento\Setup\Model\SchemaListenerHandlers\StagingHandler</item>
</argument>
</arguments>
</type>
</config>
51 changes: 51 additions & 0 deletions clean_db_schemas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Application entry point
*
* Example - run a particular store or website:
* --------------------------------------------
* require __DIR__ . '/app/bootstrap.php';
* $params = $_SERVER;
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
* $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
* \/** @var \Magento\Framework\App\Http $app *\/
* $app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
* $bootstrap->run($app);
* --------------------------------------------
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

try {
require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
<div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
<h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
Autoload error</h3>
</div>
<p>{$e->getMessage()}</p>
</div>
HTML;
exit(1);
}

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$om = $bootstrap->getObjectManager();
/** @var \Magento\Framework\Component\ComponentRegistrar $componentRegistrar */
$componentRegistrar = $om->get(\Magento\Framework\Component\ComponentRegistrar::class);
/** @var \Magento\Framework\App\ResourceConnection $resourceConnection */
$resourceConnection = $om->get(Magento\Framework\App\ResourceConnection::class);

$adapter = $resourceConnection->getConnection();
$adapter->query('DROP DATABASE ' . $resourceConnection->getSchemaName('default'));
$adapter->query('CREATE DATABASE ' . $resourceConnection->getSchemaName('default'));

foreach ($componentRegistrar->getPaths(\Magento\Framework\Component\ComponentRegistrar::MODULE) as $path) {
if (strpos($path, 'magento2ee') !== false) {
@unlink($path . DIRECTORY_SEPARATOR . 'etc/db_schema.xml');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

<xs:attributeGroup name="baseConstraint">
<xs:attributeGroup ref="basicOperations" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="name" type="nameType" />
</xs:attributeGroup>
</xs:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

<xs:sequence />

<xs:attribute name="table" type="xs:string" use="required" />
<xs:attribute name="column" type="xs:string" use="required" />
<xs:attribute name="referenceColumn" type="xs:string" use="required" />
<xs:attribute name="referenceTable" type="xs:string" use="required" />
<xs:attribute name="table" type="xs:string" />
<xs:attribute name="column" type="xs:string" />
<xs:attribute name="referenceColumn" type="xs:string" />
<xs:attribute name="referenceTable" type="xs:string" />
<xs:attribute name="onDelete" type="referenceOptionsType" />

<xs:attributeGroup ref="baseConstraint" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</xs:annotation>

<xs:sequence>
<xs:element name="column" type="constraintColumnType" maxOccurs="unbounded" />
<xs:element name="column" type="constraintColumnType" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>

<xs:attributeGroup ref="baseConstraint" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</xs:annotation>

<xs:sequence>
<xs:element name="column" type="constraintColumnTypeUnique" maxOccurs="unbounded"/>
<xs:element name="column" type="constraintColumnTypeUnique" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>

<xs:attributeGroup ref="baseConstraint" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
</xs:annotation>

<xs:sequence>
<xs:element name="column" type="indexColumnType" maxOccurs="unbounded" />
<xs:element name="column" type="indexColumnType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>

<xs:attribute name="indexType" type="indexType" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="name" type="nameType" />
<xs:attribute name="disabled" type="xs:boolean" />
</xs:complexType>

<xs:complexType name="indexColumnType">
Expand Down
19 changes: 19 additions & 0 deletions setup/src/Magento/Setup/Model/Declaration/Schema/etc/name.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:simpleType name="nameType">
<xs:annotation>
<xs:documentation>
We can`t create column index or constraint with name length more than 64 char
</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:maxLength value="64" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!--Types-->
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/name.xsd" />
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/types/decimals/decimal.xsd" />
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/types/decimals/float.xsd" />
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/types/decimals/double.xsd" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

<xs:attributeGroup name="baseColumn">
<xs:attributeGroup ref="basicOperations" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="name" type="nameType" use="required" />
</xs:attributeGroup>
</xs:schema>
27 changes: 14 additions & 13 deletions setup/src/Magento/Setup/Model/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function install($request)
$script[] = ['Cleaning up database...', 'cleanupDb', []];
}
$script[] = ['Installing database schema:', 'declarativeInstallSchema', [$request]];
$script[] = ['Installing database schema:', 'installSchema', []];
$script[] = ['Installing database schema:', 'installSchema', [$request]];
$script[] = ['Installing user configuration...', 'installUserConfig', [$request]];
$script[] = ['Enabling caches:', 'enableCaches', []];
$script[] = ['Installing data...', 'installDataFixtures', [$request]];
Expand All @@ -344,7 +344,6 @@ public function install($request)
$script[] = ['Disabling Maintenance Mode:', 'setMaintenanceMode', [0]];
$script[] = ['Post installation file permissions check...', 'checkApplicationFilePermissions', []];
$script[] = ['Write installation date...', 'writeInstallationDate', []];

$estimatedModules = $this->createModulesConfig($request, true);
$total = count($script) + 4 * count(array_filter($estimatedModules));
$this->progress = new Installer\Progress($total, 0);
Expand All @@ -356,7 +355,7 @@ public function install($request)
call_user_func_array([$this, $method], $params);
$this->logProgress();
}

$this->schemaPersistor->persist($this->schemaListener);
$this->log->logSuccess('Magento installation complete.');
$this->log->logSuccess(
'Magento Admin URI: /'
Expand Down Expand Up @@ -793,15 +792,15 @@ public function declarativeInstallSchema(array $request)
* @param array $request
* @return void
*/
public function installSchema()
public function installSchema(array $request)
{
$this->assertDbConfigExists();
$this->assertDbAccessible();
$setup = $this->setupFactory->create($this->context->getResources());
$this->setupModuleRegistry($setup);
$this->setupCoreTables($setup);
$this->log->log('Schema creation/updates:');
$this->handleDBSchemaData($setup, 'schema');
$this->handleDBSchemaData($setup, 'schema', $request);
}

/**
Expand All @@ -818,7 +817,7 @@ public function installDataFixtures(array $request)
$this->checkFilePermissionsForDbUpgrade();
$this->log->log('Data install/update:');
#if (!isset($request[InstallCommand::DECLARATION_MODE_KEY]) || !$request[InstallCommand::DECLARATION_MODE_KEY]) {
$this->handleDBSchemaData($setup, 'data');
$this->handleDBSchemaData($setup, 'data', $request);
#}
}

Expand Down Expand Up @@ -855,13 +854,13 @@ private function throwExceptionForNotWritablePaths(array $paths)
*
* @param SchemaSetupInterface | ModuleDataSetupInterface $setup
* @param string $type
* @param array $request
* @return void
* @throws \Exception
*
* @throws \Magento\Setup\Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
private function handleDBSchemaData($setup, $type)
private function handleDBSchemaData($setup, $type, array $request)
{
if (!(($type === 'schema') || ($type === 'data'))) {
throw new \Magento\Setup\Exception("Unsupported operation type $type is requested");
Expand All @@ -874,7 +873,9 @@ private function handleDBSchemaData($setup, $type)
$upgradeType = $type . '-upgrade';
$moduleNames = $this->moduleList->getNames();
$moduleContextList = $this->generateListOfModuleContext($resource, $verType);
#if ($type !== 'schema') {
if ($type !== 'schema' ||
(!isset($request[InstallCommand::DECLARATION_MODE_KEY]) || !$request[InstallCommand::DECLARATION_MODE_KEY])
) {
foreach ($moduleNames as $moduleName) {
$this->schemaListener->setModuleName($moduleName);
$this->log->log("Module '{$moduleName}':");
Expand Down Expand Up @@ -914,10 +915,9 @@ private function handleDBSchemaData($setup, $type)
}
$this->logProgress();
}
#}
}

//Before recurring generate schema
$this->schemaPersistor->persist($this->schemaListener);
$this->schemaListener->toogleIgnore(SchemaListener::IGNORE_ON);
if ($type === 'schema') {
$this->log->log('Schema post-updates:');
$handlerType = 'schema-recurring';
Expand All @@ -934,6 +934,7 @@ private function handleDBSchemaData($setup, $type)
}
$this->logProgress();
}
$this->schemaListener->toogleIgnore(SchemaListener::IGNORE_OFF);
}

/**
Expand Down
Loading

0 comments on commit 9c4aa72

Please sign in to comment.