Skip to content

Commit

Permalink
Map MySQL tinyint(1) into boolean. Fixes #149
Browse files Browse the repository at this point in the history
  • Loading branch information
rmp135 committed Nov 22, 2024
1 parent 835c13b commit 6bf60a2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Adapters/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function getAllColumns(db: Knex, config: Config, table: string, schema: st
.map((c: MySQLColumn) => (
{
name: c.name,
type: c.type,
type: c.fullType == 'tinyint(1)' ? c.fullType : c.type, // tinyint(1) typically aliased as a boolean
nullable: c.isNullable == 'YES',
optional: c.isOptional === 1 || c.isNullable == 'YES',
columnType: c.type == 'enum' ? 'StringEnum' : 'Standard',
Expand Down
3 changes: 2 additions & 1 deletion src/TypeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
'string[]' : ['_char', '_varchar', '_bpchar', '_character_data']
},
mssql: {
string: ['timestamp']
string: ['timestamp'],
boolean: ['tinyint(1)']
}
}
34 changes: 32 additions & 2 deletions src/specs/Adapters/mysql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ beforeAll(async () => {
sqlContainer = await new MySqlContainer().start()

// Requires root user to create databases which act as schemas for our purposes
const connectionString = sqlContainer.getConnectionUri().replace('test:', 'root:')
const connectionString = sqlContainer.getConnectionUri().replace('test:', 'root:') + "?tinyInt1isBit=true"
const connectionDetails = new URL(connectionString)

const config = {
Expand All @@ -45,6 +45,9 @@ beforeAll(async () => {
CREATE TABLE schema_one.table_one (
id INT PRIMARY KEY AUTO_INCREMENT COMMENT 'schema_one.id comment',
name VARCHAR(255),
bool BOOLEAN,
smallNumber tinyint(3),
boolNumber tinyint(1),
enum_column ENUM('enum_one', 'enum_two')
) COMMENT 'schema_one.table_one comment';
`);
Expand Down Expand Up @@ -99,7 +102,7 @@ describe('getAllColumns', () => {
}
const columns = await mysql.getAllColumns(db, config, 'table_one', 'schema_one')

expect(columns.length).toEqual(3)
expect(columns.length).toEqual(6)
expect(columns).toEqual<ColumnDefinition[]>([{
name: 'id',
type: 'int',
Expand All @@ -118,6 +121,33 @@ describe('getAllColumns', () => {
columnType: 'Standard',
isPrimaryKey: false,
optional: true
}, {
name: 'bool',
type: 'tinyint(1)',
nullable: true,
defaultValue: null,
comment: '',
columnType: 'Standard',
isPrimaryKey: false,
optional: true
}, {
name: 'smallNumber',
type: 'tinyint',
nullable: true,
defaultValue: null,
comment: '',
columnType: 'Standard',
isPrimaryKey: false,
optional: true
}, {
name: 'boolNumber',
type: 'tinyint(1)',
nullable: true,
defaultValue: null,
comment: '',
columnType: 'Standard',
isPrimaryKey: false,
optional: true
}, {
name: 'enum_column',
type: 'enum',
Expand Down

0 comments on commit 6bf60a2

Please sign in to comment.