Module: PgTypes::SchemaStatements
- Defined in:
- lib/pg_types/schema_statements.rb
Instance Method Summary collapse
- #create_type(name, version: nil, sql_definition: nil) ⇒ Object
- #drop_type(name, force: false) ⇒ Object
Instance Method Details
#create_type(name, version: nil, sql_definition: nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pg_types/schema_statements.rb', line 5 def create_type(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 type already exists to avoid duplicate creation attempts return if type_exists?(name) if sql_definition execute sql_definition else # Try both Rails.root and current directory for type definition type_definition = PgTypes::TypeDefinition.new(name, version: version) paths = [ type_definition.path, File.join(Dir.pwd, "db", "types", "#{name}_v#{version}.sql") ] sql_file = paths.find { |path| File.exist?(path) } raise ArgumentError, "Could not find type definition file in paths: #{paths.join(", ")}" unless sql_file execute File.read(sql_file) end rescue ActiveRecord::StatementInvalid => e puts "WARNING: Failed to create type #{name}." puts " Error: #{e.}" raise end |
#drop_type(name, force: false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pg_types/schema_statements.rb', line 33 def drop_type(name, force: false) force_clause = force ? " CASCADE" : "" # Drop the type and any dependent objects when force: true execute "DROP TYPE IF EXISTS #{name}#{force_clause}" # Ensure any dependent objects are really gone when using CASCADE return unless force execute <<-SQL DO $$ BEGIN EXECUTE format('DROP TABLE IF EXISTS %I CASCADE', 'test_contacts'); END; $$; SQL end |