Skip to content

Instantly share code, notes, and snippets.

@5000164
Last active August 29, 2015 14:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save 5000164/1276251388acda1033e4 to your computer and use it in GitHub Desktop.
FuelPHP でデータを YAML 形式でインポート/エクスポートするタスク
<?php
namespace Fuel\Tasks;
class DB
{
public static function export()
{
try {
$tables = \DB::list_tables();
$export_base_dir = APPPATH . 'tmp/';
$export_dir_name = \Date::forge()->format('%Y%m%d%H%M%S');
\File::create_dir($export_base_dir, $export_dir_name);
foreach ($tables as $table) {
$data = \DB::select('*')->from($table)->execute()->as_array();
$table_data = array($table => $data);
$yaml_content = \Format::forge($table_data)->to_yaml();
$export_dir = $export_base_dir . $export_dir_name . '/';
$export_file_name = $table . '.yml';
\File::create($export_dir, $export_file_name, $yaml_content);
}
} catch (\Exception $e) {
\Log::error($e);
echo 'failure.' . "\n";
throw $e;
}
echo 'data was exported.' . "\n";
}
public static function import($data_dir = null)
{
if (empty($data_dir)) {
$data_dir = __DIR__ . '/../../../setup/initial_data/';
}
try {
$files = \File::read_dir($data_dir);
// 外部キー制約を一時的に外す
\DB::query('set foreign_key_checks=0;')->execute();
\DB::start_transaction();
foreach ($files as $file) {
$file_path = $data_dir . '/' . $file;
if (self::is_yaml_file($file_path)) {
self::insert_data($file_path);
}
}
// 外部キー制約を戻す
\DB::query('set foreign_key_checks=1;')->execute();
\DB::commit_transaction();
} catch (\Exception $e) {
// 外部キー制約を戻す
\DB::query('set foreign_key_checks=1;')->execute();
\DB::rollback_transaction();
\Log::error($e);
echo 'failure.' . "\n";
throw $e;
}
echo 'data was imported.' . "\n";
}
/**
* @param string $file_path
* @return bool
*/
public static function is_yaml_file($file_path)
{
if (!(is_file($file_path))) {
return false;
}
$extension = pathinfo($file_path, PATHINFO_EXTENSION);
if ($extension !== 'yml') {
return false;
}
return true;
}
/**
* @param string $file_path
* @throws \Exception
*/
public static function insert_data($file_path)
{
$data = file_get_contents($file_path);
$tables = \Format::forge($data, 'yaml')->to_array();
foreach ($tables as $table => $rows) {
// ファイルのデータに合わせるために
// 現在入っているデータを全て削除
\DB::delete($table)->execute();
foreach ($rows as $row) {
\DB::insert($table)->set($row)->execute();
}
}
}
}
NYSL Version 0.9982
----------------------------------------
A. 本ソフトウェアは Everyone'sWare です。このソフトを手にした一人一人が、
ご自分の作ったものを扱うのと同じように、自由に利用することが出来ます。
A-1. フリーウェアです。作者からは使用料等を要求しません。
A-2. 有料無料や媒体の如何を問わず、自由に転載・再配布できます。
A-3. いかなる種類の 改変・他プログラムでの利用 を行っても構いません。
A-4. 変更したものや部分的に使用したものは、あなたのものになります。
公開する場合は、あなたの名前の下で行って下さい。
B. このソフトを利用することによって生じた損害等について、作者は
責任を負わないものとします。各自の責任においてご利用下さい。
C. 著作者人格権は 菅原 浩 に帰属します。著作権は放棄します。
D. 以上の3項は、ソース・実行バイナリの双方に適用されます。
NYSL Version 0.9982 (en) (Unofficial)
----------------------------------------
A. This software is "Everyone'sWare". It means:
Anybody who has this software can use it as if he/she is
the author.
A-1. Freeware. No fee is required.
A-2. You can freely redistribute this software.
A-3. You can freely modify this software. And the source
may be used in any software with no limitation.
A-4. When you release a modified version to public, you
must publish it with your name.
B. The author is not responsible for any kind of damages or loss
while using or misusing this software, which is distributed
"AS IS". No warranty of any kind is expressed or implied.
You use AT YOUR OWN RISK.
C. Copyrighted to Hiroshi Sugawara.
D. Above three clauses are applied both to source and binary
form of this software.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment