ecto icon indicating copy to clipboard operation
ecto copied to clipboard

INSERT INTO "schema_migrations" line in structure.sql file targets incorrect schema

Open nigelramsay opened this issue 8 years ago • 13 comments

Environment

  • Elixir version (elixir -v): Elixir 1.6.0 (compiled with OTP 20)
  • Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.): PostgreSQL 10.1
  • Ecto version (mix deps): 2.1.6
  • Database adapter and version (mix deps): postgrex 0.13.4
  • Operating system: macOS 10.13.2

Current behavior

We make extensive use of Postgresql schemas for grouping data sets from different sources. For example, all tables that we use from Australia Post are stored in a schema named "auspost".

When we run mix echo.dump, the lines preceding the INSERT INTO statement are targeting another schema (in this case, the "auspost" schema).

Here is a snippet of the relevant part of our structure.sql file:

--
-- PostgreSQL database dump
--

-- Dumped from database version 10.1
-- Dumped by pg_dump version 10.1

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

--
-- Name: auspost; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA auspost;

--
-- lots of create/alter table statements here
--

SET search_path = auspost, pg_catalog;

--
-- lots of create/alter table statements relating to Australia Post here
--

--
-- PostgreSQL database dump complete
--

INSERT INTO "elixir_migrations" (version) VALUES (20171113202613), (20171117020709), (20171123042030), (20171123202636), (20171128201143), (20171128204531), (20171204014157), (20171206014224), (20171208003942), (20180108031618), (20180108220956), (20180109033939), (20180109201207), (20180109210334), (20180109213531), (20180109224202), (20180109231419), (20180109623137), (20180109623140), (20180110014855), (20180112025830), (20180112030000), (20180112030100), (20180115220036), (20180116215512), (20180117000507), (20180201214329), (20180202012149), (20180204224648);

So, when we run mix ecto.load, we get the following error message in the PostgreSQL logs:

2018-02-08 22:22:27.204 UTC [168] ERROR:  relation "elixir_migrations" does not exist at character 13
postgresql_1     | 2018-02-08 22:22:27.204 UTC [168] STATEMENT:  INSERT INTO "elixir_migrations" (version) VALUES (20171113202613), (20171117020709), (20171123042030), (20171123202636), (20171128201143), (20171128204531), (20171204014157), (20171206014224), (20171208003942), (20180108031618), (20180108220956), (20180109033939), (20180109201207), (20180109210334), (20180109213531), (20180109224202), (20180109231419), (20180109623137), (20180109623140), (20180110014855), (20180112025830), (20180112030000), (20180112030100), (20180115220036), (20180116215512), (20180117000507), (20180201214329), (20180202012149), (20180204224648);

This is because psql is using the most recent search_path of auspost to locate the elixir_migrations table.

We have confirmed this by manually creating an identical elixir_migrations table in the auspost schema, and this indeed has the migration versions inserted into it.

Our temporary workaround

After each mix ecto.dump, we then manually edit the structure.sql file, adding the following to just before the insert statement:

SET search_path = public, pg_catalog;

This now ensures that the correct public schema is targeted.

Expected behavior

The generated structure.sql file has a statement setting the search_path just before declaring the INSERT INTO "elixir_migrations statement.

nigelramsay avatar Feb 08 '18 23:02 nigelramsay

I would be happy to submit a PR that updates Ecto.Adapters.Postgres.append_versions/3 to prefix the INSERT statement with SET search_path = public, pg_catalog;

Also would be happy to implement a different solution if required.

nigelramsay avatar Feb 08 '18 23:02 nigelramsay

I can see two solutions:

  • always prepending the SET search_path part,
  • always writing to public.<table> instead of just just <table>

I'm not really sure which one would be better.

michalmuskala avatar Feb 08 '18 23:02 michalmuskala

Both options do seem viable. I had tried adding the following to config.exs:

config :address_builder, AddressBuilder.Repo, migration_source: "public.elixir_migrations"

But this resulted in an INSERT statement such as:

INSERT INTO "public.elixir_migrations" (version) VALUES (20171113202613), (20171117020709)... 

PostgreSQL didn't like this as it thought "public." to be part of the table name.

We'd need to change the generation code to wrap the double-quotes around just the table name.

Configuration

Another question to ask is if the schema name itself should be configurable. For example, are there scenarios where you'd want to store the elixir_migrations table in a non-public schema?

I could see some configuration such as:

config :address_builder, AddressBuilder.Repo, migration_source: "elixir_migrations", migration_schema: "other"

Personally, I can't think of any common scenarios where this might be required.

My thoughts

Not entirely sure. Both options seem viable. The schema-prefixing (option 2) does have a slight advantage in that it does not introduce the side effect of altering the search_path for subsequent Sql statements. But that said, there are no subsequent SQL statements.

nigelramsay avatar Feb 08 '18 23:02 nigelramsay

I would be happy to submit a PR that updates Ecto.Adapters.Postgres.append_versions/3

Please send a PR to append_versions/3 but let's change this line and this line to always include the public prefix.

josevalim avatar Feb 09 '18 08:02 josevalim

Could be an option to scan the output of pg_dump to extract SET search_path directives, aggregate all the schemas and prepend SET search_path = <schemas>; before the INSERT INTO schema_migrations?

fertapric avatar Feb 09 '18 19:02 fertapric

@fertapric I would go with the simplest solution until somebody has a different case.

josevalim avatar Feb 09 '18 19:02 josevalim

I'll go ahead and make the changes that @josevalim indicated, and send through the PR early next week.

nigelramsay avatar Feb 09 '18 19:02 nigelramsay

I'm adding this comment here, I'm more than happy to raise a more detailed issue and/or PR, I just figured it would make sense to check with people who had worked on this recently on a similar issue before doing so; just let me know what's the best approach :smiley:

Problem

We've hit a similar problem recently, thanks to a change in the way that Postgres handles a default search_path in both 10.3 and 9.6.8

We're having issues because our pg_dump output changes all the schema definitions to be prefixed with public. Consequently, our structure.sql has changed:

-CREATE TABLE events (
+CREATE TABLE public.events (

The problem is that because the append_versions/3 manually constructs an INSERT INTO statement, the migrations aren't actually inserted into a table which is in our search_path and we are essentially unable to run migrations.

The change in #2415 fixes that, but is not necessarily backwards with older versions of pg_dump which don't prefix the search_path.

Potential solution

A better solution may be to change the append_versions/3 function to actually use pg_dump to get the contents of the table, rather than constructing an INSERT INTO string. That way the schema versions will always be consistent with whatever version of pg_dump is being used. It should also be more future-proof.

I've tested this locally and it appears to work fine, would you be open to me opening a PR up about this?

seddy avatar Mar 08 '18 19:03 seddy

@seddy Thank you for the report!

Unfortunately I don't quite understand why #2415 is not enough. Would you mind expanding on that a bit?

josevalim avatar Mar 08 '18 19:03 josevalim

Thanks for the quick response @josevalim! Apologies that my description isn't too clear, I'll try and clarify it. I suppose ultimately what I'm suggesting is an enhancement to #2415.

Currently, Ecto.Adapters.Postgres.structure_dump does two things:

  1. Use pg_dump to unload the current schema to a file
  2. Constructs an INSERT INTO statement to insert all the versions which is then appended to the structure file

The problem I'm experiencing is that with 10.3 and 9.6.8, postgres have removed public from the default search_path (I think!). Consequently, when trying to load my current structure.sql, the task blows up because it can't find my schema_migrations table. This is very similar to the issue raised here.

#2415 does fix that by enforcing we always look in the public namespace, but it just seems odd to me that we're manually constructing the statement to insert versions, rather than pg_dump.

If we had been using pg_dump, then we wouldn't have hit this problem on upgrading to 10.3, because pg_dump would have correctly prepended the public namespace to schema_migrations. It also would have avoided the issue that was originally raised here, so I think this might be a more future-proof solution.

Does that make more sense? I'm happy to open up a PR to show the change as I think it should be fairly straightforward to do.

seddy avatar Mar 08 '18 20:03 seddy

@seddy it makes sense now, thank you.

However, it seems there isn't any gain in moving to pg_dump, since master works correctly. Sure, if we used pg_dump since the beginning it would always work, but that's also true if we had included public. since the beginning. :)

josevalim avatar Mar 08 '18 20:03 josevalim

Heh, true @josevalim ;)!

I'd still be keen to change this to use pg_dump instead of having to manually build up an INSERT INTO statement. It seems like the right tool for the job, and the issues raised here are just examples of where it can go wrong; I think it's entirely possible that similar problems could happen again in the future.

If you don't think it's worth it then that's fine, I won't worry about it :).

However, although #2415 is on master, there hasn't been a patch release yet for it; so consequently ecto is currently incompatible with postgres 10.3 or 9.6.8 (to be fair, they were only released a week ago!), any idea when we'll get a new release please?

seddy avatar Mar 09 '18 10:03 seddy

@seddy I will release a new version of the stable release with this backported. :)

josevalim avatar Mar 09 '18 14:03 josevalim