Skip to content
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

[BEHAVIOR] Add ability to choose between DATETIME and TIMESTAMP #1978

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ class TimestampableBehavior extends Behavior
'update_column' => 'updated_at',
'disable_created_at' => 'false',
'disable_updated_at' => 'false',
'is_timestamp' => 'true',
];

/**
* @return bool
*/
protected function isTimestamp(): bool
{
return $this->booleanValue($this->getParameter('is_timestamp'));
}

/**
* @return bool
*/
Expand Down Expand Up @@ -57,13 +66,13 @@ public function modifyTable(): void
if ($this->withCreatedAt() && !$table->hasColumn($this->getParameter('create_column'))) {
$table->addColumn([
'name' => $this->getParameter('create_column'),
'type' => 'TIMESTAMP',
'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME',
]);
}
if ($this->withUpdatedAt() && !$table->hasColumn($this->getParameter('update_column'))) {
$table->addColumn([
'name' => $this->getParameter('update_column'),
'type' => 'TIMESTAMP',
'type' => $this->isTimestamp() ? 'TIMESTAMP' : 'DATETIME',
]);
}
}
Expand Down Expand Up @@ -106,7 +115,7 @@ public function preUpdate(AbstractOMBuilder $builder): string
: '\\Propel\\Runtime\\Util\\PropelDateTime::createHighPrecision()';

return 'if ($this->isModified() && !$this->isColumnModified(' . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "(${valueSource});
\$this->" . $this->getColumnSetter('update_column') . "($valueSource);
}";
}

Expand All @@ -131,7 +140,7 @@ public function preInsert(AbstractOMBuilder $builder): string
: '$highPrecision';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('create_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('create_column') . "(${valueSource});
\$this->" . $this->getColumnSetter('create_column') . "($valueSource);
}";
}

Expand All @@ -141,7 +150,7 @@ public function preInsert(AbstractOMBuilder $builder): string
: '$highPrecision';
$script .= "
if (!\$this->isColumnModified(" . $this->getColumnConstant('update_column', $builder) . ")) {
\$this->" . $this->getColumnSetter('update_column') . "(${valueSource});
\$this->" . $this->getColumnSetter('update_column') . "($valueSource);
}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,54 @@ public function testDisableCreatedAt()
$this->assertEquals(1, $obj->save());
$this->assertNotNull($obj->getUpdatedAt());
}

/**
* @return void
*/
public function testWithDatetimeSqlType()
{
$schema = <<<EOF
<database name="timestampable_database">
<table name="table_with_datetime_sql_type">
<column name="id" type="INTEGER" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar"/>

<behavior name="timestampable">
<parameter name="is_timestamp" value="false"/>
</behavior>
</table>
</database>
EOF;
$builder = new QuickBuilder();
$builder->setSchema($schema);
$builder->build();
$sql = $builder->getSQL();
$this->assertStringContainsString('created_at DATETIME,', $sql, 'created_at column should be DATETIME');
$this->assertStringContainsString('updated_at DATETIME,', $sql, 'updated_at column should be DATETIME');
}

/**
* @return void
*/
public function testWithTimestampSqlType()
{
$schema = <<<EOF
<database name="timestampable_database">
<table name="table_with_timestamp_sql_type">
<column name="id" type="INTEGER" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar"/>

<behavior name="timestampable">
<parameter name="is_timestamp" value="true"/>
</behavior>
</table>
</database>
EOF;
$builder = new QuickBuilder();
$builder->setSchema($schema);
$builder->build();
$sql = $builder->getSQL();
$this->assertStringContainsString('created_at TIMESTAMP,', $sql, 'created_at column should be TIMESTAMP');
$this->assertStringContainsString('updated_at TIMESTAMP,', $sql, 'updated_at column should be TIMESTAMP');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public function testBehaviors()
'update_column' => 'updated_on',
'disable_created_at' => 'false',
'disable_updated_at' => 'false',
'is_timestamp' => 'true'
],
];
$this->assertEquals(
Expand Down
3 changes: 2 additions & 1 deletion tests/Propel/Tests/Generator/Model/BehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public function testSchemaReader()
'leParameterList' => [
['leListItem1Value' => 'leValue1'],
['leListItem2Value1' => 'leValue2.1', 'leListItem2Value2' => 'leValue2.2'],
]
],
'is_timestamp' => 'true'
];
$this->assertEquals($expectedParameters, $behavior->getParameters(), 'SchemaReader sets the behavior parameters correctly');
}
Expand Down
1 change: 1 addition & 0 deletions tests/Propel/Tests/Resources/blog-database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<parameter name="update_column" value="updated_at"/>
<parameter name="disable_created_at" value="false"/>
<parameter name="disable_updated_at" value="false"/>
<parameter name="is_timestamp" value="true"/>
</behavior>
<behavior name="sluggable">
<parameter name="slug_column" value="slug"/>
Expand Down
1 change: 1 addition & 0 deletions tests/Propel/Tests/Resources/blog-schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<parameter name="update_column" value="updated_at"/>
<parameter name="disable_created_at" value="false"/>
<parameter name="disable_updated_at" value="false"/>
<parameter name="is_timestamp" value="true"/>
</behavior>
<behavior name="sluggable">
<parameter name="slug_column" value="slug"/>
Expand Down