Refactoring: Adding a New Field or Property
Refactoring: Adding a New Field or Property
How hard is it to refactor fields or properties in graph databases as opposed to SQL databases? Read on to find out.
Join the DZone community and get the full member experience.
Join For FreeDownload "Why Your MySQL Needs Redis" and discover how to extend your current MySQL or relational database to a Redis database.
A couple of months ago I presented a webinar comparing how you’d model and evolve a data model using a Postgres SQL database and Neo4j.
This is what the two data models looked like after the initial data import and before any refactoring/migration had been done:
Relational
Graph
I wanted to add a ‘nationality’ property to the players table in the SQL schema and to the nodes with the ‘Player’ label in the graph.
This refactoring is quite easy in both models. In the relational database we first run a query to add the ‘nationality’ field to the table:
ALTER TABLE players
ADD COLUMN nationality VARYING(30);
And then, we need to generate UPDATE statements from our data dump to update all the existing records:
UPDATE players
SET nationality = 'Brazil'
WHERE players.id = '/aldair/profil/spieler/4151';
...
In the graph, we can do this in a single step by processing our data dump using the LOAD CSV command and then setting a property on each player:
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///transfers.csv" AS row
MATCH (player:Player {id: row.playerUri})
SET player.nationality = row.playerNationality
If we wanted to make the nationality field non-nullable we could go back and run the following queries:
ALTER TABLE players
ALTER COLUMN nationality SET NOT NULL
CREATE CONSTRAINT ON (player:Player)
ASSERT exists(player.nationality)
And we’re done!
Read "Developing Apps Using Active-Active Redis Enterprise" and discover the advantages over other active-actve databases.
Published at DZone with permission of Mark Needham , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}