Skip to content
Merged
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "5.25.0",
"version": "5.26.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
10 changes: 10 additions & 0 deletions src/database/DatabaseImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ export class DatabaseImpl<Driver extends DriverImpl = any> extends Macroable {
await this.driver.createTable(table, closure)
}

/**
* Alter a table in database.
*/
public async alterTable(
table: string,
closure: (builder: Knex.TableBuilder) => void | Promise<void>
): Promise<void> {
await this.driver.alterTable(table, closure)
}

/**
* Drop a table in database.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/database/drivers/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ export abstract class Driver<Client = any, QB = any> {
closure?: TableBuilder
): Promise<void>

/**
* Alter a table in database.
*/
public abstract alterTable(
table: string,
closure?: TableBuilder
): Promise<void>

/**
* Drop a table in database.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/database/drivers/FakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ export class FakeDriver {
this.tables.push(table)
}

/**
* Alter a table in database.
*/
public static async alterTable(): Promise<void> {}

/**
* Drop a table in database.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/database/drivers/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ export class MongoDriver extends Driver<Connection, Collection> {
throw new NotImplementedMethodException(this.createTable.name, 'mongo')
}

/**
* Alter a table in database.
*/
public async alterTable(): Promise<void> {
throw new NotImplementedMethodException(this.alterTable.name, 'mongo')
}

/**
* Drop a table in database.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/database/drivers/MySqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ export class MySqlDriver extends Driver<Knex, Knex.QueryBuilder> {
await this.client.schema.createTable(table, closure)
}

/**
* Alter a table in database.
*/
public async alterTable(
table: string,
closure: (builder: Knex.TableBuilder) => void | Promise<void>
): Promise<void> {
await this.client.schema.alterTable(table, closure)
}

/**
* Drop a table in database.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/database/drivers/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ export class PostgresDriver extends Driver<Knex, Knex.QueryBuilder> {
await this.client.schema.createTable(table, closure)
}

/**
* Alter a table in database.
*/
public async alterTable(
table: string,
closure: (builder: Knex.TableBuilder) => void | Promise<void>
): Promise<void> {
await this.client.schema.alterTable(table, closure)
}

/**
* Drop a table in database.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/database/drivers/SqliteDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ export class SqliteDriver extends Driver<Knex, Knex.QueryBuilder> {
await this.client.schema.createTable(table, closure)
}

/**
* Alter a table in database.
*/
public async alterTable(
table: string,
closure: (builder: Knex.TableBuilder) => void | Promise<void>
): Promise<void> {
await this.client.schema.alterTable(table, closure)
}

/**
* Drop a table in database.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/database/transactions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ export class Transaction<Client = any, QB = any> extends Macroable {
return this.driver.createTable(table, closure)
}

/**
* Alter a table in database.
*/
public async alterTable(
table: string,
closure?: (builder: Knex.TableBuilder) => void | Promise<void>
): Promise<void> {
return this.driver.alterTable(table, closure)
}

/**
* Drop a table in database.
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/database/DatabaseImplTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ export default class DatabaseImplTest {
assert.calledOnceWith(database.driver.createTable, 'users')
}

@Test()
public async shouldBeAbleToAlterTableInDatabase({ assert }: Context) {
Mock.when(FakeDriver, 'alterTable').resolve(undefined)
Mock.when(ConnectionFactory, 'fabricate').return(FakeDriver)

const database = new DatabaseImpl().connection('postgres')
await database.alterTable('users', builder => {
builder.string('id').primary()
})

assert.calledOnceWith(database.driver.alterTable, 'users')
}

@Test()
public async shouldBeAbleToDropTheDatabaseTable({ assert }: Context) {
Mock.when(FakeDriver, 'dropTable').resolve(undefined)
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/database/transactions/TransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ export default class TransactionTest {
assert.calledOnceWith(trx.driver.createTable, 'users')
}

@Test()
public async shouldBeAbleToAlterTableInDatabase({ assert }: Context) {
Mock.when(FakeDriver, 'alterTable').resolve(undefined)
Mock.when(ConnectionFactory, 'fabricate').return(FakeDriver)

const trx = new Transaction(FakeDriver)
await trx.alterTable('users', builder => {
builder.string('id').primary()
})

assert.calledOnceWith(trx.driver.alterTable, 'users')
}

@Test()
public async shouldBeAbleToDropTheDatabaseTable({ assert }: Context) {
Mock.when(FakeDriver, 'dropTable').resolve(undefined)
Expand Down