DELETE FROM article WHERE legacyId IS NOT NULL;

INSERT INTO article (
    legacyId,
    legacySlug,
    legacyPicture,
    createdAt,
    updatedAt,
    name,
    lead,
    body
)

SELECT
    n.nid AS legacyId,
    SUBSTRING_INDEX(ua.alias, '/', -1) AS legacySlug,
    IF( fm.fid IS NOT NULL, JSON_OBJECT(
        'id', fm.fid,
        'name', fm.filename,
        'uri', fm.uri,
        'mime', fm.filemime,
        'size', fm.filesize,
        'timestamp', fm.timestamp,
        'alt', fi.field_image_alt,
        'width', fi.field_image_width,
        'height', fi.field_image_height
    ), NULL ) AS legacyPicture,
    FROM_UNIXTIME(n.created) AS createdAt,
    FROM_UNIXTIME(n.changed) AS updatedAt,
    n.title AS name,
    REGEXP_REPLACE( b.body_summary, '<[^>]+>', '' ) AS lead,
    b.body_value AS body
FROM raktar24_old.node AS n
JOIN raktar24_old.field_data_body AS b ON n.nid = b.entity_id AND b.bundle = 'article'
LEFT JOIN raktar24_old.url_alias AS ua ON ua.source = CONCAT('node/', n.nid)
LEFT JOIN raktar24_old.field_data_field_image AS fi ON n.nid = fi.entity_id AND fi.bundle = 'article'
LEFT JOIN raktar24_old.file_managed AS fm ON fi.field_image_fid = fm.fid
WHERE n.type = 'article';