Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions dataLayer/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const { transformReq } = require('../utility/common_utils.js');
const { server } = require('../server/Server.ts');
const { cleanupOrphans } = require('../resources/blob.ts');

function rejectLegacySchemaField(obj) {
if (obj.schema && !obj.database) {
throw new ClientError("'database' is required. Use 'database' instead of the deprecated 'schema' field.");
}
}

const DB_NAME_CONSTRAINTS = Joi.string()
.min(1)
.max(commonValidators.schema_length.maximum)
Expand Down Expand Up @@ -75,7 +81,7 @@ async function createSchemaStructure(schemaCreateObject) {
})
);
if (validation) throw new ClientError(validation.message);

rejectLegacySchemaField(schemaCreateObject);
transformReq(schemaCreateObject);

if (!(await schemaMetadataValidator.checkSchemaExists(schemaCreateObject.schema))) {
Expand All @@ -95,6 +101,7 @@ async function createSchemaStructure(schemaCreateObject) {
}

async function createTable(createTableObject) {
rejectLegacySchemaField(createTableObject);
transformReq(createTableObject);
createTableObject.primary_key = createTableObject.primary_key ?? createTableObject.hash_attribute;
return await createTableStructure(createTableObject);
Expand Down Expand Up @@ -166,7 +173,7 @@ async function dropSchema(dropSchemaObject) {
})
);
if (validation) throw new ClientError(validation.message);

rejectLegacySchemaField(dropSchemaObject);
transformReq(dropSchemaObject);

let invalidSchemaMsg = await schemaMetadataValidator.checkSchemaExists(dropSchemaObject.schema);
Expand Down Expand Up @@ -199,7 +206,7 @@ async function dropTable(dropTableObject) {
})
);
if (validation) throw new ClientError(validation.message);

rejectLegacySchemaField(dropTableObject);
transformReq(dropTableObject);

let invalidSchemaTableMsg = await schemaMetadataValidator.checkSchemaTableExists(
Expand Down Expand Up @@ -242,7 +249,7 @@ async function dropAttribute(dropAttributeObject) {
})
);
if (validation) throw new ClientError(validation.message);

rejectLegacySchemaField(dropAttributeObject);
transformReq(dropAttributeObject);

let invalidSchemaTableMsg = await schemaMetadataValidator.checkSchemaTableExists(
Expand Down Expand Up @@ -322,6 +329,7 @@ function dropAttributeFromGlobal(dropAttributeObject) {
}

async function createAttribute(createAttributeObject) {
rejectLegacySchemaField(createAttributeObject);
transformReq(createAttributeObject);

const tableAttr = getDatabases()[createAttributeObject.schema][createAttributeObject.table].attributes;
Expand Down
50 changes: 50 additions & 0 deletions unitTests/dataLayer/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,53 @@ describe.skip('Test schema module', function () {
});
});
});

describe('Schema legacy field rejection', function () {
const LEGACY_ERROR_MSG = "Use 'database' instead of the deprecated 'schema' field";

async function expectLegacyRejection(fn, args) {
let error;
try {
await fn(...args);
} catch (err) {
error = err;
}
expect(error).to.be.instanceOf(Error);
expect(error.statusCode).to.equal(400);
expect(error.message).to.include(LEGACY_ERROR_MSG);
}

it('rejects dropTable with schema-only field', () =>
expectLegacyRejection(schema.dropTable, [{ operation: 'drop_table', schema: 'mydb', table: 'mytable' }]));

it('rejects dropSchema with schema-only field', () =>
expectLegacyRejection(schema.dropSchema, [{ operation: 'drop_schema', schema: 'mydb' }]));

it('rejects createSchemaStructure with schema-only field', () =>
expectLegacyRejection(schema.createSchemaStructure, [{ operation: 'create_schema', schema: 'mydb' }]));

it('rejects createTable with schema-only field', () =>
expectLegacyRejection(schema.createTable, [
{ operation: 'create_table', schema: 'mydb', table: 'mytable', primary_key: 'id' },
]));

it('rejects dropAttribute with schema-only field', () =>
expectLegacyRejection(schema.dropAttribute, [
{ operation: 'drop_attribute', schema: 'mydb', table: 'mytable', attribute: 'name' },
]));

it('rejects createAttribute with schema-only field', () =>
expectLegacyRejection(schema.createAttribute, [
{ operation: 'create_attribute', schema: 'mydb', table: 'mytable', attribute: 'name' },
]));

it('does not reject dropTable when both database and schema are set', async function () {
let error;
try {
await schema.dropTable({ operation: 'drop_table', database: 'mydb', schema: 'mydb', table: 'mytable' });
} catch (err) {
error = err;
}
expect(error?.message).to.not.include(LEGACY_ERROR_MSG);
});
});
Loading