mirror of
				https://github.com/chaisql/chai.git
				synced 2025-10-31 19:02:48 +08:00 
			
		
		
		
	sql: improve and fix ALTER TABLE ADD FIELD logic
This commit is contained in:
		| @@ -3,6 +3,7 @@ package parser_test | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/genjidb/genji/document" | ||||
| 	"github.com/genjidb/genji/internal/database" | ||||
| 	"github.com/genjidb/genji/internal/expr" | ||||
| 	"github.com/genjidb/genji/internal/query/statement" | ||||
| @@ -19,10 +20,10 @@ func TestParserAlterTable(t *testing.T) { | ||||
| 		expected statement.Statement | ||||
| 		errored  bool | ||||
| 	}{ | ||||
| 		{"Basic", "ALTER TABLE foo RENAME TO bar", statement.AlterStmt{TableName: "foo", NewTableName: "bar"}, false}, | ||||
| 		{"With error / missing TABLE keyword", "ALTER foo RENAME TO bar", statement.AlterStmt{}, true}, | ||||
| 		{"With error / two identifiers for table name", "ALTER TABLE foo baz RENAME TO bar", statement.AlterStmt{}, true}, | ||||
| 		{"With error / two identifiers for new table name", "ALTER TABLE foo RENAME TO bar baz", statement.AlterStmt{}, true}, | ||||
| 		{"Basic", "ALTER TABLE foo RENAME TO bar", statement.AlterTableRenameStmt{TableName: "foo", NewTableName: "bar"}, false}, | ||||
| 		{"With error / missing TABLE keyword", "ALTER foo RENAME TO bar", statement.AlterTableRenameStmt{}, true}, | ||||
| 		{"With error / two identifiers for table name", "ALTER TABLE foo baz RENAME TO bar", statement.AlterTableRenameStmt{}, true}, | ||||
| 		{"With error / two identifiers for new table name", "ALTER TABLE foo RENAME TO bar baz", statement.AlterTableRenameStmt{}, true}, | ||||
| 	} | ||||
|  | ||||
| 	for _, test := range tests { | ||||
| @@ -46,41 +47,47 @@ func TestParserAlterTableAddField(t *testing.T) { | ||||
| 		expected statement.Statement | ||||
| 		errored  bool | ||||
| 	}{ | ||||
| 		{"Basic", "ALTER TABLE foo ADD FIELD bar", nil, true}, | ||||
| 		{"With type", "ALTER TABLE foo ADD FIELD bar integer", statement.AlterTableAddField{ | ||||
| 			Info: database.TableInfo{ | ||||
| 				TableName: "foo", | ||||
| 				FieldConstraints: database.MustNewFieldConstraints( | ||||
| 					&database.FieldConstraint{ | ||||
| 						Field: "bar", | ||||
| 						Type:  types.IntegerValue, | ||||
| 					}, | ||||
| 				), | ||||
| 		{"Basic", "ALTER TABLE foo ADD FIELD bar", &statement.AlterTableAddFieldStmt{ | ||||
| 			TableName: "foo", | ||||
| 			FieldConstraint: &database.FieldConstraint{ | ||||
| 				Field: "bar", | ||||
| 				Type:  types.AnyValue, | ||||
| 			}, | ||||
| 		}, false}, | ||||
| 		{"With not null", "ALTER TABLE foo ADD FIELD bar NOT NULL", statement.AlterTableAddField{ | ||||
| 			Info: database.TableInfo{ | ||||
| 				TableName: "foo", | ||||
| 				FieldConstraints: database.MustNewFieldConstraints( | ||||
| 					&database.FieldConstraint{ | ||||
| 						Field:     "bar", | ||||
| 						IsNotNull: true, | ||||
| 					}, | ||||
| 				), | ||||
| 		{"With type", "ALTER TABLE foo ADD FIELD bar integer", &statement.AlterTableAddFieldStmt{ | ||||
| 			TableName: "foo", | ||||
| 			FieldConstraint: &database.FieldConstraint{ | ||||
| 				Field: "bar", | ||||
| 				Type:  types.IntegerValue, | ||||
| 			}, | ||||
| 		}, false}, | ||||
| 		{"With primary key", "ALTER TABLE foo ADD FIELD bar PRIMARY KEY", nil, true}, | ||||
| 		{"With multiple constraints", "ALTER TABLE foo ADD FIELD bar integer NOT NULL DEFAULT 0", statement.AlterTableAddField{ | ||||
| 			Info: database.TableInfo{ | ||||
| 				TableName: "foo", | ||||
| 				FieldConstraints: database.MustNewFieldConstraints( | ||||
| 					&database.FieldConstraint{ | ||||
| 						Field:        "bar", | ||||
| 						Type:         types.IntegerValue, | ||||
| 						IsNotNull:    true, | ||||
| 						DefaultValue: expr.Constraint(expr.LiteralValue{Value: types.NewIntegerValue(0)}), | ||||
| 					}, | ||||
| 				), | ||||
| 		{"With not null", "ALTER TABLE foo ADD FIELD bar NOT NULL", &statement.AlterTableAddFieldStmt{ | ||||
| 			TableName: "foo", | ||||
| 			FieldConstraint: &database.FieldConstraint{ | ||||
| 				Field:     "bar", | ||||
| 				IsNotNull: true, | ||||
| 			}, | ||||
| 		}, false}, | ||||
| 		{"With primary key", "ALTER TABLE foo ADD FIELD bar PRIMARY KEY", &statement.AlterTableAddFieldStmt{ | ||||
| 			TableName: "foo", | ||||
| 			FieldConstraint: &database.FieldConstraint{ | ||||
| 				Field: "bar", | ||||
| 				Type:  types.AnyValue, | ||||
| 			}, | ||||
| 			TableConstraints: database.TableConstraints{ | ||||
| 				&database.TableConstraint{ | ||||
| 					Paths:      document.Paths{document.NewPath("bar")}, | ||||
| 					PrimaryKey: true, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, false}, | ||||
| 		{"With multiple constraints", "ALTER TABLE foo ADD FIELD bar integer NOT NULL DEFAULT 0", &statement.AlterTableAddFieldStmt{ | ||||
| 			TableName: "foo", | ||||
| 			FieldConstraint: &database.FieldConstraint{ | ||||
| 				Field:        "bar", | ||||
| 				Type:         types.IntegerValue, | ||||
| 				IsNotNull:    true, | ||||
| 				DefaultValue: expr.Constraint(expr.LiteralValue{Value: types.NewIntegerValue(0)}), | ||||
| 			}, | ||||
| 		}, false}, | ||||
| 		{"With error / missing FIELD keyword", "ALTER TABLE foo ADD bar", nil, true}, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Asdine El Hrychy
					Asdine El Hrychy