From 0d5457fca3758e8224ea12b695a823a587cd31d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Wed, 31 May 2023 16:26:16 +0800 Subject: [PATCH] =?UTF-8?q?enhance(migration):=E5=88=A0=E9=99=A4=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=81=E7=A7=BB=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- migration/20220915101901_initialize.tx.up.sql | 240 ------------------ .../20220915101901_uninitialize.tx.down.sql | 21 -- .../20220915131901_add_constraints.up.sql | 31 --- ...20220915131901_remove_constraints.down.sql | 31 --- ...0220920131201_jsonb_to_summary.tx.down.sql | 23 -- .../20220920131201_summary_to_jsonb.tx.up.sql | 46 ---- migration/20220921143001_maintenance.down.sql | 15 -- migration/20220921143001_maintenance.up.sql | 37 --- migration/main.go | 21 -- 9 files changed, 465 deletions(-) delete mode 100644 migration/20220915101901_initialize.tx.up.sql delete mode 100644 migration/20220915101901_uninitialize.tx.down.sql delete mode 100644 migration/20220915131901_add_constraints.up.sql delete mode 100644 migration/20220915131901_remove_constraints.down.sql delete mode 100644 migration/20220920131201_jsonb_to_summary.tx.down.sql delete mode 100644 migration/20220920131201_summary_to_jsonb.tx.up.sql delete mode 100644 migration/20220921143001_maintenance.down.sql delete mode 100644 migration/20220921143001_maintenance.up.sql delete mode 100644 migration/main.go diff --git a/migration/20220915101901_initialize.tx.up.sql b/migration/20220915101901_initialize.tx.up.sql deleted file mode 100644 index a8fb2ab..0000000 --- a/migration/20220915101901_initialize.tx.up.sql +++ /dev/null @@ -1,240 +0,0 @@ -create table if not exists region ( - code varchar(15) not null primary key, - name varchar(50) not null, - level smallint not null default 0, - parent varchar(15) not null default '0' -); - -create table if not exists `user` ( - created_at timestamptz not null, - id varchar(120) not null primary key, - username varchar(30) not null, - password varchar(256) not null, - reset_needed boolean not null default false, - type smallint not null, - enabled boolean not null default true -); - -create table if not exists user_detail ( - created_at timestamptz not null, - created_by varchar(100), - last_modified_at timestamptz, - last_modified_by varchar(100), - deleted_at timestamptz, - deleted_by varchar(100), - id varchar(120) not null primary key, - name varchar(100), - abbr varchar(50), - region varchar(10), - address varchar(120), - contact varchar(100), - phone varchar(50), - unit_service_fee numeric(8,2) not null default 0, - service_expiration date not null -); - -create table if not exists user_charge ( - created_at timestamptz not null, - seq bigserial not null primary key, - user_id varchar(120) not null, - fee numeric(12,2), - discount numeric(5,4), - amount numeric(12,2), - charge_to date not null, - settled boolean not null default false, - settled_at timestamptz, - cancelled boolean not null default false, - cancelled_at timestamptz, - refunded boolean not null default false, - refunded_at timestamptz -); - -create table if not exists park ( - created_at timestamptz not null, - created_by varchar(100), - last_modified_at timestamptz, - last_modified_by varchar(100), - deleted_at timestamptz, - deleted_by varchar(100), - id varchar(120) not null primary key, - user_id varchar(120) not null, - name varchar(70) not null, - abbr varchar(50), - area numeric(14,2), - tenement_quantity numeric(8,0), - capacity numeric(16,2), - category smallint not null default 0, - meter_04kv_type smallint not null default 0, - region varchar(10), - address varchar(120), - contact varchar(100), - phone varchar(50), - enabled boolean not null default true -); - -create table if not exists meter_04kv ( - created_at timestamptz not null, - created_by varchar(100), - last_modified_at timestamptz, - last_modified_by varchar(100), - code varchar(120) not null, - park_id varchar(120) not null, - address varchar(100), - customer_name varchar(100), - contact_name varchar(70), - contact_phone varchar(50), - ratio numeric(8,4) not null default 1, - seq bigint not null default 0, - public_meter boolean not null default false, - dilute boolean not null default false, - enabled boolean not null default true, - primary key (code, park_id) -); - -create table if not exists maintenance_fee ( - created_at timestamptz not null, - created_by varchar(100), - last_modified_at timestamptz, - last_modified_by varchar(100), - deleted_at timestamptz, - deleted_by varchar(100), - id varchar(120) not null primary key, - park_id varchar(120) not null, - name varchar(50) not null, - fee numeric(8,2) not null default 0, - memo text, - enabled boolean not null default true -); - -create table if not exists report ( - created_at timestamptz not null, - created_by varchar(100), - last_modified_at timestamptz, - last_modified_by varchar(100), - id varchar(120) not null primary key, - park_id varchar(120) not null, - period date not null, - category smallint not null default 0, - meter_04kv_type smallint not null default 0, - step_state jsonb not null, - published boolean not null default false, - published_at timestamptz, - withdraw smallint not null default 0, - last_withdraw_applied_at timestamptz, - last_withdraw_audit_at timestamptz -); - -create table if not exists report_summary ( - report_id varchar(120) not null primary key, - overall numeric(14,2) not null default 0, - overall_fee numeric(14,2) not null default 0, - consumption_fee numeric(14,2), - overall_price numeric(16,8), - critical numeric(14,2) not null default 0, - critical_fee numeric(14,2) not null default 0, - critical_price numeric(16,8), - peak numeric(14,2) not null default 0, - peak_fee numeric(14,2) not null default 0, - peak_price numeric(16,8), - flat numeric(14,2) not null default 0, - flat_fee numeric(14,2) not null default 0, - flat_price numeric(16,8), - valley numeric(14,2) not null default 0, - valley_fee numeric(14,2) not null default 0, - valley_price numeric(16,8), - loss numeric(14,2), - loss_fee numeric(16,2), - loss_proportion numeric(16,15), - customer_consumption numeric(16,2), - customer_consumption_fee numeric(14,2), - customer_consumption_critical numeric(16,2), - customer_consumption_critical_fee numeric(14,2), - customer_consumption_peak numeric(16,2), - customer_consumption_peak_fee numeric(14,2), - customer_consumption_flat numeric(16,2), - customer_consumption_flat_fee numeric(14,2), - customer_consumption_valley numeric(16,2), - customer_consumption_valley_fee numeric(14,2), - public_consumption numeric(16,2), - public_consumption_fee numeric(14,2), - public_consumption_proportion numeric(16,15), - public_consumption_critical numeric(16,2), - public_consumption_critical_fee numeric(14,2), - public_consumption_peak numeric(16,2), - public_consumption_peak_fee numeric(14,2), - public_consumption_flat numeric(16,2), - public_consumption_flat_fee numeric(14,2), - public_consumption_valley numeric(16,2), - public_consumption_valley_fee numeric(14,2), - basic_fee numeric(14,2) not null default 0, - basic_diluted_price numeric(18,8), - adjust_fee numeric(14,2) not null default 0, - adjust_diluted_price numeric(18,8), - maintenance_diluted_price numeric(16,8), - loss_diluted_price numeric(16,8), - public_consumption_diluted_price numeric(16,8), - maintenance_overall numeric(16,8), - final_diluted_overall numeric(14,2) -); - -create table if not exists will_diluted_fee ( - id varchar(120) not null primary key, - report_id varchar(120) not null, - source_id varchar(120), - name varchar(50) not null, - fee numeric(8,2) not null default 0, - memo text -); - -create table if not exists end_user_detail ( - created_at timestamptz not null, - created_by varchar(100), - last_modified_at timestamptz, - last_modified_by varchar(100), - report_id varchar(120) not null, - park_id varchar(120) not null, - meter_04kv_id varchar(120) not null, - seq bigint not null default 0, - ratio numeric(8,4) not null default 1, - address varchar(100), - customer_name varchar(100), - contact_name varchar(70), - contact_phone varcahar(50), - public_meter boolean not null default false, - dilute boolean not null default false, - last_period_overall numeric(14,2) not null default 0, - last_period_critical numeric(14,2) not null default 0, - last_period_peak numeric(14,2) not null default 0, - last_period_flat numeric(14,2) not null default 0, - last_period_valley numeric(14,2) not null default 0, - current_period_overall numeric(14,2) not null default 0, - current_period_critical numeric(14,2) not null default 0, - current_period_peak numeric(14,2) not null default 0, - current_period_flat numeric(14,2) not null default 0, - current_period_valley numeric(14,2) not null default 0, - adjust_overall numeric(14,2) not null default 0, - adjust_critical numeric(14,2) not null default 0, - adjust_peak numeric(14,2) not null default 0, - adjust_flat numeric(14,2) not null default 0, - adjust_valley numeric(14,2) not null default 0, - overall numeric(14,2), - overall_fee numeric(14,2), - overall_proportion numeric(16,15) not null default 0, - critical numeric(14,2), - critical_fee numeric(18,8), - peak numeric(14,2), - peak_fee numeric(18,8), - flat numeric(14,2), - flat_fee numeric(18,8), - valley numeric(14,2), - valley_fee numeric(18,8), - basic_fee_diluted numeric(18,8), - adjust_fee_diluted numeric(18,8), - loss_diluted numeric(18,8), - loss_fee_diluted numeric(18,8), - maintenance_fee_diluted numeric(18,8), - public_consumption_diluted numeric(18,8), - final_diluted numeric(14,2), - final_charge numeric(14,2), - primary key (report_id, park_id, meter_04kv_id) -); \ No newline at end of file diff --git a/migration/20220915101901_uninitialize.tx.down.sql b/migration/20220915101901_uninitialize.tx.down.sql deleted file mode 100644 index f117cdb..0000000 --- a/migration/20220915101901_uninitialize.tx.down.sql +++ /dev/null @@ -1,21 +0,0 @@ -drop table if exists region; - -drop table if exists user; - -drop table if exists user_detail; - -drop table if exists user_charge; - -drop table if exists park; - -drop table if exists meter_04kv; - -drop table if exists maintenance_fee; - -drop table if exists report; - -drop table if exists report_summary; - -drop table if exists will_diluted_fee; - -drop table if exists end_user_detail; \ No newline at end of file diff --git a/migration/20220915131901_add_constraints.up.sql b/migration/20220915131901_add_constraints.up.sql deleted file mode 100644 index 2289513..0000000 --- a/migration/20220915131901_add_constraints.up.sql +++ /dev/null @@ -1,31 +0,0 @@ -alter table if exists `user` add constraint user_type_check check (type in (0, 1, 2)); - -alter table if exists user_detail add constraint positive_service_fee check (unit_service_fee >= 0); - -alter table if exists user_charge add constraint positive_fee check (fee >= 0); - -alter table if exists user_charge add constraint positive_amount check (amount >= 0); - -alter table if exists park add constraint positive_tenement check (tenement_quantity >= 0); - -alter table if exists park add constraint positive_area check (area >= 0); - -alter table if exists park add constraint positive_capacity check (capacity >= 0); - -alter table if exists park add constraint category_check check (category in (0, 1, 2)); - -alter table if exists park add constraint meter_check check (meter_04kv_type in (0, 1)); - -alter table if exists meter_04kv add constraint positive_ratio check (ratio > 0); - -alter table if exists maintenance_fee add constraint positive_fee check (fee >= 0); - -alter table if exists report add constraint category_check check (category in (0, 1, 2)); - -alter table if exists report add constraint meter_check check (meter_04kv_type in (0, 1)); - -alter table if exists report add constraint withdraw_action_check check (withdraw in (0, 1, 2, 3)); - -alter table if exists will_diluted_fee add constraint positive_fee check (fee >= 0); - -alter table if exists end_user_detail add constraint positive_ratio check (ratio > 0); \ No newline at end of file diff --git a/migration/20220915131901_remove_constraints.down.sql b/migration/20220915131901_remove_constraints.down.sql deleted file mode 100644 index bf653a4..0000000 --- a/migration/20220915131901_remove_constraints.down.sql +++ /dev/null @@ -1,31 +0,0 @@ -alter table if exists user drop constraint user_type_check; - -alter table if exists user_detail drop constraint positive_service_fee; - -alter table if exists user_charge drop constraint positive_fee; - -alter table if exists user_charge drop constraint positive_amount; - -alter table if exists park drop constraint positive_tenement; - -alter table if exists park drop constraint positive_area; - -alter table if exists park drop constraint positive_capacity; - -alter table if exists park drop constraint category_check; - -alter table if exists park drop constraint meter_check; - -alter table if exists meter_04kv drop constraint positive_ratio; - -alter table if exists maintenance_fee drop constraint positive_fee; - -alter table if exists report drop constraint category_check; - -alter table if exists report drop constraint meter_check; - -alter table if exists report drop constraint withdraw_action_check; - -alter table if exists will_diluted_fee drop constraint positive_fee; - -alter table if exists end_user_detail drop constraint positive_ratio; \ No newline at end of file diff --git a/migration/20220920131201_jsonb_to_summary.tx.down.sql b/migration/20220920131201_jsonb_to_summary.tx.down.sql deleted file mode 100644 index ce25791..0000000 --- a/migration/20220920131201_jsonb_to_summary.tx.down.sql +++ /dev/null @@ -1,23 +0,0 @@ -update report_summary -set - customer_consumption = nullif(trim(customers->>'consumption'), '')::numeric(16,2), - customer_consumption_fee = nullif(trim(customers->>'consumptionFee'), '')::numeric(14,2), - customer_consumption_critical = nullif(trim(customers->>'critical'), '')::numeric(16,2), - customer_consumption_critical_fee = nullif(trim(customers->>'criticalFee'), '')::numeric(14,2), - customer_consumption_peak = nullif(trim(customers->>'peak'), '')::numeric(16,2), - customer_consumption_peak_fee = nullif(trim(customers->>'peakFee'), '')::numeric(14,2), - customer_consumption_flat = nullif(trim(customers->>'flat'), '')::numeric(16,2), - customer_consumption_flat_fee = nullif(trim(customers->>'flatFee'), '')::numeric(14,2), - customer_consumption_valley = nullif(trim(customers->>'valley'), '')::numeric(16,2), - customer_consumption_valley_fee = nullif(trim(customers->>'valleyFee'), '')::numeric(14,2), - public_consumption = nullif(trim(publics->>'consumption'), '')::numeric(16,2), - public_consumption_fee = nullif(trim(publics->>'consumptionFee'), '')::numeric(14,2), - public_consumption_critical = nullif(trim(publics->>'critical'), '')::numeric(16,2), - public_consumption_critical_fee = nullif(trim(publics->>'criticalFee'), '')::numeric(14,2), - public_consumption_peak = nullif(trim(publics->>'peak'), '')::numeric(16,2), - public_consumption_peak_fee = nullif(trim(publics->>'peakFee'), '')::numeric(14,2), - public_consumption_flat = nullif(trim(publics->>'flat'), '')::numeric(16,2), - public_consumption_flat_fee = nullif(trim(publics->>'flatFee'), '')::numeric(14,2), - public_consumption_valley = nullif(trim(publics->>'valley'), '')::numeric(16,2), - public_consumption_valley_fee = nullif(trim(publics->>'valleyFee'), '')::numeric(14,2), - public_consumption_proportion = nullif(trim(publics->>'proportion'), '')::numeric(16,15); \ No newline at end of file diff --git a/migration/20220920131201_summary_to_jsonb.tx.up.sql b/migration/20220920131201_summary_to_jsonb.tx.up.sql deleted file mode 100644 index 4497bf0..0000000 --- a/migration/20220920131201_summary_to_jsonb.tx.up.sql +++ /dev/null @@ -1,46 +0,0 @@ -alter table report_summary - add column if not exists customers jsonb, - add column if not exists publics jsonb, - add column if not exists diluteds jsonb; - -update report_summary -set - customers = jsonb_build_object( - 'consumption', customer_consumption, - 'fee', customer_consumption_fee, - 'critical', customer_consumption_critical, - 'criticalFee', customer_consumption_critical_fee, - 'peak', customer_consumption_peak, - 'peakFee', customer_consumption_peak_fee, - 'flat', customer_consumption_flat, - 'flatFee', customer_consumption_flat_fee, - 'valley', customer_consumption_valley, - 'valleyFee', customer_consumption_valley_fee, - 'proportion', null - ), - diluteds = jsonb_build_object( - 'consumption', public_consumption, - 'fee', public_consumption_fee, - 'critical', public_consumption_critical, - 'criticalFee', public_consumption_critical_fee, - 'peak', public_consumption_peak, - 'peakFee', public_consumption_peak_fee, - 'flat', public_consumption_flat, - 'flatFee', public_consumption_flat_fee, - 'valley', public_consumption_valley, - 'valleyFee', public_consumption_valley_fee, - 'proportion', public_consumption_proportion - ), - publics = jsonb_build_object( - 'consumption', null, - 'fee', null, - 'critical', null, - 'criticalFee', null, - 'peak', null, - 'peakFee', null, - 'flat', null, - 'flatFee', null, - 'valley', null, - 'valleyFee', null, - 'proportion', null - ); \ No newline at end of file diff --git a/migration/20220921143001_maintenance.down.sql b/migration/20220921143001_maintenance.down.sql deleted file mode 100644 index 8c30b46..0000000 --- a/migration/20220921143001_maintenance.down.sql +++ /dev/null @@ -1,15 +0,0 @@ -alter table meter_04kv - add column dilute boolean not null default false; - -alter table end_user_detail - add column dilute boolean not null default false, - add column maintenance_fee_diluted numeric(18,8), - add column public_consumption_diluted numeric(18,8); - -alter table maintenance_fee - drop column period; - -alter table report_summary - drop column authorize_loss, - drop column authorize_loss_fee, - drop column authorize_loss_proportion; \ No newline at end of file diff --git a/migration/20220921143001_maintenance.up.sql b/migration/20220921143001_maintenance.up.sql deleted file mode 100644 index 1eff678..0000000 --- a/migration/20220921143001_maintenance.up.sql +++ /dev/null @@ -1,37 +0,0 @@ -alter table meter_04kv - drop column dilute; - -alter table end_user_detail - drop column dilute, - drop column maintenance_fee_diluted, - drop column public_consumption_diluted; - -alter table maintenance_fee - add column period varchar(10); - -alter table report_summary - drop column customer_consumption, - drop column customer_consumption_fee, - drop column customer_consumption_critical, - drop column customer_consumption_critical_fee, - drop column customer_consumption_peak, - drop column customer_consumption_peak_fee, - drop column customer_consumption_flat, - drop column customer_consumption_flat_fee, - drop column customer_consumption_valley, - drop column customer_consumption_valley_fee, - drop column public_consumption, - drop column public_consumption_fee, - drop column public_consumption_critical, - drop column public_consumption_critical_fee, - drop column public_consumption_peak, - drop column public_consumption_peak_fee, - drop column public_consumption_flat, - drop column public_consumption_flat_fee, - drop column public_consumption_valley, - drop column public_consumption_valley_fee, - drop column public_consumption_proportion, - drop column diluteds, - add column authorize_loss numeric(14,2), - add column authorize_loss_fee numeric(16,2), - add column authorize_loss_proportion numeric(16,15); \ No newline at end of file diff --git a/migration/main.go b/migration/main.go deleted file mode 100644 index 09979af..0000000 --- a/migration/main.go +++ /dev/null @@ -1,21 +0,0 @@ -package migration - -import ( - "electricity_bill_calc/logger" - "embed" - - "github.com/uptrace/bun/migrate" - "go.uber.org/zap" -) - -var ( - //go:embed *.sql - sqlMigrations embed.FS - Migrations = migrate.NewMigrations() -) - -func init() { - if err := Migrations.Discover(sqlMigrations); err != nil { - logger.Named("Migrations").Fatal("Unable to load migrations.", zap.Error(err)) - } -}