Skip to content

Commit

Permalink
Merge tag '2.1.1'
Browse files Browse the repository at this point in the history
Hotfix release 2.1.1

- Hotfix save empty string as null
  • Loading branch information
stefanheimes committed Feb 24, 2020
2 parents 8534878 + 61d45bc commit 9eb5dba
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
-->
<project name="metamodels/attribute_text" default="build">
<import file="vendor/phpcq/phpcq/phpcq.main.xml" />
<target name="phpspec" />
</project>
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"require": {
"php": "^7.1",
"contao-community-alliance/dc-general": "^2.1.0",
"contao-community-alliance/contao-polyfill-bundle": "^1.1.0",
"contao/core-bundle": "^4.4.8",
"metamodels/core": "^2.1",
"symfony/dependency-injection": "^3.3 || ^4.0",
Expand Down
12 changes: 11 additions & 1 deletion src/Attribute/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Text extends BaseSimple
*/
public function getSQLDataType()
{
return 'varchar(255) NOT NULL default \'\'';
return 'varchar(255) NULL';
}

/**
Expand Down Expand Up @@ -71,4 +71,14 @@ public function getFieldDefinition($arrOverrides = array())

return $arrFieldDef;
}

/**
* {@inheritDoc}
*
* This is needed for compatibility with MySQL strict mode.
*/
public function serializeData($value)
{
return $value === '' ? null : $value;
}
}
182 changes: 182 additions & 0 deletions src/Migration/AllowNullMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

/**
* This file is part of MetaModels/attribute_text.
*
* (c) 2012-2020 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels/attribute_text
* @author Ingolf Steinhardt <[email protected]>
* @copyright 2012-2020 The MetaModels team.
* @license https://github.com/MetaModels/attribute_text/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/

declare(strict_types = 1);

namespace MetaModels\AttributeTextBundle\Migration;

use Contao\CoreBundle\Migration\AbstractMigration;
use Contao\CoreBundle\Migration\MigrationResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;

/**
* This migration changes all database columns to allow null values.
*
* This became necessary with the changes for https://github.com/MetaModels/core/issues/1330.
*/
class AllowNullMigration extends AbstractMigration
{
/**
* The database connection.
*
* @var Connection
*/
private $connection;

/**
* Create a new instance.
*
* @param Connection $connection The database connection.
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* Return the name.
*
* @return string
*/
public function getName(): string
{
return 'Allow null values in MetaModels "text" attributes.';
}

/**
* Must only run if:
* - the MM tables are present AND
* - there are some columns defined AND
* - these columns do not allow null values yet.
*
* @return bool
*/
public function shouldRun(): bool
{
$schemaManager = $this->connection->getSchemaManager();

if (!$schemaManager->tablesExist(['tl_metamodel', 'tl_metamodel_attribute'])) {
return false;
}

$langColumns = $this->fetchNonNullableColumns();
if (empty($langColumns)) {
return false;
}

return true;
}

/**
* Collect the columns to be updated and update them.
*
* @return MigrationResult
*/
public function run(): MigrationResult
{
$langColumns = $this->fetchNonNullableColumns();
$message = [];
foreach ($langColumns as $tableName => $tableColumnNames) {
foreach ($tableColumnNames as $tableColumnName) {
$this->fixColumn($tableName, $tableColumnName);
$message[] = $tableName . '.' . $tableColumnName;
}
}

return new MigrationResult(true, 'Adjusted column(s): ' . implode(', ', $message));
}

/**
* Fetch all columns that are not nullable yet.
*
* @return array
*/
private function fetchNonNullableColumns(): array
{
$langColumns = $this->fetchColumnNames();
if (empty($langColumns)) {
return [];
}
$schemaManager = $this->connection->getSchemaManager();

$result = [];
foreach ($langColumns as $tableName => $tableColumnNames) {
$columns = $schemaManager->listTableColumns($tableName);
foreach ($tableColumnNames as $tableColumnName) {
$column = ($columns[$tableColumnName] ?? null);
if (null === $column) {
continue;
}
if (true === $column->getNotnull()) {
if (!isset($result[$tableName])) {
$result[$tableName] = [];
}
$result[$tableName][] = $tableColumnName;
}
}
}

return $result;
}

/**
* Obtain the names of table columns.
*
* @return array
*/
private function fetchColumnNames(): array
{
$langColumns = $this
->connection
->createQueryBuilder()
->select('metamodel.tableName AS metamodel', 'attribute.colName AS attribute')
->from('tl_metamodel_attribute', 'attribute')
->leftJoin('attribute', 'tl_metamodel', 'metamodel', 'attribute.pid = metamodel.id')
->where('attribute.type=:type')
->setParameter('type', 'text')
->execute()
->fetchAll(FetchMode::ASSOCIATIVE);

$result = [];
foreach ($langColumns as $langColumn) {
if (!isset($result[$langColumn['metamodel']])) {
$result[$langColumn['metamodel']] = [];
}
$result[$langColumn['metamodel']][] = $langColumn['attribute'];
}

return $result;
}

/**
* Fix a table column.
*
* @param string $tableName The name of the table.
* @param string $columnName The name of the column.
*
* @return void
*/
private function fixColumn(string $tableName, string $columnName): void
{
$this->connection->query(
sprintf('ALTER TABLE %1$s CHANGE %2$s %2$s varchar(255) NULL', $tableName, $columnName)
);
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ services:
class: MetaModels\AttributeTextBundle\EventListener\BackendEventListener
tags:
- { name: kernel.event_listener, event: dc-general.view.contao2backend.get-property-options, method: getRgxpOptions }

MetaModels\AttributeTextBundle\Migration\AllowNullMigration:
arguments:
- '@database_connection'
tags:
- name: contao.migration

0 comments on commit 9eb5dba

Please sign in to comment.