--
-- Disable foreign keys
--


--
-- Rename table `activities_memberships` to `tmp_devart_activities_memberships`
--
ALTER TABLE activities_memberships RENAME TO tmp_devart_activities_memberships;

--
-- Create table `activities_memberships`
--
CREATE TABLE activities_memberships (
                                        id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                        business_id INT(10) UNSIGNED NOT NULL,
                                        category_id INT(10) UNSIGNED NOT NULL,
                                        name VARCHAR(191) NOT NULL,
                                        members_limit INT(11) DEFAULT NULL,
                                        classes_limit INT(11) DEFAULT NULL,
                                        portal_enabled TINYINT(1) NOT NULL DEFAULT 0,
                                        created_at TIMESTAMP NULL DEFAULT NULL,
                                        updated_at TIMESTAMP NULL DEFAULT NULL,
                                        deleted_at TIMESTAMP NULL DEFAULT NULL,
                                        PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_memberships`
--
INSERT INTO activities_memberships(id, business_id, category_id, name, members_limit, classes_limit, portal_enabled, created_at, updated_at, deleted_at)
SELECT id, business_id, category_id, name, members_limit, classes_limit, portal_enabled, created_at, updated_at, deleted_at FROM tmp_devart_activities_memberships;

DROP TABLE tmp_devart_activities_memberships;

--
-- Create foreign key
--
ALTER TABLE activities_memberships
    ADD CONSTRAINT activities_memberships_category_id_foreign FOREIGN KEY (category_id)
        REFERENCES categories(id);

--
-- Create foreign key
--
ALTER TABLE activities_memberships
    ADD CONSTRAINT activities_memberships_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Rename table `activities_staff` to `tmp_devart_activities_staff`
--
ALTER TABLE activities_staff RENAME TO tmp_devart_activities_staff;

--
-- Create table `activities_staff`
--
CREATE TABLE activities_staff (
                                  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                  business_id INT(10) UNSIGNED NOT NULL,
                                  user_id INT(10) UNSIGNED NOT NULL,
                                  job_title VARCHAR(191) NOT NULL,
                                  created_at TIMESTAMP NULL DEFAULT NULL,
                                  updated_at TIMESTAMP NULL DEFAULT NULL,
                                  deleted_at TIMESTAMP NULL DEFAULT NULL,
                                  PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_staff`
--
INSERT INTO activities_staff(id, business_id, user_id, job_title, created_at, updated_at, deleted_at)
SELECT id, business_id, user_id, job_title, created_at, updated_at, deleted_at FROM tmp_devart_activities_staff;

DROP TABLE tmp_devart_activities_staff;

--
-- Create foreign key
--
ALTER TABLE activities_staff
    ADD CONSTRAINT activities_staff_user_id_foreign FOREIGN KEY (user_id)
        REFERENCES users(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_staff
    ADD CONSTRAINT activities_staff_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Rename table `activities_staff_attendances` to `tmp_devart_activities_staff_attendances`
--
ALTER TABLE activities_staff_attendances RENAME TO tmp_devart_activities_staff_attendances;

--
-- Create table `activities_staff_attendances`
--
CREATE TABLE activities_staff_attendances (
                                              id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
                                              staff_id INT(10) UNSIGNED NOT NULL,
                                              present TINYINT(1) NOT NULL DEFAULT 0,
                                              attendance_date DATE NOT NULL,
                                              created_at TIMESTAMP NULL DEFAULT NULL,
                                              updated_at TIMESTAMP NULL DEFAULT NULL,
                                              PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_staff_attendances`
--
INSERT INTO activities_staff_attendances(id, staff_id, present, attendance_date, created_at, updated_at)
SELECT id, staff_id, present, attendance_date, created_at, updated_at FROM tmp_devart_activities_staff_attendances;

DROP TABLE tmp_devart_activities_staff_attendances;

--
-- Create foreign key
--
ALTER TABLE activities_staff_attendances
    ADD CONSTRAINT activities_staff_attendances_staff_id_foreign FOREIGN KEY (staff_id)
        REFERENCES activities_staff(id) ON DELETE CASCADE;

--
-- Rename table `activities_physicals` to `tmp_devart_activities_physicals`
--
ALTER TABLE activities_physicals RENAME TO tmp_devart_activities_physicals;

--
-- Create table `activities_physicals`
--
CREATE TABLE activities_physicals (
                                      id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                      business_id INT(10) UNSIGNED NOT NULL,
                                      name VARCHAR(191) NOT NULL,
                                      unit VARCHAR(191) NOT NULL,
                                      placeholder VARCHAR(191) DEFAULT NULL,
                                      created_at TIMESTAMP NULL DEFAULT NULL,
                                      updated_at TIMESTAMP NULL DEFAULT NULL,
                                      deleted_at TIMESTAMP NULL DEFAULT NULL,
                                      PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_physicals`
--
INSERT INTO activities_physicals(id, business_id, name, unit, placeholder, created_at, updated_at, deleted_at)
SELECT id, business_id, name, unit, placeholder, created_at, updated_at, deleted_at FROM tmp_devart_activities_physicals;

DROP TABLE tmp_devart_activities_physicals;

--
-- Create foreign key
--
ALTER TABLE activities_physicals
    ADD CONSTRAINT activities_physicals_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Rename table `activities_classes` to `tmp_devart_activities_classes`
--
ALTER TABLE activities_classes RENAME TO tmp_devart_activities_classes;

--
-- Create table `activities_classes`
--
CREATE TABLE activities_classes (
                                    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                    business_id INT(10) UNSIGNED NOT NULL,
                                    name VARCHAR(191) NOT NULL,
                                    members_limit INT(11) DEFAULT NULL,
                                    color VARCHAR(191) DEFAULT NULL,
                                    staff_ids LONGTEXT BINARY CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`staff_ids`)),
                                    assistant_ids LONGTEXT BINARY CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`assistant_ids`)),
                                    activity_ids LONGTEXT BINARY CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`activity_ids`)),
                                    start_at DATE DEFAULT NULL,
                                    end_at DATE DEFAULT NULL,
                                    created_at TIMESTAMP NULL DEFAULT NULL,
                                    updated_at TIMESTAMP NULL DEFAULT NULL,
                                    deleted_at TIMESTAMP NULL DEFAULT NULL,
                                    PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_classes`
--
INSERT INTO activities_classes(id, business_id, name, members_limit, color, staff_ids, assistant_ids, activity_ids, start_at, end_at, created_at, updated_at, deleted_at)
SELECT id, business_id, name, members_limit, color, staff_ids, assistant_ids, activity_ids, start_at, end_at, created_at, updated_at, deleted_at FROM tmp_devart_activities_classes;

DROP TABLE tmp_devart_activities_classes;

--
-- Create foreign key
--
ALTER TABLE activities_classes
    ADD CONSTRAINT activities_classes_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Rename table `activities_class_schedules` to `tmp_devart_activities_class_schedules`
--
ALTER TABLE activities_class_schedules RENAME TO tmp_devart_activities_class_schedules;

--
-- Create table `activities_class_schedules`
--
CREATE TABLE activities_class_schedules (
                                            class_id INT(10) UNSIGNED NOT NULL,
                                            day ENUM('SATURDAY','SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY') NOT NULL,
                                            start_time TIME NOT NULL,
                                            end_time TIME NOT NULL
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_class_schedules`
--
INSERT INTO activities_class_schedules(class_id, day, start_time, end_time)
SELECT class_id, day, start_time, end_time FROM tmp_devart_activities_class_schedules;

DROP TABLE tmp_devart_activities_class_schedules;

--
-- Create foreign key
--
ALTER TABLE activities_class_schedules
    ADD CONSTRAINT activities_class_schedules_class_id_foreign FOREIGN KEY (class_id)
        REFERENCES activities_classes(id) ON DELETE CASCADE;

--
-- Rename table `activities_activities` to `tmp_devart_activities_activities`
--
ALTER TABLE activities_activities RENAME TO tmp_devart_activities_activities;

--
-- Create table `activities_activities`
--
CREATE TABLE activities_activities (
                                       id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                       business_id INT(10) UNSIGNED NOT NULL,
                                       name VARCHAR(191) NOT NULL,
                                       product_id INT(10) UNSIGNED NOT NULL,
                                       category_id INT(10) UNSIGNED NOT NULL,
                                       staff_ids LONGTEXT BINARY CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`staff_ids`)),
                                       membership_ids LONGTEXT BINARY CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`membership_ids`)),
                                       created_at TIMESTAMP NULL DEFAULT NULL,
                                       updated_at TIMESTAMP NULL DEFAULT NULL,
                                       deleted_at TIMESTAMP NULL DEFAULT NULL,
                                       PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_activities`
--
INSERT INTO activities_activities(id, business_id, name, product_id, category_id, staff_ids, membership_ids, created_at, updated_at, deleted_at)
SELECT id, business_id, name, product_id, category_id, staff_ids, membership_ids, created_at, updated_at, deleted_at FROM tmp_devart_activities_activities;

DROP TABLE tmp_devart_activities_activities;

--
-- Create foreign key
--
ALTER TABLE activities_activities
    ADD CONSTRAINT activities_activities_product_id_foreign FOREIGN KEY (product_id)
        REFERENCES products(id);

--
-- Create foreign key
--
ALTER TABLE activities_activities
    ADD CONSTRAINT activities_activities_category_id_foreign FOREIGN KEY (category_id)
        REFERENCES categories(id);

--
-- Create foreign key
--
ALTER TABLE activities_activities
    ADD CONSTRAINT activities_activities_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Rename table `activities_activity_staff` to `tmp_devart_activities_activity_staff`
--
ALTER TABLE activities_activity_staff RENAME TO tmp_devart_activities_activity_staff;

--
-- Create table `activities_activity_staff`
--
CREATE TABLE activities_activity_staff (
                                           activity_id INT(10) UNSIGNED NOT NULL,
                                           staff_id INT(10) UNSIGNED NOT NULL
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_activity_staff`
--
INSERT INTO activities_activity_staff(activity_id, staff_id)
SELECT activity_id, staff_id FROM tmp_devart_activities_activity_staff;

DROP TABLE tmp_devart_activities_activity_staff;

--
-- Create foreign key
--
ALTER TABLE activities_activity_staff
    ADD CONSTRAINT activities_activity_staff_staff_id_foreign FOREIGN KEY (staff_id)
        REFERENCES activities_staff(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_activity_staff
    ADD CONSTRAINT activities_activity_staff_activity_id_foreign FOREIGN KEY (activity_id)
        REFERENCES activities_activities(id) ON DELETE CASCADE;

--
-- Rename table `activities_activity_membership` to `tmp_devart_activities_activity_membership`
--
ALTER TABLE activities_activity_membership RENAME TO tmp_devart_activities_activity_membership;

--
-- Create table `activities_activity_membership`
--
CREATE TABLE activities_activity_membership (
                                                activity_id INT(10) UNSIGNED NOT NULL,
                                                membership_id INT(10) UNSIGNED NOT NULL
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_activity_membership`
--
INSERT INTO activities_activity_membership(activity_id, membership_id)
SELECT activity_id, membership_id FROM tmp_devart_activities_activity_membership;

DROP TABLE tmp_devart_activities_activity_membership;

--
-- Create foreign key
--
ALTER TABLE activities_activity_membership
    ADD CONSTRAINT activities_activity_membership_membership_id_foreign FOREIGN KEY (membership_id)
        REFERENCES activities_memberships(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_activity_membership
    ADD CONSTRAINT activities_activity_membership_activity_id_foreign FOREIGN KEY (activity_id)
        REFERENCES activities_activities(id) ON DELETE CASCADE;

--
-- Rename table `activities_members` to `tmp_devart_activities_members`
--
ALTER TABLE activities_members RENAME TO tmp_devart_activities_members;

--
-- Create table `activities_members`
--
CREATE TABLE activities_members (
                                    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                    business_id INT(10) UNSIGNED NOT NULL,
                                    contact_id INT(10) UNSIGNED NOT NULL,
                                    membership_id INT(10) UNSIGNED NOT NULL,
                                    rfid_serial VARCHAR(191) DEFAULT NULL,
                                    member_id VARCHAR(191) NOT NULL,
                                    national_id VARCHAR(191) NOT NULL,
                                    username VARCHAR(191) DEFAULT NULL,
                                    password VARCHAR(191) DEFAULT NULL,
                                    gender ENUM('male','female') NOT NULL DEFAULT 'male',
                                    status ENUM('PROSPECT','ACTIVE','ALUMNI') NOT NULL DEFAULT 'PROSPECT',
                                    created_at TIMESTAMP NULL DEFAULT NULL,
                                    updated_at TIMESTAMP NULL DEFAULT NULL,
                                    deleted_at TIMESTAMP NULL DEFAULT NULL,
                                    PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_members`
--
INSERT INTO activities_members(id, business_id, contact_id, membership_id, rfid_serial, member_id, national_id, username, password, gender, status, created_at, updated_at, deleted_at)
SELECT id, business_id, contact_id, membership_id, rfid_serial, member_id, national_id, username, password, gender, status, created_at, updated_at, deleted_at FROM tmp_devart_activities_members;

DROP TABLE tmp_devart_activities_members;

--
-- Create foreign key
--
ALTER TABLE activities_members
    ADD CONSTRAINT activities_members_membership_id_foreign FOREIGN KEY (membership_id)
        REFERENCES activities_memberships(id);

--
-- Create foreign key
--
ALTER TABLE activities_members
    ADD CONSTRAINT activities_members_contact_id_foreign FOREIGN KEY (contact_id)
        REFERENCES contacts(id);

--
-- Create foreign key
--
ALTER TABLE activities_members
    ADD CONSTRAINT activities_members_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Create index `activities_members_username_unique` on table `activities_members`
--
ALTER TABLE activities_members
    ADD UNIQUE INDEX activities_members_username_unique(username);

--
-- Create index `activities_members_rfid_serial_unique` on table `activities_members`
--
ALTER TABLE activities_members
    ADD UNIQUE INDEX activities_members_rfid_serial_unique(rfid_serial);

--
-- Create index `activities_members_national_id_unique` on table `activities_members`
--
ALTER TABLE activities_members
    ADD UNIQUE INDEX activities_members_national_id_unique(national_id);

--
-- Create index `activities_members_member_id_unique` on table `activities_members`
--
ALTER TABLE activities_members
    ADD UNIQUE INDEX activities_members_member_id_unique(member_id);

--
-- Drop foreign key
--
ALTER TABLE activities_member_subscriptions
DROP FOREIGN KEY activities_member_subscriptions_ibfk_1;

--
-- Drop index `sell_line_id` from table `activities_member_subscriptions`
--
ALTER TABLE activities_member_subscriptions
DROP INDEX sell_line_id;

--
-- Create foreign key
--
ALTER TABLE activities_member_subscriptions
    ADD CONSTRAINT activities_member_subscriptions_sell_line_id_foreign FOREIGN KEY (sell_line_id)
        REFERENCES transaction_sell_lines(id) ON DELETE SET NULL;

--
-- Create foreign key
--
ALTER TABLE activities_member_subscriptions
    ADD CONSTRAINT activities_member_subscriptions_activity_id_foreign FOREIGN KEY (activity_id)
        REFERENCES activities_activities(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_subscriptions
    ADD CONSTRAINT activities_member_subscriptions_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_subscriptions
    ADD CONSTRAINT activities_member_subscriptions_location_id_foreign FOREIGN KEY (location_id)
        REFERENCES business_locations(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_subscriptions
    ADD CONSTRAINT activities_member_subscriptions_member_id_foreign FOREIGN KEY (member_id)
        REFERENCES activities_members(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_subscriptions
    ADD CONSTRAINT activities_member_subscriptions_transaction_id_foreign FOREIGN KEY (transaction_id)
        REFERENCES transactions(id) ON DELETE SET NULL;

--
-- Create foreign key
--
ALTER TABLE activities_member_subscriptions
    ADD CONSTRAINT activities_member_subscriptions_user_id_foreign FOREIGN KEY (user_id)
        REFERENCES users(id) ON DELETE SET NULL;

--
-- Rename table `activities_member_classes` to `tmp_devart_activities_member_classes`
--
ALTER TABLE activities_member_classes RENAME TO tmp_devart_activities_member_classes;

--
-- Create table `activities_member_classes`
--
CREATE TABLE activities_member_classes (
                                           id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                           class_id INT(10) UNSIGNED NOT NULL,
                                           member_id INT(10) UNSIGNED NOT NULL,
                                           subscription_id INT(10) UNSIGNED NOT NULL,
                                           created_at TIMESTAMP NULL DEFAULT NULL,
                                           updated_at TIMESTAMP NULL DEFAULT NULL,
                                           PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_member_classes`
--
INSERT INTO activities_member_classes(id, class_id, member_id, subscription_id, created_at, updated_at)
SELECT id, class_id, member_id, subscription_id, created_at, updated_at FROM tmp_devart_activities_member_classes;

DROP TABLE tmp_devart_activities_member_classes;

--
-- Create foreign key
--
ALTER TABLE activities_member_classes
    ADD CONSTRAINT activities_member_classes_subscription_id_foreign FOREIGN KEY (subscription_id)
        REFERENCES activities_member_subscriptions(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_classes
    ADD CONSTRAINT activities_member_classes_member_id_foreign FOREIGN KEY (member_id)
        REFERENCES activities_members(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_classes
    ADD CONSTRAINT activities_member_classes_class_id_foreign FOREIGN KEY (class_id)
        REFERENCES activities_classes(id) ON DELETE CASCADE;

--
-- Rename table `activities_member_physicals` to `tmp_devart_activities_member_physicals`
--
ALTER TABLE activities_member_physicals RENAME TO tmp_devart_activities_member_physicals;

--
-- Create table `activities_member_physicals`
--
CREATE TABLE activities_member_physicals (
                                             id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                             business_id INT(10) UNSIGNED NOT NULL,
                                             member_id INT(10) UNSIGNED NOT NULL,
                                             image VARCHAR(191) DEFAULT NULL,
                                             created_at TIMESTAMP NULL DEFAULT NULL,
                                             updated_at TIMESTAMP NULL DEFAULT NULL,
                                             PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_member_physicals`
--
INSERT INTO activities_member_physicals(id, business_id, member_id, image, created_at, updated_at)
SELECT id, business_id, member_id, image, created_at, updated_at FROM tmp_devart_activities_member_physicals;

DROP TABLE tmp_devart_activities_member_physicals;

--
-- Create foreign key
--
ALTER TABLE activities_member_physicals
    ADD CONSTRAINT activities_member_physicals_member_id_foreign FOREIGN KEY (member_id)
        REFERENCES activities_members(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_physicals
    ADD CONSTRAINT activities_member_physicals_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Rename table `activities_member_physical_items` to `tmp_devart_activities_member_physical_items`
--
ALTER TABLE activities_member_physical_items RENAME TO tmp_devart_activities_member_physical_items;

--
-- Create table `activities_member_physical_items`
--
CREATE TABLE activities_member_physical_items (
                                                  member_physical_id INT(10) UNSIGNED NOT NULL,
                                                  member_id INT(10) UNSIGNED NOT NULL,
                                                  physical_id INT(10) UNSIGNED NOT NULL,
                                                  value VARCHAR(191) NOT NULL
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_member_physical_items`
--
INSERT INTO activities_member_physical_items(member_physical_id, member_id, physical_id, value)
SELECT member_physical_id, member_id, physical_id, value FROM tmp_devart_activities_member_physical_items;

DROP TABLE tmp_devart_activities_member_physical_items;

--
-- Create foreign key
--
ALTER TABLE activities_member_physical_items
    ADD CONSTRAINT main_physical_id_foreign FOREIGN KEY (member_physical_id)
        REFERENCES activities_member_physicals(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_physical_items
    ADD CONSTRAINT activities_member_physical_items_physical_id_foreign FOREIGN KEY (physical_id)
        REFERENCES activities_physicals(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_physical_items
    ADD CONSTRAINT activities_member_physical_items_member_id_foreign FOREIGN KEY (member_id)
        REFERENCES activities_members(id) ON DELETE CASCADE;

--
-- Rename table `activities_member_images` to `tmp_devart_activities_member_images`
--
ALTER TABLE activities_member_images RENAME TO tmp_devart_activities_member_images;

--
-- Create table `activities_member_images`
--
CREATE TABLE activities_member_images (
                                          id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                          business_id INT(10) UNSIGNED NOT NULL,
                                          member_id INT(10) UNSIGNED NOT NULL,
                                          type ENUM('MEMBER_IMAGE','NATIONAL_ID_FRONT','NATIONAL_ID_BACK','ARMED_ID','OTHER') NOT NULL,
                                          path VARCHAR(191) NOT NULL,
                                          name VARCHAR(191) DEFAULT NULL,
                                          size VARCHAR(191) DEFAULT NULL,
                                          token VARCHAR(191) DEFAULT NULL,
                                          created_at TIMESTAMP NULL DEFAULT NULL,
                                          updated_at TIMESTAMP NULL DEFAULT NULL,
                                          PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_member_images`
--
INSERT INTO activities_member_images(id, business_id, member_id, type, path, name, size, token, created_at, updated_at)
SELECT id, business_id, member_id, type, path, name, size, token, created_at, updated_at FROM tmp_devart_activities_member_images;

DROP TABLE tmp_devart_activities_member_images;

--
-- Create foreign key
--
ALTER TABLE activities_member_images
    ADD CONSTRAINT activities_member_images_member_id_foreign FOREIGN KEY (member_id)
        REFERENCES activities_members(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_images
    ADD CONSTRAINT activities_member_images_business_id_foreign FOREIGN KEY (business_id)
        REFERENCES business(id) ON DELETE CASCADE;

--
-- Rename table `activities_member_attendances` to `tmp_devart_activities_member_attendances`
--
ALTER TABLE activities_member_attendances RENAME TO tmp_devart_activities_member_attendances;

--
-- Create table `activities_member_attendances`
--
CREATE TABLE activities_member_attendances (
                                               id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                               class_id INT(10) UNSIGNED DEFAULT NULL,
                                               member_id INT(10) UNSIGNED NOT NULL,
                                               present TINYINT(1) NOT NULL DEFAULT 0,
                                               checkin_at DATETIME DEFAULT NULL,
                                               checkout_at DATETIME DEFAULT NULL,
                                               attendance_date DATE NOT NULL,
                                               created_at TIMESTAMP NULL DEFAULT NULL,
                                               updated_at TIMESTAMP NULL DEFAULT NULL,
                                               PRIMARY KEY (id)
)
    ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_unicode_ci;

--
-- Restore data to the table `activities_member_attendances`
--
INSERT INTO activities_member_attendances(id, class_id, member_id, present, checkin_at, checkout_at, attendance_date, created_at, updated_at)
SELECT id, class_id, member_id, present, checkin_at, checkout_at, attendance_date, created_at, updated_at FROM tmp_devart_activities_member_attendances;

DROP TABLE tmp_devart_activities_member_attendances;

--
-- Create foreign key
--
ALTER TABLE activities_member_attendances
    ADD CONSTRAINT activities_member_attendances_member_id_foreign FOREIGN KEY (member_id)
        REFERENCES activities_members(id) ON DELETE CASCADE;

--
-- Create foreign key
--
ALTER TABLE activities_member_attendances
    ADD CONSTRAINT activities_member_attendances_class_id_foreign FOREIGN KEY (class_id)
        REFERENCES activities_classes(id) ON DELETE SET NULL;

--
-- Enable foreign keys
--
