create table `frequent_errors` (
  `code` varchar(255) not null,
  `error_types` varchar(255) not null,
  `position` varchar(255) not null,
  `deleted_at` timestamp null,
  `id` bigint unsigned not null auto_increment primary key,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

create table `frequent_error_types` (
  `name` varchar(255) not null,
  `id` bigint unsigned not null auto_increment primary key,
  `created_at` timestamp null,
  `updated_at` timestamp null,
  `deleted_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `frequent_error_types` add `created_by` bigint unsigned null after `deleted_at`;
alter table `frequent_error_types` add `updated_by` bigint unsigned null after `created_by`;

create table `frequent_error_entries` (
  `id` bigint unsigned not null auto_increment primary key,
  `form_id` int not null,
  `form_item_id` int not null,
  `examination_value` int not null,
  `comment` varchar(255) not null,
  `created_at` timestamp null,
  `updated_at` timestamp null,
  `deleted_at` timestamp null,
  `created_by` bigint unsigned null,
  `updated_by` bigint unsigned null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `frequent_error_entries` add `error_type_id` int not null after `id`;

create table `qr_codes` (
  `id` bigint unsigned not null auto_increment primary key,
  `status` varchar(255) not null,
  `starting_serial_number` varchar(255) not null,
  `ending_serial_number` varchar(255) not null,
  `paired_tags` varchar(255) not null,
  `created_by` bigint unsigned null,
  `updated_by` bigint unsigned null,
  `deleted_at` timestamp null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `qr_codes` add `quantity` int not null after `status`;

alter table `frequent_error_types` add `frequent_error_id` int not null after `id`;

alter table `qr_codes` drop `starting_serial_number`, drop `ending_serial_number`, drop `paired_tags`;

create table `qr_codes_batch` (
  `id` bigint unsigned not null auto_increment primary key,
  `starting_serial_number` varchar(255) not null,
  `ending_serial_number` varchar(255) not null,
  `paired_tags` varchar(255) not null,
  `created_by` bigint unsigned null,
  `updated_by` bigint unsigned null,
  `deleted_at` timestamp null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `qr_codes_batch` add `qr_code_id` int not null after `id`;

drop table if exists `qr_codes`;
drop table if exists `qr_codes_batch`;

create table `qr_batches` (
  `id` bigint unsigned not null auto_increment primary key,
  `status` varchar(255) not null,
  `created_by` bigint unsigned null,
  `updated_by` bigint unsigned null,
  `deleted_at` timestamp null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

create table `qr_codes` (
  `id` bigint unsigned not null auto_increment primary key,
  `batch_id` bigint unsigned not null,
  `machine_id` bigint unsigned null,
  `serial_number` varchar(255) not null,
  `created_by` bigint unsigned null,
  `updated_by` bigint unsigned null,
  `deleted_at` timestamp null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

create table `errors` (
  `id` bigint unsigned not null auto_increment primary key,
  `component_id` int not null,
  `error_type_id` int not null,
  `position` varchar(255) not null,
  `created_at` timestamp null,
  `updated_at` timestamp null
) default character set utf8mb4 collate 'utf8mb4_unicode_ci';

alter table `errors` add `created_by` varchar(255) null after `position`;
alter table `errors` add `updated_by` varchar(255) null after `created_by`;
alter table `errors` add `deleted_at` timestamp null after `updated_by`;

alter table `errors` add `machine_id` int not null after `id`;

alter table `forms` drop column `standard`;
alter table `tool_types` add `standard` varchar(255) not null after `type_name`;

