Friday, September 7, 2018

MySQL 搬移大量資料 (import/export)

工作上會需要做 MySQL 搬移大量資料,因為 development 環境沒有足夠的測試資料,我必須從 production 的資料庫搬過去。

本來打算用 MySQL Workbench 的 export/import 功能。但是,由於需要搬的資料多達 3 MB 。匯出還可以使用 MySQL Workbench ,匯入的話,就非常非常慢,完全是慢到我受不了。所以我決定改用 LOAD DATA LOCAL INFILE

真的操作指令時,還是遇到了一些問題:
沒有仔細去計較 CSV 檔的 FIELDS TERMINATED BY CHARACTER 或是 FIELDS ENCLOSED BY CHARACTER 。資料就是無法匯入成功。

最後實驗成功的指令記錄如下:

Wednesday, September 5, 2018

The Legacy Code Change Algorithm

  • Identify change point
  • Identify test point
  • Break dependencies
  • Write test
  • Make change and refactor
Notice that the LAST thing you do is write new code. Most of your work with legacy code is understanding and carefully selecting where to make changes so as to minimize risk and minimize the amount of new test code you have to write to make your target change safely.

參考資料

Tuesday, September 4, 2018

Testability/Dependency Injection in Clojure

在 OOP 語言的時候,因為要處理「可測試性」的問題,常常需要去考慮 dependency injection。但是, Clojure 已經有了 with-redefs 這種可以將任意的 function 都變成 software seams 的 language syntax 了,還需要特別寫 DI 嗎?

上網查了一下,各家有各家的說法,我個人比較認同的說法是這樣子:
原則上不太需要用 DI 去處理 external dependency 的問題,用 with-redefs 即可以。然而,如果某個外部資源它有 life cycle management 的問題,最好要用 component 或是 mount 來處理。

要達成 Testability 不一定要用 DI。有一篇文章 Testing the Clojure way 指出了這一點:
如果在 C# 之類的靜態語言,要做出類似 with-redefs 的語意的話,則需要用 preprocessing seams 的技巧來替換 dependency

再從另一個觀點來看,在 let over lambda 一書指出 let over lambda 其實就是 object 。而 lambda over let over lambda 可以看成是 anonymous class 。順著這個邏輯去想, dependency injection 也可以視為是 higher order function 的一種語意應用。