Skip to content

Commit 9c4aa72

Browse files
author
Sergii Kovalenko
committed
MAGETWO-85326: Tool for generating schema
--add staging schema
1 parent 86fb388 commit 9c4aa72

File tree

17 files changed

+338
-55
lines changed

17 files changed

+338
-55
lines changed

app/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,9 @@
14671467
<item name="date" xsi:type="object">Magento\Setup\Model\SchemaListenerDefinition\DateDefinition</item>
14681468
<item name="boolean" xsi:type="object">Magento\Setup\Model\SchemaListenerDefinition\BooleanDefinition</item>
14691469
</argument>
1470+
<argument name="handlers" xsi:type="array">
1471+
<item name="staging" xsi:type="object">Magento\Setup\Model\SchemaListenerHandlers\StagingHandler</item>
1472+
</argument>
14701473
</arguments>
14711474
</type>
14721475
</config>

clean_db_schemas.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Application entry point
4+
*
5+
* Example - run a particular store or website:
6+
* --------------------------------------------
7+
* require __DIR__ . '/app/bootstrap.php';
8+
* $params = $_SERVER;
9+
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
10+
* $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
11+
* $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
12+
* \/** @var \Magento\Framework\App\Http $app *\/
13+
* $app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
14+
* $bootstrap->run($app);
15+
* --------------------------------------------
16+
*
17+
* Copyright © Magento, Inc. All rights reserved.
18+
* See COPYING.txt for license details.
19+
*/
20+
21+
try {
22+
require __DIR__ . '/app/bootstrap.php';
23+
} catch (\Exception $e) {
24+
echo <<<HTML
25+
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
26+
<div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
27+
<h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
28+
Autoload error</h3>
29+
</div>
30+
<p>{$e->getMessage()}</p>
31+
</div>
32+
HTML;
33+
exit(1);
34+
}
35+
36+
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
37+
$om = $bootstrap->getObjectManager();
38+
/** @var \Magento\Framework\Component\ComponentRegistrar $componentRegistrar */
39+
$componentRegistrar = $om->get(\Magento\Framework\Component\ComponentRegistrar::class);
40+
/** @var \Magento\Framework\App\ResourceConnection $resourceConnection */
41+
$resourceConnection = $om->get(Magento\Framework\App\ResourceConnection::class);
42+
43+
$adapter = $resourceConnection->getConnection();
44+
$adapter->query('DROP DATABASE ' . $resourceConnection->getSchemaName('default'));
45+
$adapter->query('CREATE DATABASE ' . $resourceConnection->getSchemaName('default'));
46+
47+
foreach ($componentRegistrar->getPaths(\Magento\Framework\Component\ComponentRegistrar::MODULE) as $path) {
48+
if (strpos($path, 'magento2ee') !== false) {
49+
@unlink($path . DIRECTORY_SEPARATOR . 'etc/db_schema.xml');
50+
}
51+
}

setup/src/Magento/Setup/Model/Declaration/Schema/etc/constraints/constraint.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
<xs:attributeGroup name="baseConstraint">
1212
<xs:attributeGroup ref="basicOperations" />
13-
<xs:attribute name="name" type="xs:string" />
13+
<xs:attribute name="name" type="nameType" />
1414
</xs:attributeGroup>
1515
</xs:schema>

setup/src/Magento/Setup/Model/Declaration/Schema/etc/constraints/foreign.xsd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
<xs:sequence />
2121

22-
<xs:attribute name="table" type="xs:string" use="required" />
23-
<xs:attribute name="column" type="xs:string" use="required" />
24-
<xs:attribute name="referenceColumn" type="xs:string" use="required" />
25-
<xs:attribute name="referenceTable" type="xs:string" use="required" />
22+
<xs:attribute name="table" type="xs:string" />
23+
<xs:attribute name="column" type="xs:string" />
24+
<xs:attribute name="referenceColumn" type="xs:string" />
25+
<xs:attribute name="referenceTable" type="xs:string" />
2626
<xs:attribute name="onDelete" type="referenceOptionsType" />
2727

2828
<xs:attributeGroup ref="baseConstraint" />

setup/src/Magento/Setup/Model/Declaration/Schema/etc/constraints/primary.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</xs:annotation>
1919

2020
<xs:sequence>
21-
<xs:element name="column" type="constraintColumnType" maxOccurs="unbounded" />
21+
<xs:element name="column" type="constraintColumnType" maxOccurs="unbounded" minOccurs="0" />
2222
</xs:sequence>
2323

2424
<xs:attributeGroup ref="baseConstraint" />

setup/src/Magento/Setup/Model/Declaration/Schema/etc/constraints/unique.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</xs:annotation>
1717

1818
<xs:sequence>
19-
<xs:element name="column" type="constraintColumnTypeUnique" maxOccurs="unbounded"/>
19+
<xs:element name="column" type="constraintColumnTypeUnique" minOccurs="0" maxOccurs="unbounded"/>
2020
</xs:sequence>
2121

2222
<xs:attributeGroup ref="baseConstraint" />

setup/src/Magento/Setup/Model/Declaration/Schema/etc/index.xsd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
</xs:annotation>
1616

1717
<xs:sequence>
18-
<xs:element name="column" type="indexColumnType" maxOccurs="unbounded" />
18+
<xs:element name="column" type="indexColumnType" minOccurs="0" maxOccurs="unbounded" />
1919
</xs:sequence>
2020

2121
<xs:attribute name="indexType" type="indexType" />
22-
<xs:attribute name="name" type="xs:string" />
22+
<xs:attribute name="name" type="nameType" />
23+
<xs:attribute name="disabled" type="xs:boolean" />
2324
</xs:complexType>
2425

2526
<xs:complexType name="indexColumnType">
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
9+
<xs:simpleType name="nameType">
10+
<xs:annotation>
11+
<xs:documentation>
12+
We can`t create column index or constraint with name length more than 64 char
13+
</xs:documentation>
14+
</xs:annotation>
15+
<xs:restriction base="xs:string">
16+
<xs:maxLength value="64" />
17+
</xs:restriction>
18+
</xs:simpleType>
19+
</xs:schema>

setup/src/Magento/Setup/Model/Declaration/Schema/etc/schema.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
1010
<!--Types-->
11+
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/name.xsd" />
1112
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/types/decimals/decimal.xsd" />
1213
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/types/decimals/float.xsd" />
1314
<xs:include schemaLocation="urn:magento:setup:Model/Declaration/Schema/etc/types/decimals/double.xsd" />

setup/src/Magento/Setup/Model/Declaration/Schema/etc/types/column.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
<xs:attributeGroup name="baseColumn">
1212
<xs:attributeGroup ref="basicOperations" />
13-
<xs:attribute name="name" type="xs:string" use="required" />
13+
<xs:attribute name="name" type="nameType" use="required" />
1414
</xs:attributeGroup>
1515
</xs:schema>

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public function install($request)
328328
$script[] = ['Cleaning up database...', 'cleanupDb', []];
329329
}
330330
$script[] = ['Installing database schema:', 'declarativeInstallSchema', [$request]];
331-
$script[] = ['Installing database schema:', 'installSchema', []];
331+
$script[] = ['Installing database schema:', 'installSchema', [$request]];
332332
$script[] = ['Installing user configuration...', 'installUserConfig', [$request]];
333333
$script[] = ['Enabling caches:', 'enableCaches', []];
334334
$script[] = ['Installing data...', 'installDataFixtures', [$request]];
@@ -344,7 +344,6 @@ public function install($request)
344344
$script[] = ['Disabling Maintenance Mode:', 'setMaintenanceMode', [0]];
345345
$script[] = ['Post installation file permissions check...', 'checkApplicationFilePermissions', []];
346346
$script[] = ['Write installation date...', 'writeInstallationDate', []];
347-
348347
$estimatedModules = $this->createModulesConfig($request, true);
349348
$total = count($script) + 4 * count(array_filter($estimatedModules));
350349
$this->progress = new Installer\Progress($total, 0);
@@ -356,7 +355,7 @@ public function install($request)
356355
call_user_func_array([$this, $method], $params);
357356
$this->logProgress();
358357
}
359-
358+
$this->schemaPersistor->persist($this->schemaListener);
360359
$this->log->logSuccess('Magento installation complete.');
361360
$this->log->logSuccess(
362361
'Magento Admin URI: /'
@@ -793,15 +792,15 @@ public function declarativeInstallSchema(array $request)
793792
* @param array $request
794793
* @return void
795794
*/
796-
public function installSchema()
795+
public function installSchema(array $request)
797796
{
798797
$this->assertDbConfigExists();
799798
$this->assertDbAccessible();
800799
$setup = $this->setupFactory->create($this->context->getResources());
801800
$this->setupModuleRegistry($setup);
802801
$this->setupCoreTables($setup);
803802
$this->log->log('Schema creation/updates:');
804-
$this->handleDBSchemaData($setup, 'schema');
803+
$this->handleDBSchemaData($setup, 'schema', $request);
805804
}
806805

807806
/**
@@ -818,7 +817,7 @@ public function installDataFixtures(array $request)
818817
$this->checkFilePermissionsForDbUpgrade();
819818
$this->log->log('Data install/update:');
820819
#if (!isset($request[InstallCommand::DECLARATION_MODE_KEY]) || !$request[InstallCommand::DECLARATION_MODE_KEY]) {
821-
$this->handleDBSchemaData($setup, 'data');
820+
$this->handleDBSchemaData($setup, 'data', $request);
822821
#}
823822
}
824823

@@ -855,13 +854,13 @@ private function throwExceptionForNotWritablePaths(array $paths)
855854
*
856855
* @param SchemaSetupInterface | ModuleDataSetupInterface $setup
857856
* @param string $type
857+
* @param array $request
858858
* @return void
859-
* @throws \Exception
860-
*
859+
* @throws \Magento\Setup\Exception
861860
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
862861
* @SuppressWarnings(PHPMD.NPathComplexity)
863862
*/
864-
private function handleDBSchemaData($setup, $type)
863+
private function handleDBSchemaData($setup, $type, array $request)
865864
{
866865
if (!(($type === 'schema') || ($type === 'data'))) {
867866
throw new \Magento\Setup\Exception("Unsupported operation type $type is requested");
@@ -874,7 +873,9 @@ private function handleDBSchemaData($setup, $type)
874873
$upgradeType = $type . '-upgrade';
875874
$moduleNames = $this->moduleList->getNames();
876875
$moduleContextList = $this->generateListOfModuleContext($resource, $verType);
877-
#if ($type !== 'schema') {
876+
if ($type !== 'schema' ||
877+
(!isset($request[InstallCommand::DECLARATION_MODE_KEY]) || !$request[InstallCommand::DECLARATION_MODE_KEY])
878+
) {
878879
foreach ($moduleNames as $moduleName) {
879880
$this->schemaListener->setModuleName($moduleName);
880881
$this->log->log("Module '{$moduleName}':");
@@ -914,10 +915,9 @@ private function handleDBSchemaData($setup, $type)
914915
}
915916
$this->logProgress();
916917
}
917-
#}
918+
}
918919

919-
//Before recurring generate schema
920-
$this->schemaPersistor->persist($this->schemaListener);
920+
$this->schemaListener->toogleIgnore(SchemaListener::IGNORE_ON);
921921
if ($type === 'schema') {
922922
$this->log->log('Schema post-updates:');
923923
$handlerType = 'schema-recurring';
@@ -934,6 +934,7 @@ private function handleDBSchemaData($setup, $type)
934934
}
935935
$this->logProgress();
936936
}
937+
$this->schemaListener->toogleIgnore(SchemaListener::IGNORE_OFF);
937938
}
938939

939940
/**

0 commit comments

Comments
 (0)