darold / ora2pg

Ora2Pg is a free tool used to migrate an Oracle database to a PostgreSQL compatible schema. It connects your Oracle database, scan it automatically and extracts its structure or data, it then generates SQL scripts that you can load into PostgreSQL.
http://www.ora2pg.com/
GNU General Public License v3.0
978 stars 341 forks source link

Fix issue with multi style comments #1760

Closed newtora2pg closed 3 months ago

newtora2pg commented 3 months ago

Hi team, We have found one issue in the code when we have multi-style comments vis-a-vis single-line and multi-line comments.

Example Problem

Suppose we have table ddl like below, when we are trying to convert with ora2pg code we are missing the column "col4" line because of the below-explained reason,

Example ddl:

CREATE TABLE tbname(
col1 int,
/* col2 varchar(20),
-- col3 varchar(20),*/
col4 varchar(20),
/* col5 varchar(20),
col6 int, */
col7 int);

Expected result:

CREATE TABLE tbname (
    col1 numeric(38),
    col4 varchar(20),
    col7 numeric(38)
) ;

Ora2pg Current Result:

CREATE TABLE tbname (
    col1 numeric(38),
    col7 numeric(38)
) ;

Issue Overview:

When we have the end pattern of the block comment in the same line as that of the single line comment (as we have in column 3 line number above) then the end pattern of the block comment as well as single line comment both get replaced when ora2pg changes the commented code to "ORA2PGCOMMENT{commentnumber}".

Issue Explanation:

In the first step ora2PG replaces line starting with "--" (single line comment) with "ORA2PGCOMMENT{commentnumber}". Inadvertently it also replaces "*/" which was the end pattern of the block comment.

In the second step, when it tries to replace comments within "/ /" (block comment) it finds the opening comment pattern "/" in Column 2nd line but the closing comment pattern "/" it finds in the 5th column line instead of the 3rd column and it replaces the whole block with "ORA2PGCOMMENT{commentnumber}" thus missing valid col4 from final ddl . Because of this reason, it is missing one column in the output ddl in the provided example code.

Solution Implemented:

we are also checking if a DDL line has the single line comment '--' and it also contains '/'. if so, then we are giving a line break before /. With this, the end pattern of block comment will remain intact and won't be replaced with single line comment which solves the issue at hand.

Above issue happens for single file input as well as db Input