# AMANDA: A Middleware for Automatic Migration between Different Database Paradigms
## 3 The Middleware
```
1{
2\"edges\":[{
3\"Source table on Releational DB
4\"source\":{
5\"tableName\":\"products\",
6\"orderField\":\"ProductID\",
7\"primaryKey\":\"ProductID\",
8\"foreignKey\":\"SupplierID\"
9},
10//Target vertex on Graph DB
11//(Product)--[Product.supplier]-->(Supplier)
12\"target\":{
13\"leftVertexName\":\"Product\",
14\"edgeName\":\"Product.supplier\",
15\"rightVertexName\":\"Supplier\"
16}
17}
18...
19}
20}
```
Listing 3: Example of Data migrated to a graph with RDF Syntax. ```
1//Exampleofnodesinthegraph,migratedfromPostgres
2<Customer_ALFKI><Customer_CustomerID>-ALFKI\". 3<Customer_ALFKI><Customer.CompanyName>\"AlfredsFutterkist\". 4<Customer_ALFKI><Customer.ContactName>\"MariaAnders\". 5<Customer_ALFKI><Customer.ContactTitle\"SalesRepresentative\". 6
7//Exampleofanedgecalledcustomer,betweenOrder10702 andCustomerALFKI
8<Order_10702><Order.customer><Customer_ALFKI>. ```
Listing 4: SQL Queries. ```
1//Querytogetattributesfrom=table
2 query=f'SELECT{cols}FROM{table.name}LMIT{limit}OFFSET{offset};'
3//Thesameoftheabovequery,butwithordering
4 query=f'SELECT{cols}FROM{table.name}ORDERBY{order_by}
5 ASCLDMIT{limit}OFFSET{offset};'
6
7//Usedtogetattributesfromtwotables,thatwillbeconsolidatedinonegraphnode
8 join_str=f'INNERJOIN{join_table}ON{table}.(src_fk)={join_table}. (dst_pk)\"
9//Usedtobuildedgesbasedonthetablesrelationships
10 join_str=f'INNERJOIN{join_table}ON
11{table}.{join[\"src_fk\"]}={join_table}. {join[\"dst_pk\"]}\"In Listing 4, the variables _cols_, _table_name_, _table_, _src_fk_, _join_table_ and _dst_pk_ are obtained from the _schema.json_ file, which has its syntax explained in the next paragraphs. With the queries, tables can be converted into graph nodes, and relationships into graph edges, as shown in Figure 2.  
Through the _schema.json_ file, the middleware is aware of which data must be migrated. Such a file holds a syntax that correctly describes the data properties and relations. Listing 1 demonstrates an example of vertex syntax. In Listing 1, _vertices_ is an array in which \(N\) vertices can be defined. A vertex definition is composed of three property definitions, namely: (I) source--sets _tableName_ to the table's name in the source database (e.g., Postgres), and _orderField_ to the result order; (II) target--sets _vertexName_ to the vertex's name in the target database (e.g., Dgraph). Given that Dgraph makes use of an ID field (analog to a primary key), a value must be provided as _vertexID_, which can be composed of \(N\) attributes; and (III) fields--a property that defines all of the attributes that are expected to be migrated from the source database (Postgres) to the target database (DGraph). Once the vertices are created, it makes sense to connect them. In a graph, a connection between vertices is called an edge. An edge represents a relationship between vertices in the same way that a foreign key represents a relationship between tables. In the _schema.json_ file, edges are defined in the syntax shown in Listing 2. In Listing 2, _edges_ is an array so that \(N\) edges can be defined. To define an edge, two properties must be defined: (I) source--data of the source database, which are needed to build the edge. _PrimaryKey_ is the edge's starting point, _foreignKey_ is the edge's ending point. Both data are found in the _tableName_ field; (II) target--edge configuration in the target database. Here is defined where the edge begins (_leftVertexName_) and ends (_rightVertexName_), and the edge's name (_edgeName_). The examples presented in Listings 1 and 2 illustrate a migration from Postgres to Dgraph, following the UML diagram in Figure 3.  
Figure 2: AMANDA workflow. (**A**) query tables, (**B**) create nodes in graph and (**C**) make relationships between nodes. orders_details is an associative table; it is converted into an edge in the graph. The architecture presented in Figure 1 can be extended to support migration between other source RDBMS and target graph databases, e.g., migration from MySQL to Neo4J.