[ADDED] coin_addresses table and support

* [ADDED] New coin_addresses table to upgrade script and base file
* [MIGRATE] Move coin_address from accounts to coin_addresses table
* [UPDATE] Updated all classes/pages/scripts to use new coin address class
* [UPDATE] DB_VERSION 0.0.12
This commit is contained in:
Sebastian Grewe 2014-06-12 12:32:05 +02:00
parent a9bfc91e7a
commit 3248a487df
12 changed files with 190 additions and 61 deletions

View File

@ -16,6 +16,7 @@ require_once(INCLUDE_DIR . '/config/error_codes.inc.php');
// We need to load these first // We need to load these first
require_once(CLASS_DIR . '/base.class.php'); require_once(CLASS_DIR . '/base.class.php');
require_once(CLASS_DIR . '/coins/coin_base.class.php'); require_once(CLASS_DIR . '/coins/coin_base.class.php');
require_once(CLASS_DIR . '/coin_address.class.php');
require_once(CLASS_DIR . '/setting.class.php'); require_once(CLASS_DIR . '/setting.class.php');
require_once(INCLUDE_DIR . '/version.inc.php'); require_once(INCLUDE_DIR . '/version.inc.php');
if (PHP_OS == 'WINNT') require_once(CLASS_DIR . '/memcached.class.php'); if (PHP_OS == 'WINNT') require_once(CLASS_DIR . '/memcached.class.php');

View File

@ -22,6 +22,9 @@ class Base {
public function setCoin($coin) { public function setCoin($coin) {
$this->coin = $coin; $this->coin = $coin;
} }
public function setCoinAddress($coin_address) {
$this->coin_address = $coin_address;
}
public function setLog($log) { public function setLog($log) {
$this->log = $log; $this->log = $log;
} }

View File

@ -0,0 +1,92 @@
<?php
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
class CoinAddress extends Base {
protected $table = 'coin_addresses';
private $cache = array();
/**
* Fetch users coin address for a currency
* @param userID int UserID
* @return data string Coin Address
**/
public function getCoinAddress($userID, $currency=NULL) {
if ($currency === NULL) $currency = $this->config['currency'];
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("
SELECT coin_address
FROM " . $this->getTableName() . "
WHERE account_id = ? AND currency = ?
");
if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) {
if ($result->num_rows == 1) {
return $result->fetch_object()->coin_address;
}
}
$this->debug->append("Unable to fetch users coin address for " . $currency);
return $this->sqlError();
}
/**
* Check if a coin address is already set
* @param address string Coin Address to check for
* @return bool true or false
**/
public function existsCoinAddress($address) {
$this->debug->append("STA " . __METHOD__, 4);
return $this->getSingle($address, 'coin_address', 'coin_address', 's') === $address;
}
/**
* Add a new coin address record for a user
* @param userID int Account ID
* @param address string Coin Address
* @param currency string Currency short handle, defaults to config option
* @return bool true or false
**/
public function add($userID, $address, $currency=NULL) {
if ($currency === NULL) $currency = $this->config['currency'];
if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) {
$this->setErrorMessage('Unable to update coin address, address already exists');
return false;
}
$stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (account_id, currency, coin_address) VALUES (?, ?, ?)");
if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $userID, $currency, $address) && $stmt->execute()) {
return true;
}
return $this->sqlError();
}
/**
* Update a coin address record for a user and a currency
* @param userID int Account ID
* @param address string Coin Address
* @param currency string Currency short handle, defaults to config option
* @return bool true or false
**/
public function update($userID, $address, $currency=NULL) {
if ($currency === NULL) $currency = $this->config['currency'];
if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) {
$this->setErrorMessage('Unable to update coin address, address already exists');
return false;
}
if ($this->getCoinAddress($userID) != NULL) {
$stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ? WHERE account_id = ? AND currency = ?");
if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) {
return true;
}
} else {
$stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, account_id, currency) VALUES (?, ?, ?)");
if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) {
return true;
}
}
return $this->sqlError();
}
}
$coin_address = new CoinAddress();
$coin_address->setDebug($debug);
$coin_address->setConfig($config);
$coin_address->setMysql($mysqli);
$coin_address->setErrorCodes($aErrorCodes);

View File

@ -355,7 +355,7 @@ class Transaction extends Base {
a.id, a.id,
a.username, a.username,
a.ap_threshold, a.ap_threshold,
a.coin_address, ca.coin_address,
IFNULL( IFNULL(
ROUND( ROUND(
( (
@ -370,11 +370,13 @@ class Transaction extends Base {
ON t.block_id = b.id ON t.block_id = b.id
LEFT JOIN " . $this->user->getTableName() . " AS a LEFT JOIN " . $this->user->getTableName() . " AS a
ON t.account_id = a.id ON t.account_id = a.id
WHERE t.archived = 0 AND a.ap_threshold > 0 AND a.coin_address IS NOT NULL AND a.coin_address != '' LEFT JOIN " . $this->coin_address->getTableName() . " AS ca
ON ca.account_id = a.id
WHERE t.archived = 0 AND a.ap_threshold > 0 AND ca.coin_address IS NOT NULL AND ca.coin_address != '' AND ca.currency = ?
GROUP BY t.account_id GROUP BY t.account_id
HAVING confirmed > a.ap_threshold AND confirmed > " . $this->config['txfee_auto'] . " HAVING confirmed > a.ap_threshold AND confirmed > " . $this->config['txfee_auto'] . "
LIMIT ?"); LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $limit) && $stmt->execute() && $result = $stmt->get_result()) if ($this->checkStmt($stmt) && $stmt->bind_param('si', $this->config['currency'], $limit) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_all(MYSQLI_ASSOC); return $result->fetch_all(MYSQLI_ASSOC);
return $this->sqlError(); return $this->sqlError();
} }
@ -446,7 +448,7 @@ class Transaction extends Base {
a.id, a.id,
a.username, a.username,
a.ap_threshold, a.ap_threshold,
a.coin_address, ca.coin_address,
p.id AS payout_id, p.id AS payout_id,
IFNULL( IFNULL(
ROUND( ROUND(
@ -464,11 +466,13 @@ class Transaction extends Base {
ON t.account_id = p.account_id ON t.account_id = p.account_id
LEFT JOIN " . $this->block->getTableName() . " AS b LEFT JOIN " . $this->block->getTableName() . " AS b
ON t.block_id = b.id ON t.block_id = b.id
WHERE p.completed = 0 AND t.archived = 0 AND a.coin_address IS NOT NULL AND a.coin_address != '' LEFT JOIN " . $this->coin_address->getTableName() . " AS ca
ON ca.account_id = a.id
WHERE p.completed = 0 AND t.archived = 0 AND ca.currency = ? AND ca.coin_address IS NOT NULL AND ca.coin_address != ''
GROUP BY t.account_id GROUP BY t.account_id
HAVING confirmed > " . $this->config['txfee_manual'] . " HAVING confirmed > " . $this->config['txfee_manual'] . "
LIMIT ?"); LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $limit) && $stmt->execute() && $result = $stmt->get_result()) if ($this->checkStmt($stmt) && $stmt->bind_param('si', $this->config['currency'], $limit) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_all(MYSQLI_ASSOC); return $result->fetch_all(MYSQLI_ASSOC);
return $this->sqlError('E0050'); return $this->sqlError('E0050');
} }
@ -478,6 +482,7 @@ $transaction = new Transaction();
$transaction->setMemcache($memcache); $transaction->setMemcache($memcache);
$transaction->setNotification($notification); $transaction->setNotification($notification);
$transaction->setDebug($debug); $transaction->setDebug($debug);
$transaction->setCoinAddress($coin_address);
$transaction->setMysql($mysqli); $transaction->setMysql($mysqli);
$transaction->setConfig($config); $transaction->setConfig($config);
$transaction->setBlock($block); $transaction->setBlock($block);

View File

@ -163,7 +163,7 @@ class User extends Base {
$invitation->setDebug($this->debug); $invitation->setDebug($this->debug);
$invitation->setLog($this->log); $invitation->setLog($this->log);
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT COUNT(i.account_id) AS invitationcount,a.id,a.username,a.email, SELECT COUNT(i.account_id) AS invitationcount,a.id,a.username,a.email,
(SELECT COUNT(account_id) FROM " . $invitation->getTableName() . " WHERE account_id = i.account_id AND is_activated = 1 GROUP BY account_id) AS activated (SELECT COUNT(account_id) FROM " . $invitation->getTableName() . " WHERE account_id = i.account_id AND is_activated = 1 GROUP BY account_id) AS activated
FROM " . $invitation->getTableName() . " AS i FROM " . $invitation->getTableName() . " AS i
LEFT JOIN " . $this->getTableName() . " AS a LEFT JOIN " . $this->getTableName() . " AS a
@ -340,38 +340,20 @@ class User extends Base {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT SELECT
id, username, coin_address, ap_threshold a.id, a.username, ca.coin_address AS coin_address, a.ap_threshold
FROM " . $this->getTableName() . " FROM " . $this->getTableName() . " AS a
WHERE ap_threshold > 0 LEFT JOIN " . $this->coin_address->getTableName() . " AS ca
AND coin_address IS NOT NULL ON a.id = ca.account_id
WHERE ap_threshold > 0 AND ca.currency = ?
AND ca.coin_address IS NOT NULL
"); ");
if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) { if ( $this->checkStmt($stmt) && $stmt->bind_param('s', $this->config['currency']) && $stmt->execute() && $result = $stmt->get_result()) {
return $result->fetch_all(MYSQLI_ASSOC); return $result->fetch_all(MYSQLI_ASSOC);
} }
$this->debug->append("Unable to fetch users with AP set"); $this->debug->append("Unable to fetch users with AP set");
return false; return false;
} }
/**
* Fetch users coin address
* @param userID int UserID
* @return data string Coin Address
**/
public function getCoinAddress($userID) {
$this->debug->append("STA " . __METHOD__, 4);
return $this->getSingle($userID, 'coin_address', 'id');
}
/**
* Check if a coin address exists already
* @param address string Coin Address
* @return bool True of false
**/
public function existsCoinAddress($address) {
$this->debug->append("STA " . __METHOD__, 4);
return $this->getSingle($address, 'coin_address', 'coin_address', 's') === $address;
}
/** /**
* Fetch users donation value * Fetch users donation value
* @param userID int UserID * @param userID int UserID
@ -519,7 +501,7 @@ class User extends Base {
return false; return false;
} }
if (!empty($address)) { if (!empty($address)) {
if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) { if ($address != $this->coin_address->getCoinAddress($userID) && $this->coin_address->existsCoinAddress($address)) {
$this->setErrorMessage('Address is already in use'); $this->setErrorMessage('Address is already in use');
return false; return false;
} }
@ -559,10 +541,12 @@ class User extends Base {
} }
// We passed all validation checks so update the account // We passed all validation checks so update the account
$stmt = $this->mysqli->prepare("UPDATE $this->table SET coin_address = ?, ap_threshold = ?, donate_percent = ?, email = ?, timezone = ?, is_anonymous = ? WHERE id = ?"); $stmt = $this->mysqli->prepare("UPDATE $this->table SET ap_threshold = ?, donate_percent = ?, email = ?, timezone = ?, is_anonymous = ? WHERE id = ?");
if ($this->checkStmt($stmt) && $stmt->bind_param('sddssii', $address, $threshold, $donate, $email, $timezone, $is_anonymous, $userID) && $stmt->execute()) { if ($this->checkStmt($stmt) && $stmt->bind_param('ddssii', $threshold, $donate, $email, $timezone, $is_anonymous, $userID) && $stmt->execute()) {
$this->log->log("info", $this->getUserName($userID)." updated their account details"); $this->log->log("info", $this->getUserName($userID)." updated their account details");
return true; // Update coin address too
if ($this->coin_address->update($userID, $address))
return true;
} }
// Catchall // Catchall
$this->setErrorMessage('Failed to update your account'); $this->setErrorMessage('Failed to update your account');
@ -703,22 +687,18 @@ class User extends Base {
$this->debug->append("Fetching user information for user id: $userID"); $this->debug->append("Fetching user information for user id: $userID");
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT SELECT
id, username, pin, api_key, is_admin, is_anonymous, email, timezone, no_fees, id AS id, username, pin, api_key, is_admin, is_anonymous, email, timezone, no_fees,
IFNULL(donate_percent, '0') as donate_percent, coin_address, ap_threshold IFNULL(donate_percent, '0') as donate_percent, ap_threshold
FROM $this->table FROM " . $this->getTableName() . "
WHERE id = ? LIMIT 0,1"); WHERE id = ? LIMIT 0,1");
if ($this->checkStmt($stmt)) { if ($this->checkStmt($stmt) && $stmt->bind_param('i', $userID) && $stmt->execute() && $result = $stmt->get_result()) {
$stmt->bind_param('i', $userID); $aData = $result->fetch_assoc();
if (!$stmt->execute()) { $aData['coin_address'] = $this->coin_address->getCoinAddress($userID);
$this->debug->append('Failed to execute statement');
return false;
}
$result = $stmt->get_result();
$stmt->close(); $stmt->close();
return $result->fetch_assoc(); return $aData;
} }
$this->debug->append("Failed to fetch user information for $userID"); $this->debug->append("Failed to fetch user information for $userID");
return false; return $this->sqlError();
} }
/** /**
@ -742,6 +722,10 @@ class User extends Base {
return false; return false;
} }
if (!is_null($coinaddress)) { if (!is_null($coinaddress)) {
if ($this->coin_address->existsCoinAddress($coinaddress)) {
$this->setErrorMessage('Coin address is already taken');
return false;
}
if (!$this->bitcoin->validateaddress($coinaddress)) { if (!$this->bitcoin->validateaddress($coinaddress)) {
$this->setErrorMessage('Coin address is not valid'); $this->setErrorMessage('Coin address is not valid');
return false; return false;
@ -755,7 +739,7 @@ class User extends Base {
$this->setErrorMessage( 'This e-mail address is already taken' ); $this->setErrorMessage( 'This e-mail address is already taken' );
return false; return false;
} }
if (strlen($password1) < 8) { if (strlen($password1) < 8) {
$this->setErrorMessage( 'Password is too short, minimum of 8 characters required' ); $this->setErrorMessage( 'Password is too short, minimum of 8 characters required' );
return false; return false;
} }
@ -801,15 +785,15 @@ class User extends Base {
! $this->setting->getValue('accounts_confirm_email_disabled') ? $is_locked = 1 : $is_locked = 0; ! $this->setting->getValue('accounts_confirm_email_disabled') ? $is_locked = 1 : $is_locked = 0;
$is_admin = 0; $is_admin = 0;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
INSERT INTO $this->table (username, pass, email, signup_timestamp, pin, api_key, is_locked, coin_address) INSERT INTO $this->table (username, pass, email, signup_timestamp, pin, api_key, is_locked)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?)
"); ");
} else { } else {
$is_locked = 0; $is_locked = 0;
$is_admin = 1; $is_admin = 1;
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
INSERT INTO $this->table (username, pass, email, signup_timestamp, pin, api_key, is_admin, is_locked, coin_address) INSERT INTO $this->table (username, pass, email, signup_timestamp, pin, api_key, is_admin, is_locked)
VALUES (?, ?, ?, ?, ?, ?, 1, ?, ?) VALUES (?, ?, ?, ?, ?, ?, 1, ?)
"); ");
} }
@ -820,7 +804,9 @@ class User extends Base {
$username_clean = strip_tags($username); $username_clean = strip_tags($username);
$signup_time = time(); $signup_time = time();
if ($this->checkStmt($stmt) && $stmt->bind_param('sssissis', $username_clean, $password_hash, $email1, $signup_time, $pin_hash, $apikey_hash, $is_locked, $coinaddress) && $stmt->execute()) { if ($this->checkStmt($stmt) && $stmt->bind_param('sssissi', $username_clean, $password_hash, $email1, $signup_time, $pin_hash, $apikey_hash, $is_locked) && $stmt->execute()) {
$new_account_id = $this->mysqli->insert_id;
if (!is_null($coinaddress)) $this->coin_address->add($new_account_id, $coinaddress);
if (! $this->setting->getValue('accounts_confirm_email_disabled') && $is_admin != 1) { if (! $this->setting->getValue('accounts_confirm_email_disabled') && $is_admin != 1) {
if ($token = $this->token->createToken('confirm_email', $stmt->insert_id)) { if ($token = $this->token->createToken('confirm_email', $stmt->insert_id)) {
$aData['username'] = $username_clean; $aData['username'] = $username_clean;
@ -843,7 +829,8 @@ class User extends Base {
} else { } else {
$this->setErrorMessage( 'Unable to register' ); $this->setErrorMessage( 'Unable to register' );
$this->debug->append('Failed to insert user into DB: ' . $this->mysqli->error); $this->debug->append('Failed to insert user into DB: ' . $this->mysqli->error);
if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Username, email or Coinaddress already registered' ); echo $this->mysqli->error;
if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Username or email already registered' );
return false; return false;
} }
return false; return false;
@ -997,4 +984,5 @@ $user->setMail($mail);
$user->setToken($oToken); $user->setToken($oToken);
$user->setBitcoin($bitcoin); $user->setBitcoin($bitcoin);
$user->setSetting($setting); $user->setSetting($setting);
$user->setCoinAddress($coin_address);
$user->setErrorCodes($aErrorCodes); $user->setErrorCodes($aErrorCodes);

View File

@ -104,7 +104,7 @@ if ($user->isAuthenticated()) {
$_SESSION['POPUP'][] = array('CONTENT' => 'You have not yet unlocked account withdrawls.', 'TYPE' => 'alert alert-danger'); $_SESSION['POPUP'][] = array('CONTENT' => 'You have not yet unlocked account withdrawls.', 'TYPE' => 'alert alert-danger');
} else if ($aBalance['confirmed'] < $config['mp_threshold']) { } else if ($aBalance['confirmed'] < $config['mp_threshold']) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Payout must be greater or equal than ' . $config['mp_threshold'] . '.', 'TYPE' => 'info'); $_SESSION['POPUP'][] = array('CONTENT' => 'Payout must be greater or equal than ' . $config['mp_threshold'] . '.', 'TYPE' => 'info');
} else if (!$user->getCoinAddress($_SESSION['USERDATA']['id'])) { } else if (!$coin_address->getCoinAddress($_SESSION['USERDATA']['id'])) {
$_SESSION['POPUP'][] = array('CONTENT' => 'You have no payout address set.', 'TYPE' => 'alert alert-danger'); $_SESSION['POPUP'][] = array('CONTENT' => 'You have no payout address set.', 'TYPE' => 'alert alert-danger');
} else { } else {
$user->log->log("info", $_SESSION['USERDATA']['username']." requesting manual payout"); $user->log->log("info", $_SESSION['USERDATA']['username']." requesting manual payout");

View File

@ -4,12 +4,10 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
if ($user->isAuthenticated()) { if ($user->isAuthenticated()) {
if (!$user->getCoinAddress($_SESSION['USERDATA']['id']) AND $setting->getValue('disable_worker_edit')) { if (!$coin_address->getCoinAddress($_SESSION['USERDATA']['id']) AND $setting->getValue('disable_worker_edit')) {
$_SESSION['POPUP'][] = array('CONTENT' => 'You have no payout address set.', 'TYPE' => 'alert alert-danger'); $_SESSION['POPUP'][] = array('CONTENT' => 'You have no payout address set.', 'TYPE' => 'alert alert-danger');
$_SESSION['POPUP'][] = array('CONTENT' => 'You can not add workers unless a valid Payout Address is set in your User Settings.', 'TYPE' => 'alert alert-danger'); $_SESSION['POPUP'][] = array('CONTENT' => 'You can not add workers unless a valid Payout Address is set in your User Settings.', 'TYPE' => 'alert alert-danger');
$smarty->assign('CONTENT', 'disabled.tpl'); $smarty->assign('CONTENT', 'disabled.tpl');
} else { } else {
switch (@$_REQUEST['do']) { switch (@$_REQUEST['do']) {
case 'delete': case 'delete':

View File

@ -2,7 +2,7 @@
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
define('MPOS_VERSION', '0.0.4'); define('MPOS_VERSION', '0.0.4');
define('DB_VERSION', '0.0.11'); define('DB_VERSION', '0.0.12');
define('CONFIG_VERSION', '0.0.8'); define('CONFIG_VERSION', '0.0.8');
define('HASH_VERSION', 1); define('HASH_VERSION', 1);

View File

@ -42,6 +42,7 @@ echo 'Validating all coin addresses. This may take some time.' . PHP_EOL . PHP_E
printf($mask, 'Username', 'E-Mail', 'Address', 'Status'); printf($mask, 'Username', 'E-Mail', 'Address', 'Status');
foreach ($users as $aData) { foreach ($users as $aData) {
$aData['coin_address'] = $coin_address->getCoinAddress($aData['id']);
if (empty($aData['coin_address']) && $aData['is_locked'] == 0) { if (empty($aData['coin_address']) && $aData['is_locked'] == 0) {
$status = 'UNSET'; $status = 'UNSET';
} else if ($aData['is_locked'] == 1) { } else if ($aData['is_locked'] == 1) {

View File

@ -36,7 +36,6 @@
$username = $user['username']; $username = $user['username'];
$loggedIp = $user['loggedIp']; $loggedIp = $user['loggedIp'];
$lastLogin = $user['last_login']; $lastLogin = $user['last_login'];
$coinAddress = $user['coin_address'];
$mailAddress = $user['email']; $mailAddress = $user['email'];
$everLoggedIn = !empty($lastLogin); $everLoggedIn = !empty($lastLogin);

View File

@ -53,6 +53,16 @@ CREATE TABLE IF NOT EXISTS `blocks` (
KEY `time` (`time`) KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service'; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service';
CREATE TABLE IF NOT EXISTS `coin_addresses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL,
`currency` varchar(5) NOT NULL,
`coin_address` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `coin_address` (`coin_address`),
KEY `account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE IF NOT EXISTS `invitations` ( CREATE TABLE IF NOT EXISTS `invitations` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(11) unsigned NOT NULL, `account_id` int(11) unsigned NOT NULL,

View File

@ -0,0 +1,32 @@
<?php
function run_0012() {
// Ugly but haven't found a better way
global $setting, $config, $user, $mysqli;
// Version information
$db_version_old = '0.0.11'; // What version do we expect
$db_version_new = '0.0.12'; // What is the new version we wish to upgrade to
$db_version_now = $setting->getValue('DB_VERSION'); // Our actual version installed
// Upgrade specific variables
$aSql[] = "CREATE TABLE `coin_addresses` ( `id` int(11) NOT NULL AUTO_INCREMENT, `account_id` int(11) NOT NULL, `currency` varchar(55555) NOT NULL, `coin_address` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `coin_address` (`coin_address`), KEY `account_id` (`account_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$aSql[] = "INSERT INTO coin_addresses (account_id, currency, coin_address) SELECT id, '" . $config['currency'] . "', coin_address FROM " . $user->getTableName();
$aSql[] = "ALTER TABLE `" . $user->getTableName() . "` DROP `coin_address`";
$aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '0.0.12' WHERE name = 'DB_VERSION'";
if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) {
// Run the upgrade
echo '- Starting database migration to version ' . $db_version_new . PHP_EOL;
foreach ($aSql as $sql) {
echo '- Preparing: ' . $sql . PHP_EOL;
$stmt = $mysqli->prepare($sql);
if ($stmt && $stmt->execute()) {
echo '- success' . PHP_EOL;
} else {
echo '- failed: ' . $mysqli->error . PHP_EOL;
exit(1);
}
}
}
}
?>