enhance(migrate):增加基于bun的迁移脚本。

This commit is contained in:
徐涛
2022-09-15 13:48:46 +08:00
parent 4254d020b9
commit 9380879ab5
8 changed files with 391 additions and 11 deletions

View File

@@ -0,0 +1,240 @@
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 bigint 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)
);

View File

@@ -0,0 +1,21 @@
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;

View File

@@ -0,0 +1,31 @@
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);

View File

@@ -0,0 +1,31 @@
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;

21
migration/main.go Normal file
View File

@@ -0,0 +1,21 @@
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))
}
}