简体   繁体   中英

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.

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

I experienced a similar issue where the database.yml specified multiple schemas for like schema_search_path: public, third_party . I found the answer why the CREATE EXTENSION statements didn't end up in structure.sql .

The reason is explained in https://github.com/rails/rails/issues/17157 but here is it for reference

If the user has specified a schema_search_path and also has extensions, the resulting pg_dump call generated by rake db:structure:dump includes the --schema flag which means that no CREATE EXTENSION statements will be created in the resulting structure.sql. According to the pg_dump documentation:

After digging thru the code for the schema dumper, I found an option that seems to help.

ActiveRecord::Base.dump_schemas = :all

I added this to a config/initializers/schema_dumper.rb

您还可以将所有扩展名移到另一个模式,例如shared_extension并通过set config.active_record.dump_schemas = "public"控制要转储哪些数据库模式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM