PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/content/server/framework/atlassian-sdk/upgrading-your-plugin-and-handling-data-model-updates.md

https://bitbucket.org/zchristmas/atlassian-sdk-docs
Markdown | 160 lines | 104 code | 56 blank | 0 comment | 0 complexity | 058559ee4fea2d69cbef3a0772fb750a MD5 | raw file
Possible License(s): LGPL-2.0
  1. ---
  2. aliases:
  3. - /server/framework/atlassian-sdk/upgrading-your-plugin-and-handling-data-model-updates-5669184.html
  4. - /server/framework/atlassian-sdk/upgrading-your-plugin-and-handling-data-model-updates-5669184.md
  5. category: devguide
  6. confluence_id: 5669184
  7. dac_edit_link: https://developer.atlassian.com/pages/editpage.action?cjm=wozere&pageId=5669184
  8. dac_view_link: https://developer.atlassian.com/pages/viewpage.action?cjm=wozere&pageId=5669184
  9. date: '2017-12-08'
  10. guides: guides
  11. legacy_title: Upgrading your plugin and handling data model updates
  12. platform: server
  13. product: atlassian-sdk
  14. subcategory: learning
  15. title: Upgrading your plugin and handling data model updates
  16. ---
  17. # Upgrading your plugin and handling data model updates
  18. Sometimes your data model will evolve. This page will describe how to handle upgrades for various use cases of model changes.{{% note %}}
  19. The current upgrade framework of the Active Objects plugin is not final yet and there might be some changes and adjustment before AO goes to version 1.0. We will however strive to make any change as simple as possible and with as little impact as possible.
  20. Any <a href="https://studio.atlassian.com/browse/AO" class="external-link">feedback you might have</a> using this framework is more than welcome.
  21. {{% /note %}}
  22. ## General usage
  23. ### Module definition
  24. You need to define your upgrade tasks in the module definition within the plugin descriptor. Use the `upgradeTask` element:
  25. **Module definition example**
  26. ``` xml
  27. <atlassian-plugin name="Hello World" key="example.plugin.helloworld" plugins-version="2">
  28. <plugin-info>
  29. <description>A basic Active Objects module test</description>
  30. <vendor name="Atlassian Software Systems" url="http://www.atlassian.com"/>
  31. <version>1.0</version>
  32. </plugin-info>
  33. <ao key="ao-module">
  34. <description>The AO module for this plugin.</description>
  35. <entity>com.myapp.MyEntity</entity>
  36. <upgradeTask>com.myapp.upgrades.v1.Version1UpgradeTask</upgradeTask>
  37. </ao>
  38. </atlassian-plugin>
  39. ```
  40. ### Upgrade task definition
  41. An upgrade task is defined as an implementation of the **`com.atlassian.activeobjects.external.ActiveObjectsUpgradeTask`** interface.
  42. It defines two methods that should be implemented:
  43. - `ModelVersion getModelVersion()`: defines the version of the model once this upgrade task will have run
  44. - `void upgrade(ModelVersion currentVersion, ActiveObjects ao)`: performs the actual upgrade
  45. ### Implementing the upgrade
  46. The upgrade method has 2 parameters that are important to understand well:
  47. - `ModelVersion currentVersion`, this is the current version of the model in the database. This allows for checking whether the upgrade can deal with the current model and take appropriate actions either way.
  48. - `ActiveObjects ao`, this is a version of a configured `ActiveObjects` service. There are several things to note about this instance:
  49. - It is not configured to handle any entities. The `migrate` method should be used to associate it with entities. This can be used to migrate to an intermediate model and perform some data migration (from one table to another for example).
  50. - It is not a shared instance. Each upgrade task gets its own `ActiveObjects` instance, in order to be able to handle different entity models/schemas. All those instance are also different to the one actually used by the plugin once the `ao` module has been fully initialised.
  51. Most of the time one upgrade task will be associated with one specific version of the model. So there will be a copy of the entities in a specific package for that version of the model.
  52. {{% warning %}}
  53. Warning
  54. If you are using a version of Active Objects earlier than 0.22.1, data belonging to any entities not listed in the `migrate` method call will be permanently deleted. If you are using Active Objects 0.22.1 or later versions, the data will not be deleted.
  55. {{% /warning %}}
  56. To be able to use that model, in the upgrade method, it is necessary to call the `migrate` method, with the list of entity classes.
  57. Once the model is available, it is very simple to manipulate the model and its data. This is all done using the provided `ActiveObjects` service as you would do in the plugin.
  58. {{% note %}}
  59. Note
  60. Ultimately the Active Objects module when initialised always updates the model/schema to the version defined by the entities declared.
  61. This means that any upgrade task should prepare the model/schema and data for this last update to happen successfully.
  62. {{% /note %}}
  63. ## Common upgrades
  64. ### Adding an entity/table
  65. There shouldn't be any upgrade to do when adding an entity/table to an existing model. This is unless you need to populate the new entities with some data.
  66. The Active Objects framework and plugin will take care of adding the table for you.
  67. ### Adding a field/column
  68. This is the same as adding an entity/table. Nothing to do!
  69. ### Removing an entity/table
  70. **Note:** When upgrading your plugin to a new version, do not remove entities/tables unless you are aware of the consequences. Active Objects will make the database match the entity interface in the Java code. It alters tables to match the current interface. Removing tables and columns will result in data loss. For that reason, we recommend that you do not delete tables or columns, only add them.
  71. With the above note in mind: Removing an entity/table is as simple as removing it from your model. In other words, simply don't declare it in your `ao` module descriptor. The Active Objects framework and plugin will drop the table.
  72. ### Removing a field/column
  73. **Note:** When upgrading your plugin to a new version, do not remove columns unless you are aware of the consequences. Active Objects will make the database match the entity interface in the Java code. It alters tables to match the current interface. Removing columns will result in data loss. For that reason, we recommend that you do not delete tables or columns, only add them.
  74. ### Renaming an entity/table
  75. The Active Objects framework doesn't know about renaming. If you change the name of an entity, it will remove the other entity and create a new one. All the data in the entity will be lost.
  76. Doing this in practice, you should create an upgrade task, keeping the old entity and creating the new one as a copy with a different name. Then copy the data over.
  77. The Active Objects plugin offers a simpler solution, using the `net.java.ao.schema.Table` annotation:
  78. **Renamed entity**
  79. ``` java
  80. @Table("MyEntity")
  81. public interface RenamedEntity extends Entity
  82. {
  83. ...
  84. }
  85. ```
  86. In this code sample, the entity was previously named `MyEntity` with its actual table name looking like `AO_XXX_MY_ENTITY`. We've now renamed the entity `RenamedEntity` keeping its table name the same using the `@Table` annotation.
  87. ### Renaming a field/column
  88. This behaves the same as for entity/table names. So the same precautions should apply when changing names. You can also rename a field without renaming its column.
  89. **RenamedField**
  90. ``` java
  91. public interface MyEntity extends Entity
  92. {
  93. @Mutator("Field")
  94. public void setNewField(String newField);
  95. @Accessor("Field")
  96. public String getNewField();
  97. }
  98. ```
  99. In this code sample, the `field` has been renamed `newField`. This is done through the `net.java.ao.Mutator` and `net.java.ao.Accessor` annotations.
  100. ### Changing from one data type to another
  101. If a plugin changes the data type of a column, Active Objects will try to automatically migrate that column. However, the recommended approach is **not** to use an in-place type conversion. Instead, create a new column and migrate the data during the upgrade process.
  102. See <a href="https://ecosystem.atlassian.net/browse/AO-273" class="external-link">AO-273</a>.
  103. ## Best practices
  104. See [Best practices for developing with Active Objects](/server/framework/atlassian-sdk/best-practices-for-developing-with-active-objects).