Data loss when upgrading plugin

Data loss when upgrading plugin

by Monika Gujar -
Number of replies: 15

Hello,

What I was doing till now, whenever I want to give my plugin to QA team for testing, I used to tell them that uninstall the previous 1 and intsall the new one. Because I was not having time (so many bugs was there in existing plugin) to do R&D on upgrade API.

But now I have to stop the data loss.

Now finally I have started reading upgrade API, but I just understood from that is, We have to change version in version.php and add new file in db folder as upgrade.php

But what to write in that file, Please help ASAP cool

Average of ratings: -
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers
As with most things in Moodle development, the best thing to do is to look at the upgrade.php file from another plugin. It is most commonly used to modify the database tables of the plugin. However if you are not making any changes that require actions like that, then you do not actually need to put anything in there, or even to have the file.

A good place to look is: /mod/quiz/db/upgrade.php
For the most part you will be using the data definition API
https://docs.moodle.org/dev/Data_definition_API
Average of ratings: Useful (2)
In reply to Justin Hunt

Re: Data loss when upgrading plugin

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

If you are changing any of the db structure during upgrade, you need to become familiar with the xmldb editor. In fact never change anything in install.xml or install.php without generating the code through the xmldb editor.

Average of ratings: Useful (2)
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by Monika Gujar -
If I am doing the code changes in any files according to the new requirement and I am not maintaining the upgrade.php file. Is it possible? If yes then, what end user has to do while installing the new updated plugin? What are the steps? Till now I was uninstalling the old plugin, It was working fine but issue was of loss of data i.e. the activities which are created using my plugin that all gets deleted automatically, after uninstallation of old plugin (installation of new plugin)
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I am not certain what you mean by maintaining the upgrade.php file. If you mean that you are not making any changes to the database, then upgrading without uninstalling should be straightforward, and involve no data loss. Just bump the value in version.php with the latest date e.g.

$plugin->version = 2019072200;

and keep clicking next when prompted.
Average of ratings: Useful (1)
In reply to Marcus Green

Re: Data loss when upgrading plugin

by Monika Gujar -
I have written like below in version.php

defined('MOODLE_INTERNAL') || die();
//$plugin->version = 20190524; //Plugin released on 24th May 2019 YYYYMMDD.
$plugin->version = 20190722; //updated.
$plugin->requires = 2018120303; // Moodle 3.6.0 is required.
$plugin->component = 'mod_tgwhiteboard'; // Declare the type and name of this plugin.

In case if I want to make any changes in database, then what I have to do.
Lets say, I have 1 table as tgwhiteboard containing 2 columns as id and name, and now I want to add 1 new column as date.
So, What should I do?

Thanks smile if there are no changes in DB then I don't have to do anything excpet changing the version. And it's working fine. The plugin is already exist, and I am installing upper version plugin. It keeps all the data big grin Tank you so much
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To add a column to a table, you need to use XMLDB Editor to load your install.xml file, add the column to the table, and use the "View PHP Code" function to generate the code that will go in your upgrade.php. You then save the changes to your install.xml file (so new installations will already have the extra column), and follow the instructions in that documentation to run the column-creation code when upgrading to your new version number,

Average of ratings: Useful (1)
In reply to Mark Johnson

Re: Data loss when upgrading plugin

by Monika Gujar -
Added UNIQUE index to the existing column 'name' and I am getting below code after clicking on View PHP Code

if ($oldversion < XXXXXXXXXX) {
// Define field id to be added to tgwhiteboard.
$table = new xmldb_table('tgwhiteboard');
$field = new xmldb_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
// Conditionally launch add field id.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Tgwhiteboard savepoint reached.
upgrade_mod_savepoint(true, XXXXXXXXXX, 'tgwhiteboard');
}


Exactly when I have to click on View PHP Code? Now I just clicked where all columns are displaying.
And what is XXXXXXXX ?
Which version I have to write there?
Below is the code of version.php

defined('MOODLE_INTERNAL') || die(); 
//$plugin->version = 20190524; //Plugin released on 24th May 2019 YYYYMMDD.
$plugin->version = 20190722; //updated.
$plugin->requires = 2018120303; // Moodle 3.6.0 is required.
$plugin->component = 'mod_tgwhiteboard'; // Declare the type and name of this plugin.
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You replace XXXXXXXXXX with the new version of your plugin, so in your case 20190722. The idea is that you might have one of these if statements in your upgrade.php for each version of your plugin, so someone can upgrade from any old version to the latest version, and it will run all the necessary upgrade steps.

If the only change you are making is to add the index, you should add the index to the field in XMLDB Editor, then click on "View PHP Code". Then you set "select action" to "Add index" and "Select field/key/index" to the name of your new index. Clicking "View" will generate the upgrade step to create this index.

Average of ratings: Useful (1)
In reply to Mark Johnson

Re: Data loss when upgrading plugin

by Monika Gujar -
Did the same. But my database is not getting updated (I can see in phpmyadmin), as install.xml
And that's why it's throwning an error as "Error writing to database". Because the column which I have deleted is still exist in database and I have changed 1 filed's datatype, still it is as it is like before. In short my database is not getting updated, after adding the code in upgrade.php
Main issue is I am not finding the deleted field, so how can I get the PHP code of that.

The thing is, till now what I did. Whenever I am doing any changes in my plugin I used to uninstall the existing one and again install the updated one. Which causes data loss. Because I was not maintaining the upgrade.php file and I have never did changes in version.php file. So it was working really very fine.

Then I came to know about upgrade.php, which keeps all the existing data.
But now the gap is, I have did so many changes in database. Even I have dropped some columns, changed length and added indexes and many more. Now all these changes are not coming in "View PHP code" option.

So, What can I do?
Please help

If you want then I can share the old version and new version of my plugin.
Give me your contact email
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Monika,

Where do you keep you plugin development code? Are you using github?

In reply to AL Rachels

Re: Data loss when upgrading plugin

by Monika Gujar -
No. I am not using github. But I can upload if you want to gothrough my plugin code.

https://github.com/monikagujar/TGWhiteboard/tree/monikagujar-patch-1
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Monika,

Sorry, I have not been able to look at the code for your plugin due to just had cataract surgery on my right eye. Not really feeling up to reading much of anything until today. I do not see anything at the page listed in your last entry. Any other place to look?

In reply to AL Rachels

Re: Data loss when upgrading plugin

by Monika Gujar -
It's okay
Take care

Sorry... the repository was private, now I made it public
In reply to Monika Gujar

Re: Data loss when upgrading plugin

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Monika,

Actually, if you look at the version files for other Moodle plugins, you will see that you are missing two digits from you version number.  For instance, a plugin I am working on shows this for yesterday:

$plugin->version  = 2019072200;      // The current module version (Date: YYYYMMDDXX).

The extra two digits, 00 in this example, allow me to bump the version up to 99 times on the same day, although personally, I've never went higher than 06.

Average of ratings: Useful (3)
In reply to AL Rachels

Re: Data loss when upgrading plugin

by Monika Gujar -
Yes. I did the change

defined('MOODLE_INTERNAL') || die();

//$plugin->version = 2019052400; //Plugin released on 24th May 2019 YYYYMMDD.
$plugin->version = 2019072200; //updated.
$plugin->requires = 2018120303; // Moodle 3.6.0 is required.
$plugin->component = 'mod_tgwhiteboard'; // Declare the type and name of this plugin.