contentful / contentful-database-importer.rb

Adapter to extract data from SQL Databases https://www.contentful.com
MIT License
23 stars 10 forks source link

References end up as JSON or are mixed up #19

Open andrezimpel opened 6 years ago

andrezimpel commented 6 years ago

I'm currently trying to migrate a Wordpress blog to contentful. The new contentful space will have the content model posts which has a many reference field "sections" which will then consist of different other content models (text, youtube embed etc). To make the import fairly simple I've also added a "legacy content" model which has a text "html" field. The import should now take all posts, import them into contentful with their title and slug and then use the wordpress html content to create the legacy content entries. Those entries should be connected to the post of course.

So the legacy content entry with the id 555 should be linked to the sections field of the post with the id 555.

require 'contentful/database_importer'

class LegacyContent
  include Contentful::DatabaseImporter::Resource

  self.table_name = 'dQiVHYroposts' # Optional - By default it's the class name in snake case. E.g. 'my_table'
  self.content_type_id = 'legacyContent' # Optional - By default it's the class name in snake case. E.g 'my_table'
  self.content_type_name = 'Legacy Content' # Optional - By default it's the class name

  self.query = "post_type = 'post' AND post_status = 'publish' AND post_content != ''"

  id Contentful::DatabaseImporter::IdGenerator::Base#, template: '{{content_type_id}}_{{id}}'

  field :post_title, maps_to: :title, type: :string
  field :post_content, maps_to: :html, type: :text
end

class Posts
  include Contentful::DatabaseImporter::Resource

  self.table_name = 'dQiVHYroposts' # Optional - By default it's the class name in snake case. E.g. 'my_table'
  self.content_type_id = 'post' # Optional - By default it's the class name in snake case. E.g 'my_table'
  self.content_type_name = 'Post' # Optional - By default it's the class name

  self.query = "post_type = 'post' AND post_status = 'publish' AND post_content != ''"

  id Contentful::DatabaseImporter::IdGenerator::Base#, template: ''#, template: '{{content_type_id}}_{{id}}'

  field :post_title, maps_to: :title, type: :string
  field :post_name, maps_to: :slug, type: :string
  field :post_excerpt, maps_to: :sharing_description, type: :text
  field :sections, type: LegacyContent, relationship: :many, id_field: :id, key: :id
end

Contentful::DatabaseImporter.setup do |config|
  config.space_name = 'testspace'
  config.space_id = '3bh67gu8ux38'
  config.database_connection = 'mysql2://root@localhost:3306/diffus'
end

What I struggle with is using this code the sections will not end up as references but as JSON objects and the contentful web app then says that the widget for that type is not available.

If I remove the sections field after that and add is as a many reference field and run the import (udpate_space!) again the legacy content will be referenced with in the sections field of each post BUT post_0 will have legacy_content_0, post_1 legacy_content_0 and so on.

Currently:

[
      {
        "sys": {
          "id": "post_0"
        },
        "fields": {
          "title": "Impressum",
          "sharing_description": "",
          "slug": "impressum",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_0"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_1"
        },
        "fields": {
          "title": "Teilnahmebedingungen für Gewinnspiele",
          "sharing_description": "",
          "slug": "gewinnspiele-teilnahmebedingungen",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_0"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_2"
        },
        "fields": {
          "title": "Kontakt",
          "sharing_description": "",
          "slug": "kontakt",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_0"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_3"
        },
        "fields": {
          "title": "Jobs",
          "sharing_description": "",
          "slug": "jobs",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_0"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_4"
        },
        "fields": {
          "title": "Praktikant Produktion (m/w), Berlin",
          "sharing_description": "",
          "slug": "praktikum-produktion",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_0"
            }
          ]
        }
      }
    ]

Should be:

[
      {
        "sys": {
          "id": "post_0"
        },
        "fields": {
          "title": "Impressum",
          "sharing_description": "",
          "slug": "impressum",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_0"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_1"
        },
        "fields": {
          "title": "Teilnahmebedingungen für Gewinnspiele",
          "sharing_description": "",
          "slug": "gewinnspiele-teilnahmebedingungen",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_1"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_2"
        },
        "fields": {
          "title": "Kontakt",
          "sharing_description": "",
          "slug": "kontakt",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_2"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_3"
        },
        "fields": {
          "title": "Jobs",
          "sharing_description": "",
          "slug": "jobs",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_3"
            }
          ]
        }
      },
      {
        "sys": {
          "id": "post_4"
        },
        "fields": {
          "title": "Praktikant Produktion (m/w), Berlin",
          "sharing_description": "",
          "slug": "praktikum-produktion",
          "sections": [
            {
              "linkType": "Entry",
              "id": "legacyContent_4"
            }
          ]
        }
      }
    ]
dlitvakb commented 6 years ago

Sorry for taking so long, I'll take a look into this as soon as possible.

Cheers