Skip to content

Commit

Permalink
chore(dp): Make relation property level instead of table level (#12518)
Browse files Browse the repository at this point in the history
This allows making multiple relations to the same table.

Signed-off-by: Kuba Marciniszyn <kuba@freedomfi.com>
  • Loading branch information
jkmar committed Apr 21, 2022
1 parent a0167de commit 6978196
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 63 deletions.
1 change: 1 addition & 0 deletions dp/cloud/go/services/dp/storage/db/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ type Field struct {
HasDefault bool
DefaultValue interface{}
Unique bool
Relation string
}
1 change: 0 additions & 1 deletion dp/cloud/go/services/dp/storage/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type Model interface {

type ModelMetadata struct {
Table string
Relations map[string]string
Properties []*Field
CreateObject func() Model
}
9 changes: 2 additions & 7 deletions dp/cloud/go/services/dp/storage/db/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,6 @@ func (s *someModel) GetMetadata() *db.ModelMetadata {
SqlType: sqorc.ColumnTypeDatetime,
},
},
Relations: nil,
CreateObject: func() db.Model {
return &someModel{}
},
Expand Down Expand Up @@ -607,6 +606,7 @@ func (o *otherModel) GetMetadata() *db.ModelMetadata {
Name: "some_id",
SqlType: sqorc.ColumnTypeInt,
Nullable: true,
Relation: someTable,
},
{
Name: "value",
Expand All @@ -629,7 +629,6 @@ func (o *otherModel) GetMetadata() *db.ModelMetadata {
Nullable: true,
},
},
Relations: makeRelationsMap(someTable),
CreateObject: func() db.Model {
return &otherModel{}
},
Expand Down Expand Up @@ -673,6 +672,7 @@ func (a *anotherModel) GetMetadata() *db.ModelMetadata {
Name: "other_id",
SqlType: sqorc.ColumnTypeInt,
Nullable: true,
Relation: otherTable,
},
{
Name: "default_value",
Expand All @@ -681,7 +681,6 @@ func (a *anotherModel) GetMetadata() *db.ModelMetadata {
DefaultValue: defaultValue,
},
},
Relations: makeRelationsMap(otherTable),
CreateObject: func() db.Model {
return &anotherModel{}
},
Expand Down Expand Up @@ -742,7 +741,3 @@ func (m *modelWithUniqueFields) Fields() []db.BaseType {
db.IntType{X: &m.anotherUniqueFied},
}
}

func makeRelationsMap(table string) map[string]string {
return map[string]string{table: table + "_id"}
}
19 changes: 7 additions & 12 deletions dp/cloud/go/services/dp/storage/db/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func CreateTable(tx sq.BaseRunner, builder sqorc.StatementBuilder, metadata *Mod
PrimaryKey("id")
fields := metadata.Properties
tableBuilder = addColumns(tableBuilder, fields)
tableBuilder = addRelations(tableBuilder, metadata)
_, err := tableBuilder.Exec()
return err
}
Expand All @@ -47,17 +46,13 @@ func addColumns(builder sqorc.CreateTableBuilder, fields []*Field) sqorc.CreateT
if field.Unique {
builder = builder.Unique(field.Name)
}
}
return builder
}

func addRelations(builder sqorc.CreateTableBuilder, metadata *ModelMetadata) sqorc.CreateTableBuilder {
for table, column := range metadata.Relations {
builder = builder.ForeignKey(
table,
map[string]string{column: "id"},
sqorc.ColumnOnDeleteCascade,
)
if field.Relation != "" {
builder = builder.ForeignKey(
field.Relation,
map[string]string{field.Name: "id"},
sqorc.ColumnOnDeleteCascade,
)
}
}
return builder
}
34 changes: 13 additions & 21 deletions dp/cloud/go/services/dp/storage/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (gs *DBGrantState) GetMetadata() *db.ModelMetadata {
SqlType: sqorc.ColumnTypeText,
},
},
Relations: map[string]string{},
CreateObject: func() db.Model {
return &DBGrantState{}
},
Expand Down Expand Up @@ -112,13 +111,15 @@ func (g *DBGrant) GetMetadata() *db.ModelMetadata {
SqlType: sqorc.ColumnTypeInt,
},
{
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Relation: GrantStateTable,
},
{
Name: "cbsd_id",
SqlType: sqorc.ColumnTypeInt,
Nullable: true,
Relation: CbsdTable,
},
{
Name: "grant_id",
Expand Down Expand Up @@ -157,10 +158,6 @@ func (g *DBGrant) GetMetadata() *db.ModelMetadata {
SqlType: sqorc.ColumnTypeReal,
},
},
Relations: map[string]string{
GrantStateTable: "state_id",
CbsdTable: "cbsd_id",
},
CreateObject: func() db.Model {
return &DBGrant{}
},
Expand Down Expand Up @@ -192,7 +189,6 @@ func (cs *DBCbsdState) GetMetadata() *db.ModelMetadata {
SqlType: sqorc.ColumnTypeText,
},
},
Relations: map[string]string{},
CreateObject: func() db.Model {
return &DBCbsdState{}
},
Expand Down Expand Up @@ -262,8 +258,9 @@ func (c *DBCbsd) GetMetadata() *db.ModelMetadata {
SqlType: sqorc.ColumnTypeText,
},
{
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Relation: CbsdStateTable,
},
{
Name: "cbsd_id",
Expand Down Expand Up @@ -338,9 +335,6 @@ func (c *DBCbsd) GetMetadata() *db.ModelMetadata {
DefaultValue: false,
},
},
Relations: map[string]string{
CbsdStateTable: "state_id",
},
CreateObject: func() db.Model {
return &DBCbsd{}
},
Expand Down Expand Up @@ -370,18 +364,16 @@ func (amc *DBActiveModeConfig) GetMetadata() *db.ModelMetadata {
SqlType: sqorc.ColumnTypeInt,
},
{
Name: "cbsd_id",
SqlType: sqorc.ColumnTypeInt,
Name: "cbsd_id",
SqlType: sqorc.ColumnTypeInt,
Relation: CbsdTable,
},
{
Name: "desired_state_id",
SqlType: sqorc.ColumnTypeInt,
Name: "desired_state_id",
SqlType: sqorc.ColumnTypeInt,
Relation: CbsdStateTable,
},
},
Relations: map[string]string{
CbsdTable: "cbsd_id",
CbsdStateTable: "desired_state_id",
},
CreateObject: func() db.Model {
return &DBActiveModeConfig{}
},
Expand Down
35 changes: 13 additions & 22 deletions dp/cloud/go/services/dp/storage/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func TestGetMetadata(t *testing.T) {
SqlType: sqorc.ColumnTypeText,
},
},
Relations: map[string]string{},
},
},
{
Expand All @@ -142,13 +141,15 @@ func TestGetMetadata(t *testing.T) {
SqlType: sqorc.ColumnTypeInt,
},
{
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Relation: storage.GrantStateTable,
},
{
Name: "cbsd_id",
SqlType: sqorc.ColumnTypeInt,
Nullable: true,
Relation: storage.CbsdTable,
},
{
Name: "grant_id",
Expand Down Expand Up @@ -187,10 +188,6 @@ func TestGetMetadata(t *testing.T) {
SqlType: sqorc.ColumnTypeReal,
},
},
Relations: map[string]string{
storage.CbsdTable: "cbsd_id",
storage.GrantStateTable: "state_id",
},
},
},
{
Expand All @@ -208,7 +205,6 @@ func TestGetMetadata(t *testing.T) {
SqlType: sqorc.ColumnTypeText,
},
},
Relations: map[string]string{},
},
},
{
Expand All @@ -226,8 +222,9 @@ func TestGetMetadata(t *testing.T) {
SqlType: sqorc.ColumnTypeText,
},
{
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Name: "state_id",
SqlType: sqorc.ColumnTypeInt,
Relation: storage.CbsdStateTable,
},
{
Name: "cbsd_id",
Expand Down Expand Up @@ -302,9 +299,6 @@ func TestGetMetadata(t *testing.T) {
DefaultValue: false,
},
},
Relations: map[string]string{
storage.CbsdStateTable: "state_id",
},
},
},
{
Expand All @@ -318,18 +312,16 @@ func TestGetMetadata(t *testing.T) {
SqlType: sqorc.ColumnTypeInt,
},
{
Name: "cbsd_id",
SqlType: sqorc.ColumnTypeInt,
Name: "cbsd_id",
SqlType: sqorc.ColumnTypeInt,
Relation: storage.CbsdTable,
},
{
Name: "desired_state_id",
SqlType: sqorc.ColumnTypeInt,
Name: "desired_state_id",
SqlType: sqorc.ColumnTypeInt,
Relation: storage.CbsdStateTable,
},
},
Relations: map[string]string{
storage.CbsdTable: "cbsd_id",
storage.CbsdStateTable: "desired_state_id",
},
},
},
}
Expand All @@ -338,7 +330,6 @@ func TestGetMetadata(t *testing.T) {
actual := tc.model.GetMetadata()
obj := actual.CreateObject()

assert.Equal(t, tc.expected.Relations, actual.Relations)
assert.Equal(t, tc.expected.Properties, actual.Properties)
assert.Equal(t, tc.expected.Table, actual.Table)
assert.Equal(t, tc.model, obj)
Expand Down

0 comments on commit 6978196

Please sign in to comment.