Repository: ariga/atlas
Pull request #1521 (2023-03-09 11:41:02)

sql/postgres: accept sequence quoted as identifiers
Fixed https://github.com/ariga/atlas/issues/1520


Patch:
diff --git a/sql/postgres/inspect.go b/sql/postgres/inspect.go
index 184fbc23..5c0fd5e4 100644
--- a/sql/postgres/inspect.go
+++ b/sql/postgres/inspect.go
@@ -598,7 +598,7 @@ func nArgs(start, n int) string {
 	return b.String()
 }
 
-var reNextval = regexp.MustCompile(`(?i) *nextval\('(?:[\w$]+\.)*([\w$]+_[\w$]+_seq)'(?:::regclass)*\) *$`)
+var reNextval = regexp.MustCompile(`(?i) *nextval\('(?:")?(?:[\w$]+\.)*([\w$]+_[\w$]+_seq)(?:")?'(?:::regclass)*\) *$`)
 
 func defaultExpr(c *schema.Column, s string) {
 	switch m := reNextval.FindStringSubmatch(s); {


Test patch:
diff --git a/internal/integration/postgres_test.go b/internal/integration/postgres_test.go
index b18b60a5..213b523e 100644
--- a/internal/integration/postgres_test.go
+++ b/internal/integration/postgres_test.go
@@ -1228,45 +1228,27 @@ create table atlas_types_sanity
 				},
 				{
 					Name: "tSmallSerial",
-					Type: &schema.ColumnType{Type: &schema.IntegerType{T: "smallint", Unsigned: false}, Raw: "smallint", Null: false},
-					Default: &schema.RawExpr{
-						X: "nextval('\"atlas_types_sanity_tSmallSerial_seq\"'::regclass)",
-					},
+					Type: &schema.ColumnType{Type: &postgres.SerialType{T: "smallserial", SequenceName: "atlas_types_sanity_tSmallSerial_seq"}, Raw: "smallserial", Null: false},
 				},
 				{
 					Name: "tSerial",
-					Type: &schema.ColumnType{Type: &schema.IntegerType{T: "integer", Unsigned: false}, Raw: "integer", Null: false},
-					Default: &schema.RawExpr{
-						X: "nextval('\"atlas_types_sanity_tSerial_seq\"'::regclass)",
-					},
+					Type: &schema.ColumnType{Type: &postgres.SerialType{T: "serial", SequenceName: "atlas_types_sanity_tSerial_seq"}, Raw: "serial", Null: false},
 				},
 				{
 					Name: "tBigSerial",
-					Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint", Unsigned: false}, Raw: "bigint", Null: false},
-					Default: &schema.RawExpr{
-						X: "nextval('\"atlas_types_sanity_tBigSerial_seq\"'::regclass)",
-					},
+					Type: &schema.ColumnType{Type: &postgres.SerialType{T: "bigserial", SequenceName: "atlas_types_sanity_tBigSerial_seq"}, Raw: "bigserial", Null: false},
 				},
 				{
 					Name: "tSerial2",
-					Type: &schema.ColumnType{Type: &schema.IntegerType{T: "smallint", Unsigned: false}, Raw: "smallint", Null: false},
-					Default: &schema.RawExpr{
-						X: "nextval('\"atlas_types_sanity_tSerial2_seq\"'::regclass)",
-					},
+					Type: &schema.ColumnType{Type: &postgres.SerialType{T: "smallserial", SequenceName: "atlas_types_sanity_tSerial2_seq"}, Raw: "smallserial", Null: false},
 				},
 				{
 					Name: "tSerial4",
-					Type: &schema.ColumnType{Type: &schema.IntegerType{T: "integer", Unsigned: false}, Raw: "integer", Null: false},
-					Default: &schema.RawExpr{
-						X: "nextval('\"atlas_types_sanity_tSerial4_seq\"'::regclass)",
-					},
+					Type: &schema.ColumnType{Type: &postgres.SerialType{T: "serial", SequenceName: "atlas_types_sanity_tSerial4_seq"}, Raw: "serial", Null: false},
 				},
 				{
 					Name: "tSerial8",
-					Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint", Unsigned: false}, Raw: "bigint", Null: false},
-					Default: &schema.RawExpr{
-						X: "nextval('\"atlas_types_sanity_tSerial8_seq\"'::regclass)",
-					},
+					Type: &schema.ColumnType{Type: &postgres.SerialType{T: "bigserial", SequenceName: "atlas_types_sanity_tSerial8_seq"}, Raw: "bigserial", Null: false},
 				},
 				{
 					Name: "tArray",
diff --git a/sql/postgres/inspect_test.go b/sql/postgres/inspect_test.go
index 51419fd6..da918e32 100644
--- a/sql/postgres/inspect_test.go
+++ b/sql/postgres/inspect_test.go
@@ -84,6 +84,7 @@ func TestDriver_InspectTable(t *testing.T) {
  users       |  c37         | tsquery                     | tsquery             | NO          |                                        |                          |                   |                    |               |                     |                    |                | NO          |                |                    |                  |                     |                       |         | b       |  16774  |         | 16779
  users       |  c38         | datemultirange              | datemultirange      | NO          |                                        |                          |                   |                    |               |                     |                    |                | NO          |                |                    |                  |                     |                       |         | m       |         |         | 4535
  users       |  c39         | numrange                    | numrange            | NO          |                                        |                          |                   |                    |               |                     |                    |                | NO          |                |                    |                  |                     |                       |         | m       |         |         | 4536
+ users       |  c40         | smallint                    | int4                | NO          | nextval('"Users_c40_seq"'::regclass)   |                          |                32 |                    |             0 |                     |                    |                | NO          |                |                    |                  |                     |                       |         | b       |         |         |    23
 `))
 				m.ExpectQuery(sqltest.Escape(`SELECT enumtypid, enumlabel FROM pg_enum WHERE enumtypid IN ($1, $2)`)).
 					WithArgs(16774, 16775).
@@ -144,6 +145,7 @@ func TestDriver_InspectTable(t *testing.T) {
 					{Name: "c37", Type: &schema.ColumnType{Raw: "tsquery", Type: &TextSearchType{T: "tsquery"}}},
 					{Name: "c38", Type: &schema.ColumnType{Raw: "datemultirange", Type: &RangeType{T: "datemultirange"}}},
 					{Name: "c39", Type: &schema.ColumnType{Raw: "numrange", Type: &RangeType{T: "numrange"}}},
+					{Name: "c40", Type: &schema.ColumnType{Raw: "smallserial", Type: &SerialType{T: "smallserial", SequenceName: "Users_c40_seq"}}},
 				}, t.Columns)
 			},
 		},