mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
More robust migration; avoids timeout failures
This commit is contained in:
parent
b5b3fa56ac
commit
71b6b6eb87
@ -13,7 +13,14 @@ class EnterprisePlan extends Migration
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function up() {
|
public function up() {
|
||||||
|
$timeout = ini_get('max_execution_time');
|
||||||
|
if ($timeout == 0) {
|
||||||
|
$timeout = 600;
|
||||||
|
}
|
||||||
|
$timeout = max($timeout - 10, $timeout * .9);
|
||||||
|
$startTime = time();
|
||||||
|
|
||||||
|
if (!Schema::hasTable('companies')) {
|
||||||
Schema::create('companies', function($table)
|
Schema::create('companies', function($table)
|
||||||
{
|
{
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
@ -36,13 +43,15 @@ class EnterprisePlan extends Migration
|
|||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
$table->softDeletes();
|
$table->softDeletes();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Schema::hasColumn('accounts', 'company_id')) {
|
||||||
Schema::table('accounts', function($table)
|
Schema::table('accounts', function($table)
|
||||||
{
|
{
|
||||||
$table->unsignedInteger('company_id')->nullable();
|
$table->unsignedInteger('company_id')->nullable();
|
||||||
$table->foreign('company_id')->references('id')->on('companies');
|
$table->foreign('company_id')->references('id')->on('companies');
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$single_account_ids = \DB::table('users')
|
$single_account_ids = \DB::table('users')
|
||||||
->leftJoin('user_accounts', function ($join) {
|
->leftJoin('user_accounts', function ($join) {
|
||||||
@ -52,35 +61,55 @@ class EnterprisePlan extends Migration
|
|||||||
$join->orOn('user_accounts.user_id4', '=', 'users.id');
|
$join->orOn('user_accounts.user_id4', '=', 'users.id');
|
||||||
$join->orOn('user_accounts.user_id5', '=', 'users.id');
|
$join->orOn('user_accounts.user_id5', '=', 'users.id');
|
||||||
})
|
})
|
||||||
|
->leftJoin('accounts', 'accounts.id', '=', 'users.account_id')
|
||||||
->whereNull('user_accounts.id')
|
->whereNull('user_accounts.id')
|
||||||
|
->whereNull('accounts.company_id')
|
||||||
->where(function ($query) {
|
->where(function ($query) {
|
||||||
$query->whereNull('users.public_id');
|
$query->whereNull('users.public_id');
|
||||||
$query->orWhere('users.public_id', '=', 0);
|
$query->orWhere('users.public_id', '=', 0);
|
||||||
})
|
})
|
||||||
->lists('users.account_id');
|
->lists('users.account_id');
|
||||||
|
|
||||||
$group_accounts = \DB::select(
|
if (count($single_account_ids)) {
|
||||||
'SELECT u1.account_id as account1, u2.account_id as account2, u3.account_id as account3, u4.account_id as account4, u5.account_id as account5 FROM `user_accounts`
|
|
||||||
LEFT JOIN users u1 ON (u1.public_id IS NULL OR u1.public_id = 0) AND user_accounts.user_id1 = u1.id
|
|
||||||
LEFT JOIN users u2 ON (u2.public_id IS NULL OR u2.public_id = 0) AND user_accounts.user_id2 = u2.id
|
|
||||||
LEFT JOIN users u3 ON (u3.public_id IS NULL OR u3.public_id = 0) AND user_accounts.user_id3 = u3.id
|
|
||||||
LEFT JOIN users u4 ON (u4.public_id IS NULL OR u4.public_id = 0) AND user_accounts.user_id4 = u4.id
|
|
||||||
LEFT JOIN users u5 ON (u5.public_id IS NULL OR u5.public_id = 0) AND user_accounts.user_id5 = u5.id');
|
|
||||||
|
|
||||||
foreach (Account::find($single_account_ids) as $account) {
|
foreach (Account::find($single_account_ids) as $account) {
|
||||||
$this->upAccounts($account);
|
$this->upAccounts($account);
|
||||||
|
$this->checkTimeout($timeout, $startTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$group_accounts = \DB::select(
|
||||||
|
'SELECT u1.account_id as account1, u2.account_id as account2, u3.account_id as account3, u4.account_id as account4, u5.account_id as account5 FROM `user_accounts`
|
||||||
|
LEFT JOIN users u1 ON (u1.public_id IS NULL OR u1.public_id = 0) AND user_accounts.user_id1 = u1.id
|
||||||
|
LEFT JOIN users u2 ON (u2.public_id IS NULL OR u2.public_id = 0) AND user_accounts.user_id2 = u2.id
|
||||||
|
LEFT JOIN users u3 ON (u3.public_id IS NULL OR u3.public_id = 0) AND user_accounts.user_id3 = u3.id
|
||||||
|
LEFT JOIN users u4 ON (u4.public_id IS NULL OR u4.public_id = 0) AND user_accounts.user_id4 = u4.id
|
||||||
|
LEFT JOIN users u5 ON (u5.public_id IS NULL OR u5.public_id = 0) AND user_accounts.user_id5 = u5.id
|
||||||
|
LEFT JOIN accounts a1 ON a1.id = u1.account_id
|
||||||
|
LEFT JOIN accounts a2 ON a2.id = u2.account_id
|
||||||
|
LEFT JOIN accounts a3 ON a3.id = u3.account_id
|
||||||
|
LEFT JOIN accounts a4 ON a4.id = u4.account_id
|
||||||
|
LEFT JOIN accounts a5 ON a5.id = u5.account_id
|
||||||
|
WHERE (a1.id IS NOT NULL AND a1.company_id IS NULL)
|
||||||
|
OR (a2.id IS NOT NULL AND a2.company_id IS NULL)
|
||||||
|
OR (a3.id IS NOT NULL AND a3.company_id IS NULL)
|
||||||
|
OR (a4.id IS NOT NULL AND a4.company_id IS NULL)
|
||||||
|
OR (a5.id IS NOT NULL AND a5.company_id IS NULL)');
|
||||||
|
|
||||||
|
if (count($group_accounts)) {
|
||||||
foreach ($group_accounts as $group_account) {
|
foreach ($group_accounts as $group_account) {
|
||||||
$this->upAccounts(null, Account::find(get_object_vars($group_account)));
|
$this->upAccounts(null, Account::find(get_object_vars($group_account)));
|
||||||
|
$this->checkTimeout($timeout, $startTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Schema::hasColumn('accounts', 'pro_plan_paid')) {
|
||||||
Schema::table('accounts', function($table)
|
Schema::table('accounts', function($table)
|
||||||
{
|
{
|
||||||
$table->dropColumn('pro_plan_paid');
|
$table->dropColumn('pro_plan_paid');
|
||||||
$table->dropColumn('pro_plan_trial');
|
$table->dropColumn('pro_plan_trial');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function upAccounts($primaryAccount, $otherAccounts = array()) {
|
private function upAccounts($primaryAccount, $otherAccounts = array()) {
|
||||||
if(!$primaryAccount) {
|
if(!$primaryAccount) {
|
||||||
@ -128,6 +157,12 @@ LEFT JOIN users u5 ON (u5.public_id IS NULL OR u5.public_id = 0) AND user_accoun
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function checkTimeout($timeout, $startTime) {
|
||||||
|
if (time() - $startTime >= $timeout) {
|
||||||
|
exit('Migration reached time limit; please run again to continue');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverse the migrations.
|
* Reverse the migrations.
|
||||||
*
|
*
|
||||||
@ -135,25 +170,51 @@ LEFT JOIN users u5 ON (u5.public_id IS NULL OR u5.public_id = 0) AND user_accoun
|
|||||||
*/
|
*/
|
||||||
public function down()
|
public function down()
|
||||||
{
|
{
|
||||||
|
$timeout = ini_get('max_execution_time');
|
||||||
|
if ($timeout == 0) {
|
||||||
|
$timeout = 600;
|
||||||
|
}
|
||||||
|
$timeout = max($timeout - 10, $timeout * .9);
|
||||||
|
$startTime = time();
|
||||||
|
|
||||||
|
if (!Schema::hasColumn('accounts', 'pro_plan_paid')) {
|
||||||
Schema::table('accounts', function($table)
|
Schema::table('accounts', function($table)
|
||||||
{
|
{
|
||||||
$table->date('pro_plan_paid')->nullable();
|
$table->date('pro_plan_paid')->nullable();
|
||||||
$table->date('pro_plan_trial')->nullable();
|
$table->date('pro_plan_trial')->nullable();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
foreach (Company::all() as $company) {
|
$company_ids = \DB::table('companies')
|
||||||
|
->leftJoin('accounts', 'accounts.company_id', '=', 'companies.id')
|
||||||
|
->whereNull('accounts.pro_plan_paid')
|
||||||
|
->whereNull('accounts.pro_plan_trial')
|
||||||
|
->where(function ($query) {
|
||||||
|
$query->whereNotNull('companies.plan_paid');
|
||||||
|
$query->orWhereNotNull('companies.trial_started');
|
||||||
|
})
|
||||||
|
->lists('companies.id');
|
||||||
|
|
||||||
|
$company_ids = array_unique($company_ids);
|
||||||
|
|
||||||
|
if (count($company_ids)) {
|
||||||
|
foreach (Company::find($company_ids) as $company) {
|
||||||
foreach ($company->accounts as $account) {
|
foreach ($company->accounts as $account) {
|
||||||
$account->pro_plan_paid = $company->plan_paid;
|
$account->pro_plan_paid = $company->plan_paid;
|
||||||
$account->pro_plan_trial = $company->trial_started;
|
$account->pro_plan_trial = $company->trial_started;
|
||||||
$account->save();
|
$account->save();
|
||||||
}
|
}
|
||||||
|
$this->checkTimeout($timeout, $startTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Schema::hasColumn('accounts', 'company_id')) {
|
||||||
Schema::table('accounts', function($table)
|
Schema::table('accounts', function($table)
|
||||||
{
|
{
|
||||||
$table->dropForeign('accounts_company_id_foreign');
|
$table->dropForeign('accounts_company_id_foreign');
|
||||||
$table->dropColumn('company_id');
|
$table->dropColumn('company_id');
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Schema::dropIfExists('companies');
|
Schema::dropIfExists('companies');
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user