Skip to content

Commit

Permalink
fix: set column canNull default to false (#195)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Enhanced SQL query logging for table creation to aid in debugging.

- **Bug Fixes**
- Adjusted default `canNull` behavior in column definitions to enhance
data integrity.
- Updated various column definitions to include `NOT NULL` constraints,
ensuring stricter data validation.

- **Refactor**
	- Removed unused code and comments to clean up the codebase.
- Improved flexibility in the data access layer by allowing reassignment
of a key property.

- **Tests**
- Added and modified tests to reflect changes in SQL generation logic
and column nullability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu committed Mar 22, 2024
1 parent 1c7d145 commit 24628ec
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 123 deletions.
3 changes: 1 addition & 2 deletions core/dal-decorator/src/model/ColumnModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export class ColumnModel {

static build(property: string, type: ColumnTypeParams, params?: ColumnParams) {
const columnName = params?.name ?? snakecase(property);
// TODO can null default should be false
let canNull = params?.canNull ?? true;
let canNull = params?.canNull ?? false;
if (params?.primaryKey) {
canNull = false;
}
Expand Down
1 change: 0 additions & 1 deletion core/dal-decorator/src/model/TableModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export class TableModel<T=object> {
const name = params?.name ?? snakecase(pluralize(clazz.name));
const columnInfoMap = ColumnInfoUtil.getColumnInfoMap(clazz as EggProtoImplClass);
const columnTypeMap = ColumnInfoUtil.getColumnTypeMap(clazz as EggProtoImplClass);
// TODO set to default name
const dataSourceName = params?.dataSourceName ?? 'default';
assert(TableInfoUtil.getIsTable(clazz as EggProtoImplClass), `${name} is not Table`);
assert(columnTypeMap, `${name} has no columns`);
Expand Down
1 change: 1 addition & 0 deletions core/dal-runtime/test/DataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('test/Datasource.test.ts', () => {

const sqlGenerator = new SqlGenerator();
const createTableSql = sqlGenerator.generate(tableModel);
console.log('create table: ', createTableSql);

await mysql.query(createTableSql);

Expand Down
76 changes: 38 additions & 38 deletions core/dal-runtime/test/SqlGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,45 @@ describe('test/SqlGenerator.test.ts', () => {
const sql = generator.generate(fooModel);
assert.equal(sql, 'CREATE TABLE IF NOT EXISTS egg_foo (\n' +
' id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \'the primary key\',\n' +
' name VARCHAR(100) NULL UNIQUE KEY,\n' +
' col1 VARCHAR(100) NULL,\n' +
' bit_column BIT(10) NULL,\n' +
' bool_column BOOL NULL,\n' +
' tiny_int_column TINYINT(5) UNSIGNED ZEROFILL NULL,\n' +
' small_int_column SMALLINT(5) UNSIGNED ZEROFILL NULL,\n' +
' medium_int_column MEDIUMINT(5) UNSIGNED ZEROFILL NULL,\n' +
' int_column INT(5) UNSIGNED ZEROFILL NULL,\n' +
' big_int_column BIGINT(5) UNSIGNED ZEROFILL NULL,\n' +
' decimal_column DECIMAL(10,5) UNSIGNED ZEROFILL NULL,\n' +
' float_column FLOAT(10,5) UNSIGNED ZEROFILL NULL,\n' +
' double_column DOUBLE(10,5) UNSIGNED ZEROFILL NULL,\n' +
' date_column DATE NULL,\n' +
' date_time_column DATETIME(3) NULL,\n' +
' name VARCHAR(100) NOT NULL UNIQUE KEY,\n' +
' col1 VARCHAR(100) NOT NULL,\n' +
' bit_column BIT(10) NOT NULL,\n' +
' bool_column BOOL NOT NULL,\n' +
' tiny_int_column TINYINT(5) UNSIGNED ZEROFILL NOT NULL,\n' +
' small_int_column SMALLINT(5) UNSIGNED ZEROFILL NOT NULL,\n' +
' medium_int_column MEDIUMINT(5) UNSIGNED ZEROFILL NOT NULL,\n' +
' int_column INT(5) UNSIGNED ZEROFILL NOT NULL,\n' +
' big_int_column BIGINT(5) UNSIGNED ZEROFILL NOT NULL,\n' +
' decimal_column DECIMAL(10,5) UNSIGNED ZEROFILL NOT NULL,\n' +
' float_column FLOAT(10,5) UNSIGNED ZEROFILL NOT NULL,\n' +
' double_column DOUBLE(10,5) UNSIGNED ZEROFILL NOT NULL,\n' +
' date_column DATE NOT NULL,\n' +
' date_time_column DATETIME(3) NOT NULL,\n' +
' timestamp_column TIMESTAMP(3) NULL,\n' +
' time_column TIME(3) NULL,\n' +
' year_column YEAR NULL,\n' +
' var_char_column VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n' +
' binary_column BINARY NULL,\n' +
' var_binary_column VARBINARY(100) NULL,\n' +
' tiny_blob_column TINYBLOB NULL,\n' +
' tiny_text_column TINYTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n' +
' blob_column BLOB(100) NULL,\n' +
' text_column TEXT(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n' +
' medium_blob_column MEDIUMBLOB NULL,\n' +
' long_blob_column LONGBLOB NULL,\n' +
' medium_text_column MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n' +
' long_text_column LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n' +
' enum_column ENUM(\'A\',\'B\') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n' +
' set_column SET(\'A\',\'B\') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n' +
' geometry_column GEOMETRY NULL,\n' +
' point_column POINT NULL,\n' +
' line_string_column LINESTRING NULL,\n' +
' polygon_column POLYGON NULL,\n' +
' multipoint_column MULTIPOINT NULL,\n' +
' multi_line_string_column MULTILINESTRING NULL,\n' +
' multi_polygon_column MULTIPOLYGON NULL,\n' +
' geometry_collection_column GEOMETRYCOLLECTION NULL,\n' +
' json_column JSON NULL,\n' +
' time_column TIME(3) NOT NULL,\n' +
' year_column YEAR NOT NULL,\n' +
' var_char_column VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,\n' +
' binary_column BINARY NOT NULL,\n' +
' var_binary_column VARBINARY(100) NOT NULL,\n' +
' tiny_blob_column TINYBLOB NOT NULL,\n' +
' tiny_text_column TINYTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,\n' +
' blob_column BLOB(100) NOT NULL,\n' +
' text_column TEXT(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,\n' +
' medium_blob_column MEDIUMBLOB NOT NULL,\n' +
' long_blob_column LONGBLOB NOT NULL,\n' +
' medium_text_column MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,\n' +
' long_text_column LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,\n' +
' enum_column ENUM(\'A\',\'B\') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,\n' +
' set_column SET(\'A\',\'B\') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,\n' +
' geometry_column GEOMETRY NOT NULL,\n' +
' point_column POINT NOT NULL,\n' +
' line_string_column LINESTRING NOT NULL,\n' +
' polygon_column POLYGON NOT NULL,\n' +
' multipoint_column MULTIPOINT NOT NULL,\n' +
' multi_line_string_column MULTILINESTRING NOT NULL,\n' +
' multi_polygon_column MULTIPOLYGON NOT NULL,\n' +
' geometry_collection_column GEOMETRYCOLLECTION NOT NULL,\n' +
' json_column JSON NOT NULL,\n' +
' FULLTEXT KEY idx_col1 (col1) COMMENT \'index comment\\n\',\n' +
' UNIQUE KEY uk_name_col1 (name,col1) USING BTREE COMMENT \'index comment\\n\'\n' +
') DEFAULT CHARACTER SET utf8mb4, DEFAULT COLLATE utf8mb4_unicode_ci, COMMENT=\'foo table\';');
Expand Down
2 changes: 2 additions & 0 deletions core/dal-runtime/test/fixtures/modules/dal/Foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export class Foo {
@Column({
type: ColumnType.TIMESTAMP,
precision: 3,
}, {
canNull: true,
})
timestampColumn: Date;

Expand Down
Loading

0 comments on commit 24628ec

Please sign in to comment.