From e64f7a58167c1bb6a60bf4ad6422e6d1b6111769 Mon Sep 17 00:00:00 2001 From: yx179971 <375986529@qq.com> Date: Wed, 22 Nov 2023 11:14:29 +0800 Subject: [PATCH] fix ddl from exists table (#172) --- ddlmod.go | 2 +- ddlmod_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ddlmod.go b/ddlmod.go index 53570614..463a1d62 100644 --- a/ddlmod.go +++ b/ddlmod.go @@ -13,7 +13,7 @@ import ( var ( sqliteSeparator = "`|\"|'|\t" - indexRegexp = regexp.MustCompile(fmt.Sprintf(`(?is)CREATE(?: UNIQUE)? INDEX [%v]?[\w\d-]+[%v]? ON (.*)$`, sqliteSeparator, sqliteSeparator)) + indexRegexp = regexp.MustCompile(fmt.Sprintf(`(?is)CREATE(?: UNIQUE)? INDEX [%v]?[\w\d-]+[%v]?(?s:.*?)ON (.*)$`, sqliteSeparator, sqliteSeparator)) tableRegexp = regexp.MustCompile(fmt.Sprintf(`(?is)(CREATE TABLE [%v]?[\w\d-]+[%v]?)(?:\s*\((.*)\))?`, sqliteSeparator, sqliteSeparator)) separatorRegexp = regexp.MustCompile(fmt.Sprintf("[%v]", sqliteSeparator)) columnsRegexp = regexp.MustCompile(fmt.Sprintf(`[(,][%v]?(\w+)[%v]?`, sqliteSeparator, sqliteSeparator)) diff --git a/ddlmod_test.go b/ddlmod_test.go index 963c2712..02d36691 100644 --- a/ddlmod_test.go +++ b/ddlmod_test.go @@ -97,6 +97,24 @@ func TestParseDDL(t *testing.T) { }, }, }, + { + "index with \n from .schema sqlite", + []string{ + "CREATE TABLE `test-d` (`field` integer NOT NULL)", + "CREATE INDEX `idx_uq`\n ON `test-b`(`field`) WHERE field = 0", + }, + 1, + []migrator.ColumnType{ + { + NameValue: sql.NullString{String: "field", Valid: true}, + DataTypeValue: sql.NullString{String: "integer", Valid: true}, + ColumnTypeValue: sql.NullString{String: "integer", Valid: true}, + PrimaryKeyValue: sql.NullBool{Bool: false, Valid: true}, + UniqueValue: sql.NullBool{Bool: false, Valid: true}, + NullableValue: sql.NullBool{Bool: false, Valid: true}, + }, + }, + }, } for _, p := range params {