|
| 1 | += TypeDB 2.x to TypeDB 3.x: migration process |
| 2 | + |
| 3 | +This page gives recommendations for server and driver upgrades between the two major versions of TypeDB, as well as a step-by-step example of such a migration. |
| 4 | + |
| 5 | +== Migration plan |
| 6 | + |
| 7 | +1. **Understand the scope of changes** by referring to the xref:{page-version}@manual::migration/2_to_3/diff.adoc[major changes] page. |
| 8 | +1. **Review the xref:{page-version}@manual::migration/2_to_3/diff.adoc#_temporarily_missing[missing features] list** before starting your migration. |
| 9 | +Some features might not yet be available in 3.x, and you may need to wait for a newer version to proceed. |
| 10 | +1. *(Optional)* Refresh your knowledge and adjust to the new versions of TypeDB and TypeQL by following the updated xref:{page-version}@home::quickstart.adoc[Quickstart Guide]. |
| 11 | +1. **Set up TypeDB 3.x** by following the xref:{page-version}@manual::configure/index.adoc[Configuration Manual]. |
| 12 | +1. **Migrate your TypeDB 2.x database** following the xref:#_step_by_step[step-by-step instructions below]. |
| 13 | +The setup process remains largely unchanged from 2.x. |
| 14 | +1. *(Optional)* Update your supporting queries using the xref:{page-version}@manual::schema/index.adoc[Schema] and xref:{page-version}@manual::CRUD/index.adoc[CRUD] manuals. |
| 15 | +1. *(Optional)* Update your drivers. |
| 16 | +Consult the xref:{page-version}@drivers::index.adoc[Drivers Manual] and the xref:{page-version}@manual::migration/2_to_3/diff.adoc#_major_changes_drivers[major driver changes] list for details. |
| 17 | + |
| 18 | +[#_step_by_step] |
| 19 | +== Step-by-step example |
| 20 | + |
| 21 | +This section showcases a step-by-step example of migrating a database between TypeDB 2.x and TypeDB 3.x using xref:{page-version}@manual::maintenance/export_import.adoc[major changes][export and import]. |
| 22 | + |
| 23 | +This example uses a database called `social-network`. |
| 24 | +It contains an arbitrary set of data and the 2.x schema provided below. |
| 25 | + |
| 26 | +[#_schema_reference] |
| 27 | +.social-network's 2.x schema |
| 28 | +[%collapsible] |
| 29 | +==== |
| 30 | +.social-network.typeql |
| 31 | +[,typeql] |
| 32 | +---- |
| 33 | +include::{page-version}@manual:resources:partial$migration/2_to_3/schema-2x.tql[tag=full-query] |
| 34 | +---- |
| 35 | +==== |
| 36 | + |
| 37 | +[#_prepare_data] |
| 38 | +=== Optional: resolve data conflicts |
| 39 | + |
| 40 | +While most of the changes in TypeDB 3.x affect only the schema, there is also one very important change in data, which can lead to errors on the <<_import, import stage>> of this guide. |
| 41 | + |
| 42 | +**Attributes can no longer own attributes** in TypeDB 3.x. |
| 43 | +If your data contains attributes owning attributes, you will see an error like: |
| 44 | + |
| 45 | +[source] |
| 46 | +---- |
| 47 | +Invalid migration item received: attributes cannot own attributes in this version of TypeDB (deprecated). |
| 48 | +---- |
| 49 | + |
| 50 | +In this situation, an additional step is required before proceeding, and it will require modifications of the original 2.x's database. |
| 51 | + |
| 52 | +==== Move attributed owned by attributes |
| 53 | + |
| 54 | +This is an example of migration of attributes owned by attributes for the <<_schema_reference, referenced schema>>. |
| 55 | + |
| 56 | +Our goal is to move `bio-version` from `bio` to `page` owning the `bio` attributes. |
| 57 | +This way, both `bio` and `bio-version` will be attached to the same entity (note that it's only one of the ways to redesign this schema). |
| 58 | + |
| 59 | +First, using a schema write transaction, define the additional required trait of `page`: |
| 60 | + |
| 61 | +[,typeql] |
| 62 | +---- |
| 63 | +include::{page-version}@manual:resources:partial$migration/2_to_3/attribute-migration-queries.tql[tag=step1-define-entity-ownership] |
| 64 | +---- |
| 65 | + |
| 66 | +Then, using a data write transaction, perform the data moving between `bio`s and `page`s: |
| 67 | + |
| 68 | +[,typeql] |
| 69 | +---- |
| 70 | +include::{page-version}@manual:resources:partial$migration/2_to_3/attribute-migration-queries.tql[tag=step2-move-data] |
| 71 | +---- |
| 72 | + |
| 73 | +Finally, verify that the data is moved correctly. |
| 74 | +As an additional step of verification, you can try undefining the original ownership to verify that it's now redundant: |
| 75 | + |
| 76 | +[,typeql] |
| 77 | +---- |
| 78 | +include::{page-version}@manual:resources:partial$migration/2_to_3/attribute-migration-queries.tql[tag=step3-undefine-attribute-ownership] |
| 79 | +---- |
| 80 | + |
| 81 | +Now your data is ready to get migrated to TypeDB 3.x! |
| 82 | + |
| 83 | +[#_export] |
| 84 | +=== Export the database from TypeDB 2.x |
| 85 | + |
| 86 | +Use xref:2.x@manual::configuring/export.adoc#_export[2.x export] to export your TypeDB 2.x database as a schema definition file and a data file. |
| 87 | + |
| 88 | +While your server is running, execute `typedb server export` using the same server binary: |
| 89 | + |
| 90 | +[source,console] |
| 91 | +---- |
| 92 | +typedb server export --database=social-network --port=1729 --schema=social-network.typeql --data=social-network.typedb |
| 93 | +---- |
| 94 | + |
| 95 | +[#_prepare_schema] |
| 96 | +=== Prepare the new schema |
| 97 | + |
| 98 | +**Manually update your schema definition file.** |
| 99 | + |
| 100 | +- The xref:{page-version}@manual::migration/2_to_3/diff.adoc[major changes] page highlights the most common updates needed. |
| 101 | +- For simplicity, rules can be completely removed. |
| 102 | +Additional queries can be run to define functions as their replacements later. |
| 103 | + |
| 104 | +This step can require a couple of iterations until you reach the final state of your new schema you will be satisfied with. |
| 105 | +Use error messages during application to guide adjustments. |
| 106 | + |
| 107 | +This is a sample example of the updated ``social-network``'s schema, written in TypeQL 3.x: |
| 108 | + |
| 109 | +.social-network's 3.x schema |
| 110 | +[%collapsible] |
| 111 | +==== |
| 112 | +.social-network-3x.typeql |
| 113 | +[,typeql] |
| 114 | +---- |
| 115 | +include::{page-version}@manual:resources:partial$migration/2_to_3/schema-3x.tql[tag=full-query] |
| 116 | +---- |
| 117 | +
|
| 118 | +Noticeable highlights: |
| 119 | +
|
| 120 | +- Attribute types can be subtyped even if they are not abstract. |
| 121 | +- Attributes can no longer own attributes. |
| 122 | +See `bio-version` for an example of such redesign. |
| 123 | +- `long` is now `integer`. |
| 124 | +- We can restrict cardinalities of `relates`, `owns`, and `plays` using the `@card` annotation. |
| 125 | +Default cardinality values protect your data, but they may not suite your needs. Consider overriding them explicitly. |
| 126 | +- We can redeclare traits like `owns` on subtypes to put additional constraints on them. |
| 127 | +- While there are no `as` keyword for `owns` and `plays`, in most cases, like ours, it's not needed. |
| 128 | +E.g., the previously overridden `owns id` does not require overriding in 3.x as it's an ownership of an abstract attribute, so it cannot be instantiated without type specialization (e.g., `owns post-id`). |
| 129 | +- More constraints can be moved from your application logic to the schema: see the implication of `@values`, an enum-like constraint of TypeQL. |
| 130 | +
|
| 131 | +If you want to put data constraints, but your data does not yet satisfy them, you can start with a relaxed schema and then add the constraints required via additional `define` queries. |
| 132 | +==== |
| 133 | + |
| 134 | +[#_import] |
| 135 | +=== Import the database into TypeDB 3.x |
| 136 | + |
| 137 | +Launch a xref:{page-version}@manual::install/index.adoc[TypeDB 3.x server] and connect to it using a compatible xref:{page-version}@manual::tools/console.adoc[TypeDB Console]. |
| 138 | + |
| 139 | +In TypeDB Console, execute a `database import` command: |
| 140 | + |
| 141 | +[source,console] |
| 142 | +---- |
| 143 | +database import social-network /path-to-updated-schema/social-network-3x.typeql /path-to-2.x-export/social-network.typedb |
| 144 | +---- |
| 145 | + |
| 146 | +[NOTE] |
| 147 | +==== |
| 148 | +You can operate other server's databases without restrictions while the new database is being imported. |
| 149 | +==== |
| 150 | + |
| 151 | +In case of successful migration, you will see: |
| 152 | + |
| 153 | +[source] |
| 154 | +---- |
| 155 | +Successfully imported database. |
| 156 | +---- |
| 157 | + |
| 158 | +Otherwise, a descriptive error will be shown. |
| 159 | +After fixing this error, retry the same command. |
| 160 | + |
| 161 | +[#_after_import] |
| 162 | +=== After import |
| 163 | + |
| 164 | +From this point, your database is completely functional. |
| 165 | +Its schema and data are synchronized and technically correct. |
| 166 | + |
| 167 | +We recommend running a few read queries to verify that the migration was logically successful and familiarize yourself with the updated TypeQL and your new schema. |
| 168 | + |
| 169 | +If necessary, define TypeQL functions and update any queries that previously relied on the removed rules to use the new functions instead. |
| 170 | + |
| 171 | +Visit xref:{page-version}@manual::migration/2_to_3/diff.adoc[] and xref:{page-version}@manual::index.adoc[] to learn more about TypeDB 3.x. |
| 172 | +Enjoy your journey! |
| 173 | + |
| 174 | +[#_having_troubles] |
| 175 | +== Having troubles? |
| 176 | + |
| 177 | +For troubleshooting tips and guidance, refer to the xref:{page-version}@manual::troubleshooting/index.adoc[Troubleshooting Manual]. |
0 commit comments