Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql extensions removed on db/structure.sql when migrations runs

I have a rails app that uses postgresql + some extensions, but every time I run rails db:migrate it removes the lines that enable the extensions. I have to copy and paste it manually every time.

Lines being removed on db/structure.sql:

-- Name: EXTENSION "postgis"; Type: COMMENT; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS "postgis" WITH SCHEMA public;


--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;


--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';


--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;


--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';


--
-- Name: uuid-ossp; Type: EXTENSION; Schema: -; Owner: -
--

CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;


--
-- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';

My database.yml:

default: &default
  adapter: postgis
  encoding: unicode
  host: <%= ENV.fetch('DB_HOST', 'localhost') %>
  username: <%= ENV.fetch('DB_USERNAME') %>
  password: <%= ENV.fetch('DB_PASSWORD') %>
  schema_search_path: public

test:
  <<: *default
  database: db_test

development:
  <<: *default
  database: db_development

production:
  <<: *default
  database: db_production

Any ideas how can I fix it?

I'm using the following versions:

postgresql: 9.6

postgis: 2.3

rails: 5.0

macOS: 10.12

UPDATE:

I managed to find a workaround to the problem. As I'm using schema_search_path as public, if not defined the default option is public. Just removed this line from database.yml and it works now. Still no clues why it's happening when defining the schema_search_path explicitly.

like image 609
bitmaybewise Avatar asked Oct 16 '25 07:10

bitmaybewise


1 Answers

You could generate a migration and use enable_extension method:

class AddExtensions < ActiveRecord::Migration[5.1]
   def change
     enable_extension "postgis"
     enable_extension "plpgsql"
     enable_extension "pg_trgm"
     enable_extension "uuid-ossp"
     # ...
   end
end
like image 143
s3tjan Avatar answered Oct 17 '25 20:10

s3tjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!