我正在使用db/structure.sql保留数据库状态,因为我们具有PostGIS扩展名和内置功能,这些功能使使用schema.rb不切实际。

当运行db:structure:dump rails时,在文件的顶部和底部附近设置搜索路径的行为很奇怪。这里的问题是,顶部的搜索路径不正确,导致db:schema:load严重失败。

目前,我正在手动对其进行编辑(即,在顶部搜索路径中添加postgis),但是如果我能以某种方式通过转储任务正确设置搜索路径,那就太好了。

database.yml

development: &dev
  adapter: postgis
  database: myapp_dev
  host: localhost
  encoding: utf8
  template: template0 # Required for UTF8 encoding
  postgis_extension: true
  schema_search_path: "public,postgis"

db/structure.sql
--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

SET search_path = public, pg_catalog;

... Table / Sequence / Index creation happens here ...

--
-- PostgreSQL database dump complete
--

SET search_path TO public,postgis;

... Migrations inserted here ...

这里的问题是,这些表需要在搜索路径中创建postgis(毕竟它们确实使用postgis数据类型)

我认为第二个搜索路径集是作为database.yml中设置的搜索路径的结果而添加的。

是否有可能使rails将正确的搜索路径放在文件顶部?

最佳答案

我已经完成了测试项目,并重复了上述问题的一系列操作-一切正常!我注意到规律性-structure.sql包含代码:

SET search_path = public, pg_catalog;

CREATE TABLE spatial_tests (
    id integer NOT NULL,
    latlon postgis.geography(Point,4326),
    geo_col postgis.geometry(Geometry,4326)
);

请注意列类型的postgis前缀。仅当postgis扩展驻留在postgis模式中时,才会发生这样的代码:
postgis_test=# \dx
                                     List of installed extensions
  Name   | Version |   Schema   |                             Description
---------+---------+------------+---------------------------------------------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
 postgis | 2.1.7   | postgis    | PostGIS geometry, geography, and raster spatial types and functions
(2 rows)

而且这段代码执行得很好。在另一种情况下,当postgis扩展名存在于公共(public)方案中时,structure.sql如下所示:
SET search_path = public, pg_catalog;

CREATE TABLE spatial_tests (
    id integer NOT NULL,
    latlon geography(Point,4326),
    geo_col geometry(Geometry,4326)
);

列名中没有前缀,此代码在执行时会产生错误。

克里斯·诺尔德斯(Chris Noldus),请检查在进行转储的数据库中哪个方案包含postgis扩展名(可以通过psql控制台中的\dx命令进行设置)-可能是造成问题的原因。

rake db:create创建数据库后,在database.yml中没有postgis_schema: postgis的情况下,可能会发生这种情况。您可以在activerecord-postgis-adapter documentation中阅读有关此选项的详细信息。

关于ruby-on-rails - Rails db :structure:dump has incorrect search paths,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34731931/

10-15 19:43