Created
February 14, 2012 11:40
-
-
Save boxbilling/1826116 to your computer and use it in GitHub Desktop.
Migration script from BoxBilling 0.0.41 to 2.2.8 (or later)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Migration script from BoxBilling 0.0.41 to 2.2.8 (or later) | |
* | |
* Instructions: | |
* | |
* 1. Make your old database backup | |
* 2. Make fresh install of BoxBilling 2.2.8 (or later) | |
* 3. Put this file in same directory as bb-config.php file | |
* 4. Define BB_DB_OLD_* parameters below. Script needs to login to your old | |
* database. | |
* 5. Run `php bb-migrate.php` from command line. Running from browser may | |
* fail due to execution timeout if your database is very big. | |
* 6. After migration is complete. Check your products/servers/gateways | |
* configuration details. | |
*/ | |
/* Define your OLD database connection */ | |
define('BB_DB_OLD_HOST', 'localhost'); | |
define('BB_DB_OLD_NAME', 'db_name'); | |
define('BB_DB_OLD_USER', 'db_user'); | |
define('BB_DB_OLD_PASSWORD', 'db_pass'); | |
/* Do not edit below this line */ | |
/* ************************************************************************** */ | |
ini_set('display_errors', '0'); | |
ini_set('display_startup_errors', '0'); | |
ini_set('log_errors', '0'); | |
require_once dirname(__FILE__) . '/bb-config.php'; | |
try { | |
$m = new Migrate(); | |
$m->migrate(); | |
} catch(Exception $e) { | |
print $e->getMessage(); | |
print 'on line: '. $e->getLine().PHP_EOL; | |
} | |
class Migrate | |
{ | |
private $o = null; | |
private $n = null; | |
public function __construct() | |
{ | |
$this->o = $this->getOldDbPdo(); | |
$this->n = $this->getNewDbPdo(); | |
} | |
public function migrate() | |
{ | |
$this->_l('Started Import'); | |
$this->migrateStaff(); | |
$this->migrateClients(); | |
$this->migrateProducts(); | |
$this->migrateOrders(); | |
$this->migrateInvoices(); | |
$this->migrateSupport(); | |
$this->migrateForum(); | |
$this->migrateKb(); | |
$this->migrateLogs(); | |
$query="ALTER TABLE cart AUTO_INCREMENT=70000"; | |
$stmt = $this->n->prepare($query); | |
$stmt->execute(); | |
$this->_l('Done'); | |
} | |
private function migrateInvoices() | |
{ | |
$this->_l('Migrating Invoices'); | |
$this->truncateNewTable('invoice'); | |
$sql="SELECT m.*, | |
cp.first_name as buyer_first_name, | |
cp.last_name as buyer_last_name, | |
cp.company as buyer_company, | |
cp.address_1 as buyer_address, | |
cp.city as buyer_city, | |
cp.state as buyer_state, | |
cp.country as buyer_country, | |
cp.postcode as buyer_zip, | |
cp.phone as buyer_phone, | |
c.email as buyer_email, | |
1 as approved, | |
'USD' as currency, | |
DATE_FORMAT(NOW(),'%Y-%m-%dT%TZ') as reminded_at | |
FROM client_invoice m | |
LEFT JOIN client c ON (c.id = m.client_id) | |
LEFT JOIN client_profile cp ON (c.id = cp.client_id) | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
if($row['paid_at']) { | |
$row['paid_at'] = date('c', $row['paid_at']); | |
} | |
$this->insertToNew('invoice' , $row); | |
$this->_l('Imported Invoice #'.$row['id']); | |
} | |
$this->truncateNewTable('invoice_item'); | |
$sql="SELECT m.*, | |
m.client_invoice_id as invoice_id, | |
'custom' as type, | |
'executed' as status, | |
m.qty as quantity, | |
m.description as title, | |
1 as charged | |
FROM client_invoice_item m | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('invoice_item' , $row); | |
$this->_l('Imported Invoice line #'.$row['id']); | |
} | |
} | |
private function migrateStaff() | |
{ | |
$this->_l('Migrating staff members'); | |
$this->simpleMigrate('admin_group'); | |
$this->simpleMigrate('admin'); | |
} | |
private function migrateOrders() | |
{ | |
$this->truncateNewTable('client_order'); | |
$this->migrateHosting(); | |
$this->migrateDomains(); | |
$this->migrateDownloads(); | |
$this->migrateLicenses(); | |
$this->migrateCustom(); | |
} | |
private function migrateCustom() | |
{ | |
$this->_l('Migrating custom orders'); | |
$this->truncateNewTable('service_custom'); | |
$sql="SELECT m.*, pt.title as p_title, c.code as currency_code | |
FROM client_custom m | |
LEFT JOIN currency c ON (c.id = m.currency_id) | |
LEFT JOIN product p ON (p.id = m.product_id) | |
LEFT JOIN product_translation pt ON (p.id = pt.id AND pt.lang = 'en') | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $idx=>$row) { | |
$order = array( | |
'client_id' => $row['client_id'], | |
'product_id' => $row['product_id'], | |
'group_id' => '50000'.$row['id'], | |
'group_master' => 1, | |
'currency' => $row['currency_code'], | |
'title' => $row['p_title'], | |
'service_type' => 'custom', | |
'price' => $row['price'], | |
'status' => $row['status'], | |
'activated_at' => date('c', $row['created_at']), | |
'created_at' => date('c', $row['created_at']), | |
'updated_at' => date('c', $row['updated_at']), | |
); | |
$order_id = $this->insertToNew('client_order' , $order); | |
$service = array(); | |
$service['client_id'] = $row['client_id']; | |
$service['created_at'] = date('c', $row['created_at']); | |
$service['updated_at'] = date('c', $row['updated_at']); | |
$service_id = $this->insertToNew('service_custom' , $service); | |
$this->updateNew('client_order' , $order_id, array('service_id'=>$service_id)); | |
$this->_l('Imported Custom order: '.$order_id); | |
} | |
} | |
private function migrateDownloads() | |
{ | |
$this->_l('Migrating downloads'); | |
$this->truncateNewTable('service_downloadable'); | |
$sql="SELECT m.*, pt.title as p_title, c.code as currency_code, p.file | |
FROM client_download m | |
LEFT JOIN currency c ON (c.id = m.currency_id) | |
LEFT JOIN product p ON (p.id = m.product_id) | |
LEFT JOIN product_translation pt ON (p.id = pt.id AND pt.lang = 'en') | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $idx=>$row) { | |
$order = array( | |
'client_id' => $row['client_id'], | |
'product_id' => $row['product_id'], | |
'group_id' => '40000'.$row['id'], | |
'group_master' => 1, | |
'currency' => $row['currency_code'], | |
'title' => $row['p_title'], | |
'service_type' => 'downloadable', | |
'price' => $row['price'], | |
'status' => $row['status'], | |
'activated_at' => date('c', $row['created_at']), | |
'created_at' => date('c', $row['created_at']), | |
'updated_at' => date('c', $row['updated_at']), | |
); | |
$order_id = $this->insertToNew('client_order' , $order); | |
$service = array(); | |
$service['client_id'] = $row['client_id']; | |
$service['filename'] = $row['p_title']; | |
$service['downloads'] = $row['counter']; | |
$service['created_at'] = date('c', $row['created_at']); | |
$service['updated_at'] = date('c', $row['updated_at']); | |
$service_id = $this->insertToNew('service_downloadable' , $service); | |
$this->updateNew('client_order' , $order_id, array('service_id'=>$service_id)); | |
$this->_l('Imported Downloadable order: '.$order_id); | |
} | |
} | |
private function migrateDomains() | |
{ | |
$this->_l('Migrating domains'); | |
$this->simpleMigrate('tld'); | |
$this->simpleMigrate('tld_registrar'); | |
$sql="SELECT id | |
FROM product | |
WHERE type = 'domain' | |
LIMIT 1 | |
"; | |
$stmt = $this->n->prepare($sql); | |
$stmt->execute(); | |
$domain_product_id = $stmt->fetchColumn(); | |
if(!$domain_product_id) { | |
throw new Exception('Domain product not found.'); | |
} | |
$this->truncateNewTable('service_domain'); | |
$sql="SELECT m.*, c.code as currency_code, cl.*, cp.* | |
FROM client_domain m | |
LEFT JOIN client cl ON (m.client_id = cl.id) | |
LEFT JOIN client cp ON (m.client_id = cp.id) | |
LEFT JOIN currency c ON (c.id = m.currency_id) | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $idx=>$row) { | |
$order = array( | |
'client_id' => $row['client_id'], | |
'product_id' => $domain_product_id, | |
'group_id' => '30000'.$row['id'], | |
'group_master' => 1, | |
'currency' => $row['currency_code'], | |
'title' => 'Domain: ' .$row['domain'], | |
'service_type' => 'domain', | |
'price' => $row['renewal_price'], | |
'status' => $row['status'], | |
'activated_at' => date('c', $row['activated_at']), | |
'created_at' => date('c', $row['created_at']), | |
'updated_at' => date('c', $row['updated_at']), | |
); | |
$order['period'] = $row['registration_period'].'Y'; | |
if($row['expires_at']) { | |
$order['expires_at'] = date('c', $row['expires_at']); | |
} | |
$order_id = $this->insertToNew('client_order' , $order); | |
$service = array(); | |
$service['client_id'] = $row['client_id']; | |
$service['tld_registrar_id'] = $row['tld_registrar_id']; | |
$service['sld'] = substr($row['domain'], 0, strpos($row['domain'], '.')); | |
$service['tld'] = substr($row['domain'], strpos($row['domain'], '.')); | |
$service['ns1'] = $row['']; | |
$service['ns2'] = $row['']; | |
$service['ns3'] = $row['']; | |
$service['ns4'] = $row['']; | |
$service['period'] = $row['registration_period']; | |
$service['privacy'] = 0; | |
$service['locked'] = 0; | |
$service['transfer_code'] = null; | |
$service['contact_first_name'] = $row['first_name']; | |
$service['contact_last_name'] = $row['last_name']; | |
$service['contact_email'] = $row['email']; | |
$service['contact_company'] = $row['company']; | |
$service['contact_address1'] = $row['address_1']; | |
$service['contact_address2'] = $row['address_2']; | |
$service['contact_country'] = $row['country']; | |
$service['contact_city'] = $row['city']; | |
$service['contact_state'] = $row['state']; | |
$service['contact_postcode'] = $row['postcode']; | |
$service['contact_phone_cc'] = $row['phone_cc']; | |
$service['contact_phone'] = $row['phone']; | |
$service['registered_at'] = date('c', $row['registered_at']); | |
$service['expires_at'] = date('c', $row['expires_at']); | |
$service['created_at'] = date('c', $row['activated_at']); | |
$service['updated_at'] = date('c', $row['activated_at']); | |
$service_id = $this->insertToNew('service_domain' , $service); | |
$this->updateNew('client_order' , $order_id, array('service_id'=>$service_id)); | |
$this->_l('Imported Domain order: '.$order_id); | |
} | |
} | |
private function migrateHosting() | |
{ | |
$this->_l('Migrating hosting accounts'); | |
$this->simpleMigrate('hosting_plan', 'service_hosting_hp'); | |
$this->simpleMigrate('server', 'service_hosting_server'); | |
$this->truncateNewTable('service_hosting'); | |
$sql="SELECT m.*, pt.title as p_title, c.code as currency_code, p.hosting_plan_id | |
FROM client_account m | |
LEFT JOIN currency c ON (c.id = m.currency_id) | |
LEFT JOIN product p ON (p.id = m.product_id) | |
LEFT JOIN product_translation pt ON (p.id = pt.id AND pt.lang = 'en') | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $idx=>$row) { | |
$order = array( | |
'client_id' => $row['client_id'], | |
'product_id' => $row['product_id'], | |
'group_id' => '20000'.$row['id'], | |
'group_master' => 1, | |
'currency' => $row['currency_code'], | |
'title' => $row['p_title'], | |
'service_type' => 'hosting', | |
'price' => $row['price'], | |
'status' => $row['status'], | |
'activated_at' => date('c', $row['activated_at']), | |
'created_at' => date('c', $row['created_at']), | |
'updated_at' => date('c', $row['updated_at']), | |
); | |
if($row['period']) { | |
$order['period'] = $this->_convertPeriod($row['period']); | |
} | |
if($row['expires_at']) { | |
$order['expires_at'] = date('c', $row['expires_at']); | |
} | |
$order_id = $this->insertToNew('client_order' , $order); | |
$service = array(); | |
$service['client_id'] = $row['client_id']; | |
$service['service_hosting_server_id'] = $row['server_id']; | |
$service['service_hosting_hp_id'] = $row['hosting_plan_id']; | |
$service['sld'] = substr($row['domain'], 0, strpos($row['domain'], '.')); | |
$service['tld'] = substr($row['domain'], strpos($row['domain'], '.')); | |
$service['ip'] = ''; | |
$service['username'] = $row['username']; | |
$service['pass'] = $row['pass']; | |
$service['reseller'] = $row['reseller']; | |
$service['created_at'] = date('c', $row['activated_at']); | |
$service['updated_at'] = date('c', $row['activated_at']); | |
$service_id = $this->insertToNew('service_hosting' , $service); | |
$this->updateNew('client_order' , $order_id, array('service_id'=>$service_id)); | |
$this->_l('Imported Hosting order: '.$order_id); | |
} | |
} | |
private function migrateLicenses() | |
{ | |
$this->_l('Migrating licenses'); | |
$this->truncateNewTable('service_license'); | |
$sql="SELECT m.*, pt.title as p_title, c.code as currency_code | |
FROM client_license m | |
LEFT JOIN currency c ON (c.id = m.currency_id) | |
LEFT JOIN product p ON (p.id = m.product_id) | |
LEFT JOIN product_translation pt ON (p.id = pt.id AND pt.lang = 'en') | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $idx=>$row) { | |
$order = array( | |
'client_id' => $row['client_id'], | |
'product_id' => $row['product_id'], | |
'group_id' => '10000'.$row['id'], | |
'group_master' => 1, | |
'currency' => $row['currency_code'], | |
'title' => $row['p_title'], | |
'service_type' => 'license', | |
'price' => $row['price'], | |
'status' => $row['status'], | |
'activated_at' => date('c', $row['activated_at']), | |
'created_at' => date('c', $row['created_at']), | |
'updated_at' => date('c', $row['updated_at']), | |
); | |
if($row['period']) { | |
$order['period'] = $this->_convertPeriod($row['period']); | |
} | |
if($row['expires_at']) { | |
$order['expires_at'] = date('c', $row['expires_at']); | |
} | |
$order_id = $this->insertToNew('client_order' , $order); | |
$service = array(); | |
$service['client_id'] = $row['client_id']; | |
$service['license_key'] = $row['license_key']; | |
$service['validate_ip'] = $row['validate_ip']; | |
$service['validate_host'] = $row['validate_host']; | |
$service['validate_path'] = $row['validate_path']; | |
$service['validate_version'] = $row['validate_version']; | |
$service['plugin'] = 'Default'; | |
$service['created_at'] = date('c', $row['activated_at']); | |
$service['updated_at'] = date('c', $row['activated_at']); | |
if($a = unserialize($row['ips'])) { | |
$service['ips'] = json_encode($a); | |
} | |
if($a = unserialize($row['hosts'])) { | |
$service['hosts'] = json_encode($a); | |
} | |
if($a = unserialize($row['paths'])) { | |
$service['paths'] = json_encode($a); | |
} | |
if($a = unserialize($row['versions'])) { | |
$service['versions'] = json_encode($a); | |
} | |
$service_id = $this->insertToNew('service_license' , $service); | |
$this->updateNew('client_order' , $order_id, array('service_id'=>$service_id)); | |
$this->_l('Imported License order: '.$order_id); | |
} | |
} | |
private function migrateProducts() | |
{ | |
$this->_l('Migrating products'); | |
$this->truncateNewTable('currency'); | |
$list = $this->selectFromOld('currency'); | |
foreach($list as $row) { | |
$row['format'] = '%price% '.$row['code']; | |
$this->insertToNew('currency' , $row); | |
} | |
$this->truncateNewTable('product_category'); | |
$sql="SELECT m.*, mt.* | |
FROM product_category m | |
LEFT JOIN product_category_translation mt ON (m.id = mt.id AND mt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('product_category' , $row); | |
} | |
$this->truncateNewTable('product'); | |
$sql="SELECT m.*, mt.* | |
FROM product m | |
LEFT JOIN product_translation mt ON (m.id = mt.id AND mt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$row['enabled'] = $row['active']; | |
$row['slug'] = 'product-'.$row['id']; | |
$this->insertToNew('product' , $row); | |
} | |
$this->simpleMigrate('product_payment'); | |
//Insert domain product | |
$sql="INSERT INTO `product` (`title`, `slug`, `description`, `unit`, `active`, `status`, `hidden`, `is_addon`, `setup`, `addons`, `icon_url`, `allow_quantity_select`, `stock_control`, `quantity_in_stock`, `plugin`, `upgrades`, `priority`, `config`, `created_at`, `updated_at`, `type`) | |
VALUES | |
('Domains registration and transfer', 'domain-checker', NULL, 'product', 1, 'enabled', 0, 0, 'after_payment', NULL, '', 0, 0, 0, NULL, NULL, 1, NULL, '2012-01-01T12:00:00+00:00', '2012-01-01T12:00:00+00:00', 'domain'); | |
"; | |
$stmt = $this->n->prepare($sql); | |
$stmt->execute(); | |
/* | |
$this->truncateNewTable('addon'); | |
$sql="SELECT m.*, mt.* | |
FROM addon m | |
LEFT JOIN addon_translation mt ON (m.id = mt.id AND mt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('product' , $row); | |
} | |
*/ | |
} | |
private function migrateKb() | |
{ | |
$this->_l('Migrating knowledge base'); | |
$this->truncateNewTable('kb_article'); | |
$sql="SELECT m.*, mt.* | |
FROM kb_article m | |
LEFT JOIN kb_article_translation mt ON (m.id = mt.id AND mt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('kb_article' , $row); | |
} | |
$this->truncateNewTable('kb_article_category'); | |
$sql="SELECT m.*, mt.* | |
FROM kb_article_category m | |
LEFT JOIN kb_article_category_translation mt ON (m.id = mt.id AND mt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('kb_article_category' , $row); | |
} | |
} | |
private function migrateForum() | |
{ | |
$this->_l('Migrating forum'); | |
$this->simpleMigrate('forum'); | |
$this->simpleMigrate('forum_topic'); | |
$this->simpleMigrate('forum_topic_message'); | |
} | |
private function migrateSupport() | |
{ | |
$this->_l('Migrating support tickets'); | |
$this->simpleMigrate('support_helpdesk'); | |
$this->simpleMigrate('support_ticket'); | |
$this->simpleMigrate('support_ticket_message'); | |
$this->simpleMigrate('support_ticket_note'); | |
$this->simpleMigrate('contact_ticket', 'support_p_ticket'); | |
$this->truncateNewTable('support_p_ticket_message'); | |
$list = $this->selectFromOld('contact_ticket_message'); | |
foreach($list as $row) { | |
$row['support_p_ticket_id'] = $row['contact_ticket_id']; | |
$this->insertToNew('support_p_ticket_message' , $row); | |
} | |
//predefines | |
$this->truncateNewTable('support_pr'); | |
$sql="SELECT pr.*, prt.* | |
FROM support_pr pr | |
LEFT JOIN support_pr_translation prt ON (pr.id = prt.id AND prt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('support_pr' , $row); | |
} | |
$this->truncateNewTable('support_pr_category'); | |
$sql="SELECT pr.*, prt.* | |
FROM support_pr_category pr | |
LEFT JOIN support_pr_category_translation prt ON (pr.id = prt.id AND prt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('support_pr_category' , $row); | |
} | |
} | |
private function migrateClients() | |
{ | |
$this->_l('Migrating clients'); | |
$this->truncateNewTable('client'); | |
$sql="SELECT c.*,cp.* | |
FROM client c | |
LEFT JOIN client_profile cp ON (c.id = cp.client_id) | |
WHERE 1=1 | |
#LIMIT 200"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$r = array( | |
'id' => $row['id'], | |
'aid' => $row['aid'], | |
'client_group_id' => 1, | |
'role' => $row['role'], | |
'email' => $row['email'], | |
'pass' => $row['pass'], | |
'status' => $row['status'], | |
'tax_exempt' => 0, | |
'company' => $row['company'], | |
'first_name' => $row['first_name'], | |
'last_name' => $row['last_name'], | |
'gender' => $row['gender'], | |
'birthday' => $row['birthday'], | |
'phone_cc' => $row['phone_cc'], | |
'phone' => $row['phone'], | |
'address_1' => $row['address_1'], | |
'address_2' => $row['address_2'], | |
'city' => $row['city'], | |
'state' => $row['state'], | |
'postcode' => $row['postcode'], | |
'country' => $row['country'], | |
'currency' => 'USD', | |
'notes' => $row['notes'], | |
'lang' => $row['lang'], | |
'ip' => $row['ip'], | |
'created_at' => date('c', $row['created_at']), | |
'updated_at' => date('c', $row['created_at']), | |
); | |
$this->insertToNew('client', $r); | |
$this->_l('Imported client #'.$row['id']); | |
} | |
} | |
private function migrateEmails() | |
{ | |
$this->_l('Migrating email templates'); | |
$this->simpleMigrate('email_template'); | |
$sql="SELECT m.*, mt.* | |
FROM email_template m | |
LEFT JOIN email_template_translation mt ON (m.id = mt.id AND mt.lang = 'en') | |
WHERE 1=1 | |
"; | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
$list = $stmt->fetchAll(); | |
foreach($list as $row) { | |
$this->insertToNew('email_template' , $row); | |
} | |
} | |
private function migrateLogs() | |
{ | |
$this->_l('Migrating logs'); | |
$this->simpleMigrate('activity_admin_history'); | |
$this->simpleMigrate('activity_client_email'); | |
$this->simpleMigrate('activity_client_history'); | |
$this->simpleMigrate('activity_system'); | |
} | |
private function getOldDbPdo() | |
{ | |
$db_host=BB_DB_OLD_HOST; | |
$db_name=BB_DB_OLD_NAME; | |
$db_user=BB_DB_OLD_USER; | |
$db_pass=BB_DB_OLD_PASSWORD; | |
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user, $db_pass); | |
$pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8"); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
return $pdo; | |
} | |
private function getNewDbPdo() | |
{ | |
$db_host=BB_DB_HOST; | |
$db_name=BB_DB_NAME; | |
$db_user=BB_DB_USER; | |
$db_pass=BB_DB_PASSWORD; | |
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user, $db_pass); | |
$pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8"); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
return $pdo; | |
} | |
private function simpleMigrate($old_table, $new_table = null) | |
{ | |
if(null === $new_table ) { | |
$new_table = $old_table; | |
} | |
$this->truncateNewTable($new_table); | |
$list = $this->selectFromOld($old_table); | |
foreach($list as $row) { | |
$this->insertToNew($new_table , $row); | |
} | |
} | |
private function truncateNewTable($table) | |
{ | |
$query="TRUNCATE ".$table; | |
$stmt = $this->n->prepare($query); | |
$stmt->execute(); | |
$query="ALTER TABLE $table AUTO_INCREMENT=1"; | |
$stmt = $this->n->prepare($query); | |
$stmt->execute(); | |
} | |
private function updateNew($table, $id, $data) | |
{ | |
$d = array(); | |
foreach($data as $k=>$v) { | |
$d[] = sprintf("%s = '%s'", $k, $v); | |
} | |
$query="UPDATE $table SET " . implode(", ", $d) . " WHERE id = ".$id; | |
$stmt = $this->n->prepare($query); | |
$stmt->execute(); | |
} | |
private function insertToNew($table, $row) | |
{ | |
$sql="SHOW COLUMNS FROM ".$table; | |
$stmt = $this->n->prepare($sql); | |
$stmt->execute($row); | |
$columns = $stmt->fetchAll(); | |
$fields = array(); | |
foreach($columns as $col) { | |
$fields[] = $col['Field']; | |
} | |
// todays date for new tables | |
if(!isset($row['created_at'])) { | |
$row['created_at'] = date('c'); | |
} | |
if(!isset($row['updated_at'])) { | |
$row['updated_at'] = date('c'); | |
} | |
foreach($row as $k=>$val) { | |
if(!in_array($k, $fields)) { | |
unset($row[$k]); | |
} | |
} | |
if(isset($row['created_at']) && is_numeric($row['created_at'])) { | |
$row['created_at'] = date('c', $row['created_at']); | |
} | |
if(isset($row['updated_at']) && is_numeric($row['updated_at'])) { | |
$row['updated_at'] = date('c', $row['updated_at']); | |
} | |
$keys = array_keys($row); | |
$query="INSERT INTO $table (".implode(', ',$keys).") VALUES (:". implode(', :',$keys) .")"; | |
$stmt = $this->n->prepare($query); | |
$stmt->execute($row); | |
return $this->n->lastInsertId(); | |
} | |
private function selectFromOld($table, $limit = null) | |
{ | |
$sql="SELECT * FROM $table WHERE 1=1"; | |
if($limit) { $sql .= " LIMIT ".$limit; } | |
$stmt = $this->o->prepare($sql); | |
$stmt->execute(); | |
return $stmt->fetchAll(); | |
} | |
private function _convertPeriod($old_period) | |
{ | |
$new_period = null; | |
switch ($old_period) { | |
case 'a': | |
$new_period = '1Y'; | |
case 'bia': | |
$new_period = '2Y'; | |
case 'tria': | |
$new_period = '3Y'; | |
case 'm': | |
$new_period = '1M'; | |
case 'q': | |
$new_period = '3M'; | |
case 'b': | |
$new_period = '6M'; | |
break; | |
default: | |
break; | |
} | |
return $new_period; | |
} | |
private function _l($msg) | |
{ | |
print $msg . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment