Skip to content

Commit

Permalink
Merge pull request #430 from tablelandnetwork/bcalza/fixgetschema
Browse files Browse the repository at this point in the history
fix: get schema was not parsing statements with autoincrement
  • Loading branch information
brunocalza authored Jan 16, 2023
2 parents 2153717 + a052117 commit 46bcf9d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/system/impl/sqlstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestGetSchemaByTableName(t *testing.T) {
&ethereum.ContractCreateTable{
TableId: big.NewInt(42),
Owner: common.HexToAddress("0xb451cee4A42A652Fe77d373BAe66D42fd6B8D8FF"),
Statement: "create table foo_1337 (a int primary key, b text not null default 'foo' unique, check (a > 0))",
Statement: "create table foo_1337 (a integer primary key, b text not null default 'foo' unique, check (a > 0))",
},
},
})
Expand All @@ -148,9 +148,9 @@ func TestGetSchemaByTableName(t *testing.T) {
require.Len(t, schema.TableConstraints, 1)

require.Equal(t, "a", schema.Columns[0].Name)
require.Equal(t, "int", schema.Columns[0].Type)
require.Equal(t, "integer", schema.Columns[0].Type)
require.Len(t, schema.Columns[0].Constraints, 1)
require.Equal(t, "primary key", schema.Columns[0].Constraints[0])
require.Equal(t, "primary key autoincrement", schema.Columns[0].Constraints[0])

require.Equal(t, "b", schema.Columns[1].Name)
require.Equal(t, "text", schema.Columns[1].Type)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sqlstore/impl/system/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ func (s *SystemStore) GetSchemaByTableName(ctx context.Context, name string) (sq
return sqlstore.TableSchema{}, fmt.Errorf("failed to get the table: %s", err)
}

if strings.Contains(strings.ToLower(createStmt), "autoincrement") {
createStmt = strings.Replace(createStmt, "autoincrement", "", -1)
}

index := strings.LastIndex(strings.ToLower(createStmt), "strict")
ast, err := sqlparser.Parse(createStmt[:index])
if err != nil {
Expand Down

0 comments on commit 46bcf9d

Please sign in to comment.