skull-squadron / pg_sequencer

Create and dump postgres sequences in your Rails app!
http://www.code42.com/
MIT License
0 stars 0 forks source link

Rollback doesn't work #3

Closed skull-squadron closed 1 year ago

skull-squadron commented 1 year ago

Missing CommandRecorder

skull-squadron commented 1 year ago

Okay, now it works.

For change_sequence, use a reversible migration block to ignore it on down

class CreateDeleteMes < ActiveRecord::Migration[7.0]
  disable_ddl_transaction!

  def _seq
    :delete_mes_creation_ordering_serial_seq
  end
  def _table
    :delete_mes
  end
  def _serial
    :creation_ordering_serial
  end

  def change
    reversible do |dir|
      dir.up { create_sequence _seq, start: 100, increment: 5, min: 10, max: 2000 }
    end
    create_table _table, id: :uuid do |t|
      t.integer _serial, \
        null: false, \
        default: -> { "nextval('#{_seq}'::regclass)" }
      t.string :name
      t.string :email
      t.date :birthday

      t.timestamps
      t.index :creation_ordering_serial, unique: true, algorithm: :concurrently
    end
    reversible do |dir|
      dir.up { change_sequence _seq, owned_by: "#{_table}.#{_serial}" }
    end
  end
end
skull-squadron commented 1 year ago

Use-case like:

config/initializers/uuid.rb

# frozen_string_literal: true

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

require_relative '../../app/models/application_record'
ApplicationRecord.implicit_order_column = 'creation_ordering_serial'