Skip to content

Commit

Permalink
add drop column test
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 19, 2024
1 parent ccf8004 commit b4134bd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 22 deletions.
9 changes: 9 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface QueryBuilderInternal {
// ------------------------------------------
// This is use for alter or create schema
// ------------------------------------------
dropColumn?: string;
columns: {
name: string;
definition?: TableColumnDefinition; // For alter or create column
Expand Down Expand Up @@ -345,6 +346,12 @@ class QueryBuilderAlterTable extends IQueryBuilder {
return this;
}

dropColumn(name: string) {
this.state.action = QueryBuilderAction.DROP_COLUMNS;
this.state.dropColumn = name;
return this;
}

renameTable(newTableName: string) {
this.state.action = QueryBuilderAction.RENAME_TABLE;
this.state.newTableName = newTableName;
Expand Down Expand Up @@ -463,6 +470,8 @@ function buildQueryString(
return dialect.alterColumn(queryBuilder);
case QueryBuilderAction.RENAME_COLUMNS:
return dialect.renameColumn(queryBuilder);
case QueryBuilderAction.DROP_COLUMNS:
return dialect.dropColumn(queryBuilder);
default:
throw new Error('Invalid action');
}
Expand Down
6 changes: 6 additions & 0 deletions src/connections/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@ export abstract class Connection {
columnName: string,
defintion: TableColumnDefinition
): Promise<QueryResult>;

abstract dropColumn(
schemaName: string | undefined,
tableName: string,
columnName: string
): Promise<QueryResult>;
}
4 changes: 4 additions & 0 deletions src/connections/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ export class MongoDBConnection implements Connection {
return createOkResult();
}

async dropColumn(): Promise<QueryResult> {
return createOkResult();
}

async select(
schemaName: string,
tableName: string,
Expand Down
17 changes: 17 additions & 0 deletions src/connections/sql-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ export abstract class SqlConnection extends Connection {
);
}

async dropColumn(
schemaName: string | undefined,
tableName: string,
columnName: string
): Promise<QueryResult> {
const qb = Outerbase(this);

return await this.query(
qb
.alterTable(
schemaName ? `${schemaName}.${tableName}` : tableName
)
.dropColumn(columnName)
.toQuery()
);
}

async testConnection(): Promise<boolean> {
try {
await this.connect();
Expand Down
21 changes: 21 additions & 0 deletions src/query-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface Dialect {
renameTable(builder: QueryBuilderInternal): Query;
renameColumn(builder: QueryBuilderInternal): Query;
alterColumn(builder: QueryBuilderInternal): Query;
dropColumn(builder: QueryBuilderInternal): Query;
addColumn(builder: QueryBuilderInternal): Query;
}

Expand Down Expand Up @@ -394,6 +395,26 @@ export abstract class AbstractDialect implements Dialect {
};
}

dropColumn(builder: QueryBuilderInternal): Query {
const tableName = builder.table;
const columnName = builder.dropColumn;

if (!tableName) {
throw new Error(
'Table name is required to build a DROP COLUMN query.'
);
}

if (!columnName) {
throw new Error('Column name is required to drop.');
}

return {
query: `ALTER TABLE ${this.escapeId(tableName)} DROP COLUMN ${this.escapeId(columnName)}`,
parameters: [],
};
}

renameColumn(builder: QueryBuilderInternal): Query {
const tableName = builder.table;

Expand Down
48 changes: 26 additions & 22 deletions tests/connections/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ describe('Database Connection', () => {
]);
});

test('Add table column', async () => {
test('Add and drop table column', async () => {
// Skip Mongodb because it does not have schema
if (process.env.CONNECTION_TYPE === 'mongodb') return;

const { error } = await db.addColumn(
DEFAULT_SCHEMA,
'persons',
Expand All @@ -194,40 +197,42 @@ describe('Database Connection', () => {

expect(error).not.toBeTruthy();

// Need to update email because MongoDB does not have schema
if (process.env.CONNECTION_TYPE === 'bigquery') {
await db.raw(
`UPDATE \`${DEFAULT_SCHEMA}.persons\` SET email = '[email protected]' WHERE TRUE;`
);
} else {
await db.update(
DEFAULT_SCHEMA,
'persons',
{
email: '[email protected]',
},
{}
);
}

const { data } = await db.select(DEFAULT_SCHEMA, 'persons', {
orderBy: ['id'],
limit: 1000,
offset: 0,
});

expect(cleanup(data)).toEqual([
{
id: 1,
full_name: 'Visal In',
age: 25,
email: '[email protected]',
email: null,
},
{
id: 2,
full_name: 'Outerbase',
age: 30,
email: null,
},
]);

// Remove the column
await db.dropColumn(DEFAULT_SCHEMA, 'persons', 'email');

const { data: data2 } = await db.select(DEFAULT_SCHEMA, 'persons', {
orderBy: ['id'],
});

expect(cleanup(data2)).toEqual([
{
id: 1,
full_name: 'Visal In',
age: 25,
},
{
id: 2,
full_name: 'Outerbase',
age: 30,
email: '[email protected]',
},
]);
});
Expand Down Expand Up @@ -264,7 +269,6 @@ describe('Database Connection', () => {
id: 2,
full_name: 'Outerbase',
age: 30,
email: '[email protected]',
},
]);
});
Expand Down

0 comments on commit b4134bd

Please sign in to comment.