Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Database always recreating Decimal fields on DB rebuild (dev/build) despite fields not changing...

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 741 Views
  • vwd
    Avatar
    Community Member
    158 Posts

    Database always recreating Decimal fields on DB rebuild (dev/build) despite fields not changing... Link to this post

    Hi,

    I've noticed that a couple of Decimal fields now always are highlighted as changing/being updated every time I do a dev/build.

    They are 2 fields from Geocodable.php - Lat/Lng which I changed a while ago from from Floats to Decimal(9,6)

    Current Code:

       public function extraStatics() {
          return array('db' => array(
             'Lat' => 'Decimal(9,6)',
             'Lng' => 'Decimal(9,6)'
          ));
       }

    Old code:

       public function extraStatics() {
          return array('db' => array(
             'Lat' => 'Float',
             'Lng' => 'Float
          ));
       }

    Since that change, every time I do a dev/build I get the following output from the build:

    Building database mydb_ss using MySQL 5.5.9

    Creating database tables

    PageComment
    SiteConfig
    Field SiteConfig.Lat: changed to decimal(9,6) not null default 0 (from decimal(9,6) not null default 0.000000)
    Field SiteConfig.Lng: changed to decimal(9,6) not null default 0 (from decimal(9,6) not null default 0.000000)
    SiteTree
    <snip>

    Database build completed!

    Note that I haven't change the DB Field types between rebuilds.

    After a bit of investigation - I've found that the problem might be in Decimal.php. The way the field is created, it appears to be passing in a double where it was requiring a string equivalent of $defaultField in the requireField(...) method. So there is always a discrepancy between the DB's default value for the Decimal(9,6) field (0.000000) and what it is being compared with in Sapphire which defaults to "0".

    The code that seems to work:

    <?php
    /**
    * Represents a Decimal field.
    * @package sapphire
    * @subpackage model
    */
    class Decimal extends DBField {
       protected $wholeSize, $decimalSize, $defaultValue;
       
    <snip>

       function requireField() {
          $beforeDecimal = $this->wholeSize - $this->decimalSize;
          $formatString = "%" . $beforeDecimal . "." . $this->decimalSize . "f";
          $defaultStr = sprintf($formatString, $this->defaultValue);
          $parts=Array('datatype'=>'decimal', 'precision'=>"$this->wholeSize,$this->decimalSize", 'default'=>$defaultStr, 'arrayValue'=>$this->arrayValue);
          $values=Array('type'=>'decimal', 'parts'=>$parts);
          DB::requireField($this->tableName, $this->name, $values);
    }
       
    <snip>

       public function nullValue() {
          $beforeDecimal = $this->wholeSize - $this->decimalSize;
          $formatString = "%" . $beforeDecimal . "." . $this->decimalSize . "f";
          $nullValueStr = sprintf($formatString, (double)0);
          return $nullValueStr;
       }
    <snip>
    ?>

    So is this a genuine bug that needs to be reported? I'm trying to get the unit tests running to see if this change has broken anything else.

    Thanks,
    VWD.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Database always recreating Decimal fields on DB rebuild (dev/build) despite fields not changing... Link to this post

    Yes it is a bug, if it hasn't been reported yet on open.silverstripe.org I recommend posting it to that and submitting your fix as a pull request to github for sapphire.

  • MarcelKlomp
    Avatar
    Community Member
    5 Posts

    Re: Database always recreating Decimal fields on DB rebuild (dev/build) despite fields not changing... Link to this post

    Awesome VWD. Had seen it as well in this project I'm working on, figured what it could be but no time to look into it (yet plenty of time to be bugged by it). Applied your fix et voila. Thanks!

    741 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.