Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make postgres initialDatabases schema creation more robust #663

Merged
merged 4 commits into from
Jun 18, 2023

Conversation

stig
Copy link
Contributor

@stig stig commented Jun 14, 2023

I have a project with ~150 migration files going back to 2016, added by many different people using different conventions. The commits in this PR are required in order for me to successfully apply all those sql files.

The repo is private (sorry!), but I've tried to give a representative example of each fix in its commit message.

@stig stig marked this pull request as draft June 15, 2023 08:09
@stig stig force-pushed the postgres-schemas branch 4 times, most recently from 08538cd to eea5a4d Compare June 15, 2023 11:19
@stig stig changed the title Remove quotes around '*.sql' preventing glob expansion Misc fixes to postgres initialDatabases schema creation Jun 15, 2023
@stig stig changed the title Misc fixes to postgres initialDatabases schema creation Make postgres initialDatabases schema creation more robust Jun 15, 2023
stig added 4 commits June 15, 2023 12:27
The quotes around the "*.sql" glob prevented cat from expanding it,
leading to this error:

    14:55:40 postgres.1 | cat: '/nix/store/k30kwm1p6zn23ml95q1ldrykbi1v99qr-sql/*.sql': No such file or directory

Instead read migration files in version order (with ls -v), and use a
shell loop to concatenate them.
Say we have two schema files:

v1_create_foo.sql (note missing ; at the end)

    CREATE TABLE foo (id varchar)

v2_create_bar.sql:

    CREATE TABLE bar (id varchar);

If we simply cat together all the schema files we get an error because
of the missing ;, but by applying the migrations one at a time we
handle this situation.
Without -j statements are terminated at ";" followed by newline. This
frequently fails for files creating plpgsql functions like this
example, because it tries to execute it as 4 separate statements:

    CREATE FUNCTION truncate_all() RETURNS void AS $$
    BEGIN
    TRUNCATE foo;
    TRUNCATE bar;
    END;
    $$ LANGUAGE plpgsql;
Even with -j statements are terminated after ";" followed by two
newlines, which fails on files like this because it tries to execute
it as 4 separate statements, which results in an error:

    CREATE FUNCTION truncate_all() RETURNS void AS $$
    BEGIN

    TRUNCATE foo;

    TRUNCATE bar;

    END;

    $$ LANGUAGE plpgsql;
@stig stig marked this pull request as ready for review June 15, 2023 11:39
@domenkozar domenkozar merged commit fda406a into cachix:main Jun 18, 2023
@stig stig deleted the postgres-schemas branch June 18, 2023 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants