Page 1 of 1
[Solved]How to write upgrade code for module
Posted: Wed Apr 22, 2009 1:17 pm
by Bigge
I made version 0.2 of DewPlayer and did not even think about how the upgrading of database was managed by the core...
Now I realise that is a problem since I never added the method.upgrade.php
I look at skeleton module but there is so little to guide me how to write the code to alter the tables. I have made quite big changes in the database table...
Do you know where to get information in this matter?
Re: Howto write upgrade code for module
Posted: Wed Apr 22, 2009 1:46 pm
by alby
Bigge wrote:
I made version 0.2 of DewPlayer and did not even think about how the upgrading of database was managed by the core...
Now I realise that is a problem since I never added the method.upgrade.php
I look at skeleton module but there is so little to guide me how to write the code to alter the tables. I have made quite big changes in the database table...
Do you know where to get information in this matter?
Better if you look in some modules
Alby
Re: Howto write upgrade code for module
Posted: Wed Apr 22, 2009 2:17 pm
by Bigge
OK, I will do that. I have checked some already and none of them include ALTER, but I search on.
By the way, I see a lot of "SetPreference" and it would be nice if someone could explain for a newbe what function and effect they have. It may affect my upgrading...
Re: Howto write upgrade code for module
Posted: Wed Apr 22, 2009 2:56 pm
by Nullig
The old version of Calendar has a table altering command and a field renaming command near the beginning:
Code: Select all
$db =& $this->GetDb();
$dict = NewDataDictionary($db);
if(version_compare($oldversion, 0.3, "<"))
{
// this is version 0.2 or 0.1
$sqlarray = $dict->RenameColumnSQL($this->events_table_name, "event_date", "event_date_start", "event_date_start DT");
$dict->ExecuteSQLArray($sqlarray);
$sqlarray = $dict->AddColumnSQL($this->events_table_name, "event_date_end DT");
$dict->ExecuteSQLArray($sqlarray);
...
Nullig
Re: Howto write upgrade code for module
Posted: Wed Apr 22, 2009 2:58 pm
by Bigge
Thanks Nullig!
That will help me a lot!
/Bigge
Re: Howto write upgrade code for module
Posted: Wed Apr 22, 2009 3:11 pm
by NaN
Take a look into the method.upgrade.php of the News module.
I learned lots of module API when diggin in the News module source.
I had a similar problem with my FEUmailer.
The new Version needs some changes to the database. (Including new columns in a table, or renaming columns.)
Just take a look there too.
Its is not that much like in the News module so it might be easier to understand.
SetPreference() stores in the table "siteprefs" a value of your module. (as a string)
It doesn't matter if there already exists this value or not.
You can use it to save some settings of your module.
Using this function allows also other modules to check your preferences since there is another function in the module API called GetPreference() what does nothing else to retrieve a desired setting of the module from this table.
Just take a look into the databse of the structure of that table.
I guess this will make some things clear to you.
E.g. in my FEUmailer there is an option "Save sent messages".
When this is checked the function SetPreference('save_message', $value) stores this preference in the table siteprefs.
When a mail has been sent the module can check with GetPreference('save_message',$default_value) if the message will be stored or not.
($default_value is a value of your choice that will be the return value if there is nothing stored in the database)
Hope that helps.
Re: Howto write upgrade code for module
Posted: Wed Apr 22, 2009 4:26 pm
by alby
You can see in Forum Made Simple too, there are many changed
Alby
Re: [Solved]How to write upgrade code for module
Posted: Thu Apr 23, 2009 7:04 am
by Bigge
Thank you NaN and alby. I really need this information! It helps me to understand module making and the API better. Maybe I add some module preferences now...
I have decided to drop the old 0.1-tables of DewPlayer module. I see no good reason to reuse old settings for the mp3-files since the mp3-files have to move into directories to support multiple songs/player. I am almost done with method.upgrade.php thanks to you!