ankane / ahoy_email

First-party email analytics for Rails
MIT License
1.11k stars 137 forks source link

Fix a personal annoyance #159

Open rromanchuk opened 1 year ago

rromanchuk commented 1 year ago

I have to use two hands for the amount of times i've had to tear everything down once realizing bigints fks started exploding.

# application.rb
config.generators do |g|
   g.orm :active_record, primary_key_type: :uuid
 end

class BigintToUuids2 < ActiveRecord::Migration[7.0]
  def change
    drop_table :ahoy_clicks
    drop_table :mailkick_subscriptions
    drop_table :ahoy_messages
  end
end

# bin/rails generate ahoy:messages --encryption=none --force
# bin/rails generate mailkick:install --force
# bin/rails generate ahoy:clicks --force
class CreateAhoyMessages < ActiveRecord::Migration[7.0]
  def change
    primary_key_type, foreign_key_type = primary_and_foreign_key_types
    create_table :ahoy_messages, id: primary_key_type do |t|
      t.references :user, polymorphic: true, type: foreign_key_type
      t.string :to, index: true
      t.string :mailer
      t.text :subject
      t.datetime :sent_at
    end
    add_column :ahoy_messages, :pomotion_code, :string
  end
  private
    def primary_and_foreign_key_types
      config = Rails.configuration.generators
      setting = config.options[config.orm][:primary_key_type]
      primary_key_type = setting || :primary_key
      foreign_key_type = setting || :bigint
      [primary_key_type, foreign_key_type]
    end
end

class CreateMailkickSubscriptions < ActiveRecord::Migration[7.0]
  def change
    primary_key_type, foreign_key_type = primary_and_foreign_key_types
    create_table :mailkick_subscriptions, id: primary_key_type do |t|
      t.references :subscriber, polymorphic: true, index: false, type: foreign_key_type
      t.string :list
      t.timestamps
    end

    add_index :mailkick_subscriptions, [:subscriber_type, :subscriber_id, :list], unique: true, name: "index_mailkick_subscriptions_on_subscriber_and_list"
  end
  private
    def primary_and_foreign_key_types
      config = Rails.configuration.generators
      setting = config.options[config.orm][:primary_key_type]
      primary_key_type = setting || :primary_key
      foreign_key_type = setting || :bigint
      [primary_key_type, foreign_key_type]
    end
end

class CreateAhoyClicks < ActiveRecord::Migration[7.0]
  def change
    primary_key_type, foreign_key_type = primary_and_foreign_key_types
    create_table :ahoy_clicks, id: primary_key_type do |t|
      t.string :campaign, index: true
      t.string :token
    end

    add_column :ahoy_messages, :campaign, :string
    add_index :ahoy_messages, :campaign
  end
  private
    def primary_and_foreign_key_types
      config = Rails.configuration.generators
      setting = config.options[config.orm][:primary_key_type]
      primary_key_type = setting || :primary_key
      foreign_key_type = setting || :bigint
      [primary_key_type, foreign_key_type]
    end
end
 bin/rails db:migrate                       
== 20230519180150 CreateAhoyMessages: migrating ===============================
-- create_table(:ahoy_messages, {:id=>:uuid})
   -> 0.0107s
-- add_column(:ahoy_messages, :pomotion_code, :string)
   -> 0.0006s
== 20230519180150 CreateAhoyMessages: migrated (0.0113s) ======================

== 20230519180357 CreateMailkickSubscriptions: migrating ======================
-- create_table(:mailkick_subscriptions, {:id=>:uuid})
   -> 0.0026s
-- add_index(:mailkick_subscriptions, [:subscriber_type, :subscriber_id, :list], {:unique=>true, :name=>"index_mailkick_subscriptions_on_subscriber_and_list"})
   -> 0.0029s
== 20230519180357 CreateMailkickSubscriptions: migrated (0.0056s) =============

== 20230519181143 CreateAhoyClicks: migrating =================================
-- create_table(:ahoy_clicks, {:id=>:uuid})
   -> 0.0047s
-- add_column(:ahoy_messages, :campaign, :string)
   -> 0.0005s
-- add_index(:ahoy_messages, :campaign)
   -> 0.0008s
== 20230519181143 CreateAhoyClicks: migrated (0.0060s) ========================
--
-- Name: ahoy_messages; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.ahoy_messages (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_type character varying,
    user_id uuid,
    "to" character varying,
    mailer character varying,
    subject text,
    sent_at timestamp(6) without time zone,
    pomotion_code character varying
);

--
-- Name: mailkick_subscriptions; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.mailkick_subscriptions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    subscriber_type character varying,
    subscriber_id uuid,
    list character varying,
    created_at timestamp(6) without time zone NOT NULL,
    updated_at timestamp(6) without time zone NOT NULL
);

--
-- Name: ahoy_clicks; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.ahoy_clicks (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    campaign character varying,
    token character varying
);