defmodule Fleetms.Repo.TenantMigrations.InitialMigration do
  @moduledoc """
  Updates resources based on their most recent snapshots.

  This file was autogenerated with `mix ash_postgres.generate_migrations`
  """

  use Ecto.Migration

  def up do
    create table(:inspection_form_vehicles, primary_key: false, prefix: prefix()) do
      add :inspection_form_id, :uuid, null: false, primary_key: true
      add :vehicle_id, :uuid, null: false, primary_key: true
    end

    create table(:fuel_histories, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :odometer_reading, :decimal, null: false
      add :refuel_datetime, :utc_datetime, null: false
      add :refuel_quantity, :decimal, null: false
      add :refuel_cost, :money_with_currency, null: false
      add :refuel_location, :text
      add :fuel_type, :text, null: false
      add :payment_method, :text, null: false
      add :notes, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id, :uuid, null: false
      add :refueled_by_id, :uuid
    end

    create table(:tokens, primary_key: false, prefix: prefix()) do
      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :extra_data, :map
      add :purpose, :text, null: false
      add :expires_at, :utc_datetime, null: false
      add :subject, :text, null: false
      add :jti, :text, null: false, primary_key: true
    end

    create table(:inspection_forms, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:inspection_form_vehicles, prefix: prefix()) do
      modify :inspection_form_id,
             references(:inspection_forms,
               column: :id,
               name: "inspection_form_vehicles_inspection_form_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:inspection_forms, prefix: prefix()) do
      add :slug, :text
      add :title, :text, null: false
      add :labels, {:array, :text}

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:inspection_forms, [:title], name: "inspection_forms_unique_title_index")

    create table(:vehicle_drivetrain_specs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :manufacturer, :text
      add :model, :text
      add :serial_number, :text
      add :transmission_type, :text
      add :number_of_gears, :bigint
      add :transmission_oil_capacity, :decimal
      add :transmission_oil_specification, :text
      add :drive_type, :text
      add :axles, :bigint
      add :axle_configuration, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id, :uuid, null: false
    end

    create table(:work_order_service_tasks, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :work_order_id, :uuid, null: false
      add :service_task_id, :uuid, null: false
    end

    create table(:vehicle_makes, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:vehicle_makes, [:name], name: "vehicle_makes_unique_name_index")

    create table(:text_input_values, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :slug, :text, null: false
      add :type, :text, null: false, default: "text_input_type"

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :inspection_submission_id, :uuid, null: false
    end

    create table(:radio_input_values, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :slug, :text, null: false
      add :type, :text, null: false, default: "radio_input_type"
      add :status, :text, null: false
      add :comments, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :inspection_submission_id, :uuid, null: false
    end

    create table(:service_reminders, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :mileage_interval, :decimal
      add :hours_interval, :decimal
      add :time_interval, :bigint
      add :time_interval_unit, :text
      add :mileage_threshold, :decimal
      add :hours_threshold, :decimal
      add :time_threshold, :bigint
      add :time_threshold_unit, :text
      add :last_completed_mileage, :decimal
      add :last_completed_hours, :decimal
      add :last_completed_date, :date
      add :next_due_mileage, :decimal
      add :next_due_hours, :decimal
      add :next_due_date, :date
      add :due_status, :text, null: false, default: "Upcoming"

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :service_group_schedule_id, :uuid
      add :service_task_id, :uuid, null: false
      add :vehicle_id, :uuid, null: false
    end

    create table(:part_manufacturers, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :description, :text, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:part_manufacturers, [:name],
             name: "part_manufacturers_unique_name_index"
           )

    create table(:radio_inputs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :slug, :text, null: false
      add :type, :text, null: false, default: "radio_input"
      add :name, :text, null: false
      add :help_text, :text
      add :pass_label, :text, default: "Pass"
      add :fail_label, :text, default: "Fail"
      add :allow_na, :boolean, null: false, default: true
      add :comment_required_on_pass, :boolean, null: false, default: false
      add :comment_required_on_fail, :boolean, null: false, default: false

      add :inspection_form_id,
          references(:inspection_forms,
            column: :id,
            name: "radio_inputs_inspection_form_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:sequences, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :resource_name, :text, null: false
      add :current_count, :bigint, null: false
    end

    create unique_index(:sequences, [:resource_name],
             name: "sequences_unique_resource_name_index"
           )

    create table(:service_group_schedule_service_tasks, primary_key: false, prefix: prefix()) do
      add :service_group_schedule_id, :uuid, null: false, primary_key: true
      add :service_task_id, :uuid, null: false, primary_key: true
    end

    create table(:number_inputs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :slug, :text, null: false
      add :type, :text, null: false, default: "number_input"
      add :name, :text, null: false
      add :pass_range_min, :bigint, null: false, default: 0
      add :pass_range_max, :bigint, null: false, default: 999_999_999
      add :help_text, :text
      add :comment_required_on_pass, :boolean, null: false, default: false
      add :comment_required_on_fail, :boolean, null: false, default: false

      add :inspection_form_id,
          references(:inspection_forms,
            column: :id,
            name: "number_inputs_inspection_form_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:service_group_vehicles, primary_key: false, prefix: prefix()) do
      add :service_group_id, :uuid, null: false, primary_key: true
      add :vehicle_id, :uuid, null: false, primary_key: true
    end

    create table(:signature_input_values, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :role, :text
      add :encoded_image, :text, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :inspection_submission_id, :uuid, null: false
    end

    create table(:vendors_parts, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :unit_cost, :decimal

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :part_id, :uuid, null: false, primary_key: true
      add :vendor_id, :uuid, null: false, primary_key: true
    end

    create table(:part_photos, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :filename, :text, null: false
      add :caption, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :part_id, :uuid, null: false
    end

    create table(:service_group_schedules, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:service_reminders, prefix: prefix()) do
      modify :service_group_schedule_id,
             references(:service_group_schedules,
               column: :id,
               name: "service_reminders_service_group_schedule_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:service_group_schedule_service_tasks, prefix: prefix()) do
      modify :service_group_schedule_id,
             references(:service_group_schedules,
               column: :id,
               name: "service_group_schedule_service_tasks_service_group_schedule_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:service_group_schedules, prefix: prefix()) do
      add :mileage_interval, :bigint
      add :hours_interval, :bigint
      add :time_interval, :bigint
      add :time_interval_unit, :text
      add :mileage_threshold, :bigint
      add :hours_threshold, :bigint
      add :time_threshold, :bigint
      add :time_threshold_unit, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :service_group_id, :uuid, null: false
    end

    create table(:vehicle_photos, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :filename, :text, null: false
      add :caption, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id, :uuid, null: false
    end

    create table(:part_inventory_location, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :quantity, :decimal, default: "0"

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :part_id, :uuid, null: false, primary_key: true
      add :inventory_location_id, :uuid, null: false, primary_key: true
    end

    create table(:vehicle_reminder_purposes, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :description, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:vehicle_reminder_purposes, [:name],
             name: "vehicle_reminder_purposes_unique_name_index"
           )

    create table(:work_order_photos, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :filename, :text, null: false
      add :caption, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :work_order_id, :uuid, null: false
    end

    create table(:vehicle_engine_specs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :manufacturer, :text
      add :model, :text
      add :serial_number, :text
      add :aspiration, :text
      add :displacement, :decimal
      add :cylinders, :bigint
      add :cylinder_bore, :decimal
      add :valves_per_cylinder, :bigint
      add :compression_ratio, :text
      add :piston_stroke, :decimal
      add :power, :decimal
      add :torque, :decimal
      add :fuel_type, :text
      add :fuel_delivery_type, :text
      add :config_type, :text
      add :valve_train_design, :text
      add :cooling_type, :text
      add :oil_capacity, :decimal
      add :oil_specification, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id, :uuid, null: false
    end

    create table(:vehicle_groups, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :description, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:vehicle_groups, [:name], name: "vehicle_groups_unique_name_index")

    create table(:work_orders, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:work_order_service_tasks, prefix: prefix()) do
      modify :work_order_id,
             references(:work_orders,
               column: :id,
               name: "work_order_service_tasks_work_order_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:work_order_photos, prefix: prefix()) do
      modify :work_order_id,
             references(:work_orders,
               column: :id,
               name: "work_order_photos_work_order_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:work_orders, prefix: prefix()) do
      add :work_order_number, :bigint, null: false
      add :status, :text, null: false
      add :description, :text
      add :comments, :text
      add :start_mileage, :decimal
      add :start_hours, :decimal
      add :repair_category, :text
      add :date_and_time_issued, :utc_datetime, null: false
      add :date_and_time_started, :utc_datetime
      add :date_and_time_completed, :utc_datetime
      add :documents, {:array, :map}
      add :labels, {:array, :text}

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id, :uuid, null: false
      add :assigned_to_id, :uuid
      add :issued_by_id, :uuid, null: false
    end

    create table(:vehicle_performance_specs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :fuel_consumption_urban, :decimal
      add :fuel_consumption_extra_urban, :decimal
      add :fuel_consumption_combined, :decimal
      add :co2_emission, :decimal
      add :acceleration_0_to_100, :decimal
      add :maximum_speed, :decimal
      add :emission_standard, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id, :uuid, null: false
    end

    create table(:work_order_service_task_vendor_labor_details,
             primary_key: false,
             prefix: prefix()
           ) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :hours, :decimal, default: "0"
      add :rate, :money_with_currency
      add :subtotal, :money_with_currency
      add :comments, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vendor_id, :uuid, null: false
      add :work_order_service_task_id, :uuid, null: false
    end

    create table(:inventory_locations, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :description, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:inventory_locations, [:name],
             name: "inventory_locations_unique_name_index"
           )

    create table(:vehicle_documents, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :filename, :text, null: false
      add :storage_filename, :text, null: false
      add :file_type, :text
      add :caption, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id, :uuid, null: false
    end

    create table(:vehicles, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:inspection_form_vehicles, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "inspection_form_vehicles_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:fuel_histories, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "fuel_histories_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )

      modify :refueled_by_id,
             references(:users,
               column: :id,
               name: "fuel_histories_refueled_by_id_fkey",
               type: :uuid,
               prefix: "public",
               on_delete: :nilify_all
             )
    end

    alter table(:vehicle_drivetrain_specs, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "vehicle_drivetrain_specs_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    create unique_index(:vehicle_drivetrain_specs, [:vehicle_id],
             name: "vehicle_drivetrain_specs_unique_vehicle_id_index"
           )

    alter table(:vehicle_photos, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "vehicle_photos_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:vehicle_engine_specs, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "vehicle_engine_specs_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    create unique_index(:vehicle_engine_specs, [:vehicle_id],
             name: "vehicle_engine_specs_unique_vehicle_id_index"
           )

    alter table(:work_orders, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "work_orders_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix()
             )

      modify :assigned_to_id,
             references(:users,
               column: :id,
               name: "work_orders_assigned_to_id_fkey",
               type: :uuid,
               prefix: "public"
             )

      modify :issued_by_id,
             references(:users,
               column: :id,
               name: "work_orders_issued_by_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    alter table(:vehicle_performance_specs, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "vehicle_performance_specs_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    create unique_index(:vehicle_performance_specs, [:vehicle_id],
             name: "vehicle_performance_specs_unique_vehicle_id_index"
           )

    alter table(:vehicle_documents, prefix: prefix()) do
      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "vehicle_documents_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:vehicles, prefix: prefix()) do
      add :name, :text, null: false
      add :model_code, :text
      add :category, :text
      add :type, :text, null: false
      add :body_style, :text
      add :chassis_number, :text
      add :license_plate, :text
      add :vin, :text
      add :year, :bigint, null: false
      add :base_price, :decimal
      add :purchase_date, :date
      add :purchase_price, :decimal
      add :dealer_name, :text
      add :color, :text
      add :hours, :decimal
      add :image_url, :text
      add :mileage, :decimal, default: "0"
      add :status, :text, null: false
      add :photo, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_model_id, :uuid
      add :vehicle_group_id, :uuid
    end

    create table(:inspection_submissions, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:text_input_values, prefix: prefix()) do
      modify :inspection_submission_id,
             references(:inspection_submissions,
               column: :id,
               name: "text_input_values_inspection_submission_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:radio_input_values, prefix: prefix()) do
      modify :inspection_submission_id,
             references(:inspection_submissions,
               column: :id,
               name: "radio_input_values_inspection_submission_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:signature_input_values, prefix: prefix()) do
      modify :inspection_submission_id,
             references(:inspection_submissions,
               column: :id,
               name: "signature_input_values_inspection_submission_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:inspection_submissions, prefix: prefix()) do
      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id,
          references(:vehicles,
            column: :id,
            name: "inspection_submissions_vehicle_id_fkey",
            type: :uuid,
            prefix: prefix()
          ),
          null: false

      add :user_id,
          references(:users,
            column: :id,
            name: "inspection_submissions_user_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false

      add :inspection_form_id,
          references(:inspection_forms,
            column: :id,
            name: "inspection_submissions_inspection_form_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :nilify_all
          ),
          null: false
    end

    create table(:dropdown_inputs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :slug, :text, null: false
      add :type, :text, null: false, default: "dropdown_input"
      add :name, :text, null: false
      add :help_text, :text
      add :options, {:array, :map}
      add :comment_required_on_pass, :boolean, null: false, default: false
      add :comment_required_on_fail, :boolean, null: false, default: false

      add :inspection_form_id,
          references(:inspection_forms,
            column: :id,
            name: "dropdown_inputs_inspection_form_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:fuel_history_photos, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :filename, :text, null: false
      add :caption, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :fuel_history_id,
          references(:fuel_histories,
            column: :id,
            name: "fuel_history_photos_fuel_history_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:text_inputs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :slug, :text, null: false
      add :type, :text, null: false, default: "text_input"
      add :name, :text, null: false
      add :help_text, :text

      add :inspection_form_id,
          references(:inspection_forms,
            column: :id,
            name: "text_inputs_inspection_form_id_fkey",
            type: :uuid,
            prefix: prefix()
          ),
          null: false
    end

    create table(:part_categories, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:part_categories, [:name], name: "part_categories_unique_name_index")

    create table(:signature_input_types, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :slug, :text, null: false
      add :type, :text, null: false, default: "signature_input_type"
      add :name, :text, null: false
      add :help_text, :text
      add :required, :boolean, null: false, default: true

      add :inspection_form_id,
          references(:inspection_forms,
            column: :id,
            name: "signature_input_types_inspection_form_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          )
    end

    create table(:vehicle_general_reminders, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :description, :text
      add :time_interval, :decimal
      add :time_interval_unit, :text
      add :due_date_threshold, :bigint
      add :due_date_threshold_unit, :text
      add :last_completed_date, :date
      add :due_status, :text, null: false
      add :due_date, :date, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id,
          references(:vehicles,
            column: :id,
            name: "vehicle_general_reminders_vehicle_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false

      add :vehicle_reminder_purpose_id,
          references(:vehicle_reminder_purposes,
            column: :id,
            name: "vehicle_general_reminders_vehicle_reminder_purpose_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create unique_index(:vehicle_general_reminders, [:vehicle_id, :vehicle_reminder_purpose_id],
             name: "vehicle_general_reminders_unique_vehicle_general_reminder_index"
           )

    create table(:vendors, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:work_order_service_task_vendor_labor_details, prefix: prefix()) do
      modify :vendor_id,
             references(:vendors,
               column: :id,
               name: "work_order_service_task_vendor_labor_details_vendor_id_fkey",
               type: :uuid,
               prefix: prefix()
             )

      modify :work_order_service_task_id,
             references(:work_order_service_tasks,
               column: :id,
               name:
                 "work_order_service_task_vendor_labor_details_work_order_service_task_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:vendors, prefix: prefix()) do
      add :name, :text, null: false
      add :description, :text
      add :phone, :text
      add :email, :text
      add :address, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:vendors, [:name], name: "vendors_unique_vendor_index")

    create table(:parts, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:vendors_parts, prefix: prefix()) do
      modify :part_id,
             references(:parts,
               column: :id,
               name: "vendors_parts_part_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )

      modify :vendor_id,
             references(:vendors,
               column: :id,
               name: "vendors_parts_vendor_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:part_photos, prefix: prefix()) do
      modify :part_id,
             references(:parts,
               column: :id,
               name: "part_photos_part_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:part_inventory_location, prefix: prefix()) do
      modify :part_id,
             references(:parts,
               column: :id,
               name: "part_inventory_location_part_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )

      modify :inventory_location_id,
             references(:inventory_locations,
               column: :id,
               name: "part_inventory_location_inventory_location_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    create unique_index(:part_inventory_location, [:inventory_location_id, :part_id],
             name: "part_inventory_location_unique_part_inventory_location_index"
           )

    alter table(:parts, prefix: prefix()) do
      add :name, :text
      add :description, :text
      add :part_number, :text, null: false
      add :oem_part_number, :text
      add :unit_cost, :money_with_currency
      add :unit_measurement, :text
      add :quantity_threshold, :decimal, default: "0"
      add :track_inventory, :boolean, null: false, default: false
      add :stock_quantity, :decimal, default: "0"
      add :stock_quantity_status, :text, default: "Not tracked"
      add :documents, {:array, :map}
      add :photo, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :part_manufacturer_id,
          references(:part_manufacturers,
            column: :id,
            name: "parts_part_manufacturer_id_fkey",
            type: :uuid,
            prefix: prefix()
          )

      add :part_category_id,
          references(:part_categories,
            column: :id,
            name: "parts_part_category_id_fkey",
            type: :uuid,
            prefix: prefix()
          )
    end

    create unique_index(:parts, [:name, :part_number],
             name: "parts_unique_name_and_part_number_index"
           )

    create table(:dropdown_input_values, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :slug, :text, null: false
      add :type, :text, null: false, default: "dropdown_input_type"
      add :status, :text, null: false
      add :selected_option, :text, null: false
      add :comments, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :inspection_submission_id,
          references(:inspection_submissions,
            column: :id,
            name: "dropdown_input_values_inspection_submission_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:number_input_values, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false
      add :pass_range_min, :bigint, null: false, default: 0
      add :pass_range_max, :bigint, null: false, default: 999_999_999
      add :slug, :text, null: false
      add :type, :text, null: false, default: "number_input_type"
      add :status, :text, null: false
      add :value, :bigint, null: false
      add :comments, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :inspection_submission_id,
          references(:inspection_submissions,
            column: :id,
            name: "number_input_values_inspection_submission_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:vehicle_assignments, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :start_datetime, :utc_datetime, null: false
      add :end_datetime, :utc_datetime
      add :start_mileage, :decimal
      add :end_mileage, :decimal
      add :comments, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id,
          references(:vehicles,
            column: :id,
            name: "vehicle_assignments_vehicle_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false

      add :assignee_id,
          references(:users,
            column: :id,
            name: "vehicle_assignments_assignee_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false
    end

    create table(:vehicle_models, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:vehicles, prefix: prefix()) do
      modify :vehicle_model_id,
             references(:vehicle_models,
               column: :id,
               name: "vehicles_vehicle_model_id_fkey",
               type: :uuid,
               prefix: prefix()
             )

      modify :vehicle_group_id,
             references(:vehicle_groups,
               column: :id,
               name: "vehicles_vehicle_group_id_fkey",
               type: :uuid,
               prefix: prefix()
             )
    end

    alter table(:vehicle_models, prefix: prefix()) do
      add :name, :text, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_make_id,
          references(:vehicle_makes,
            column: :id,
            name: "vehicle_models_vehicle_make_id_fkey",
            type: :uuid,
            prefix: prefix()
          ),
          null: false
    end

    create unique_index(:vehicle_models, [:name], name: "vehicle_models_unique_name_index")

    create table(:work_order_service_task_parts, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :unit_cost, :money_with_currency
      add :quantity, :bigint
      add :subtotal, :money_with_currency
      add :comments, :text
      add :update_inventory, :boolean, null: false, default: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :work_order_service_task_id,
          references(:work_order_service_tasks,
            column: :id,
            name: "work_order_service_task_parts_work_order_service_task_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false

      add :part_id,
          references(:parts,
            column: :id,
            name: "work_order_service_task_parts_part_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false

      add :inventory_location_id,
          references(:inventory_locations,
            column: :id,
            name: "work_order_service_task_parts_inventory_location_id_fkey",
            type: :uuid,
            prefix: prefix()
          )
    end

    create table(:service_tasks, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:work_order_service_tasks, prefix: prefix()) do
      modify :service_task_id,
             references(:service_tasks,
               column: :id,
               name: "work_order_service_tasks_service_task_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:service_reminders, prefix: prefix()) do
      modify :service_task_id,
             references(:service_tasks,
               column: :id,
               name: "service_reminders_service_task_id_fkey",
               type: :uuid,
               prefix: prefix()
             )

      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "service_reminders_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    create unique_index(:service_reminders, [:vehicle_id, :service_task_id],
             name: "service_reminders_unique_vehicle_service_reminder_index"
           )

    alter table(:service_group_schedule_service_tasks, prefix: prefix()) do
      modify :service_task_id,
             references(:service_tasks,
               column: :id,
               name: "service_group_schedule_service_tasks_service_task_id_fkey",
               type: :uuid,
               prefix: prefix()
             )
    end

    alter table(:service_tasks, prefix: prefix()) do
      add :name, :text, null: false
      add :description, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:service_tasks, [:name], name: "service_tasks_unique_name_index")

    create table(:part_unit_measurements, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :name, :text, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:part_unit_measurements, [:name],
             name: "part_unit_measurements_unique_name_index"
           )

    create table(:issues, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :issue_number, :bigint, null: false, default: 0
      add :date_reported, :utc_datetime, null: false
      add :title, :text, null: false
      add :description, :text
      add :labels, {:array, :text}, default: []
      add :mileage, :decimal
      add :hours, :decimal
      add :due_date, :date
      add :date_resolved, :utc_datetime
      add :priority, :text, default: "Low"
      add :status, :text, null: false
      add :resolve_comments, :text
      add :close_comments, :text
      add :date_closed, :utc_datetime
      add :documents, {:array, :map}

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id,
          references(:vehicles,
            column: :id,
            name: "issues_vehicle_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false

      add :assigned_to_id,
          references(:users,
            column: :id,
            name: "issues_assigned_to_id_fkey",
            type: :uuid,
            prefix: "public"
          )

      add :reported_by_id,
          references(:users,
            column: :id,
            name: "issues_reported_by_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false

      add :work_order_id,
          references(:work_orders,
            column: :id,
            name: "issues_work_order_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :nilify_all
          )
    end

    create unique_index(:issues, [:issue_number], name: "issues_unique_issue_number_index")

    create table(:service_reminder_histories, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :last_completed_mileage, :decimal
      add :last_completed_hours, :decimal
      add :last_completed_date, :date
      add :next_due_mileage, :decimal
      add :next_due_hours, :decimal
      add :next_due_date, :date
      add :vehicle_mileage, :decimal, null: false

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :work_order_id,
          references(:work_orders,
            column: :id,
            name: "service_reminder_histories_work_order_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false

      add :service_reminder_id,
          references(:service_reminders,
            column: :id,
            name: "service_reminder_histories_service_reminder_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :nilify_all
          )
    end

    create unique_index(:service_reminder_histories, [:service_reminder_id, :work_order_id],
             name: "service_reminder_histories_unique_history_index"
           )

    create table(:work_order_service_task_technician_labor_details,
             primary_key: false,
             prefix: prefix()
           ) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :hours, :decimal, default: "0"
      add :rate, :money_with_currency
      add :subtotal, :money_with_currency
      add :comments, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :technician_id,
          references(:users,
            column: :id,
            name: "work_order_service_task_technician_labor_details_technician_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false

      add :work_order_service_task_id,
          references(:work_order_service_tasks,
            column: :id,
            name:
              "work_order_service_task_technician_labor_details_work_order_service_task_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:issue_photos, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :filename, :text, null: false
      add :caption, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :issue_id,
          references(:issues,
            column: :id,
            name: "issue_photos_issue_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create table(:service_groups, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:service_group_vehicles, prefix: prefix()) do
      modify :service_group_id,
             references(:service_groups,
               column: :id,
               name: "service_group_vehicles_service_group_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )

      modify :vehicle_id,
             references(:vehicles,
               column: :id,
               name: "service_group_vehicles_vehicle_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:service_group_schedules, prefix: prefix()) do
      modify :service_group_id,
             references(:service_groups,
               column: :id,
               name: "service_group_schedules_service_group_id_fkey",
               type: :uuid,
               prefix: prefix(),
               on_delete: :delete_all
             )
    end

    alter table(:service_groups, prefix: prefix()) do
      add :name, :text, null: false
      add :thumbnail_url, :text
      add :logo, :text
      add :description, :text

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create unique_index(:service_groups, [:name], name: "service_groups_unique_name_index")

    create table(:vehicle_other_specs, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :width, :decimal
      add :length, :decimal
      add :height, :decimal
      add :wheelbase, :decimal
      add :brake_system_type, :text
      add :front_brakes, :text
      add :rear_brakes, :text
      add :assisting_system, :text
      add :front_tire_spec, :text
      add :rear_tire_spec, :text
      add :steering_side, :text
      add :steering_type, :text
      add :gross_weight, :decimal
      add :fuel_tank_capacity, :decimal
      add :towing_capacity, :decimal
      add :net_weight, :decimal
      add :max_payload, :decimal

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :vehicle_id,
          references(:vehicles,
            column: :id,
            name: "vehicle_other_specs_vehicle_id_fkey",
            type: :uuid,
            prefix: prefix(),
            on_delete: :delete_all
          ),
          null: false
    end

    create unique_index(:vehicle_other_specs, [:vehicle_id],
             name: "vehicle_other_specs_unique_vehicle_id_index"
           )
  end

  def down do
    drop_if_exists unique_index(:vehicle_other_specs, [:vehicle_id],
                     name: "vehicle_other_specs_unique_vehicle_id_index"
                   )

    drop constraint(:vehicle_other_specs, "vehicle_other_specs_vehicle_id_fkey")

    drop table(:vehicle_other_specs, prefix: prefix())

    drop_if_exists unique_index(:service_groups, [:name],
                     name: "service_groups_unique_name_index"
                   )

    alter table(:service_groups, prefix: prefix()) do
      remove :updated_at
      remove :created_at
      remove :description
      remove :logo
      remove :thumbnail_url
      remove :name
    end

    drop constraint(:service_group_schedules, "service_group_schedules_service_group_id_fkey")

    alter table(:service_group_schedules, prefix: prefix()) do
      modify :service_group_id, :uuid
    end

    drop constraint(:service_group_vehicles, "service_group_vehicles_service_group_id_fkey")

    drop constraint(:service_group_vehicles, "service_group_vehicles_vehicle_id_fkey")

    alter table(:service_group_vehicles, prefix: prefix()) do
      modify :vehicle_id, :uuid
      modify :service_group_id, :uuid
    end

    drop table(:service_groups, prefix: prefix())

    drop constraint(:issue_photos, "issue_photos_issue_id_fkey")

    drop table(:issue_photos, prefix: prefix())

    drop constraint(
           :work_order_service_task_technician_labor_details,
           "work_order_service_task_technician_labor_details_technician_id_fkey"
         )

    drop constraint(
           :work_order_service_task_technician_labor_details,
           "work_order_service_task_technician_labor_details_work_order_service_task_id_fkey"
         )

    drop table(:work_order_service_task_technician_labor_details, prefix: prefix())

    drop_if_exists unique_index(
                     :service_reminder_histories,
                     [:service_reminder_id, :work_order_id],
                     name: "service_reminder_histories_unique_history_index"
                   )

    drop constraint(:service_reminder_histories, "service_reminder_histories_work_order_id_fkey")

    drop constraint(
           :service_reminder_histories,
           "service_reminder_histories_service_reminder_id_fkey"
         )

    drop table(:service_reminder_histories, prefix: prefix())

    drop_if_exists unique_index(:issues, [:issue_number],
                     name: "issues_unique_issue_number_index"
                   )

    drop constraint(:issues, "issues_vehicle_id_fkey")

    drop constraint(:issues, "issues_assigned_to_id_fkey")

    drop constraint(:issues, "issues_reported_by_id_fkey")

    drop constraint(:issues, "issues_work_order_id_fkey")

    drop table(:issues, prefix: prefix())

    drop_if_exists unique_index(:part_unit_measurements, [:name],
                     name: "part_unit_measurements_unique_name_index"
                   )

    drop table(:part_unit_measurements, prefix: prefix())

    drop_if_exists unique_index(:service_tasks, [:name], name: "service_tasks_unique_name_index")

    alter table(:service_tasks, prefix: prefix()) do
      remove :updated_at
      remove :created_at
      remove :description
      remove :name
    end

    drop constraint(
           :service_group_schedule_service_tasks,
           "service_group_schedule_service_tasks_service_task_id_fkey"
         )

    alter table(:service_group_schedule_service_tasks, prefix: prefix()) do
      modify :service_task_id, :uuid
    end

    drop_if_exists unique_index(:service_reminders, [:vehicle_id, :service_task_id],
                     name: "service_reminders_unique_vehicle_service_reminder_index"
                   )

    drop constraint(:service_reminders, "service_reminders_service_task_id_fkey")

    drop constraint(:service_reminders, "service_reminders_vehicle_id_fkey")

    alter table(:service_reminders, prefix: prefix()) do
      modify :vehicle_id, :uuid
      modify :service_task_id, :uuid
    end

    drop constraint(:work_order_service_tasks, "work_order_service_tasks_service_task_id_fkey")

    alter table(:work_order_service_tasks, prefix: prefix()) do
      modify :service_task_id, :uuid
    end

    drop table(:service_tasks, prefix: prefix())

    drop constraint(
           :work_order_service_task_parts,
           "work_order_service_task_parts_work_order_service_task_id_fkey"
         )

    drop constraint(:work_order_service_task_parts, "work_order_service_task_parts_part_id_fkey")

    drop constraint(
           :work_order_service_task_parts,
           "work_order_service_task_parts_inventory_location_id_fkey"
         )

    drop table(:work_order_service_task_parts, prefix: prefix())

    drop_if_exists unique_index(:vehicle_models, [:name],
                     name: "vehicle_models_unique_name_index"
                   )

    drop constraint(:vehicle_models, "vehicle_models_vehicle_make_id_fkey")

    alter table(:vehicle_models, prefix: prefix()) do
      remove :vehicle_make_id
      remove :updated_at
      remove :created_at
      remove :name
    end

    drop constraint(:vehicles, "vehicles_vehicle_model_id_fkey")

    drop constraint(:vehicles, "vehicles_vehicle_group_id_fkey")

    alter table(:vehicles, prefix: prefix()) do
      modify :vehicle_group_id, :uuid
      modify :vehicle_model_id, :uuid
    end

    drop table(:vehicle_models, prefix: prefix())

    drop constraint(:vehicle_assignments, "vehicle_assignments_vehicle_id_fkey")

    drop constraint(:vehicle_assignments, "vehicle_assignments_assignee_id_fkey")

    drop table(:vehicle_assignments, prefix: prefix())

    drop constraint(:number_input_values, "number_input_values_inspection_submission_id_fkey")

    drop table(:number_input_values, prefix: prefix())

    drop constraint(:dropdown_input_values, "dropdown_input_values_inspection_submission_id_fkey")

    drop table(:dropdown_input_values, prefix: prefix())

    drop_if_exists unique_index(:parts, [:name, :part_number],
                     name: "parts_unique_name_and_part_number_index"
                   )

    drop constraint(:parts, "parts_part_manufacturer_id_fkey")

    drop constraint(:parts, "parts_part_category_id_fkey")

    alter table(:parts, prefix: prefix()) do
      remove :part_category_id
      remove :part_manufacturer_id
      remove :updated_at
      remove :created_at
      remove :photo
      remove :documents
      remove :stock_quantity_status
      remove :stock_quantity
      remove :track_inventory
      remove :quantity_threshold
      remove :unit_measurement
      remove :unit_cost
      remove :oem_part_number
      remove :part_number
      remove :description
      remove :name
    end

    drop_if_exists unique_index(:part_inventory_location, [:inventory_location_id, :part_id],
                     name: "part_inventory_location_unique_part_inventory_location_index"
                   )

    drop constraint(:part_inventory_location, "part_inventory_location_part_id_fkey")

    drop constraint(
           :part_inventory_location,
           "part_inventory_location_inventory_location_id_fkey"
         )

    alter table(:part_inventory_location, prefix: prefix()) do
      modify :inventory_location_id, :uuid
      modify :part_id, :uuid
    end

    drop constraint(:part_photos, "part_photos_part_id_fkey")

    alter table(:part_photos, prefix: prefix()) do
      modify :part_id, :uuid
    end

    drop constraint(:vendors_parts, "vendors_parts_part_id_fkey")

    drop constraint(:vendors_parts, "vendors_parts_vendor_id_fkey")

    alter table(:vendors_parts, prefix: prefix()) do
      modify :vendor_id, :uuid
      modify :part_id, :uuid
    end

    drop table(:parts, prefix: prefix())

    drop_if_exists unique_index(:vendors, [:name], name: "vendors_unique_vendor_index")

    alter table(:vendors, prefix: prefix()) do
      remove :updated_at
      remove :created_at
      remove :address
      remove :email
      remove :phone
      remove :description
      remove :name
    end

    drop constraint(
           :work_order_service_task_vendor_labor_details,
           "work_order_service_task_vendor_labor_details_vendor_id_fkey"
         )

    drop constraint(
           :work_order_service_task_vendor_labor_details,
           "work_order_service_task_vendor_labor_details_work_order_service_task_id_fkey"
         )

    alter table(:work_order_service_task_vendor_labor_details, prefix: prefix()) do
      modify :work_order_service_task_id, :uuid
      modify :vendor_id, :uuid
    end

    drop table(:vendors, prefix: prefix())

    drop_if_exists unique_index(
                     :vehicle_general_reminders,
                     [:vehicle_id, :vehicle_reminder_purpose_id],
                     name: "vehicle_general_reminders_unique_vehicle_general_reminder_index"
                   )

    drop constraint(:vehicle_general_reminders, "vehicle_general_reminders_vehicle_id_fkey")

    drop constraint(
           :vehicle_general_reminders,
           "vehicle_general_reminders_vehicle_reminder_purpose_id_fkey"
         )

    drop table(:vehicle_general_reminders, prefix: prefix())

    drop constraint(:signature_input_types, "signature_input_types_inspection_form_id_fkey")

    drop table(:signature_input_types, prefix: prefix())

    drop_if_exists unique_index(:part_categories, [:name],
                     name: "part_categories_unique_name_index"
                   )

    drop table(:part_categories, prefix: prefix())

    drop constraint(:text_inputs, "text_inputs_inspection_form_id_fkey")

    drop table(:text_inputs, prefix: prefix())

    drop constraint(:fuel_history_photos, "fuel_history_photos_fuel_history_id_fkey")

    drop table(:fuel_history_photos, prefix: prefix())

    drop constraint(:dropdown_inputs, "dropdown_inputs_inspection_form_id_fkey")

    drop table(:dropdown_inputs, prefix: prefix())

    drop constraint(:inspection_submissions, "inspection_submissions_vehicle_id_fkey")

    drop constraint(:inspection_submissions, "inspection_submissions_user_id_fkey")

    drop constraint(:inspection_submissions, "inspection_submissions_inspection_form_id_fkey")

    alter table(:inspection_submissions, prefix: prefix()) do
      remove :inspection_form_id
      remove :user_id
      remove :vehicle_id
      remove :updated_at
      remove :created_at
    end

    drop constraint(
           :signature_input_values,
           "signature_input_values_inspection_submission_id_fkey"
         )

    alter table(:signature_input_values, prefix: prefix()) do
      modify :inspection_submission_id, :uuid
    end

    drop constraint(:radio_input_values, "radio_input_values_inspection_submission_id_fkey")

    alter table(:radio_input_values, prefix: prefix()) do
      modify :inspection_submission_id, :uuid
    end

    drop constraint(:text_input_values, "text_input_values_inspection_submission_id_fkey")

    alter table(:text_input_values, prefix: prefix()) do
      modify :inspection_submission_id, :uuid
    end

    drop table(:inspection_submissions, prefix: prefix())

    alter table(:vehicles, prefix: prefix()) do
      remove :vehicle_group_id
      remove :vehicle_model_id
      remove :updated_at
      remove :created_at
      remove :photo
      remove :status
      remove :mileage
      remove :image_url
      remove :hours
      remove :color
      remove :dealer_name
      remove :purchase_price
      remove :purchase_date
      remove :base_price
      remove :year
      remove :vin
      remove :license_plate
      remove :chassis_number
      remove :body_style
      remove :type
      remove :category
      remove :model_code
      remove :name
    end

    drop constraint(:vehicle_documents, "vehicle_documents_vehicle_id_fkey")

    alter table(:vehicle_documents, prefix: prefix()) do
      modify :vehicle_id, :uuid
    end

    drop_if_exists unique_index(:vehicle_performance_specs, [:vehicle_id],
                     name: "vehicle_performance_specs_unique_vehicle_id_index"
                   )

    drop constraint(:vehicle_performance_specs, "vehicle_performance_specs_vehicle_id_fkey")

    alter table(:vehicle_performance_specs, prefix: prefix()) do
      modify :vehicle_id, :uuid
    end

    drop constraint(:work_orders, "work_orders_vehicle_id_fkey")

    drop constraint(:work_orders, "work_orders_assigned_to_id_fkey")

    drop constraint(:work_orders, "work_orders_issued_by_id_fkey")

    alter table(:work_orders, prefix: prefix()) do
      modify :issued_by_id, :uuid
      modify :assigned_to_id, :uuid
      modify :vehicle_id, :uuid
    end

    drop_if_exists unique_index(:vehicle_engine_specs, [:vehicle_id],
                     name: "vehicle_engine_specs_unique_vehicle_id_index"
                   )

    drop constraint(:vehicle_engine_specs, "vehicle_engine_specs_vehicle_id_fkey")

    alter table(:vehicle_engine_specs, prefix: prefix()) do
      modify :vehicle_id, :uuid
    end

    drop constraint(:vehicle_photos, "vehicle_photos_vehicle_id_fkey")

    alter table(:vehicle_photos, prefix: prefix()) do
      modify :vehicle_id, :uuid
    end

    drop_if_exists unique_index(:vehicle_drivetrain_specs, [:vehicle_id],
                     name: "vehicle_drivetrain_specs_unique_vehicle_id_index"
                   )

    drop constraint(:vehicle_drivetrain_specs, "vehicle_drivetrain_specs_vehicle_id_fkey")

    alter table(:vehicle_drivetrain_specs, prefix: prefix()) do
      modify :vehicle_id, :uuid
    end

    drop constraint(:fuel_histories, "fuel_histories_vehicle_id_fkey")

    drop constraint(:fuel_histories, "fuel_histories_refueled_by_id_fkey")

    alter table(:fuel_histories, prefix: prefix()) do
      modify :refueled_by_id, :uuid
      modify :vehicle_id, :uuid
    end

    drop constraint(:inspection_form_vehicles, "inspection_form_vehicles_vehicle_id_fkey")

    alter table(:inspection_form_vehicles, prefix: prefix()) do
      modify :vehicle_id, :uuid
    end

    drop table(:vehicles, prefix: prefix())

    drop table(:vehicle_documents, prefix: prefix())

    drop_if_exists unique_index(:inventory_locations, [:name],
                     name: "inventory_locations_unique_name_index"
                   )

    drop table(:inventory_locations, prefix: prefix())

    drop table(:work_order_service_task_vendor_labor_details, prefix: prefix())

    drop table(:vehicle_performance_specs, prefix: prefix())

    alter table(:work_orders, prefix: prefix()) do
      remove :issued_by_id
      remove :assigned_to_id
      remove :vehicle_id
      remove :updated_at
      remove :created_at
      remove :labels
      remove :documents
      remove :date_and_time_completed
      remove :date_and_time_started
      remove :date_and_time_issued
      remove :repair_category
      remove :start_hours
      remove :start_mileage
      remove :comments
      remove :description
      remove :status
      remove :work_order_number
    end

    drop constraint(:work_order_photos, "work_order_photos_work_order_id_fkey")

    alter table(:work_order_photos, prefix: prefix()) do
      modify :work_order_id, :uuid
    end

    drop constraint(:work_order_service_tasks, "work_order_service_tasks_work_order_id_fkey")

    alter table(:work_order_service_tasks, prefix: prefix()) do
      modify :work_order_id, :uuid
    end

    drop table(:work_orders, prefix: prefix())

    drop_if_exists unique_index(:vehicle_groups, [:name],
                     name: "vehicle_groups_unique_name_index"
                   )

    drop table(:vehicle_groups, prefix: prefix())

    drop table(:vehicle_engine_specs, prefix: prefix())

    drop table(:work_order_photos, prefix: prefix())

    drop_if_exists unique_index(:vehicle_reminder_purposes, [:name],
                     name: "vehicle_reminder_purposes_unique_name_index"
                   )

    drop table(:vehicle_reminder_purposes, prefix: prefix())

    drop table(:part_inventory_location, prefix: prefix())

    drop table(:vehicle_photos, prefix: prefix())

    alter table(:service_group_schedules, prefix: prefix()) do
      remove :service_group_id
      remove :updated_at
      remove :created_at
      remove :time_threshold_unit
      remove :time_threshold
      remove :hours_threshold
      remove :mileage_threshold
      remove :time_interval_unit
      remove :time_interval
      remove :hours_interval
      remove :mileage_interval
    end

    drop constraint(
           :service_group_schedule_service_tasks,
           "service_group_schedule_service_tasks_service_group_schedule_id_fkey"
         )

    alter table(:service_group_schedule_service_tasks, prefix: prefix()) do
      modify :service_group_schedule_id, :uuid
    end

    drop constraint(:service_reminders, "service_reminders_service_group_schedule_id_fkey")

    alter table(:service_reminders, prefix: prefix()) do
      modify :service_group_schedule_id, :uuid
    end

    drop table(:service_group_schedules, prefix: prefix())

    drop table(:part_photos, prefix: prefix())

    drop table(:vendors_parts, prefix: prefix())

    drop table(:signature_input_values, prefix: prefix())

    drop table(:service_group_vehicles, prefix: prefix())

    drop constraint(:number_inputs, "number_inputs_inspection_form_id_fkey")

    drop table(:number_inputs, prefix: prefix())

    drop table(:service_group_schedule_service_tasks, prefix: prefix())

    drop_if_exists unique_index(:sequences, [:resource_name],
                     name: "sequences_unique_resource_name_index"
                   )

    drop table(:sequences, prefix: prefix())

    drop constraint(:radio_inputs, "radio_inputs_inspection_form_id_fkey")

    drop table(:radio_inputs, prefix: prefix())

    drop_if_exists unique_index(:part_manufacturers, [:name],
                     name: "part_manufacturers_unique_name_index"
                   )

    drop table(:part_manufacturers, prefix: prefix())

    drop table(:service_reminders, prefix: prefix())

    drop table(:radio_input_values, prefix: prefix())

    drop table(:text_input_values, prefix: prefix())

    drop_if_exists unique_index(:vehicle_makes, [:name], name: "vehicle_makes_unique_name_index")

    drop table(:vehicle_makes, prefix: prefix())

    drop table(:work_order_service_tasks, prefix: prefix())

    drop table(:vehicle_drivetrain_specs, prefix: prefix())

    drop_if_exists unique_index(:inspection_forms, [:title],
                     name: "inspection_forms_unique_title_index"
                   )

    alter table(:inspection_forms, prefix: prefix()) do
      remove :updated_at
      remove :created_at
      remove :labels
      remove :title
      remove :slug
    end

    drop constraint(:inspection_form_vehicles, "inspection_form_vehicles_inspection_form_id_fkey")

    alter table(:inspection_form_vehicles, prefix: prefix()) do
      modify :inspection_form_id, :uuid
    end

    drop table(:inspection_forms, prefix: prefix())

    drop table(:tokens, prefix: prefix())

    drop table(:fuel_histories, prefix: prefix())

    drop table(:inspection_form_vehicles, prefix: prefix())
  end
end
