Repair steps

Repair steps are methods which are executed by Nextcloud on certain events which directly affect the app. You can use these repair steps to run code when your app is installed, uninstalled, upgraded etc. It’s called repair steps because they are frequently used to fix things automatically.

Note

Don’t use the install.php files anymore! This method is deprecated and is known to cause issues.

Creating a repair step

A repair step is an implementation of the OCP\Migration\IRepairStep interface. By convention these classes are placed in the lib/Migration directory. The following repairstep will log a message when executed.

  1. <?php
  2. namespace OCA\MyApp\Migration;
  3. use OCP\Migration\IOutput;
  4. use OCP\Migration\IRepairStep;
  5. use Psr\Log\LoggerInterface;
  6. class MyRepairStep implements IRepairStep {
  7. /** @var LoggerInterface */
  8. protected $logger;
  9. public function __construct(LoggerInterface $logger) {
  10. $this->logger = $logger;
  11. }
  12. /**
  13. * Returns the step's name
  14. */
  15. public function getName() {
  16. return 'A demonstration repair step!';
  17. }
  18. /**
  19. * @param IOutput $output
  20. */
  21. public function run(IOutput $output) {
  22. $this->logger->warning("Hello world from MyRepairStep!", ["app" => "MyApp"]);
  23. }
  24. }

Outputting information

A repair step can generate information while running, using the OCP\Migration\IOutput parameter to the run method. Using the info and warning methods a message can be shown in the console. In order to show a progressbar, firstly call the startProgress method. The maximum number of steps can be adjusted by passing it as argument to the startProgress method. After every step run the advance method. Once all steps are finished run the finishProgress method.

The following function will sleep for 10 seconds and show the progress:

  1. <?php
  2. /**
  3. * @param IOutput $output
  4. */
  5. public function run(IOutput $output) {
  6. $output->info("This step will take 10 seconds.");
  7. $output->startProgress(10);
  8. for ($i = 0; $i < 10; $i++) {
  9. sleep(1);
  10. $output->advance(1);
  11. }
  12. $output->finishProgress();
  13. }

Register a repair-step

To register a repair-step in Nextcloud you have to define the repair-setp in the info.xml file. The following example registers a repair-step which will be executed after installation of the app:

  1. <?xml version="1.0"?>
  2. <info xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
  4. <id>myapp</id>
  5. <name>My App</name>
  6. <summary>A test app</summary>
  7. ...
  8. <repair-steps>
  9. <install>
  10. <step>OCA\MyApp\Migration\MyRepairStep</step>
  11. </install>
  12. </repair-steps>
  13. </info>

Repair-step types

The following repair steps are available:

  • install This repair step will be executed when installing the app. This means it is executed every time the app is enabled (using the Web interface or the CLI).
  • uninstall This repair step will be executed when uninstalling the app, and when disabling the app.
  • pre-migration This repair step will be executed just before the database is migrated during an update of the app.
  • post-migration This repair step will be executed just after the database is migrated during an update of the app. This repair step will also be executed when running the occ maintenance:repair command
  • live-migration This repair step will be scheduled to be run in the background (e.g. using cron), therefore it is unpredictable when it will run. If the job isn’t required right after the update of the app and the job would take a long time this is the best choice.