使用者無縫接合 轉移百萬筆資料 · 5年 drupal 開發經驗 drupal camp taipei 2011...

39
轉移百萬筆資料 + 使用者無縫接合 201376Drupal Camp Taipei 2013

Upload: others

Post on 17-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料 +

使用者無縫接合

2013年7月6日 Drupal Camp Taipei 2013

Page 2: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

About me

● Joe Tsui,香港人● 5年 Drupal 開發經驗● Drupal Camp Taipei 2011 講者● Imagefield_crop maintainer● 成立了 ,接洽 Drupal 專案● 為香港最大的英文報紙(scmp) 和信報負責

Drupal 開發● 香港最活躍 Drupal blogger 之一● Hong Kong Drupal User group 成員

Page 3: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

WarningProgramming ahead

Page 4: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料

● 1,200,000+ entity in total● Data from 2008● 12G images● do not exists user table!

Page 5: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料

● Source● Destination● Key Mapping● Field Mapping● Migration flow● PrepareRow● Prepare● Complete, CompleteRollback● Test run● Rollback● Tricks

Page 6: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料

Legacy system Drupal

Migrate module (v2.5)

Page 7: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料

Legacy system● Article● Comment● User●● Banned user● Image, File

Drupal● Node● Comment● User● User Picture● Banned user● Image, File

Source Destination

Page 8: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料 - Source

● MigrateSourceSQL● MigrateSourceCSV● MigrateSourceList (XML)● MigrateSourceMSSQL● MigrateSourceOracle

Page 9: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料 - Destination

● MigrateDestinationNode● MigrateDestinationComment● MigrateDestinationUser● MigrateDestinationRoles● MigrateDestinationTable

Page 10: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料

● Source● Destination● Key Mapping● Field Mapping● Migration flow● PrepareRow● Prepare● Complete, CompleteRollback● Test run● Rollback● Tricks

Mappings

Page 11: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Key Mappings

Legacy: Article id: 15368

Node:Nid : 15368

Unique identifier as Key

● 一對一的關係● 唯一的 source entity● 對應一個 destination entity

Page 12: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Key Mappings

Example: {node}

Page 13: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Field Mappings/admin/content/migrate/ForumArticle

Page 14: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Migration flow

1. Migrate::__construct()2. Migrate::prepareRow()3. Migrate::prepare()4. Migrate - DB writes5. Migrate::complete()

Page 15: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料

● Source● Destination● Key Mapping● Field Mapping● Migration flow● PrepareRow● Prepare● Complete, CompleteRollback● Test run● Rollback● Tricks

Extra customization

Page 16: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

prepareRow($row)

Entity(destination) Row(source)

Page 17: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

prepare($entity, $row)

● Connect to other migration (user, user picture)

Page 18: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

complete($entity, $row)

Missing parts in other tables

Page 19: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

轉移百萬筆資料

● Source● Destination● Key Mapping● Field Mapping● Migration flow● PrepareRow● Prepare● Complete, CompleteRollback● Test run● Rollback● Tricks

Run time

Page 20: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Test run

● drush mi ForumArticle --limit="10 items"● Tip: Add order by● Tip: Add condition to MigrateSourceSQL to

pin point specific data

Conditions

Page 21: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Rollback

● Rollback 比 import 慢得多● Rollback on 1M+ rows● 在 import 之前用 sql-dump 建立原始狀態

snapshot● mysql < backup.sql to mass “rollback”

Page 22: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

tips 1

● Test 策略: 找出 data 的所有組合○ post with 1 image○ post with 2 images○ deleted post○ "spammed" post○ "comment closed" post

● 不移動 image ○ preserve_files')->defaultValue(TRUE);○ file_replace')->defaultValue(MigrateFile::

FILE_EXISTS_REUSE);● 預先將 files 放到 files/legacy

Page 23: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

tips 2

● user picture -> fid○ 用戶頭像是 {user} 內的一個 fid○ 先建立 files, 再 import User 之後在 prepare() 插入 fid

● field_image[] for multiple values

$this->addFieldMapping('field_image', 'field_image_value');//prepareRow($row)

$row->field_image_value[] = substr(trim($image_path), 1);

Page 24: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

使用者無縫接合

例子● auth.joe.com● forum.joe.com● news.joe.com

Page 25: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

使用者無縫接合

● Active action○ Login API○ Provider side○ Active logout○ Provider side

● Passive action○ cookie API○ passive logout○ Drupal side

Page 26: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Active Action - login APIforum.joe.com/user auth.joe.com/auth/login

Auth?

New user?

Register

set cookie

user_login_submit()

submit

No

Yes

Yes

No

Page 27: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Active Action - login APIforum.joe.com/user auth.joe.com/auth/login

Auth?

New user?

Register

set cookie

user_login_submit()

submit

No

Yes

Yes

No

Page 28: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Active Action - login APIforum.joe.com/user auth.joe.com/auth/login

submit

Page 29: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Active Action - login APIforum.joe.com/user auth.joe.com/auth/login

Auth?

New user?

Register

set cookie

user_login_submit()

submit

No

Yes

Yes

No

Page 30: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Active Action - login APINew user?

Register

set cookie

Yes

No

Register

Page 31: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Cookie

● auth.joe.com● forum.joe.com● news.joe.com

● Set cookie

● $cookie_domain = '.joe.com'

Page 32: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

auth.joe.com (Provider)Active Action - login API

forum.joe.com/user auth.joe.com/auth/login

Auth?

New user?

Register

set cookie

user_login_submit()

submit

No

Yes

Yes

No

Save Session

Cookie value agreement

news.joe.com

Page 33: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Active action - logout

● clear cookie● Other sites should check cookie on page

loads

hook_init()

Page 34: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Passive Action - cookie APIforum.joe.com/node/123 auth.joe.com/auth/authenticate

Auth?

New user?

Register

set cookie

user_login_submit()

found a cookie

No

Yes

Yes

No

Page 35: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Passive Action - cookie APIforum.joe.com/node/123 auth.joe.com/auth/authenticate

Auth?

New user?

Register

set cookie

user_login_submit()

found a cookie

No

Yes

Yes

No

Page 36: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Passive Action - cookie APIforum.joe.com/node/123 auth.joe.com/auth/authenticate

found a cookie

Page 37: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

Passive action - logout

● Drupal logged in● on page loads, cookie value not found● logout Drupal as well

Page 38: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

使用者無縫接合 tips

● Browser private mode● Chrome develop resources tab to check

cookie● login tester form

Page 39: 使用者無縫接合 轉移百萬筆資料 · 5年 Drupal 開發經驗 Drupal Camp Taipei 2011 講者 Imagefield_crop maintainer 成立了 ,接洽 Drupal 專案 為香港最大的英文報紙(scmp)

現場示範-網站改造馬拉松

● Ubuntu 正體中文站

● http://www.ubuntu-tw.org/