osb/database/migrations/2021_12_20_000000_int_unsigned.php
2022-02-02 10:43:59 +11:00

369 lines
18 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/*
| ab_account_billing |
| ab_account_group |
| ab_account_log |
| ab_account_memo |
| ab_account_oauth |
| ab_adsl_plan |
| ab_adsl_supplier |
| ab_adsl_supplier_plan |
| ab_affiliate |
| ab_asset |
| ab_asset_pool |
| ab_cart |
| ab_checkout |
| ab_checkout_notify |
| ab_discount |
| ab_domain_registrar |
| ab_domain_tld |
| ab_email_log |
| ab_email_setup |
| ab_email_template |
| ab_email_template_translate |
| ab_export |
| ab_export_datamap |
| ab_export_item |
| ab_export_module |
| ab_group |
| ab_group_method |
| ab_host_server |
| ab_host_server_affiliate |
| ab_host_server_id |
| ab_host_tld_id |
| ab_import |
| ab_invoice |
| ab_invoice_item |
| ab_invoice_item_discount |
| ab_invoice_item_tax |
| ab_invoice_memo |
| ab_log_error |
| ab_module |
| ab_module_method |
| ab_module_method_token |
| ab_oauth |
| ab_pivot_product_cat |
| ab_product |
| ab_product_cat |
| ab_product_cat_translate |
| ab_product_translate |
| ab_record_id |
| rtm |*DONE*
| ab_service |
| ab_service__adsl |
| ab_service__adsl_traffic |
| ab_service__hosting |
| ab_service__ssl |
| ab_service__voip |
| ab_service_change |
| ab_service_memo |
| ab_setup_invoice |
| ab_ssl |
| ab_ssl_ca |
| ab_static_page |
| ab_static_page_category |
| ab_static_page_translate |
| ab_task |
| ab_task_log |
| ab_voip_plan |
| accounts |*PARTIAL*
| charges |
| countries |*DONE*
| currencies |*DONE*
| external_account |
| external_integrations |
| failed_jobs |*N/A*
| invites |
| jobs |*N/A*
| languages |*DONE*
| migrations |*N/A*
| oauth_access_tokens |
| oauth_auth_codes |
| oauth_clients |
| oauth_personal_access_clients |
| oauth_refresh_tokens |
| password_resets |
| payment_items |
| payments |
| quickbooks_tokens |
| service__generic |
| service_domains |
| site_details |*DONE*
| sites |*DONE*
| supplier_details |*DONE*
| suppliers |*DONE*
| taxes |*DONE*
| users |*PARTIAL*
*/
class IntUnsigned extends Migration
{
public function up()
{
$constraints = [
'ab_account_billing' => ['fk_ab_a','fk_ab_s'],
'ab_account_group' => ['fk_ag_acc','fk_ag_grp'],
'ab_account_log' => ['fk_al_acc'],
'ab_account_oauth' => ['fk_ao_a','fk_ao_o','site_user_id'],
'ab_adsl_plan' => ['fk_ap_asp'],
'ab_adsl_supplier' => ['fk_as_set'],
'ab_adsl_supplier_plan' => ['fk_asp_as'],
'ab_affiliate' => ['fk_aff_acc'],
'ab_cart' => ['fk_mod_crt'],
'ab_checkout' => ['fk_cko_set'],
'ab_checkout_notify' => ['fk_cn_c'],
'ab_domain_tld' => ['fk_ht_hrp'],
'ab_email_log' => ['fk_el_acc','fk_el_ett','fk_el_mod'],
'ab_email_setup' => ['fk_es_acc'],
'ab_email_template' => ['fk_et_es'],
'ab_email_template_translate' => ['fk_emt_et','fk_emt_lan'],
'ab_export' => ['fk_exp_set'],
'ab_export_datamap' => ['fk_edm_exm','fk_edm_mod'],
'ab_export_item' => ['fk_eit_exm'],
'ab_export_module' => ['fk_exm_exp','fk_exm_mod'],
'ab_group' => ['fk_grp_set'],
'ab_group_method' => ['fk_gm_grp','fk_gm_mm'],
'ab_host_server_affiliate' => ['fk_hsa_aff','fk_hsa_hs'],
'ab_invoice' => ['fk_inv_acc'],
'ab_invoice_item' => ['fk_ii_inv','fk_ii_mod','fk_ii_pdt','fk_ii_svc'],
'ab_invoice_item_tax' => ['fk_iit_ii','fk_iit_tax'],
'ab_invoice_memo' => ['fk_im_acc','fk_im_inv'],
'ab_module' => ['fk_mod','fk_mod_set'],
'ab_module_method' => ['fk_mm_mod'],
'ab_module_method_token' => ['fk_mmt_acc','fk_mmt_mm'],
'ab_oauth' => ['fk_o_set'],
'ab_pivot_product_cat' => ['fk_p','fk_pc'],
'ab_product' => ['fk_pdt_set'],
'ab_product_cat_translate' => ['fk_pct_lan','fk_pct_pc'],
'ab_product_cat' => ['fk_pc_pc','fk_pc_set'],
'ab_product_translate' => ['fk_pt_l','fk_pt_p'],
'ab_record_id' => ['fk_rid_mod','fk_rid_set'],
'rtm' => ['fk_rtm_acc','fk_rtm_rtm'],
'ab_service__adsl_traffic' => ['ab_service__adsl_traffic_ab_service_adsl_id_foreign','fk_sat_as'],
'ab_service__adsl' => ['fk_sa_ap','fk_sa_svc'],
'ab_service__hosting' => ['fk_sh_svc','fk_sh_ht','fk_sh_hs'],
'ab_service__ssl' => ['fk_ss_sca','fk_ss_svc'],
'ab_service_change' => ['fk_sc_a','fk_sc_p','fk_sc_s'],
'ab_service_memo' => ['fk_sm_acc','fk_sm_svc'],
'ab_service' => ['ab_service_orderby_id_foreign','fk_svc_acc','fk_svc_grp'],
'ab_ssl' => ['fk_ssl_set'],
'ab_ssl_ca' => ['fk_sca','fk_sca_acc'],
'ab_task_log' => ['fk_tl_t'],
'ab_task' => ['fk_tsk_set'],
'accounts' => ['ab_account_user_id_foreign','fk_acc_cty','fk_acc_cur','fk_acc_lan','fk_acc_rtm','fk_acc_set'],
'charges' => ['charges_account_id_site_id_foreign','charges_product_id_site_id_foreign','charges_service_id_site_id_foreign','charges_user_id_site_id_foreign'],
'countries' => ['countries_currency_id_foreign'],
'external_account' => ['external_account_account_id_foreign','external_account_external_integration_id_foreign','external_account_site_id_foreign'],
'external_integrations' => ['external_integrations_user_id_foreign'],
'payment_items' => ['fk_pi_inv','payment_items_payment_id_site_id_foreign'],
'payments' => ['fk_pay_acc','fk_pay_acc_b','fk_pay_co'],
'quickbooks_tokens' => ['quickbooks_tokens_user_id_foreign'],
'service__generic' => ['service__generic_product_id_site_id_foreign','service__generic_service_id_site_id_foreign','service__generic_site_id_foreign'],
'service_domains' => ['fk_sd_hrp','fk_sd_ht','fk_sd_svc'],
'site_details' => ['site_details_site_id_foreign'],
'sites' => ['sites_admin_id_site_id_foreign','sites_country_id_foreign','sites_currency_id_foreign','sites_language_id_foreign'],
'supplier_details' => ['supplier_details_site_id_foreign','supplier_details_supplier_id_foreign'],
'taxes' => ['taxes_country_id_foreign'],
'users' => ['users_currency_id_foreign','users_language_id_foreign','users_parent_id_foreign'],
];
$indexes = [
'ab_account_billing' => ['fk_ab_a','fk_ab_s','UNIQUE'],
'ab_account_group' => ['fk_ag_acc_idx','fk_ag_grp_idx','UNIQUE'],
'ab_account_log' => ['fk_al_acc_idx'],
'ab_account_memo' => ['memo','account','IDS'],
'ab_account_oauth' => ['fk_ao_a_idx','fk_ao_o_idx','site_user_id_idx','ab_account_oauth_site_id_user_id_oauth_id_unique','UNIQUE'],
'ab_adsl_supplier_plan' => ['fk_asp_as_idx'],
'ab_adsl_supplier' => ['fk_as_set_idx'],
'ab_adsl_plan' => ['fk_ap_asp_idx'],
'ab_affiliate' => ['fk_aff_acc_idx'],
'ab_asset_pool' => ['main','IDS'],
'ab_asset' => ['assets','asso','service','asset','IDS'],
'ab_cart' => ['fk_mod_crt_idx'],
'ab_checkout_notify' => ['fk_cn_c_idx'],
'ab_checkout' => ['fk_cko_set_idx'],
'ab_discount' => ['start','expire','status','IDS'],
'ab_domain_registrar' => ['fk_hrp_set','IDS'],
'ab_domain_tld' => ['fk_ht_hrp_idx'],
'ab_email_log' => ['fk_el_acc_idx','fk_el_ett_idx','fk_el_mod_idx'],
'ab_email_setup' => ['fk_es_acc_idx'],
'ab_email_template_translate' => ['fk_emt_et_idx','fk_emt_lan_idx'],
'ab_email_template' => ['fk_et_es_idx'],
'ab_export_datamap' => ['fk_edm_mod_idx','fk_edm_mod_idx1','UNIQUE'],
'ab_export_item' => ['fk_exp_eit_idx','UNIQUE'],
'ab_export_module' => ['fk_exp_exm_idx','fk_exp_mod_idx'],
'ab_export' => ['fk_exp_set_idx','UNIQUE'],
'ab_group_method' => ['fk_gm_grp_idx','fk_gm_mm_idx','UNIQUE'],
'ab_group' => ['fk_grp_set_idx'],
'ab_host_server' => ['IDS'],
'ab_host_server_affiliate' => ['fk_hsa_aff_idx','fk_hsa_hs_idx'],
'ab_import' => ['IDS'],
'ab_invoice_item_discount' => ['discounts','invoice_id','dates','accounts','IDS'],
'ab_invoice_item_tax' => ['fk_iit_ii_idx','fk_iit_tax_idx'],
'ab_invoice_item' => ['fk_ii_inv_idx','fk_ii_mod_idx','fk_ii_pdt_idx','fk_ii_svc_idx','UNIQUE'],
'ab_invoice_memo' => ['fk_im_acc_idx','fk_im_inv_idx'],
'ab_invoice' => ['fk_inv_acc_idx'],
'ab_log_error' => ['IDS'],
'ab_module_method_token' => ['fk_mmt_acc_idx','fk_mmt_mm_idx'],
'ab_module_method' => ['fk_mm_mod_idx','UNIQUE','UNIQUE2'],
'ab_module' => ['fk_mod_idx','fk_mod_set_idx','UNIQUE'],
'ab_oauth' => ['fk_o_set_idx','ab_oauth_site_id_name_unique'],
'ab_pivot_product_cat' => ['fk_p_idx','fk_pc_idx','UNIQUE'],
'ab_product_cat' => ['fk_pc_pc_idx','fk_pc_set_idx'],
'ab_product' => ['fk_pdt_set_idx'],
'ab_product_cat_translate' => ['fk_pct_lan_idx','fk_pct_pc_idx','UNIQUE'],
'ab_product_translate' => ['fk_pt_l_idx','fk_pt_p_idx','UNIQUE'],
'ab_record_id' => ['fk_rid_mod_idx','fk_rid_set_idx'],
'rtm' => ['fk_rtm_set_idx','fk_rtm_acc_p_idx','uq_name'],
'ab_service' => ['ab_service_orderby_id_foreign','fk_svc_acc_idx','fk_svc_grp_idx','UNIQUE'],
'ab_service__adsl' => ['fk_service_number','fk_sa_svc_idx','fk_sa_ap_idx','uq_service_username','UNIQUE'],
'ab_service__adsl_traffic' => ['fk_sat_as_idx','in_sat_SERVICE_DATE','in_sat_DATE','in_sat_SERVICE','ab_service__adsl_traffic_ab_service_adsl_id_date_time_unique'],
'ab_service__hosting' => ['fk_sh_hs_idx','fk_domain_tld_id','fk_service_id','uq_ftp_username','UNIQUE'],
'ab_service__ssl' => ['fk_ss_sca_idx','fk_ss_svc_idx','UNIQUE'],
'ab_service_change' => ['fk_s_sc_idx','fk_s_p_idx','fk_sc_a_idx'],
'ab_service_memo' => ['fk_sm_acc_idx','fk_sm_svc_idx'],
'ab_setup_invoice' => ['IDS'],
'ab_ssl' => ['fk_ssl_set_idx'],
'ab_ssl_ca' => ['fk_sca_idx','fk_sca_acc_idx'],
'ab_static_page' => ['cat','start','expire','name','IDS'],
'ab_static_page_category' => ['status','name','IDS'],
'ab_static_page_translate' => ['IDS'],
'ab_task' => ['fk_tsk_set_idx'],
'ab_task_log' => ['fk_tl_task'],
'accounts' => ['ab_account_user_id_foreign','fk_acc_set_idx','fk_acc_lan_idx','fk_acc_rtm_idx','fk_acc_cty_idx','fk_acc_cur_idx'],
'charges' => ['charges_account_id_site_id_foreign','charges_product_id_site_id_foreign','charges_service_id_site_id_foreign','charges_user_id_site_id_foreign'],
'countries' => ['countries_currency_id_foreign','name_UNIQUE','two_code_UNIQUE','three_code_UNIQUE'],
'currencies' => ['three_digit_UNIQUE'],
'external_account' => ['external_account_external_integration_id_foreign','external_account_account_id_foreign','sae'],
'external_integrations' => ['external_integrations_user_id_foreign'],
'languages' => ['name_UNIQUE','iso_UNIQUE'],
'payment_items' => ['fk_pi_inv_idx','payment_items_payment_id_site_id_foreign','payment_items_site_id_payment_id_invoice_id_unique'],
'payments' => ['fk_pay_acc_idx','fk_pay_cp_idx','fk_pay_acc_b_idx','payments_site_id_id_unique'],
'quickbooks_tokens' => ['quickbooks_tokens_user_id_foreign'],
'service__generic' => ['service__generic_service_id_site_id_foreign','service__generic_product_id_site_id_foreign','service__generic_site_id_service_id_unique'],
'service_domains' => ['fk_sd_svc_idx','fk_sd_hrp_idx','fk_sd_ht_idx','UNIQUE'],
'site_details' => ['site_details_site_id_key_unique'],
'sites' => ['sites_site_id_index','sites_admin_id_site_id_foreign','sites_country_id_foreign','sites_currency_id_foreign','sites_language_id_foreign','sites_site_id_url_unique'],
'supplier_details' => ['supplier_details_site_id_foreign','supplier_details_supplier_id_site_id_unique'],
'taxes' => ['UNIQUE'],
'users' => ['users_site_id_email_unique','users_parent_id_foreign','users_language_id_foreign','users_currency_id_foreign','site_user_id_idx'],
];
Schema::dropIfExists('ab_host_server_id');
Schema::dropIfExists('ab_host_tld_id');
DB::statement('ALTER TABLE ab_rtm RENAME TO rtm');
foreach ($constraints as $table => $constraint) {
Schema::table($table, function (Blueprint $table) use ($constraint) {
foreach ($constraint as $item)
$table->dropForeign($item);
});
}
foreach ($indexes as $table => $constraint) {
Schema::table($table, function (Blueprint $table) use ($constraint) {
foreach ($constraint as $item)
$table->dropIndex($item);
});
}
$tables = collect(array_unique(array_merge(array_keys($constraints),array_keys($indexes))));
$except = [
'ab_group','ab_record_id','ab_service__adsl_traffic','accounts','external_account','site_details',
'ab_account_memo','ab_asset_pool','ab_asset','ab_discount','ab_host_server','ab_import','ab_invoice_item_discount',
'ab_log_error','ab_setup_invoice','ab_static_page','ab_static_page_category','ab_static_page_translate',
'ab_email_template',
];
foreach ($tables->diff($except) as $table) {
DB::statement(sprintf('ALTER TABLE %s MODIFY id int unsigned auto_increment',$table));
if (Schema::hasColumn($table,'site_id')) {
DB::statement(sprintf('ALTER TABLE %s MODIFY site_id int unsigned NOT NULL',$table));
}
}
Schema::table('sites', function (Blueprint $table) {
$table->index(['site_id']);
});
foreach ($tables->diff($except)->diff(['sites']) as $table) {
if (Schema::hasColumn($table,'site_id')) {
dump($table);
Schema::table($table, function (Blueprint $table) {
$table->foreign(['site_id'])->references(['site_id'])->on('sites');
$table->index(['id','site_id']);
});
}
}
DB::statement('ALTER TABLE currencies RENAME COLUMN iso_code TO iso');
Schema::table('currencies', function (Blueprint $table) {
$table->unique(['name']);
$table->unique(['iso']);
});
Schema::table('languages', function (Blueprint $table) {
$table->unique(['name']);
$table->unique(['iso']);
});
DB::statement('ALTER TABLE countries MODIFY currency_id int unsigned');
Schema::table('countries', function (Blueprint $table) {
$table->foreign(['currency_id'])->references(['id'])->on('currencies');
$table->index(['id','currency_id']);
$table->unique(['name']);
$table->unique(['two_code']);
$table->unique(['three_code']);
});
DB::statement('ALTER TABLE users MODIFY country_id int unsigned NOT NULL,MODIFY language_id int unsigned NOT NULL,MODIFY currency_id int unsigned NOT NULL');
Schema::table('users', function (Blueprint $table) {
$table->foreign(['country_id','currency_id'])->references(['id','currency_id'])->on('countries');
$table->foreign(['parent_id','site_id'])->references(['id','site_id'])->on('users');
$table->foreign(['language_id'])->references(['id'])->on('languages');
});
DB::statement('DELETE FROM accounts WHERE id=0');
DB::statement('ALTER TABLE accounts MODIFY id int unsigned auto_increment');
DB::statement('ALTER TABLE accounts MODIFY site_id int unsigned NOT NULL');
DB::statement('ALTER TABLE accounts MODIFY country_id int unsigned NOT NULL,MODIFY language_id int unsigned NOT NULL,MODIFY currency_id int unsigned NOT NULL,MODIFY rtm_id int unsigned DEFAULT NULL,MODIFY active tinyint(1) NOT NULL');
Schema::table('accounts', function (Blueprint $table) {
$table->foreign(['site_id'])->references(['site_id'])->on('sites');
$table->index(['id','site_id']);
$table->foreign(['country_id','currency_id'])->references(['id','currency_id'])->on('countries');
$table->foreign(['user_id','site_id'])->references(['id','site_id'])->on('users');
$table->foreign(['language_id'])->references(['id'])->on('languages');
$table->foreign(['rtm_id','site_id'])->references(['id','site_id'])->on('rtm');
});
DB::statement('ALTER TABLE rtm MODIFY account_id int unsigned NOT NULL,MODIFY parent_id int unsigned');
Schema::table('rtm', function (Blueprint $table) {
$table->foreign(['parent_id','site_id'])->references(['id','site_id'])->on('rtm');
$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
});
DB::statement('ALTER TABLE sites MODIFY country_id int unsigned NOT NULL,MODIFY language_id int unsigned NOT NULL,MODIFY currency_id int unsigned NOT NULL');
Schema::table('sites', function (Blueprint $table) {
$table->foreign(['country_id','currency_id'])->references(['id','currency_id'])->on('countries');
$table->foreign(['admin_id','site_id'])->references(['id','site_id'])->on('users');
$table->foreign(['language_id'])->references(['id'])->on('languages');
});
DB::statement('ALTER TABLE site_details MODIFY site_id int unsigned NOT NULL');
Schema::table('site_details', function (Blueprint $table) {
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->unique(['site_id','key']);
});
DB::statement('ALTER TABLE taxes MODIFY country_id int unsigned NOT NULL');
Schema::table('taxes', function (Blueprint $table) {
$table->foreign(['country_id'])->references(['id'])->on('countries');
$table->unique(['country_id','zone']);
});
}
}