UpgradeSchema.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * This file is part of the Klarna KP module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Kp\Setup;
  11. use Magento\Framework\DB\Ddl\Table;
  12. use Magento\Framework\Setup\ModuleContextInterface;
  13. use Magento\Framework\Setup\SchemaSetupInterface;
  14. use Magento\Framework\Setup\UpgradeSchemaInterface;
  15. class UpgradeSchema implements UpgradeSchemaInterface
  16. {
  17. /**
  18. * Upgrades DB schema for a module
  19. *
  20. * @param SchemaSetupInterface $installer
  21. * @param ModuleContextInterface $context
  22. * @return void
  23. */
  24. public function upgrade(SchemaSetupInterface $installer, ModuleContextInterface $context)
  25. {
  26. $installer->startSetup();
  27. if (version_compare($context->getVersion(), '4.0.3', '<')) {
  28. $this->addPaymentMethodsColumn($installer);
  29. }
  30. if (version_compare($context->getVersion(), '5.3.1', '<')) {
  31. $this->addPaymentMethodInfoColumn($installer);
  32. }
  33. if (version_compare($context->getVersion(), '5.5.3', '<')) {
  34. $this->dropForeignKeyInQuote($installer);
  35. }
  36. $installer->endSetup();
  37. }
  38. /**
  39. * Adding the payment_methods column to the klarna quote table
  40. *
  41. * @param SchemaSetupInterface $installer
  42. */
  43. private function addPaymentMethodsColumn(SchemaSetupInterface $installer)
  44. {
  45. $table = $installer->getTable('klarna_payments_quote');
  46. $ddl = $installer->getConnection()->describeTable($table);
  47. if (!isset($ddl['payment_methods'])) {
  48. $installer->getConnection()
  49. ->addColumn(
  50. $table,
  51. 'payment_methods',
  52. [
  53. 'type' => Table::TYPE_TEXT,
  54. 'length' => 255,
  55. 'comment' => 'Payment Method Categories'
  56. ]
  57. );
  58. }
  59. }
  60. /**
  61. * Adding the payment_method_info column to the klarna quote table
  62. *
  63. * @param SchemaSetupInterface $installer
  64. */
  65. private function addPaymentMethodInfoColumn(SchemaSetupInterface $installer)
  66. {
  67. $table = $installer->getTable('klarna_payments_quote');
  68. $ddl = $installer->getConnection()->describeTable($table);
  69. if (!isset($ddl['payment_method_info'])) {
  70. $installer->getConnection()
  71. ->addColumn(
  72. $table,
  73. 'payment_method_info',
  74. [
  75. 'type' => Table::TYPE_TEXT,
  76. 'length' => 4096,
  77. 'comment' => 'Payment Method Category Info'
  78. ]
  79. );
  80. }
  81. }
  82. /**
  83. * Dropping a foreign key in the klarna quote table
  84. *
  85. * @param SchemaSetupInterface $installer
  86. */
  87. private function dropForeignKeyInQuote(SchemaSetupInterface $installer)
  88. {
  89. $installer->getConnection()->dropForeignKey(
  90. $installer->getTable('klarna_payments_quote'),
  91. $installer->getFkName(
  92. 'klarna_payments_quote',
  93. 'quote_id',
  94. 'quote',
  95. 'entity_id'
  96. )
  97. );
  98. }
  99. }