Sha256: 9f6d6501df874dcfccb8e0104ee68372102ba6c2b3bef4141009188dcb949264

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literals: true

require "jobshop/helpers/migration.rb"

class CreateCustomers < ActiveRecord::Migration[5.2]
  include Jobshop::Helpers::Migration

  def change
    create_table_with_auditing :jobshop_customer_categories, primary_key: %i[
      organization_id category_id
    ] do |t|
      t.citext :name, null: false
      t.text :description
    end

    add_index :jobshop_customer_categories, %i[ organization_id name ], unique: true

    fk_organization :jobshop_customer_categories

    create_table_with_auditing :jobshop_customers, primary_key: %i[
      organization_id customer_id
    ] do |t|
      t.citext :name, null: false
      t.uuid :category_id, null: false
      t.string :email_from_domain
      t.timestamps
    end

    add_index :jobshop_customers, %i[ organization_id name ], unique: true

    foreign_key :jobshop_customers, :jobshop_customer_categories,
      %i[ organization_id category_id ]

    # Reverse index on email_from_domain allows for search on just a domain
    # given the entire address.
    #
    # example.com will be indexed as moc.elpmaxe
    add_index :jobshop_customers,
      "organization_id, reverse(lower(email_from_domain)) text_pattern_ops",
      name: :idx_jobshop_customers_reverse_address, unique: true

    fk_organization :jobshop_customers

    create_table_with_auditing :jobshop_customer_contacts, primary_key: %i[
      organization_id customer_id contact_id
    ] do |t|
      t.citext :email, null: false
      t.string :surname
      t.string :forename
    end

    fk_organization :jobshop_customer_contacts

    foreign_key :jobshop_customer_contacts, :jobshop_customers,
      %i[ organization_id customer_id ]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jobshop-0.0.167 db/migrate/20171216021853_create_customers.rb