When pg-minify fails, it's hard to locate the issue

オープン
#93 コメント 8 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
postgresql, typescript
領域
backend, database

調査の方向性

まず dist/commands/watch.js を読み、特に現在のマイグレーションを処理する pg-minify の呼び出しを確認して、報告されている current/12000-insert_iso3166_countries.sql の再現を実行します。マイグレーションファイルの境界とパーサーの行番号がどのように組み立てられているかを追跡します。完了の条件は、pg-minify の失敗によって元のエラーを保持したまま、該当するファイルとおおよその行が特定されることです。

索引モデルが issue の本文から書いたものです。

説明

Summary

When pg-minify fails, Graphile Migrate outputs pg-minify's error, but doesn't relate it to the file that actually caused the problem. This is particularly problematic if you have a large (e.g. initial) migration and you're using the current/ folder - you don't know which file the error occurred in.

Steps to reproduce

current/12000-insert_iso3166_countries.sql:

COPY countries (name, alpha_2, code) FROM stdin DELIMITER '|';
Kyrgyzstan|KG|417
Lao People's Democratic Republic|LA|418
Latvia|LV|428
\.
Expected results

Error on line 3 of 12000-insert_iso3166_countries.sql (we don't support COPY syntax).

Actual results
🛑 Error occurred whilst processing migration
    SQLParsingError: Error parsing SQL at {line:24153,col:11}: Unclosed text block.
Additional context

graphile-migrate 1.0.1; linux; current/ folder.

Possible Solution

The following mod to dist/commands/watch.js gives more information:

                let currentBodyMinified;
                try {
                  currentBodyMinified = pgMinify(currentBodyFromDryRun);
                } catch (e) {
                  const matches = e.message.match(/Error parsing SQL at \{line:(\d+),col:(\d+)\}/);
                  if (matches) {
                    const [_, ln, col] = matches;
                    const lines = currentBodyFromDryRun.split("\n");
                    const leadingLines = lines.slice(0, +ln);
                    const lastSplit = [...leadingLines].reverse().find(l => l.match(/^--! split/));
                    let filename = null;
                    let offset = 0;
                    if (lastSplit) {
                      filename = lastSplit.split(':')[1].trim();
                      const index = leadingLines.indexOf(lastSplit);
                      offset = index + 1;
                    }

                    const previousLine = lines[+ln - 2];
                    const line = lines[+ln - 1];
                    const nextLine = lines[+ln];
                    const details = filename ? `current/${filename} line ${+ln - offset}-ish` : `current.sql line ${ln}-ish`
                    console.error(`Error happened around here ${details}:`);
                    console.error();
                    console.error(previousLine || "");
                    console.error(line || "");
                    console.error("-".repeat(+col - 1) + "^");
                    console.error(nextLine || "");
                  }
                  throw e;
                }

produces:

Error happened around here current/12000-insert_iso3166_countries.sql line 3-ish:

Kyrgyzstan|KG|417
Lao People's Democratic Republic|LA|418
----------^
Latvia|LV|428

🛑 Error occurred whilst processing migration
    SQLParsingError: Error parsing SQL at {line:24153,col:11}: Unclosed text block.

主要言語
TypeScript
スター
836
フォーク
71
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

graphile/migrate のほかの issue

graphile/migrate の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。