insert into base.versions(id,version,updated_at,description)
values(next_id('base.versions'),'0.22.1',now(),'增加自助打印服务结构');

alter table edu.plan_audit_results add constraint uk_f6f86yl0ja9p2dveh9syviq2x unique (std_id);
alter table edu.course_grades drop constraint uk_3f0hiv52a3ohsy5aof6ye59jb cascade;
alter table edu.course_grades add constraint uk_xitxltysiibv3efvpe69hk1j unique (std_id,course_id,semester_id,crn);
create index idx_pjwqn6d874ljreci8v9elrxac on edu.exam_activities_rooms (exam_room_id);

create schema spa;
create table spa.coupons (id integer not null, begin_on date not null, end_on date not null, doc_type_id integer not null, count_per_std integer not null, updated_at timestamp not null);
create table spa.download_logs (id bigint not null, remark varchar(255), user_id bigint not null, doc_type_id integer not null, ip varchar(255) not null, updated_at timestamp not null);
create table spa.print_configs (max_limit integer not null, updated_at timestamp not null, price integer not null, id integer not null, begin_on date not null, end_on date, doc_type_id integer not null);
create table spa.print_logs (payed integer not null, id bigint not null, remark varchar(255), user_id bigint not null, doc_type_id integer not null, ip varchar(255) not null, updated_at timestamp not null);
create table spa.print_quotas (frees integer not null, print_cnt integer not null, free_cnt integer not null, user_id bigint not null, doc_type_id integer not null, last_print_at timestamp not null, id bigint not null);
alter table spa.coupons add constraint pk_4p4li8m16qqtm8iqytn92aynw primary key (id);
alter table spa.download_logs add constraint pk_hwtugxgk0i1ws7vpqbbxvgyji primary key (id);
alter table spa.print_configs add constraint pk_bfngxr4ih2js7aiimmsw54x58 primary key (id);
alter table spa.print_logs add constraint pk_gfrtd5snmjnuhycreal5usna7 primary key (id);
alter table spa.print_quotas add constraint pk_st8emevheay237gju9wkv7y2p primary key (id);
alter table spa.coupons add constraint fk_dvlhphsv2cv8mgqcp1ig2qhpk foreign key (doc_type_id) references spa.doc_types (id);
alter table spa.download_logs add constraint fk_asbpf8hyeevmvq5gt69e4qxga foreign key (doc_type_id) references spa.doc_types (id);
alter table spa.download_logs add constraint fk_pb9q5n7nqos1x5mit2q4j3nkn foreign key (user_id) references base.users (id);
alter table spa.print_configs add constraint fk_bm71oljg3facffojpmm860c5o foreign key (doc_type_id) references spa.doc_types (id);
alter table spa.print_logs add constraint fk_9me6jfaceqlx2dpcvj140f3bl foreign key (doc_type_id) references spa.doc_types (id);
alter table spa.print_logs add constraint fk_pbyh6ebfspjubwvkvv8ie12u foreign key (user_id) references base.users (id);
alter table spa.print_quotas add constraint fk_j5ah9jicykofscsccinruktmi foreign key (user_id) references base.users (id);
alter table spa.print_quotas add constraint fk_qb1ycc3kdgt2on7ej8i8cqm2l foreign key (doc_type_id) references spa.doc_types (id);

comment on column spa.coupons.begin_on is '开始日期';
comment on column spa.coupons.count_per_std is '每个人可以领取的数量';
comment on column spa.coupons.doc_type_id is '文档类型ID';
comment on column spa.coupons.end_on is '结束日期';
comment on column spa.coupons.id is '非业务主键:auto_increment';
comment on column spa.coupons.updated_at is '更新时间';
comment on column spa.download_logs.doc_type_id is '文档类型ID';
comment on column spa.download_logs.id is '非业务主键:datetime';
comment on column spa.download_logs.ip is '打印ip';
comment on column spa.download_logs.remark is '备注';
comment on column spa.download_logs.updated_at is '更新时间';
comment on column spa.download_logs.user_id is '学生ID';
comment on column spa.print_configs.begin_on is '生效日期';
comment on column spa.print_configs.doc_type_id is '文档类型ID';
comment on column spa.print_configs.end_on is '失效日期';
comment on column spa.print_configs.id is '非业务主键:auto_increment';
comment on column spa.print_configs.max_limit is '最多打印次数';
comment on column spa.print_configs.price is '单价，以分为单位，免费设置为0';
comment on column spa.print_configs.updated_at is '更新时间';
comment on column spa.print_logs.doc_type_id is '文档类型ID';
comment on column spa.print_logs.id is '非业务主键:datetime';
comment on column spa.print_logs.ip is '打印ip';
comment on column spa.print_logs.payed is '支付费用';
comment on column spa.print_logs.remark is '备注';
comment on column spa.print_logs.updated_at is '更新时间';
comment on column spa.print_logs.user_id is '学生ID';
comment on column spa.print_quotas.doc_type_id is '文档类型ID';
comment on column spa.print_quotas.free_cnt is '免支付打印张数';
comment on column spa.print_quotas.frees is '剩余免支付的张数';
comment on column spa.print_quotas.id is '非业务主键:datetime';
comment on column spa.print_quotas.last_print_at is '最后打印时间';
comment on column spa.print_quotas.print_cnt is '打印张数';
comment on column spa.print_quotas.user_id is '学生ID';
