Sha256: ea34b9460db10cb596eff3d25f761be31063b16018c59e45d2d7acc97edb6814

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module AR
  module Sequence
    module Adapter
      SEQUENCE_COMMENT = "created by ar-sequence"

      def custom_sequence?(sequence_name)
        execute(
          "SELECT obj_description('#{sequence_name}'::regclass, 'pg_class');"
        ).first["obj_description"] == SEQUENCE_COMMENT
      end

      def check_sequences
        select_all(
          "SELECT * FROM information_schema.sequences ORDER BY sequence_name"
        ).to_a
      end

      def create_sequence(name, options = {})
        increment = options[:increment] || options[:step]
        name = quote_column_name(name)

        sql = ["CREATE SEQUENCE IF NOT EXISTS #{name}"]
        sql << "INCREMENT BY #{increment}" if increment
        sql << "START WITH #{options[:start]}" if options[:start]
        sql << ";"
        sql << "COMMENT ON SEQUENCE #{name} IS '#{SEQUENCE_COMMENT}';"

        execute(sql.join("\n"))
      end

      # Drop a sequence by its name.
      #
      #   drop_sequence :user_position
      #
      def drop_sequence(name)
        name = quote_column_name(name)
        sql = "DROP SEQUENCE #{name}"
        execute(sql)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar-sequence-0.2.0 lib/ar/sequence/adapter.rb