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

feat(default-schema): allow addition of default schema through config #5480

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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: 4 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class Client extends EventEmitter {
this.version = config.version;
}

if (config.schema) {
this.defaultSchemaName = config.schema;
}

if (config.connection && config.connection instanceof Function) {
this.connectionConfigProvider = config.connection;
this.connectionConfigExpirationChecker = () => true; // causes the provider to be called on first use
Expand Down
14 changes: 9 additions & 5 deletions lib/dialects/oracle/schema/oracle-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class SchemaCompiler_Oracle extends SchemaCompiler {
}

// Check whether a table exists on the query.
hasTable(tableName) {
hasTable(tableName, schemaName) {
const sql =
(schemaName
? `select TABLE_NAME from ALL_TABLES where OWNER = '${schemaName}' AND TABLE_NAME = `
: 'select TABLE_NAME from USER_TABLES where TABLE_NAME = ') +
this.client.parameter(tableName, this.builder, this.bindingsHolder);
this.pushQuery({
sql:
'select TABLE_NAME from USER_TABLES where TABLE_NAME = ' +
this.client.parameter(tableName, this.builder, this.bindingsHolder),
sql,
output(resp) {
return resp.length > 0;
},
Expand All @@ -45,7 +48,8 @@ class SchemaCompiler_Oracle extends SchemaCompiler {
column,
this.builder,
this.bindingsHolder
)}`;
)}` +
`and OWNER = '${this.client.defaultSchemaName}'`;
this.pushQuery({ sql, output: (resp) => resp.length > 0 });
}

Expand Down
4 changes: 2 additions & 2 deletions lib/dialects/oracle/schema/oracle-tablecompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ class TableCompiler_Oracle extends TableCompiler {

index(columns, indexName) {
indexName = indexName
? this.formatter.wrap(indexName)
: this._indexCommand('index', this.tableNameRaw, columns);
? this.formatter.wrap(this._commonBuilder._schemaName+'.'+indexName)
: this._commonBuilder._schemaName+'.'+this._indexCommand('index', this.tableNameRaw, columns);
this.pushQuery(
`create index ${indexName} on ${this.tableName()}` +
' (' +
Expand Down
38 changes: 29 additions & 9 deletions lib/migrations/migrate/Migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Migrator {
undefined,
this.knex.client.logger
);
if (this.knex.client.config.schema) {
this.config.clientSchema = this.knex.client.config.schema;
}
this.generator = new MigrationGenerator(
this.knex.client.config.migrations,
this.knex.client.logger
Expand Down Expand Up @@ -91,7 +94,8 @@ class Migrator {

if (transactionForAll) {
return this.knex.transaction((trx) => {
return this._runBatch(migrations, 'up', trx);
const r = this._runBatch(migrations, 'up', trx);
return r
});
} else {
return this._runBatch(migrations, 'up');
Expand Down Expand Up @@ -353,47 +357,55 @@ class Migrator {

// Run a batch of current migrations, in sequence.
async _runBatch(migrations, direction, trx) {
console.log('ST runBaqtch')
const canGetLockInTransaction =
this.knex.client.driverName !== 'cockroachdb';
console.log('ST b try')
try {
await this._getLock(canGetLockInTransaction ? trx : undefined);
// When there is a wrapping transaction, some migrations
// could have been done while waiting for the lock:
console.log('ST 0')
const completed = trx
? await migrationListResolver.listCompleted(
this.config.tableName,
this.config.schemaName,
trx
trx,
this.config.clientSchema
)
: [];

console.log('ST 1', completed)
migrations = getNewMigrations(
this.config.migrationSource,
migrations,
completed
);

console.log('ST 2', migrations)
await Promise.all(
migrations.map(this._validateMigrationStructure.bind(this))
);

console.log('a map')
let batchNo = await this._latestBatchNumber(trx);
if (direction === 'up') batchNo++;

console.log('a _latestBatchNumber')
// Run any hooks before/after this batch
const beforeAll = this.config.beforeAll || (() => {});
const afterAll = this.config.afterAll || (() => {});

console.log('b migrations.length', migrations.length)
let res = [];
if (migrations.length > 0) {
console.log('b beforeAll batch')
await beforeAll(trx || this.knex, migrations);
console.log('b waterfall batch')
res = await this._waterfallBatch(batchNo, migrations, direction, trx);
console.log('a waterfall batch')
await afterAll(trx || this.knex, migrations);
}

await this._freeLock(canGetLockInTransaction ? trx : undefined);
return res;
} catch (error) {
console.log('ST err')
let cleanupReady = Promise.resolve();

if (error instanceof LockError) {
Expand Down Expand Up @@ -431,12 +443,15 @@ class Migrator {
// Validates some migrations by requiring and checking for an `up` and `down`
// function.
async _validateMigrationStructure(migration) {
console.log('start val')
const migrationName =
this.config.migrationSource.getMigrationName(migration);
console.log('migrationName val', migrationName)
// maybe promise
const migrationContent = await this.config.migrationSource.getMigration(
migration
);
console.log('migrationContent val', migrationContent)
if (
typeof migrationContent.up !== 'function' ||
typeof migrationContent.down !== 'function'
Expand Down Expand Up @@ -595,11 +610,16 @@ function getMissingMigrations(migrationSource, completed, all) {
}

function getNewMigrations(migrationSource, all, completed) {
return differenceWith(all, completed, (a, c) => {
return c.name === migrationSource.getMigrationName(a);
console.log('start getNewMigrations', completed)
const di = differenceWith(all, completed, (a, c) => {
if (c.name === migrationSource.getMigrationName(a)) console.log()
return c.name === migrationSource.getMigrationName(a);
});

return di
}


function checkPromise(logger, migrationPromise, name) {
if (!migrationPromise || typeof migrationPromise.then !== 'function') {
logger.warn(`migration ${name} did not return a promise`);
Expand Down
16 changes: 11 additions & 5 deletions lib/migrations/migrate/migration-list-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ function listAll(migrationSource, loadExtensions) {

// Lists all migrations that have been completed for the current db, as an
// array.
async function listCompleted(tableName, schemaName, trxOrKnex) {
await ensureTable(tableName, schemaName, trxOrKnex);

async function listCompleted(tableName, schemaName, trxOrKnex, clientSchema) {
await ensureTable(tableName, clientSchema || schemaName, trxOrKnex);
//TODO: if transactions are enabled for migrations, below params should be sent to getTableName
// console.log('ensured in listCompleted', tableName, clientSchema, schemaName, getTableName(tableName, clientSchema && !trxOrKnex.isTransaction ? undefined : clientSchema||schemaName))
return await trxOrKnex
.from(getTableName(tableName, schemaName))
.from(getTableName(tableName, clientSchema ? undefined : schemaName))
.orderBy('id')
.select('name');
}
Expand All @@ -22,7 +23,12 @@ async function listCompleted(tableName, schemaName, trxOrKnex) {
function listAllAndCompleted(config, trxOrKnex) {
return Promise.all([
listAll(config.migrationSource, config.loadExtensions),
listCompleted(config.tableName, config.schemaName, trxOrKnex),
listCompleted(
config.tableName,
config.schemaName,
trxOrKnex,
config.clientSchema
),
]);
}

Expand Down
8 changes: 6 additions & 2 deletions lib/migrations/migrate/table-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ const {
function ensureTable(tableName, schemaName, trxOrKnex) {
const lockTable = getLockTableName(tableName);
return getSchemaBuilder(trxOrKnex, schemaName)
.hasTable(tableName)
.hasTable(tableName, schemaName)
.then((exists) => {
return !exists && _createMigrationTable(tableName, schemaName, trxOrKnex);
})
.then(() => {
return getSchemaBuilder(trxOrKnex, schemaName).hasTable(lockTable);
return getSchemaBuilder(trxOrKnex, schemaName).hasTable(
lockTable,
schemaName
);
})
.then((exists) => {
return (
Expand Down Expand Up @@ -53,6 +56,7 @@ function _createMigrationLockTable(tableName, schemaName, trxOrKnex) {
}

function _insertLockRowIfNeeded(tableName, schemaName, trxOrKnex) {
schemaName = undefined
const lockTableWithSchema = getLockTableNameWithSchema(tableName, schemaName);
return trxOrKnex
.select('*')
Expand Down
3 changes: 3 additions & 0 deletions lib/query/querybuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class Builder extends EventEmitter {
saveAsyncStack(this, 5);
this._debug = client.config.debug;
}
if (client.defaultSchemaName) {
this._single.schema = client.defaultSchemaName;
}
// Internal flags used in the builder.
this._joinFlag = 'inner';
this._boolFlag = 'and';
Expand Down
3 changes: 3 additions & 0 deletions lib/schema/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class SchemaBuilder extends EventEmitter {
if (client.config) {
this._debug = client.config.debug;
saveAsyncStack(this, 4);
if (client.config.schema) {
this._schema = client.config.schema;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/schema/tablecompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class TableCompiler {
: this._indexCommand('foreign', this.tableNameRaw, foreignData.column);
const column = this.formatter.columnize(foreignData.column);
const references = this.formatter.columnize(foreignData.references);
const inTable = this.formatter.wrap(foreignData.inTable);
const inTable = this.formatter.wrap(this._commonBuilder._schemaName+'.'+foreignData.inTable);
const onUpdate = foreignData.onUpdate
? (this.lowerCase ? ' on update ' : ' ON UPDATE ') +
foreignData.onUpdate
Expand Down