Module: PgAggregates::SchemaStatements
- Defined in:
- lib/pg_aggregates/schema_statements.rb
Constant Summary collapse
- RESERVED_WORDS =
Reserved words to skip when checking function dependencies
["public"].freeze
Instance Method Summary collapse
- #create_aggregate(name, version: nil, sql_definition: nil) ⇒ Object
- #drop_aggregate(name, *arg_types, force: false) ⇒ Object
Instance Method Details
#create_aggregate(name, version: nil, sql_definition: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pg_aggregates/schema_statements.rb', line 8 def create_aggregate(name, version: nil, sql_definition: nil) raise ArgumentError, "Must provide either sql_definition or version" if sql_definition.nil? && version.nil? # First, check if the function already exists to avoid duplicate creation attempts return if aggregate_exists?(name) # Check if dependent functions exist before attempting to create check_dependent_functions(sql_definition || read_aggregate_definition(name, version)) if sql_definition execute sql_definition else # Fallback to file-based definition if needed aggregate_definition = PgAggregates::AggregateDefinition.new(name, version: version) # Check if file exists before trying to read it unless File.exist?(aggregate_definition.path) raise ArgumentError, "Could not find aggregate definition file: #{aggregate_definition.path}" end execute aggregate_definition.to_sql end rescue ActiveRecord::StatementInvalid => e raise unless /function .* does not exist/.match?(e.) puts "WARNING: Failed to create aggregate #{name} because a required function does not exist." puts " This could indicate a dependency ordering issue." puts " Error: #{e.message}" raise end |
#drop_aggregate(name, *arg_types, force: false) ⇒ Object
40 41 42 43 44 |
# File 'lib/pg_aggregates/schema_statements.rb', line 40 def drop_aggregate(name, *arg_types, force: false) arg_types_sql = arg_types.any? ? "(#{arg_types.join(", ")})" : "" force_clause = force ? " CASCADE" : "" execute "DROP AGGREGATE IF EXISTS #{name}#{arg_types_sql}#{force_clause}" end |