Sha256: 4c83fccceb8d0ef88dee5cf6f788db44141643f6cf1a60af5ae27878055be60e

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require "fx-aggregate/adapters/postgres/aggregates"

module FxAggregate
  module Adapters
    module Postgres
      # Returns an array of aggregates in the database.
      #
      # This collection of aggregates is used by the [Fx::SchemaDumper] to
      # populate the `schema.rb` file.
      #
      # @return [Array<Fx::Aggregate>]
      def aggregates
        ::Fx::Adapters::Postgres::Aggregates.all(connection)
      end

      # Creates an aggregate in the database.
      #
      # This is typically called in a migration via
      # {Fx::Statements::Aggregate#create_aggregate}.
      #
      # @param sql_definition The SQL schema for the aggregate.
      #
      # @return [void]
      def create_aggregate(sql_definition)
        execute(sql_definition)
      end

      # # Updates an aggregate in the database.
      #
      # This is typically called in a migration via
      # {Fx::Statements::Aggregate#update_aggregate}.
      #
      # @param name The name of the aggregate.
      # @param sql_definition The SQL schema for the aggregate.
      #
      # @return [void]
      def update_aggregate(name, sql_definition)
        drop_aggregate(name)
        create_aggregate(sql_definition)
      end

      # Drops the aggregate from the database
      #
      # This is typically called in a migration via
      # {Fx::Statements::Aggregate#drop_aggregate}.
      #
      # @param name The name of the aggregate to drop
      #
      # @return [void]
      def drop_aggregate(name)
        defs = aggregates.select { |aggregate| aggregate.name == name.to_s }

        defs.each do |aggregate|
          execute "DROP AGGREGATE #{name}(#{aggregate.arguments});"
        end
        # execute "DROP AGGREGATE #{name}();"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fx-aggregate-0.1.0 lib/fx-aggregate/adapters/postgres.rb