[Solved]How to write upgrade code for module

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
Bigge

[Solved]How to write upgrade code for module

Post 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?
Last edited by Bigge on Wed Apr 22, 2009 4:19 pm, edited 1 time in total.
alby

Re: Howto write upgrade code for module

Post 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
Bigge

Re: Howto write upgrade code for module

Post 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...
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Howto write upgrade code for module

Post 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
Bigge

Re: Howto write upgrade code for module

Post by Bigge »

Thanks Nullig!
That will help me a lot!

/Bigge
NaN

Re: Howto write upgrade code for module

Post 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.
alby

Re: Howto write upgrade code for module

Post by alby »

You can see in Forum Made Simple too, there are many changed

Alby
Bigge

Re: [Solved]How to write upgrade code for module

Post 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!
Post Reply

Return to “Developers Discussion”