迁移修改

This commit is contained in:
邓皓元 2019-02-14 09:09:21 +08:00
parent 21318bdff5
commit 5d1a29e1ab
4 changed files with 24 additions and 24 deletions

View File

@ -41,13 +41,13 @@ class CompanyReportService extends Service
'package_id',
'type',
'unit_price',
DB::raw('count(*) as counts'),
DB::raw('count(distinct sim) as counts'),
];
$query = $this->orderCardPartitionRepository->select($select)
->withConditions($conditions)
->groupBy(['company_id', 'package_id', 'type', 'unit_price'])
->orderBy('company_id')->orderBy('type');
->orderBy('company_id')->orderBy('package_id');
$res = $query->paginate($conditions['limit']);

View File

@ -48,18 +48,26 @@ class CreateVirtualOrderCardsTables extends Migration
Schema::table('virtual_order_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
});
Schema::table('virtual_order_renewal_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
});
Schema::table('virtual_order_renewal_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
});
Schema::table('virtual_order_flows_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
});
}

View File

@ -1,6 +1,12 @@
-- 从旧平台迁移数据
-- 第一步:复制 ckb_custom 与 ckb_custom_handle_log
-- 第一步:建立外部服务器 ckb_custom 与 ckb_custom_handle_log
CREATE EXTENSION IF NOT EXISTS mysql_fdw;
CREATE SERVER IF NOT EXISTS vd_old FOREIGN DATA WRAPPER mysql_fdw OPTIONS (host 'rm-bp1i7dsf6fi1bc5y2o.mysql.rds.aliyuncs.com', port '3306');
CREATE USER MAPPING IF NOT EXISTS FOR root SERVER vd_old OPTIONS (username 'vduser', password 'fxft@123');
DROP FOREIGN TABLE IF EXISTS ckb_custom;
DROP FOREIGN TABLE IF EXISTS ckb_custom_handle_log;
IMPORT FOREIGN SCHEMA "vd-new" limit to ("ckb_custom","ckb_custom_handle_log") FROM SERVER vd_old INTO vd_old;
-- 第二步:建立物化视图
CREATE MATERIALIZED VIEW logs AS
@ -39,7 +45,7 @@ CREATE MATERIALIZED VIEW logs AS
0
ELSE
NULL::integer
END, lpad((virtual_companies.id)::text, 3, '0'::text), lpad((virtual_packages.id)::text, 4, '0'::text), lpad((((ckb_custom_handle_log.order_account)::integer * 100))::text, 6, '0'::text)) AS order_sn,
END, lpad((virtual_companies.id)::text, 3, '0'::text), lpad((virtual_packages.id)::text, 4, '0'::text), lpad((((ckb_custom_handle_log.order_account)::float * 100))::text, 6, '0'::text)) AS order_sn,
COALESCE(virtual_companies.id, 0) AS company_id,
COALESCE(virtual_packages.id, 0) AS package_id,
COALESCE(min(virtual_products.id), 0) AS product_id,
@ -53,7 +59,7 @@ CREATE MATERIALIZED VIEW logs AS
ELSE
NULL::text
END AS pay_channel,
((ckb_custom_handle_log.order_account)::integer * 100) AS unit_price,
((ckb_custom_handle_log.order_account)::float * 100) AS unit_price,
CASE min((ckb_custom_handle_log.valid_start_time)::text)
WHEN '' THEN
NULL
@ -76,7 +82,7 @@ CREATE MATERIALIZED VIEW logs AS
LEFT JOIN vd_old.ckb_custom ON (((ckb_custom.custom_no)::text = (ckb_custom_handle_log.custom_no)::text)))
LEFT JOIN vd.virtual_companies ON ((((virtual_companies.sn)::TEXT = concat('No', lpad((ckb_custom_handle_log.company)::TEXT, 11, '0'::TEXT))) AND (virtual_companies.deleted_at IS NULL))))
LEFT JOIN vd.virtual_packages ON ((((virtual_packages.sn)::text = (ckb_custom_handle_log.content)::text) AND (virtual_packages.deleted_at IS NULL))))
LEFT JOIN vd.virtual_products ON (((virtual_products.sn)::text = concat(ckb_custom_handle_log.content, '_', virtual_companies.id, '_', ((ckb_custom_handle_log.order_account)::integer * 100)))))
LEFT JOIN vd.virtual_products ON (((virtual_products.sn)::text = concat(ckb_custom_handle_log.content, '_', virtual_companies.id, '_', ((ckb_custom_handle_log.order_account)::float * 100)))))
WHERE (ckb_custom_handle_log.type = ANY (ARRAY[11, 13, 14, 15]))
GROUP BY
ckb_custom_handle_log.create_time,
@ -156,7 +162,7 @@ INSERT INTO vd.virtual_order_cards ("type", sim, order_id, company_id, package_i
LEFT JOIN vd.virtual_orders ON virtual_orders.sn = logs.order_sn
WHERE
logs.order_type = 0
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO NOTHING;
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO UPDATE SET service_start_at = excluded.service_start_at, service_end_at = excluded.service_end_at;
INSERT INTO vd.virtual_order_renewal_cards ("type", sim, order_id, company_id, package_id, counts, unit_price, service_start_at, service_end_at, created_at, updated_at)
(
@ -177,7 +183,7 @@ INSERT INTO vd.virtual_order_renewal_cards ("type", sim, order_id, company_id, p
LEFT JOIN vd.virtual_orders ON virtual_orders.sn = logs.order_sn
WHERE
logs.order_type = 1
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO NOTHING;
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO UPDATE SET service_start_at = excluded.service_start_at, service_end_at = excluded.service_end_at;
INSERT INTO vd.virtual_order_renewal_package_cards ("type", sim, order_id, company_id, package_id, counts, unit_price, service_start_at, service_end_at, created_at, updated_at)
(
@ -198,7 +204,7 @@ INSERT INTO vd.virtual_order_renewal_package_cards ("type", sim, order_id, compa
LEFT JOIN vd.virtual_orders ON virtual_orders.sn = logs.order_sn
WHERE
logs.order_type = 2
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO NOTHING;
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO UPDATE SET service_start_at = excluded.service_start_at, service_end_at = excluded.service_end_at;
INSERT INTO vd.virtual_order_flows_package_cards ("type", sim, order_id, company_id, package_id, counts, unit_price, service_start_at, service_end_at, created_at, updated_at)
(
@ -219,5 +225,5 @@ INSERT INTO vd.virtual_order_flows_package_cards ("type", sim, order_id, company
LEFT JOIN vd.virtual_orders ON virtual_orders.sn = logs.order_sn
WHERE
logs.order_type = 3
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO NOTHING;
) ON CONFLICT (sim, order_id, COALESCE(deleted_at::TIMESTAMP, '1970-01-01 08:00:00'::TIMESTAMP)) DO UPDATE SET service_start_at = excluded.service_start_at, service_end_at = excluded.service_end_at;

View File

@ -1,17 +1,3 @@
<?php
require_once realpath(dirname(__FILE__) . '/TestCase.php');
echo human_filesize(20*pow(1024, 0), 2) . PHP_EOL;
echo human_filesize(20*pow(1024, 1), 2) . PHP_EOL;
echo human_filesize(20*pow(1024, 2), 2) . PHP_EOL;
echo human_filesize(20*pow(1024, 3), 2) . PHP_EOL;
echo human_filesize(20*pow(1024, 4), 2) . PHP_EOL;
echo PHP_EOL;
echo human_filesize(20*pow(1024, 0), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL;
echo human_filesize(20*pow(1024, 1), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL;
echo human_filesize(20*pow(1024, 2), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL;
echo human_filesize(20*pow(1024, 3), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL;
echo human_filesize(20*pow(1024, 4), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL;