-
Notifications
You must be signed in to change notification settings - Fork 105
1139: Creating a new data patch template #1188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bohdan-harniuk
merged 12 commits into
magento:5.0.0-develop
from
anzin:1139-add-setup-data-patch
Sep 20, 2022
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c399bd5
1139: Creating a new data patch template
anzin bfd0b99
1139: Fixed Static Tests
anzin 62feb60
Merge branch '5.0.0-develop' of github.com:magento/magento2-phpstorm-…
bohdan-harniuk 5b30ba0
1139: Code refactoring
bohdan-harniuk b02c439
Merge branch '5.0.0-develop' of github.com:magento/magento2-phpstorm-…
bohdan-harniuk 9da21d0
1139: Code refactoring
bohdan-harniuk 1a0309b
1139: Fixed code after review
anzin b4f622a
Merge branch '4.4.0-develop' of github.com:magento/magento2-phpstorm-…
bohdan-harniuk c07bf4f
Merge branch '5.0.0-develop' of github.com:magento/magento2-phpstorm-…
bohdan-harniuk d93bca8
1139: Fixed code
anzin 94014af
1139: Code refactoring
bohdan-harniuk 9985598
1139: Code refactoring
bohdan-harniuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
resources/fileTemplates/internal/Magento Module Setup Patch File.php.ft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
#parse("PHP File Header.php") | ||
|
||
namespace ${MODULE_NAME}\Setup\Patch\Data; | ||
|
||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\Patch\DataPatchInterface; | ||
|
||
/** | ||
* Patch is mechanism, that allows to do atomic upgrade data changes. | ||
*/ | ||
class ${CLASS_NAME} implements DataPatchInterface | ||
{ | ||
/** | ||
* @var ModuleDataSetupInterface | ||
*/ | ||
private $moduleDataSetup; | ||
|
||
/** | ||
* @param ModuleDataSetupInterface $moduleDataSetup | ||
*/ | ||
public function __construct( | ||
ModuleDataSetupInterface $moduleDataSetup | ||
) { | ||
$this->moduleDataSetup = $moduleDataSetup; | ||
} | ||
|
||
/** | ||
* Do Upgrade. | ||
* | ||
* @return void | ||
*/ | ||
public function apply() | ||
{ | ||
$this->moduleDataSetup->getConnection()->startSetup(); | ||
|
||
// TODO: The code that you want apply in the patch | ||
|
||
$this->moduleDataSetup->getConnection()->endSetup(); | ||
} | ||
|
||
/** | ||
* Get aliases (previous names) for the patch. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getAliases() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Get array of patches that have to be executed prior to this. | ||
* | ||
* Example of implementation: | ||
* | ||
* [ | ||
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class, | ||
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class | ||
* ] | ||
* | ||
* @return string[] | ||
*/ | ||
public static function getDependencies() | ||
{ | ||
return []; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
resources/fileTemplates/internal/Magento Module Setup Patch File.php.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!-- | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<html> | ||
<body> | ||
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse"> | ||
<tr> | ||
<td><font face="verdana" size="-1"> | ||
A data patch is a class that contains data modification instructions. | ||
</font><br> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td><font face="verdana" size="-1"> | ||
Read more About the data and schema patches in the | ||
<a href="https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/data-patches.html"> | ||
DevDocs</a>. | ||
</font><br> | ||
</td> | ||
</tr> | ||
</table> | ||
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse"> | ||
<tr> | ||
<td colspan="3"><font face="verdana" size="-1">Predefined variables explanation:</font></td> | ||
</tr> | ||
<tr> | ||
<td valign="top"><nobr><font face="verdana" size="-2"><b>${CLASS_NAME}</b></font></nobr></td> | ||
<td width="10"> </td> | ||
<td width="100%" valign="top"><font face="verdana" size="-1">Specifies the name of your class | ||
</font> | ||
</td> | ||
</tr> | ||
</table> | ||
</body> | ||
</html> |
80 changes: 80 additions & 0 deletions
80
src/com/magento/idea/magento2plugin/actions/context/php/NewSetupDataPatchAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.actions.context.php; | ||
|
||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.psi.PsiDirectory; | ||
import com.intellij.psi.PsiFile; | ||
import com.magento.idea.magento2plugin.actions.context.CustomGeneratorContextAction; | ||
import com.magento.idea.magento2plugin.actions.generation.dialog.NewSetupDataPatchDialog; | ||
import com.magento.idea.magento2plugin.magento.packages.ComponentType; | ||
import com.magento.idea.magento2plugin.magento.packages.Package; | ||
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class NewSetupDataPatchAction extends CustomGeneratorContextAction { | ||
|
||
public static final String ACTION_NAME = "Magento 2 Setup Data Patch"; | ||
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Setup Data Patch"; | ||
public static final String ROOT_DIRECTORY = "Setup"; | ||
public static final String PATCH_DIRECTORY = "Patch"; | ||
public static final String DATA_DIRECTORY = "Data"; | ||
|
||
public NewSetupDataPatchAction() { | ||
super(ACTION_NAME, ACTION_DESCRIPTION); | ||
} | ||
|
||
@Override | ||
public void actionPerformed(final @NotNull AnActionEvent event) { | ||
final GetMagentoModuleUtil.MagentoModuleData moduleData = getModuleData(); | ||
|
||
if (event.getProject() == null || moduleData == null || getDirectory() == null) { | ||
return; | ||
} | ||
final String[] templateData = moduleData.getName().split(Package.vendorModuleNameSeparator); | ||
|
||
if (templateData.length != 2) { //NOPMD | ||
return; | ||
} | ||
|
||
NewSetupDataPatchDialog.open( | ||
event.getProject(), | ||
getDirectory(), | ||
templateData[0], | ||
templateData[1] | ||
); | ||
} | ||
|
||
@Override | ||
protected boolean isVisible( | ||
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, | ||
final PsiDirectory targetDirectory, | ||
final PsiFile targetFile | ||
) { | ||
if (!moduleData.getType().equals(ComponentType.module)) { | ||
return false; | ||
} | ||
final String targetDirName = targetDirectory.getName(); | ||
|
||
if (!(ROOT_DIRECTORY.equals(targetDirName) || PATCH_DIRECTORY.equals(targetDirName) | ||
|| DATA_DIRECTORY.equals(targetDirName)) | ||
) { | ||
return false; | ||
} | ||
|
||
final PsiDirectory parentDirFirst = targetDirectory.getParentDirectory(); | ||
PsiDirectory parentDirSecond = null; | ||
|
||
if (parentDirFirst != null) { | ||
parentDirSecond = parentDirFirst.getParentDirectory(); | ||
} | ||
|
||
|
||
return ROOT_DIRECTORY.equals(targetDirName) | ||
|| parentDirFirst != null && ROOT_DIRECTORY.equals(parentDirFirst.getName()) | ||
|| parentDirSecond != null && ROOT_DIRECTORY.equals(parentDirSecond.getName()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/com/magento/idea/magento2plugin/actions/generation/ModuleSetupDataPatchData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.actions.generation; | ||
|
||
import com.intellij.psi.PsiDirectory; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class ModuleSetupDataPatchData { | ||
|
||
private final String packageName; | ||
private final String moduleName; | ||
private final PsiDirectory baseDir; | ||
private final String className; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param packageName String | ||
* @param moduleName String | ||
* @param baseDir PsiDirectory | ||
*/ | ||
public ModuleSetupDataPatchData( | ||
final @NotNull String packageName, | ||
final @NotNull String moduleName, | ||
final @NotNull PsiDirectory baseDir, | ||
final @NotNull String className | ||
) { | ||
this.packageName = packageName; | ||
this.moduleName = moduleName; | ||
this.baseDir = baseDir; | ||
this.className = className; | ||
} | ||
|
||
public @NotNull String getPackageName() { | ||
return packageName; | ||
} | ||
|
||
public @NotNull String getModuleName() { | ||
return moduleName; | ||
} | ||
|
||
public @NotNull PsiDirectory getBaseDir() { | ||
return baseDir; | ||
} | ||
|
||
public @NotNull String getClassName() { | ||
return className; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/com/magento/idea/magento2plugin/actions/generation/dialog/NewSetupDataPatchDialog.form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewSetupDataPatchDialog"> | ||
<grid id="1871d" binding="contentPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="10" left="10" bottom="10" right="10"/> | ||
<constraints> | ||
<xy x="63" y="217" width="405" height="143"/> | ||
</constraints> | ||
<properties> | ||
<opaque value="true"/> | ||
<preferredSize width="440" height="150"/> | ||
<requestFocusEnabled value="true"/> | ||
</properties> | ||
<border type="none"/> | ||
<children> | ||
<grid id="9ad5" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<hspacer id="4aa6d"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
</hspacer> | ||
<grid id="f892c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="1b860" class="javax.swing.JButton" binding="buttonCancel"> | ||
<constraints> | ||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text resource-bundle="magento2/common" key="common.cancel"/> | ||
</properties> | ||
</component> | ||
<component id="20801" class="javax.swing.JButton" binding="buttonOK"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text resource-bundle="magento2/common" key="common.ok"/> | ||
</properties> | ||
</component> | ||
</children> | ||
</grid> | ||
</children> | ||
</grid> | ||
<grid id="ad0b1" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="d30e2" class="javax.swing.JLabel" binding="classNameErrorMessage"> | ||
<constraints> | ||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text value=""/> | ||
</properties> | ||
</component> | ||
<component id="662ad" class="javax.swing.JLabel" binding="classNameLabel"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text value="Class Name"/> | ||
</properties> | ||
</component> | ||
<component id="292c" class="javax.swing.JTextField" binding="className"> | ||
<constraints> | ||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
</component> | ||
</children> | ||
</grid> | ||
</children> | ||
</grid> | ||
</form> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.