Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions mysql2sqlite
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,63 @@ inView != 0 { next }
alterTableName = ""
alterIndexName = ""
isAddIndex = 0

# Check if it's a single-line statement (ends with semicolon)
if( /;$/ ) {
# Process immediately

# Check if this is ADD INDEX or ADD UNIQUE
if( /ADD INDEX|add index|ADD UNIQUE|add unique/ ) {
isAddIndex = 1

# Extract table name
if( match( $0, /`[^`]+/ ) ){
alterTableName = substr( $0, RSTART+1, RLENGTH-1 )
}

# Extract index name
if( match( $0, /(ADD INDEX|add index|ADD UNIQUE|add unique) `[^`]+/ ) ){
indexMatch = substr( $0, RSTART, RLENGTH )
if( match( indexMatch, /`[^`]+$/ ) ){
alterIndexName = substr( indexMatch, RSTART+1, RLENGTH-1 )
}
}

# Extract column list
if( isAddIndex && alterTableName != "" && alterIndexName != "" ) {
if( match( alterStatement, /\([^)]+\)/ ) ){
columnList = substr( alterStatement, RSTART+1, RLENGTH-2 )
gsub( /[ \t\n\r]+/, " ", columnList )
gsub( /^ +| +$/, "", columnList )
gsub( /`/, "\"", columnList )

# Check if UNIQUE
uniquePrefix = ""
if( alterStatement ~ /ADD UNIQUE|add unique/ ) {
uniquePrefix = "UNIQUE "
}

print "CREATE " uniquePrefix "INDEX \"idx_" alterTableName "_" alterIndexName "\" ON \"" alterTableName "\" (" columnList ");"
}
}
}

# Reset
inAlterTable = 0
}
Comment on lines +154 to +196
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand why all these lines need to be added: seems that unique indexes are handled a bit lower in this file.

Copy link
Author

@dmytroun1q dmytroun1q Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, can't say too much about this, but as I see according to the search over the previous version - ADD UNIQUE / UNIQUE does not appear anywhere in a file and current include. I tested several times and no error occurs.


next
}

# Continue collecting ALTER TABLE statement lines
inAlterTable != 0 {
alterStatement = alterStatement " " $0

# Check if this is an ADD INDEX statement (might be on any line)
if( /ADD INDEX|add index/ ) {
# Check if this is an ADD INDEX or ADD UNIQUE statement (might be on any line)
if( /ADD INDEX|add index|ADD UNIQUE|add unique/ ) {
isAddIndex = 1
# Extract index name from this line
if( match( $0, /(ADD INDEX|add index) `[^`]+/ ) ){
if( match( $0, /(ADD INDEX|add index|ADD UNIQUE|add unique) `[^`]+/ ) ){
indexMatch = substr( $0, RSTART, RLENGTH )
if( match( indexMatch, /`[^`]+$/ ) ){
alterIndexName = substr( indexMatch, RSTART+1, RLENGTH-1 )
Expand All @@ -184,8 +229,14 @@ inAlterTable != 0 {
gsub( /^ +| +$/, "", columnList ) # trim leading/trailing spaces
gsub( /`/, "\"", columnList ) # convert backticks to double quotes

# Generate CREATE INDEX statement
print "CREATE INDEX \"idx_" alterTableName "_" alterIndexName "\" ON \"" alterTableName "\" (" columnList ");"
# Check if this is a UNIQUE constraint
uniquePrefix = ""
if( alterStatement ~ /ADD UNIQUE|add unique/ ) {
uniquePrefix = "UNIQUE "
}

# Generate CREATE INDEX statement (with UNIQUE if needed)
print "CREATE " uniquePrefix "INDEX \"idx_" alterTableName "_" alterIndexName "\" ON \"" alterTableName "\" (" columnList ");"
}
}
# If it's not an ADD INDEX, we just skip it
Expand Down