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

prepare statements break migration #78

Open
abraxxa opened this issue Apr 5, 2021 · 0 comments
Open

prepare statements break migration #78

abraxxa opened this issue Apr 5, 2021 · 0 comments

Comments

@abraxxa
Copy link

abraxxa commented Apr 5, 2021

  • Mojo::Pg version: 4.25
  • Perl version: 5.32.1
  • Operating system: Ubuntu 20.10 x86_64

Steps to reproduce the behavior

Create a migration SQL file which contains PREPARE statements for inserting data:

PREPARE v1_role (text) AS
INSERT INTO role (name)
VALUES ($1);

EXECUTE v1_role('Role A');

EXECUTE v1_role('Role B');

EXECUTE v1_role('Role C');

PREPARE v1_rel_ramuser_role (integer, integer) AS
INSERT INTO rel_ramuser_role (ramuser_id, role_id)
VALUES ($1, $2);

EXECUTE v1_rel_ramuser_role(1, 1);

EXECUTE v1_rel_ramuser_role(1, 2);

EXECUTE v1_rel_ramuser_role(1, 3);

Enable auto migration on startup:

sub startup ($self) {
    # Load configuration from config file
    $self->plugin('NotYAMLConfig');

    $self->helper(
        pg => sub { state $pg = Mojo::Pg->new( shift->config('pg') ) } );

    # Migrate to latest version if necessary
    my $path = $self->home->child( 'migrations', 'ram.sql' );
    $self->pg->auto_migrate(1)->migrations->name('ram')->from_file($path);
    $self->log->info(
        "current database version: " . $self->pg->migrations->active );
}

Expected behavior

Migration SQL run without errors.

Actual behavior

DBD::Pg::st execute failed: ERROR:  invalid input syntax for type integer: "ram"
LINE 63: VALUES ('1', 'ram');
                      ^ at /home/alex/.plenv/versions/5.32.1/lib/perl5/site_perl/5.32.1/Mojo/Pg/Migrations.pm line 77.

export DBI_TRACE=SQL shows the following SQL getting executed:

EXECUTE SELECT version FROM mojo_migrations WHERE name = $1 (
$1: ram
);

CREATE TABLE IF NOT EXISTS mojo_migrations (
       name    TEXT PRIMARY KEY,
       version BIGINT NOT NULL CHECK (version >= 0)
     );

EXECUTE INSERT INTO mojo_migrations VALUES ($1, $2) (
$1: ram
$2: 0
);

begin;

LOCK TABLE mojo_migrations IN EXCLUSIVE MODE;

PREPARE dbdpg_p204881_1 AS SELECT version FROM mojo_migrations WHERE name = $1;

EXECUTE dbdpg_p204881_1 (
$1: ram
);

-- 1 up
-- various create table statements etc.
PREPARE v1_role (text) AS
INSERT INTO role (name)
VALUES ('1');

EXECUTE v1_role('Role A');

EXECUTE v1_role('Role B');

EXECUTE v1_role('Role C');

PREPARE v1_rel_ramuser_role (integer, integer) AS
INSERT INTO rel_ramuser_role (ramuser_id, role_id)
VALUES ('1', 'ram');

EXECUTE v1_rel_ramuser_role(1, 1);

EXECUTE v1_rel_ramuser_role(1, 2);

EXECUTE v1_rel_ramuser_role(1, 3);
;UPDATE mojo_migrations SET version = '1' WHERE name = 'ram';;

DBD::Pg::st execute failed: ERROR:  invalid input syntax for type integer: "ram"
LINE 63: VALUES ('1', 'ram');
                      ^ at /home/alex/.plenv/versions/5.32.1/lib/perl5/site_perl/5.32.1/Mojo/Pg/Migrations.pm line 77.
rollback;

DEALLOCATE dbdpg_p204881_1;

When the second prepare statement is removed from the SQL file, it works also as expected so the different data types seem to cause the issue. This shouldn't be an issue as all prepare statements are named.

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

No branches or pull requests

1 participant