Skip to content

Commit

Permalink
refacor: schema create fun refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MXPOL committed Nov 22, 2023
1 parent ad8fb06 commit bfefb1e
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios'
import { InputField } from '@wix-velo/velo-external-db-types'
import { schemaUtils } from '@wix-velo/velo-external-db-core'
import { SystemFields } from '@wix-velo/velo-external-db-commons'


const axiosClient = axios.create({
Expand All @@ -10,9 +11,10 @@ const axiosClient = axios.create({
export const givenCollection = async(name: string, columns: InputField[], auth: any) => {
const collection = {
id: name,
fields: columns.map(schemaUtils.InputFieldToWixFormatField)
fields: [...SystemFields, ...columns].map(schemaUtils.InputFieldToWixFormatField)
}
await axiosClient.post('/collections/create', { collection }, auth)

await axiosClient.post('/collections/create', { collection, fields: SystemFields.map(schemaUtils.InputFieldToWixFormatField) }, auth)
}

export const deleteAllCollections = async(auth: any) => {
Expand Down
1 change: 1 addition & 0 deletions apps/velo-external-db/test/e2e/app_data_hooks.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as data from '../drivers/data_api_rest_test_support'
import hooks = require('../drivers/hooks_test_support')
import * as matchers from '../drivers/schema_api_rest_matchers'
import each from 'jest-each'
import { SystemFields } from '@wix-velo/velo-external-db-commons'

const { Aggregate, UpdateImmediately, DeleteImmediately } = SchemaOperations

Expand Down
13 changes: 2 additions & 11 deletions apps/velo-external-db/test/e2e/app_schema.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,13 @@ describe(`Schema REST API: ${currentDbImplementationName()}`, () => {
})

test('collection create - collection without fields', async() => {
const collection = {
id: ctx.collectionName,
fields: []
}
await axiosClient.post('/collections/create', { collection }, authOwner)
await schema.givenCollection(ctx.collectionName, [], authOwner)

await expect(schema.retrieveSchemaFor(ctx.collectionName, authOwner)).resolves.toEqual(matchers.createCollectionResponseWith(ctx.collectionName, [...SystemFields], env.capabilities))
})

test('collection create - collection with fields', async() => {
const collection = {
id: ctx.collectionName,
fields: [ctx.column].map(schemaUtils.InputFieldToWixFormatField)
}

await axiosClient.post('/collections/create', { collection }, authOwner)
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)

await expect(schema.retrieveSchemaFor(ctx.collectionName, authOwner)).resolves.toEqual(matchers.createCollectionResponseWith(ctx.collectionName, [...SystemFields, ctx.column], env.capabilities))
})
Expand Down
6 changes: 3 additions & 3 deletions apps/velo-external-db/test/e2e/app_schema_hooks.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe(`Velo External DB Schema Hooks: ${currentDbImplementationName()}`, () =
}
}
})
await axiosClient.post('/collections/create', { collection: { id: 'wrong', fields: [] } }, authOwner)
await axiosClient.post('/collections/create', { collection: { id: 'wrong', fields: schemaUtils.InputFieldsToWixFormatFields(SystemFields) } }, authOwner)

await expect(schema.retrieveSchemaFor(ctx.collectionId, authOwner)).resolves.toEqual(matchers.createCollectionResponseWith(ctx.collectionId, [...SystemFields], env.capabilities))
})
Expand Down Expand Up @@ -203,7 +203,7 @@ describe(`Velo External DB Schema Hooks: ${currentDbImplementationName()}`, () =
})
describe('Write operations', () => {
each([
['create', 'afterCreate', '/collections/create', []],
['create', 'afterCreate', '/collections/create', SystemFields],
['update', 'afterUpdate', '/collections/update', SystemFields],
['delete', 'afterDelete', '/collections/delete', []]
]).test('after %s collection request - should be able to modify the response (collection)', async(operation, hookName, api, fields) => {
Expand Down Expand Up @@ -295,7 +295,7 @@ describe(`Velo External DB Schema Hooks: ${currentDbImplementationName()}`, () =
describe('Custom context, Service context', () => {
each([
['get', 'Read', 'beforeGet', 'afterGet', '/collections/get', []],
['create', 'Write', 'beforeCreate', 'afterCreate', '/collections/create', []],
['create', 'Write', 'beforeCreate', 'afterCreate', '/collections/create', SystemFields],
['update', 'Write', 'beforeUpdate', 'afterUpdate', '/collections/update', SystemFields],
['delete', 'Write', 'beforeDelete', 'afterDelete', '/collections/delete', []]
]).test('%s - should be able to modify custom context from each hook, and use service context', async(operation, operationType, beforeHook, afterHook, api, fields) => {
Expand Down
4 changes: 2 additions & 2 deletions apps/velo-external-db/test/gen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { SystemFields } from '@wix-velo/velo-external-db-commons'
import { collectionSpi } from '@wix-velo/velo-external-db-core'
import { collectionSpi, schemaUtils } from '@wix-velo/velo-external-db-core'
import { InputField } from '@wix-velo/velo-external-db-types'
import * as Chance from 'chance'

Expand Down Expand Up @@ -110,7 +110,7 @@ export const randomMatchesValueWithDashes = () => {
export const randomCollection = (): collectionSpi.Collection => {
return {
id: randomCollectionName(),
fields: [],
fields: schemaUtils.InputFieldsToWixFormatFields(SystemFields),
pagingMode: collectionSpi.PagingMode.offset
}
}
13 changes: 7 additions & 6 deletions apps/velo-external-db/test/storage/data_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Chance = require('chance')
import gen = require('../gen')
import { env, dbTeardown, setupDb, currentDbImplementationName, supportedOperations } from '../resources/provider_resources'
import { entitiesWithOwnerFieldOnly, entityWithObjectField } from '../drivers/data_provider_matchers'
import { SystemFields } from '@wix-velo/velo-external-db-commons'
const chance = new Chance()

describe(`Data API: ${currentDbImplementationName()}`, () => {
Expand Down Expand Up @@ -138,7 +139,7 @@ describe(`Data API: ${currentDbImplementationName()}`, () => {
})

test('insert entity with number', async() => {
await env.schemaProvider.create(ctx.numericCollectionName, ctx.numericColumns)
await env.schemaProvider.create(ctx.numericCollectionName, [...SystemFields, ...ctx.numericColumns])
env.driver.stubEmptyFilterAndSortFor({}, '')
env.driver.givenAllFieldsProjectionFor?.(ctx.projection)

Expand All @@ -148,7 +149,7 @@ describe(`Data API: ${currentDbImplementationName()}`, () => {
})

testIfSupportedOperationsIncludes(supportedOperations, [ FindObject ])('insert entity with object', async() => {
await env.schemaProvider.create(ctx.objectCollectionName, [ctx.objectColumn])
await env.schemaProvider.create(ctx.objectCollectionName, [...SystemFields, ctx.objectColumn])
env.driver.stubEmptyFilterAndSortFor({}, '')
env.driver.givenAllFieldsProjectionFor?.(ctx.projection)

Expand Down Expand Up @@ -215,7 +216,7 @@ describe(`Data API: ${currentDbImplementationName()}`, () => {
})

testIfSupportedOperationsIncludes(supportedOperations, [ Aggregate ])('aggregate api without filter', async() => {
await env.schemaProvider.create(ctx.numericCollectionName, ctx.numericColumns)
await env.schemaProvider.create(ctx.numericCollectionName, [...SystemFields, ...ctx.numericColumns])
await givenCollectionWith([ctx.numberEntity, ctx.anotherNumberEntity], ctx.numericCollectionName, ctx.numberEntityFields)

env.driver.stubEmptyFilterFor(ctx.filter)
Expand All @@ -228,7 +229,7 @@ describe(`Data API: ${currentDbImplementationName()}`, () => {
})

testIfSupportedOperationsIncludes(supportedOperations, [ Aggregate ])('aggregate api without having', async() => {
await env.schemaProvider.create(ctx.numericCollectionName, ctx.numericColumns)
await env.schemaProvider.create(ctx.numericCollectionName, [...SystemFields, ...ctx.numericColumns])
await givenCollectionWith([ctx.numberEntity, ctx.anotherNumberEntity], ctx.numericCollectionName, ctx.numberEntityFields)

env.driver.stubEmptyFilterFor(ctx.filter)
Expand All @@ -241,7 +242,7 @@ describe(`Data API: ${currentDbImplementationName()}`, () => {
})

testIfSupportedOperationsIncludes(supportedOperations, [ Aggregate ])('aggregate api with filter', async() => {
await env.schemaProvider.create(ctx.numericCollectionName, ctx.numericColumns)
await env.schemaProvider.create(ctx.numericCollectionName, [...SystemFields, ...ctx.numericColumns])
await givenCollectionWith([ctx.numberEntity, ctx.anotherNumberEntity], ctx.numericCollectionName, ctx.numberEntityFields)

env.driver.givenFilterByIdWith(ctx.numberEntity._id, ctx.filter)
Expand Down Expand Up @@ -307,6 +308,6 @@ describe(`Data API: ${currentDbImplementationName()}`, () => {
ctx.anotherNumberEntity = gen.randomNumberDbEntity(ctx.numericColumns)
ctx.matchesEntity = { ...ctx.entity, [ctx.column.name]: gen.randomMatchesValueWithDashes() }

await env.schemaProvider.create(ctx.collectionName, [ctx.column])
await env.schemaProvider.create(ctx.collectionName, [...SystemFields, ctx.column])
})
})
31 changes: 16 additions & 15 deletions apps/velo-external-db/test/storage/schema_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SchemaOperations } from '@wix-velo/velo-external-db-types'
import { Uninitialized, gen, testIfSupportedOperationsIncludes } from '@wix-velo/test-commons'
import { env, dbTeardown, setupDb, currentDbImplementationName, supportedOperations } from '../resources/provider_resources'
import { toContainDefaultFields, collectionToContainFields, toBeDefaultCollectionWith, hasSameSchemaFieldsLike } from '../drivers/schema_provider_matchers'

const chance = new Chance()
const { CollectionDoesNotExists, FieldAlreadyExists, CannotModifySystemField, FieldDoesNotExist } = errors
const { RemoveColumn } = SchemaOperations
Expand All @@ -23,7 +24,7 @@ describe(`Schema API: ${currentDbImplementationName()}`, () => {
})

test('list headers will result with an array of collection names', async() => {
await env.schemaProvider.create(ctx.collectionName)
await env.schemaProvider.create(ctx.collectionName, SystemFields)

await expect( env.schemaProvider.listHeaders() ).resolves.toEqual([ctx.collectionName])
})
Expand All @@ -33,8 +34,8 @@ describe(`Schema API: ${currentDbImplementationName()}`, () => {
})

test('list db will result with a list of wix databases', async() => {
await env.schemaProvider.create(ctx.collectionName)
await env.schemaProvider.create(ctx.anotherCollectionName)
await env.schemaProvider.create(ctx.collectionName, SystemFields)
await env.schemaProvider.create(ctx.anotherCollectionName, SystemFields)

await expect( env.schemaProvider.list() ).resolves.toEqual(expect.arrayContaining([
expect.objectContaining({
Expand All @@ -49,57 +50,57 @@ describe(`Schema API: ${currentDbImplementationName()}`, () => {
})

test('create collection with default columns', async() => {
await env.schemaProvider.create(ctx.collectionName)
await env.schemaProvider.create(ctx.collectionName, SystemFields)

await expect( env.schemaProvider.describeCollection(ctx.collectionName) ).resolves.toEqual(toBeDefaultCollectionWith(ctx.collectionName, env.capabilities))
})

test('drop collection', async() => {
await env.schemaProvider.create(ctx.collectionName)
await env.schemaProvider.create(ctx.collectionName, SystemFields)

await env.schemaProvider.drop(ctx.collectionName)

await expect(env.schemaProvider.describeCollection(ctx.collectionName)).rejects.toThrow(CollectionDoesNotExists)
})

test('collection name and variables are case sensitive', async() => {
await env.schemaProvider.create(ctx.collectionName.toUpperCase())
await env.schemaProvider.create(ctx.collectionName.toUpperCase(), SystemFields)

await expect( env.schemaProvider.describeCollection(ctx.collectionName.toUpperCase()) ).resolves.toEqual(toBeDefaultCollectionWith(ctx.collectionName.toUpperCase(), env.capabilities))
})

test('retrieve collection data by collection name', async() => {
await env.schemaProvider.create(ctx.collectionName)
await env.schemaProvider.create(ctx.collectionName, SystemFields)

await expect( env.schemaProvider.describeCollection(ctx.collectionName) ).resolves.toEqual(toBeDefaultCollectionWith(ctx.collectionName, env.capabilities))
})

test('create collection twice will do nothing', async() => {
await env.schemaProvider.create(ctx.collectionName, [])
await env.schemaProvider.create(ctx.collectionName, SystemFields)

await expect( env.schemaProvider.create(ctx.collectionName, []) ).resolves.toBeUndefined()
await expect( env.schemaProvider.create(ctx.collectionName, SystemFields) ).resolves.toBeUndefined()
})

test('add column on a non existing collection will fail', async() => {
await expect(env.schemaProvider.addColumn(ctx.collectionName, { name: ctx.columnName, type: 'datetime', subtype: 'timestamp' })).rejects.toThrow(CollectionDoesNotExists)
})

test('add column on a an existing collection', async() => {
await env.schemaProvider.create(ctx.collectionName, [])
await env.schemaProvider.create(ctx.collectionName, SystemFields)
await env.schemaProvider.addColumn(ctx.collectionName, { name: ctx.columnName, type: 'datetime', subtype: 'timestamp' })
await expect( env.schemaProvider.describeCollection(ctx.collectionName) ).resolves.toEqual(collectionToContainFields(ctx.collectionName, [{ field: ctx.columnName, type: 'datetime' }], env.capabilities))
})

test('add duplicate column will fail', async() => {
await env.schemaProvider.create(ctx.collectionName, [])
await env.schemaProvider.create(ctx.collectionName, SystemFields)

await env.schemaProvider.addColumn(ctx.collectionName, { name: ctx.columnName, type: 'datetime', subtype: 'timestamp' })

await expect(env.schemaProvider.addColumn(ctx.collectionName, { name: ctx.columnName, type: 'datetime', subtype: 'timestamp' })).rejects.toThrow(FieldAlreadyExists)
})

test('add system column will fail', async() => {
await env.schemaProvider.create(ctx.collectionName, [])
await env.schemaProvider.create(ctx.collectionName, SystemFields)

SystemFields.map(f => f.name)
.forEach(async f => {
Expand All @@ -108,20 +109,20 @@ describe(`Schema API: ${currentDbImplementationName()}`, () => {
})

testIfSupportedOperationsIncludes(supportedOperations, [ RemoveColumn ])('drop column on a an existing collection', async() => {
await env.schemaProvider.create(ctx.collectionName, [])
await env.schemaProvider.create(ctx.collectionName, SystemFields)
await env.schemaProvider.addColumn(ctx.collectionName, { name: ctx.columnName, type: 'datetime', subtype: 'timestamp' })
await env.schemaProvider.removeColumn(ctx.collectionName, ctx.columnName)
await expect(env.schemaProvider.describeCollection(ctx.collectionName)).resolves.not.toEqual(hasSameSchemaFieldsLike([{ field: ctx.columnName, type: 'datetime' }]))
})


testIfSupportedOperationsIncludes(supportedOperations, [ RemoveColumn ])('drop column on a a non existing collection', async() => {
await env.schemaProvider.create(ctx.collectionName, [])
await env.schemaProvider.create(ctx.collectionName, SystemFields)
await expect(env.schemaProvider.removeColumn(ctx.collectionName, ctx.columnName)).rejects.toThrow(FieldDoesNotExist)
})

testIfSupportedOperationsIncludes(supportedOperations, [ RemoveColumn ])('drop system column will fail', async() => {
await env.schemaProvider.create(ctx.collectionName, [])
await env.schemaProvider.create(ctx.collectionName, SystemFields)
SystemFields.map(f => f.name)
.forEach(async f => {
await expect(env.schemaProvider.removeColumn(ctx.collectionName, f)).rejects.toThrow(CannotModifySystemField)
Expand Down
4 changes: 2 additions & 2 deletions libs/external-db-bigquery/src/bigquery_schema_provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dataset } from '@google-cloud/bigquery'
import { SystemFields, validateSystemFields, parseTableData, errors, EmptyCapabilities } from '@wix-velo/velo-external-db-commons'
import { validateSystemFields, parseTableData, errors, EmptyCapabilities } from '@wix-velo/velo-external-db-commons'
import { InputField, ISchemaProvider, ResponseField, Table, SchemaOperations, CollectionCapabilities, Encryption, PagingMode } from '@wix-velo/velo-external-db-types'
import { translateErrorCodes, createCollectionTranslateErrorCodes, addColumnTranslateErrorCodes, removeColumnTranslateErrorCodes } from './sql_exception_translator'
import { CollectionOperations, FieldTypes, ReadOnlyOperations, ReadWriteOperations, ColumnsCapabilities } from './bigquery_capabilities'
Expand Down Expand Up @@ -44,7 +44,7 @@ export default class SchemaProvider implements ISchemaProvider {

async create(collectionName: string, _columns: InputField[]) {
const columns = _columns || []
const dbColumnsSql = [...SystemFields, ...columns].map(c => this.sqlSchemaTranslator.columnToDbColumnSql(c, { escapeId: false, precision: false }))
const dbColumnsSql = columns.map(c => this.sqlSchemaTranslator.columnToDbColumnSql(c, { escapeId: false, precision: false }))
await this.pool.createTable(collectionName, { schema: dbColumnsSql })
.catch(createCollectionTranslateErrorCodes)
}
Expand Down
2 changes: 1 addition & 1 deletion libs/external-db-mssql/src/mssql_schema_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class SchemaProvider implements ISchemaProvider {
}

async create(collectionName: string, columns: InputField[]): Promise<void> {
const dbColumnsSql = [...SystemFields, ...(columns || [])].map( c => this.sqlSchemaTranslator.columnToDbColumnSql(c) )
const dbColumnsSql = (columns || []).map( c => this.sqlSchemaTranslator.columnToDbColumnSql(c) )
.join(', ')
const primaryKeySql = SystemFields.filter(f => f.isPrimary).map(f => escapeId(f.name)).join(', ')

Expand Down
2 changes: 1 addition & 1 deletion libs/external-db-mssql/src/sql_schema_translator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('Sql Schema Column Translator', () => {

describe('string fields', () => {
test('string', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string' }) ).toEqual(`${escapeId(ctx.fieldName)} VARCHAR(2048)`)
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string' }) ).toEqual(`${escapeId(ctx.fieldName)} VARCHAR(4096)`)
})

test('string with length', () => {
Expand Down
4 changes: 2 additions & 2 deletions libs/external-db-mssql/src/sql_schema_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ export default class SchemaColumnTranslator {
try {
const parsed = parseInt(length as string)
if (isNaN(parsed) || parsed <= 0) {
return '(2048)'
return '(4096)'
}
return `(${parsed})`
} catch (e) {
return '(2048)'
return '(4096)'
}
}

Expand Down
Loading

0 comments on commit bfefb1e

Please sign in to comment.