2

postgis在 Heroku 上使用我的数据库的扩展。在生产中,这工作得很好;我像老板一样映射。

但是,我无法让我的应用程序在 HerokuCI 上构建,因为测试环境中的数据库插件没有安装 GDAL(postgis 扩展的一部分):

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal", "GDAL", "gdal2.3.0", "gdal2.2.0", "gdal2.1.0", "gdal2.0.0", "gdal1.11.0"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
-----> test command `python manage.py migrate --noinput && python manage.py test` failed with exit status 1

我不知道如何在 heroku CI 环境中创建扩展。Heroku 的应用程序 json 模式没有说明如何在测试数据库上创建扩展。

我知道我不能将 in-dyno 数据库用于 CI,因为postgis 不是可用的扩展,但我没有使用 in-dyno 数据库,我正在使用计划......

这是我的应用架构:

{
  "name": "mymegaapp",
  "scripts": {
    "heroku-prebuild": "create extension postgis",
    "heroku-postbuild": "echo heroku-postbuild script runs here."
  },
  "env": {
  },
  "formation": {
  },
  "addons": [
  ],
  "buildpacks": [
    {
      "url": "heroku/python"
    },
    {
      "url": "heroku/nodejs"
    }
  ],
  "environments": {
    "test": {
      "scripts": {
        "test": "python manage.py migrate --noinput && python manage.py test"
      },
      "env": {
        "DJANGO_DEBUG": "1"
      },
      "addons":[
        {
          "plan": "heroku-postgresql",
          "options": {
            "version": "11"
          }
        },
        "heroku-redis:hobby-dev"
      ]
    }
  }
}

请注意,我已尝试在预构建脚本中创建 postgis 扩展,但这并没有什么不同。

我还尝试在测试脚本之前调用创建:

      "scripts": {
        "test": "heroku create extension postgis && python manage.py migrate --noinput && python manage.py test"
      },

这不起作用,因为在测功机中没有安装heroku cli。

关键问题:

  1. 如何在 HerokuCI 中创建扩展?
  2. 在 HerokuCI 中,postgis 是一个允许的扩展吗?
4

2 回答 2

3

你几乎明白了。直接从 CLI 创建 pg 扩展没有一流的支持。但是,您可以将语句传递到psql.

在您的部署后或测试设置脚本中尝试此操作: psql -c "create extension postgis" $DATABASE_URL如您所知,您需要使用真实数据库而不是动态数据库来执行此操作。如果您已将数据库附加为 以外的其他DATABASE_URL内容,则需要在脚本中更新它。

[由 OP 编辑​​以添加有用信息] 在这种情况下,还需要 GDAL 库,可以通过将BUILD_WITH_GEO_LIBRARIES环境变量设置为1. app.json 中的最终工作解决方案将是:

  "environments": {
    "test": {
      "scripts": {
        "postdeploy": "psql -c \"create extension postgis\" $DATABASE_URL",
        "test": "python manage.py migrate --noinput && python manage.py test"
      },
      "env": {
        "DJANGO_DEBUG": "1",
        "BUILD_WITH_GEO_LIBRARIES": "1"
      },
      "addons":[
        {
          "plan": "heroku-postgresql",
          "options": {
            "version": "11"
          }
        }
      ]
    }
  }
于 2019-08-14T13:54:16.113 回答
0

这里是 2021 年的更新,因为设置 BUILD_WITH_GEO_LIBRARIES 现在已被 Heroku 弃用且不支持:

此 app.json 适用于我使用 Django / Heroku 进行的 CI 设置:

{
  "buildpacks": [
    {
      "url": "https://github.com/heroku/heroku-geo-buildpack.git"
    },
    {
      "url": "heroku/python"
    }
  ],
  "environments": {
    "test": {
      "scripts": {
        "test": "python3 manage.py migrate --noinput && python3 manage.py test --keepdb"
      },
      "env": {
        "DJANGO_DEBUG": "1"
      }
    }
  }
}
于 2021-02-10T22:32:53.700 回答