Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Bluebird.method and one instance of .bind #3290

Merged
merged 2 commits into from
Jun 17, 2019
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
91 changes: 44 additions & 47 deletions src/dialects/mysql/schema/tablecompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,55 +80,52 @@ assign(TableCompiler_MySQL.prototype, {
output(resp) {
const column = resp[0];
const runner = this;
return compiler
.getFKRefs(runner)
.get(0)
.then((refs) =>
new Promise((resolve, reject) => {
try {
if (!refs.length) {
resolve();
}
resolve(compiler.dropFKRefs(runner, refs));
} catch (e) {
reject(e);
return compiler.getFKRefs(runner).then(([refs]) =>
new Promise((resolve, reject) => {
try {
if (!refs.length) {
resolve();
}
resolve(compiler.dropFKRefs(runner, refs));
} catch (e) {
reject(e);
}
})
.then(function() {
let sql = `alter table ${table} change ${wrapped} ${column.Type}`;

if (String(column.Null).toUpperCase() !== 'YES') {
sql += ` NOT NULL`;
} else {
// This doesn't matter for most cases except Timestamp, where this is important
sql += ` NULL`;
}
if (column.Default !== void 0 && column.Default !== null) {
sql += ` DEFAULT '${column.Default}'`;
}

return runner.query({
sql,
});
})
.then(function() {
let sql = `alter table ${table} change ${wrapped} ${column.Type}`;

if (String(column.Null).toUpperCase() !== 'YES') {
sql += ` NOT NULL`;
} else {
// This doesn't matter for most cases except Timestamp, where this is important
sql += ` NULL`;
}
if (column.Default !== void 0 && column.Default !== null) {
sql += ` DEFAULT '${column.Default}'`;
}

return runner.query({
sql,
});
})
.then(function() {
if (!refs.length) {
return;
}
return compiler.createFKRefs(
runner,
refs.map(function(ref) {
if (ref.REFERENCED_COLUMN_NAME === from) {
ref.REFERENCED_COLUMN_NAME = to;
}
if (ref.COLUMN_NAME === from) {
ref.COLUMN_NAME = to;
}
return ref;
})
);
})
);
.then(function() {
if (!refs.length) {
return;
}
return compiler.createFKRefs(
runner,
refs.map(function(ref) {
if (ref.REFERENCED_COLUMN_NAME === from) {
ref.REFERENCED_COLUMN_NAME = to;
}
if (ref.COLUMN_NAME === from) {
ref.COLUMN_NAME = to;
}
return ref;
})
);
})
);
},
});
},
Expand Down
84 changes: 41 additions & 43 deletions src/dialects/sqlite3/schema/ddl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ assign(SQLite3_DDL.prototype, {
return this.formatter(this.tableNameRaw, (value) => value);
},

getColumn: Bluebird.method(function(column) {
getColumn: async function(column) {
const currentCol = find(this.pragma, (col) => {
return (
this.client.wrapIdentifier(col.name) ===
Expand All @@ -49,7 +49,7 @@ assign(SQLite3_DDL.prototype, {
`The column ${column} is not in the ${this.tableName()} table`
);
return currentCol;
}),
},

getTableSql() {
this.trx.disableProcessing();
Expand All @@ -63,11 +63,11 @@ assign(SQLite3_DDL.prototype, {
});
},

renameTable: Bluebird.method(function() {
renameTable: async function() {
return this.trx.raw(
`ALTER TABLE "${this.tableName()}" RENAME TO "${this.alteredName}"`
);
}),
},

dropOriginal() {
return this.trx.raw(`DROP TABLE "${this.tableName()}"`);
Expand Down Expand Up @@ -228,50 +228,48 @@ assign(SQLite3_DDL.prototype, {
},

// Boy, this is quite a method.
renameColumn: Bluebird.method(function(from, to) {
renameColumn: async function(from, to) {
return this.client.transaction(
(trx) => {
async (trx) => {
this.trx = trx;
return this.getColumn(from)
.bind(this)
.then(this.getTableSql)
.then(function(sql) {
const a = this.client.wrapIdentifier(from);
const b = this.client.wrapIdentifier(to);
const createTable = sql[0];
const newSql = this._doReplace(createTable.sql, a, b);
if (sql === newSql) {
throw new Error('Unable to find the column to change');
}
const { from: mappedFrom, to: mappedTo } = invert(
this.client.postProcessResponse(
invert({
from,
to,
})
)
);
return Bluebird.bind(this)
.then(this.createTempTable(createTable))
.then(this.copyData)
.then(this.dropOriginal)
.then(function() {
return this.trx.raw(newSql);
})
.then(
this.reinsertData(function(row) {
row[mappedTo] = row[mappedFrom];
return omit(row, mappedFrom);
})
)
.then(this.dropTempTable);
});
const column = await this.getColumn(from);
const sql = await this.getTableSql(column);
const a = this.client.wrapIdentifier(from);
const b = this.client.wrapIdentifier(to);
const createTable = sql[0];
const newSql = this._doReplace(createTable.sql, a, b);
if (sql === newSql) {
throw new Error('Unable to find the column to change');
}
const { from: mappedFrom, to: mappedTo } = invert(
this.client.postProcessResponse(
invert({
from,
to,
})
)
);

return Bluebird.bind(this)
.then(this.createTempTable(createTable))
.then(this.copyData)
.then(this.dropOriginal)
.then(function() {
return this.trx.raw(newSql);
})
.then(
this.reinsertData(function(row) {
row[mappedTo] = row[mappedFrom];
return omit(row, mappedFrom);
})
)
.then(this.dropTempTable);
},
{ connection: this.connection }
);
}),
},

dropColumn: Bluebird.method(function(columns) {
dropColumn: async function(columns) {
return this.client.transaction(
(trx) => {
this.trx = trx;
Expand Down Expand Up @@ -306,7 +304,7 @@ assign(SQLite3_DDL.prototype, {
},
{ connection: this.connection }
);
}),
},
});

module.exports = SQLite3_DDL;
6 changes: 2 additions & 4 deletions src/migrate/migrate-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
// available.
const StubMigrate = (module.exports = function() {});

const Bluebird = require('bluebird');

const noSuchMethod = Bluebird.method(function() {
const noSuchMethod = async function() {
throw new Error('Migrations are not supported');
});
};

StubMigrate.prototype = {
make: noSuchMethod,
Expand Down
4 changes: 2 additions & 2 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ assign(Runner.prototype, {
// "Runs" a query, returning a promise. All queries specified by the builder are guaranteed
// to run in sequence, and on the same connection, especially helpful when schema building
// and dealing with foreign key constraints, etc.
query: Bluebird.method(function(obj) {
query: async function(obj) {
const { __knexUid, __knexTxId } = this.connection;

this.builder.emit('query', assign({ __knexUid, __knexTxId }, obj));
Expand Down Expand Up @@ -216,7 +216,7 @@ assign(Runner.prototype, {
);
throw error;
});
}),
},

// In the case of the "schema builder" we call `queryArray`, which runs each
// of the queries in sequence.
Expand Down
8 changes: 4 additions & 4 deletions src/seed/Seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ function Seeder(knex) {
}

// Runs all seed files for the given environment.
Seeder.prototype.run = Bluebird.method(function(config) {
Seeder.prototype.run = async function(config) {
this.config = this.setConfig(config);
return this._seedData()
.bind(this)
.then(([all]) => {
return this._runSeeds(all);
});
});
};

// Creates a new seed file, with a given name.
Seeder.prototype.make = function(name, config) {
Expand All @@ -47,7 +47,7 @@ Seeder.prototype.make = function(name, config) {
};

// Lists all available seed files as a sorted array.
Seeder.prototype._listAll = Bluebird.method(function(config) {
Seeder.prototype._listAll = async function(config) {
this.config = this.setConfig(config);
const loadExtensions = this.config.loadExtensions;
return Bluebird.promisify(fs.readdir, { context: fs })(
Expand All @@ -60,7 +60,7 @@ Seeder.prototype._listAll = Bluebird.method(function(config) {
return includes(loadExtensions, extension);
}).sort()
);
});
};

// Gets the seed file list = require(the specified seed directory.
Seeder.prototype._seedData = function() {
Expand Down
6 changes: 2 additions & 4 deletions src/seed/seed-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
// available.
const StubSeed = (module.exports = function() {});

const Bluebird = require('bluebird');

const noSuchMethod = Bluebird.method(function() {
const noSuchMethod = async function() {
throw new Error('Seeds are not supported');
});
};

StubSeed.prototype = {
make: noSuchMethod,
Expand Down
16 changes: 0 additions & 16 deletions test/integration/builder/additional.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,6 @@ module.exports = function(knex) {
});
});

it('should forward the .get() function from bluebird', function() {
return knex('accounts')
.select()
.limit(1)
.then(function(accounts) {
const firstAccount = accounts[0];
return knex('accounts')
.select()
.limit(1)
.get(0)
.then(function(account) {
expect(account.id == firstAccount.id);
});
});
});

it('should forward the .mapSeries() function from bluebird', function() {
const asyncTask = function() {
return new Promise(function(resolve, reject) {
Expand Down