Skip to content

Commit 4f82f40

Browse files
authored
Merge pull request #5 from ryarnyah/feat/custom-error
Add custom errors on each rules
2 parents 9831f5c + 4b85afa commit 4f82f40

11 files changed

+126
-29
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ Tool to maintain compatibility beetween multiple SQL database versions.
2424

2525
#### Binaries
2626

27-
- **linux** [amd64](https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-linux-amd64) [386](https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-linux-386) [arm](https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-linux-arm) [arm64](https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-linux-arm64)
28-
- **windows** [amd64](https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-windows-amd64) [386](https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-windows-386)
29-
- **darwin** [amd64](https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-darwin-amd64)
27+
- **linux** [amd64](https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-linux-amd64) [386](https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-linux-386) [arm](https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-linux-arm) [arm64](https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-linux-arm64)
28+
- **windows** [amd64](https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-windows-amd64) [386](https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-windows-386)
29+
- **darwin** [amd64](https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-darwin-amd64)
3030

3131
```bash
32-
sudo curl -L https://github.com/ryarnyah/dblock/releases/download/0.3.0/dblock-linux-amd64 -o /usr/local/bin/dblock && sudo chmod +x /usr/local/bin/dblock
32+
sudo curl -L https://github.com/ryarnyah/dblock/releases/download/0.4.0/dblock-linux-amd64 -o /usr/local/bin/dblock && sudo chmod +x /usr/local/bin/dblock
3333
```
3434

3535
#### Via Go
@@ -49,7 +49,7 @@ $ make
4949

5050
#### Running with Docker
5151
```bash
52-
docker run ryarnyah/dblock-linux-amd64:0.3.0 <option>
52+
docker run ryarnyah/dblock-linux-amd64:0.4.0 <option>
5353
```
5454

5555
## Usage
@@ -62,7 +62,7 @@ ________ __________.____ __
6262
/_______ /______ /_______ \____/ \___ >__|_ \
6363
\/ \/ \/ \/ \/
6464
Check db schema compatibility.
65-
Version: 0.3.0
65+
Version: 0.4.0
6666
Build: a6d4ec3-dirty
6767

6868
-alsologtostderr

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.0
1+
0.4.0

pkg/rules/add_not_null_column.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ func init() {
1212
RegisterRule("DCT02", addColumnNotNullTypeRule{})
1313
}
1414

15+
// NotNullRuleError raised when a not null Column is added
16+
type NotNullRuleError struct {
17+
RuleError
18+
19+
Column string `json:"column_name"`
20+
Schema string `json:"schema_name"`
21+
Table string `json:"table_name"`
22+
}
23+
24+
func (r NotNullRuleError) Error() string {
25+
return fmt.Sprintf("[%s] not null column %s of %s.%s added without default value",
26+
r.RuleCode,
27+
r.Column,
28+
r.Schema,
29+
r.Table,
30+
)
31+
}
32+
1533
func (addColumnNotNullTypeRule) CheckCompatibility(oldDatabase, newDatabase *model.DatabaseSchema) []error {
1634
return checkDatabaseSchemaTable(oldDatabase, newDatabase, func(schema model.Schema, oldTable, newTable model.TableSchema) []error {
1735
errors := make([]error, 0)
@@ -23,10 +41,14 @@ func (addColumnNotNullTypeRule) CheckCompatibility(oldDatabase, newDatabase *mod
2341
}
2442
}
2543
if isNewColunm && !newColumn.NullableConstraint && !newColumn.HasDefaultValue {
26-
errors = append(errors, fmt.Errorf("not null column %s of %s.%s added without default value",
27-
newColumn.ColunmName,
28-
schema.SchemaName,
29-
oldTable.TableName))
44+
errors = append(errors, NotNullRuleError{
45+
RuleError: RuleError{
46+
RuleCode: "DCT02",
47+
},
48+
Column: newColumn.ColunmName,
49+
Schema: schema.SchemaName,
50+
Table: oldTable.TableName,
51+
})
3052
break
3153
}
3254
}

pkg/rules/add_not_null_column_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func TestAddNewColumn(t *testing.T) {
191191
},
192192
},
193193
[]error{
194-
errors.New("not null column ID2 of test.test added without default value"),
194+
errors.New("[DCT02] not null column ID2 of test.test added without default value"),
195195
},
196196
},
197197
}

pkg/rules/changed_column_type.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,44 @@ func init() {
1212
RegisterRule("DCT01", changedColumnTypeRule{})
1313
}
1414

15+
// ChangedTypeRuleError raised when a Column changed type
16+
type ChangedTypeRuleError struct {
17+
RuleError
18+
19+
OldColumn string `json:"old_column_name"`
20+
OldColumnType string `json:"old_column_type"`
21+
Schema string `json:"schema_name"`
22+
Table string `json:"table_name"`
23+
NewColumnType string `json:"new_column_type"`
24+
}
25+
26+
func (r ChangedTypeRuleError) Error() string {
27+
return fmt.Sprintf("[%s] colunm %s type %s of table %s.%s is incohérent in new type %s",
28+
r.RuleCode,
29+
r.OldColumn,
30+
r.OldColumnType,
31+
r.Schema,
32+
r.Table,
33+
r.NewColumnType,
34+
)
35+
}
36+
1537
func (changedColumnTypeRule) CheckCompatibility(oldDatabase, newDatabase *model.DatabaseSchema) []error {
1638
return checkDatabaseSchemaTable(oldDatabase, newDatabase, func(schema model.Schema, oldTable, newTable model.TableSchema) []error {
1739
errors := make([]error, 0)
1840
for _, oldColumn := range oldTable.Columns {
1941
for _, newColumn := range newTable.Columns {
2042
if oldColumn.ColunmName == newColumn.ColunmName && oldColumn.ColumnType != newColumn.ColumnType {
21-
errors = append(errors, fmt.Errorf("colunm %s type %s of table %s.%s is incohérent in new type %s",
22-
oldColumn.ColunmName,
23-
oldColumn.ColumnType,
24-
schema.SchemaName,
25-
oldTable.TableName,
26-
newColumn.ColumnType))
43+
errors = append(errors, ChangedTypeRuleError{
44+
RuleError: RuleError{
45+
RuleCode: "DCT01",
46+
},
47+
OldColumn: oldColumn.ColunmName,
48+
OldColumnType: oldColumn.ColumnType,
49+
Schema: schema.SchemaName,
50+
Table: oldTable.TableName,
51+
NewColumnType: newColumn.ColumnType,
52+
})
2753
break
2854
}
2955
}

pkg/rules/changed_column_type_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestChangeColumnType(t *testing.T) {
5353
},
5454
},
5555
[]error{
56-
errors.New("colunm ID type INTEGER of table test.test is incohérent in new type VARCHAR2"),
56+
errors.New("[DCT01] colunm ID type INTEGER of table test.test is incohérent in new type VARCHAR2"),
5757
},
5858
},
5959
}

pkg/rules/deleted_column_table.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ func init() {
1212
RegisterRule("DC001", deletedColumnTableRule{})
1313
}
1414

15+
// DeletedColumnRuleError raised when a Column has been deleted
16+
type DeletedColumnRuleError struct {
17+
RuleError
18+
19+
Column string `json:"column_name"`
20+
Schema string `json:"schema_name"`
21+
Table string `json:"table_name"`
22+
}
23+
24+
func (r DeletedColumnRuleError) Error() string {
25+
return fmt.Sprintf("[%s] colunm %s of table %s.%s is absent in new schema",
26+
r.RuleCode,
27+
r.Column,
28+
r.Schema,
29+
r.Table,
30+
)
31+
}
32+
1533
func (deletedColumnTableRule) CheckCompatibility(oldDatabase, newDatabase *model.DatabaseSchema) []error {
1634
return checkDatabaseSchemaTable(oldDatabase, newDatabase, func(schema model.Schema, oldTable, newTable model.TableSchema) []error {
1735
errors := make([]error, 0)
@@ -24,10 +42,14 @@ func (deletedColumnTableRule) CheckCompatibility(oldDatabase, newDatabase *model
2442
}
2543
}
2644
if !colunmExist {
27-
errors = append(errors, fmt.Errorf("colunm %s of table %s.%s is absent in new schema",
28-
oldColumn.ColunmName,
29-
schema.SchemaName,
30-
oldTable.TableName))
45+
errors = append(errors, DeletedColumnRuleError{
46+
RuleError: RuleError{
47+
RuleCode: "DC001",
48+
},
49+
Column: oldColumn.ColunmName,
50+
Schema: schema.SchemaName,
51+
Table: oldTable.TableName,
52+
})
3153
}
3254
}
3355
return errors

pkg/rules/deleted_column_table_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestDeleteColumn(t *testing.T) {
5757
},
5858
},
5959
[]error{
60-
errors.New("colunm NAME of table test.test is absent in new schema"),
60+
errors.New("[DC001] colunm NAME of table test.test is absent in new schema"),
6161
},
6262
},
6363
}

pkg/rules/deleted_table.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ func init() {
1212
RegisterRule("DT001", deletedTableRule{})
1313
}
1414

15+
// DeletedTableRuleError raised when a Table has been deleted
16+
type DeletedTableRuleError struct {
17+
RuleError
18+
19+
Schema string `json:"schema_name"`
20+
Table string `json:"table_name"`
21+
}
22+
23+
func (r DeletedTableRuleError) Error() string {
24+
return fmt.Sprintf("[%s] table %s.%s is absent in new schema",
25+
r.RuleCode,
26+
r.Schema,
27+
r.Table,
28+
)
29+
}
30+
1531
func (deletedTableRule) CheckCompatibility(oldDatabase, newDatabase *model.DatabaseSchema) []error {
1632
return checkDatabaseSchemaSchema(oldDatabase, newDatabase, func(oldSchema, newSchema model.Schema) []error {
1733
errors := make([]error, 0)
@@ -24,9 +40,13 @@ func (deletedTableRule) CheckCompatibility(oldDatabase, newDatabase *model.Datab
2440
}
2541
}
2642
if !tableExist {
27-
errors = append(errors, fmt.Errorf("table %s.%s is absent in new schema",
28-
oldSchema.SchemaName,
29-
oldTable.TableName))
43+
errors = append(errors, DeletedTableRuleError{
44+
RuleError: RuleError{
45+
RuleCode: "DT001",
46+
},
47+
Schema: oldSchema.SchemaName,
48+
Table: oldTable.TableName,
49+
})
3050
}
3151
}
3252
return errors

pkg/rules/deleted_table_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestDeleteTable(t *testing.T) {
6262
},
6363
},
6464
[]error{
65-
errors.New("table test.test2 is absent in new schema"),
65+
errors.New("[DT001] table test.test2 is absent in new schema"),
6666
},
6767
},
6868
}

0 commit comments

Comments
 (0)