Skip to content

Commit

Permalink
Refactor Postgres schema column translation
Browse files Browse the repository at this point in the history
  • Loading branch information
MXPOL committed Dec 5, 2023
1 parent cc2d4eb commit 9d769bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
50 changes: 30 additions & 20 deletions libs/external-db-bigquery/src/sql_schema_translator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,17 @@ describe('Sql Schema Column Translator', () => {
})

describe('string fields', () => {
test('string', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string' }) ).toEqual({ mode: '', name: escapeId(ctx.fieldName), type: 'STRING' })
})
test.each([
'string',
'richcontent',
'image',
'video',
'audio',
'document',
'language',
])('%s', (subtype) => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype }) ).toEqual({ mode: '', name: escapeId(ctx.fieldName), type: 'STRING' })
})

test('string with length', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string', precision: '50' }) ).toEqual({ mode: '', name: escapeId(ctx.fieldName), type: 'STRING(50)' })
Expand Down Expand Up @@ -108,20 +116,15 @@ describe('Sql Schema Column Translator', () => {

describe('JSON fields', () => {
test.each([
['object'],
['image'],
['document'],
['video'],
['audio'],
['any'],
['mediaGallery'],
['address'],
['pageLink'],
['reference'],
['multiReference'],
['arrayString'],
['arrayDocument'],
['richContent'],
'object',
'any',
'mediaGallery',
'address',
'pageLink',
'reference',
'multiReference',
'arrayDocument',
'arrayString',
])('%s', (subtype) => {
expect(env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'object', subtype })).toEqual({ mode: '', name: escapeId(ctx.fieldName), type: 'JSON' })
})
Expand Down Expand Up @@ -154,10 +157,17 @@ describe('Sql Schema Column Translator', () => {
})

describe('date time fields', () => {

test.each(['DATETIME', 'TIMESTAMP'])('%t', (t) => {
expect( env.schemaTranslator.translateType(t) ).toEqual('datetime')
})

test('time', () => {
expect( env.schemaTranslator.translateType('TIME') ).toEqual('time')
})

test('date', () => {
['DATE', 'DATETIME', 'TIME', 'TIMESTAMP'].forEach(t => {
expect( env.schemaTranslator.translateType(t) ).toEqual('datetime')
})
expect( env.schemaTranslator.translateType('DATE') ).toEqual('date')
})
})

Expand Down
18 changes: 11 additions & 7 deletions libs/external-db-bigquery/src/sql_schema_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ export default class SchemaColumnTranslator {
case 'bigdecimal':
return 'number'

case 'timestamp':
case 'datetime':
case 'time':
return 'time'
case 'date':
return 'date'

case 'timestamp':
case 'datetime':
return 'datetime'

case 'string':
Expand Down Expand Up @@ -90,6 +93,11 @@ export default class SchemaColumnTranslator {
case 'text_medium':
case 'text_large':
case 'text_language':
case 'text_richcontent':
case 'text_image':
case 'text_video':
case 'text_audio':
case 'text_document':
return 'STRING'

case 'boolean_':
Expand All @@ -98,19 +106,15 @@ export default class SchemaColumnTranslator {

case 'object_':
case 'object_object':
case 'object_image':
case 'object_document':
case 'object_video':
case 'object_any':
case 'object_audio':
case 'object_mediagallery':
case 'object_address':
case 'object_pagelink':
case 'object_reference':
case 'object_multireference':
case 'object_arraystring':
case 'object_arraydocument':
case 'object_richcontent':
case 'object_array':
return 'JSON'

default:
Expand Down

0 comments on commit 9d769bc

Please sign in to comment.