Closed joaquinbravo-natgeo closed 5 years ago
/cc @shannonbux @amberleyromo
An example repo, with those files already in there, is here: https://github.com/jackbravo/joaquin.axai.mx
should I create a PR instead? (not that I have subscribed to the #Hacktoberfest :-p)
@joaquinbravo-natgeo Yes, please! A PR to add this into the blog would be excellent. Check out https://www.gatsbyjs.org/docs/how-to-contribute/#contributing-to-the-blog for details.
Thanks!
How to migrate from Drupal to Gatsby
The motivation for this blog post was the need to reduce the cost of maintaining a simple brochure or blog site. When using Drupal you need at least a shared hosting (there is no wordpress.com for drupal sites). So migrating to a static site generator like jekyll seemed like a good idea. Gatsby can do this too, and it is also a great opportunity to learn react and then get hosting for free using something like github pages. So this post is going to describe how to migrate a simple blog that has featured images on the posts, comments and tags.
To facilitate exporting the site, the first thing I did was exporting the database from the mysql database server to an sqlite file that I could use locally. To do this I used the mysql2sqlite project, which, as described on the project page, can be done with two commands like:
Our site is a simple blog so we can use the excellent gatsby-starter-blog project, so we will create our project and then add a sqlite library as a dev dependency:
Now, it is a matter of knowing your database. The useful commands on an sqlite3 command line to explore it are
.tables
to see all tables (duh) and.schema table_name
to see information about a specific table. Oh! and.help
to know more.So, we will be creating a new file on our project at
src/scripts/migrate.js
, and initially what we want is to iterate through all our posts and export basic data like title, created date, body and status (published or draft). All of that data is in two tables, the node table and the _field_databody. So initially our script will look like this:The interesting thing here is the initial query, and this is based on a Drupal 7 database, a Drupal 8 or Drupal 6 database could be different, so check your schema. Next, lets see how we can get the tags on a simple Javascript array. So, each post can have more than one tag, so we can take advantage of better-sqlite .pluck() function, that retrieves only the first column of a database query, and the .all() function, that retrieves all rows in a single array:
For the image, we will retrieve only the URL of the image, so we can download it and store it locally. And we will replace
public://
for the URL path of the images folder on our old site:And now we have all the data we wanted, so it is just a matter of creating a file with the frontmatter data and the body of the text. Luckily our Drupal blog is also using markdown. Else we would have to look for an HTML to Markdown Javascript library.
We now can import all our blog posts to markdown files with frontmatter metadata by running this command:
To export the comments we can use a service like disqus on our static site. Disqus has an import process that uses a custom XML import format based on the WXR (WordPress eXtended RSS) schema. So the process would be the same. Create a script named
src/scripts/export_comments.js
to query the database and, in this case, write a single file containing all the comments of our old site:Run
node src/scripts/export_comments.js ../mysqlite.db > comments.xml
and that's it. You now have all your posts and all your comments ready to be used on your gatsby starter blog.