Skip to content

Commit

Permalink
Add JSON schema struct generation (#210)
Browse files Browse the repository at this point in the history
All structs matched well except for `PrimaryKey` which we mapped
differently, I've refactored it to `Pk` as it doesn't seem to support
different names for go and JSON

---------

Signed-off-by: Alexis Rico <sferadev@gmail.com>
Co-authored-by: Andrew Farries <andyrb@gmail.com>
  • Loading branch information
SferaDev and andrew-farries committed Dec 1, 2023
1 parent 5812acc commit 788bac6
Show file tree
Hide file tree
Showing 33 changed files with 766 additions and 238 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: generate
generate:
# Generate the types from the JSON schema
docker run -v $$PWD/schema.json:/mnt/schema.json omissis/go-jsonschema:0.14.1 --only-models -p migrations --tags json /mnt/schema.json > pkg/migrations/types.go

# Add the license header
echo "// SPDX-License-Identifier: Apache-2.0" | cat - pkg/migrations/types.go > pkg/migrations/types.go.tmp
mv pkg/migrations/types.go.tmp pkg/migrations/types.go
5 changes: 0 additions & 5 deletions pkg/migrations/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

package migrations

type CheckConstraint struct {
Name string `json:"name"`
Constraint string `json:"constraint"`
}

func (c *CheckConstraint) Validate() error {
if c.Name == "" {
return FieldRequiredError{Name: "name"}
Expand Down
6 changes: 0 additions & 6 deletions pkg/migrations/fk_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ package migrations

import "github.com/xataio/pgroll/pkg/schema"

type ForeignKeyReference struct {
Name string `json:"name"`
Table string `json:"table"`
Column string `json:"column"`
}

func (f *ForeignKeyReference) Validate(s *schema.Schema) error {
if f.Name == "" {
return FieldRequiredError{Name: "name"}
Expand Down
8 changes: 1 addition & 7 deletions pkg/migrations/op_add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
"github.com/xataio/pgroll/pkg/schema"
)

type OpAddColumn struct {
Table string `json:"table"`
Up *string `json:"up"`
Column Column `json:"column"`
}

var _ Operation = (*OpAddColumn)(nil)

func (o *OpAddColumn) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error {
Expand Down Expand Up @@ -173,7 +167,7 @@ func (o *OpAddColumn) Validate(ctx context.Context, s *schema.Schema) error {
return FieldRequiredError{Name: "up"}
}

if o.Column.PrimaryKey {
if o.Column.Pk {
return errors.New("adding primary key columns is not supported")
}

Expand Down
54 changes: 27 additions & 27 deletions pkg/migrations/op_add_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func TestAddColumn(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down Expand Up @@ -121,9 +121,9 @@ func TestAddForeignKeyColumn(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand All @@ -136,9 +136,9 @@ func TestAddForeignKeyColumn(t *testing.T) {
Name: "orders",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "quantity",
Expand Down Expand Up @@ -223,9 +223,9 @@ func TestAddForeignKeyColumn(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand All @@ -238,9 +238,9 @@ func TestAddForeignKeyColumn(t *testing.T) {
Name: "orders",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "quantity",
Expand Down Expand Up @@ -332,9 +332,9 @@ func TestAddColumnWithUpSql(t *testing.T) {
Name: "products",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down Expand Up @@ -419,9 +419,9 @@ func TestAddNotNullColumnWithNoDefault(t *testing.T) {
Name: "products",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down Expand Up @@ -485,9 +485,9 @@ func TestAddColumnValidation(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down Expand Up @@ -604,9 +604,9 @@ func TestAddColumnWithCheckConstraint(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down
13 changes: 0 additions & 13 deletions pkg/migrations/op_alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ import (
"github.com/xataio/pgroll/pkg/schema"
)

type OpAlterColumn struct {
Table string `json:"table"`
Column string `json:"column"`
Name string `json:"name"`
Type string `json:"type"`
Check *CheckConstraint `json:"check"`
References *ForeignKeyReference `json:"references"`
Nullable *bool `json:"nullable"`
Unique *UniqueConstraint `json:"unique"`
Up string `json:"up"`
Down string `json:"down"`
}

var _ Operation = (*OpAlterColumn)(nil)

func (o *OpAlterColumn) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error {
Expand Down
12 changes: 6 additions & 6 deletions pkg/migrations/op_alter_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func TestAlterColumnValidation(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand All @@ -32,9 +32,9 @@ func TestAlterColumnValidation(t *testing.T) {
Name: "posts",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "title",
Expand Down
12 changes: 6 additions & 6 deletions pkg/migrations/op_change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func TestChangeColumnType(t *testing.T) {
Name: "reviews",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "username",
Expand Down Expand Up @@ -157,9 +157,9 @@ func TestChangeColumnTypeValidation(t *testing.T) {
Name: "reviews",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "username",
Expand Down
6 changes: 0 additions & 6 deletions pkg/migrations/op_create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
"github.com/xataio/pgroll/pkg/schema"
)

type OpCreateIndex struct {
Name string `json:"name"`
Table string `json:"table"`
Columns []string `json:"columns"`
}

var _ Operation = (*OpCreateIndex)(nil)

func (o *OpCreateIndex) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error {
Expand Down
12 changes: 6 additions & 6 deletions pkg/migrations/op_create_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func TestCreateIndex(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down Expand Up @@ -73,9 +73,9 @@ func TestCreateIndexOnMultipleColumns(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down
18 changes: 1 addition & 17 deletions pkg/migrations/op_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ import (

var _ Operation = (*OpCreateTable)(nil)

type OpCreateTable struct {
Name string `json:"name"`
Columns []Column `json:"columns"`
}

type Column struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Unique bool `json:"unique"`
PrimaryKey bool `json:"pk"`
Default *string `json:"default"`
Check *CheckConstraint `json:"check"`
References *ForeignKeyReference `json:"references"`
}

func (o *OpCreateTable) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) error {
tempName := TemporaryName(o.Name)
_, err := conn.ExecContext(ctx, fmt.Sprintf("CREATE TABLE %s (%s)",
Expand Down Expand Up @@ -117,7 +101,7 @@ func columnsToSQL(cols []Column) string {
func ColumnToSQL(col Column) string {
sql := fmt.Sprintf("%s %s", pq.QuoteIdentifier(col.Name), col.Type)

if col.PrimaryKey {
if col.Pk {
sql += " PRIMARY KEY"
}
if col.Unique {
Expand Down
36 changes: 18 additions & 18 deletions pkg/migrations/op_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func TestCreateTable(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down Expand Up @@ -84,9 +84,9 @@ func TestCreateTable(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand All @@ -104,9 +104,9 @@ func TestCreateTable(t *testing.T) {
Name: "orders",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "user_id",
Expand Down Expand Up @@ -181,9 +181,9 @@ func TestCreateTable(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand Down Expand Up @@ -246,9 +246,9 @@ func TestCreateTableValidation(t *testing.T) {
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "name",
Expand All @@ -266,9 +266,9 @@ func TestCreateTableValidation(t *testing.T) {
Name: "orders",
Columns: []migrations.Column{
{
Name: "id",
Type: "serial",
PrimaryKey: true,
Name: "id",
Type: "serial",
Pk: true,
},
{
Name: "user_id",
Expand Down
Loading

0 comments on commit 788bac6

Please sign in to comment.