-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add copyTable sample #198
Add copyTable sample #198
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,37 @@ function deleteTable (options, callback) { | |
} | ||
// [END delete_table] | ||
|
||
// [START copy_table] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete surrounding region tags. |
||
/** | ||
* Create a copy of an existing table | ||
* | ||
* @param {object} options Configuration options. | ||
* @param {string} options.srcDataset The source dataset ID. | ||
* @param {string} options.srcTable The source table ID. | ||
* @param {string} options.destDataset The destination dataset ID. | ||
* @param {string} options.destTable The destination table ID. Will be created if it doesn't exist. | ||
* @param {function} callback The callback function. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete JSDoc comment. |
||
function copyTable (options, callback) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of a single function copyTable (srcDataset, srcTable, destDataset, destTable, callback) { Plus all necessary changes that will need to be made to the CLI/tests. |
||
var srcTable = bigquery.dataset(options.srcDataset).table(options.srcTable); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
var destTable = bigquery.dataset(options.destDataset).table(options.destTable); | ||
|
||
srcTable.copy(destTable, function (err, job) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
console.log('Started job: %s', job.id); | ||
job | ||
.on('error', callback) | ||
.on('complete', function (metadata) { | ||
console.log('Completed job: %s', job.id); | ||
return callback(null, metadata); | ||
}); | ||
}); | ||
} | ||
// [END copy_table] | ||
|
||
// [START import_file] | ||
/** | ||
* Load a csv file into a BigQuery table. | ||
|
@@ -219,6 +250,7 @@ var program = module.exports = { | |
importFile: importFile, | ||
exportTableToGCS: exportTableToGCS, | ||
insertRowsAsStream: insertRowsAsStream, | ||
copyTable: copyTable, | ||
main: function (args) { | ||
// Run the command-line program | ||
cli.help().strict().parse(args).argv; | ||
|
@@ -236,6 +268,15 @@ cli | |
.command('delete <dataset> <table>', 'Delete a table in the specified dataset.', {}, function (options) { | ||
program.deleteTable(utils.pick(options, ['dataset', 'table']), utils.makeHandler()); | ||
}) | ||
.command('copy <srcDataset> <srcTable> <destDataset> <destTable>', | ||
'Make a copy of an existing table.', {}, | ||
function (options) { | ||
program.copyTable( | ||
utils.pick(options, ['srcDataset', 'srcTable', 'destDataset', 'destTable']), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to four positional arguments |
||
utils.makeHandler() | ||
); | ||
} | ||
) | ||
.command('import <dataset> <table> <file>', 'Import data from a local file or a Google Cloud Storage file into BigQuery.', { | ||
bucket: { | ||
alias: 'b', | ||
|
@@ -325,6 +366,10 @@ cli | |
'node $0 insert my_dataset my_table json_file', | ||
'Insert the JSON objects contained in json_file (one per line) into my_dataset:my_table.' | ||
) | ||
.example( | ||
'node $0 copy src_dataset src_table dest_dataset dest_table', | ||
'Copy src_dataset:src_table to dest_dataset:dest_table.' | ||
) | ||
.wrap(100) | ||
.recommendCommands() | ||
.epilogue('For more information, see https://cloud.google.com/bigquery/docs'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,13 @@ | |
var proxyquire = require('proxyquire').noCallThru(); | ||
var bucket = 'bucket'; | ||
var file = 'file'; | ||
var job = 'job'; | ||
var jobId = 'job'; | ||
var dataset = 'dataset'; | ||
var table = 'table'; | ||
var srcDataset = dataset; | ||
var srcTable = table; | ||
var destDataset = dataset + '_dest'; | ||
var destTable = table + '_dest'; | ||
var format = 'JSON'; | ||
var schema = 'schema'; | ||
var jsonArray = [ | ||
|
@@ -46,12 +50,14 @@ function getSample () { | |
var fileMock = {}; | ||
var metadataMock = { status: { state: 'DONE' } }; | ||
var jobMock = { | ||
id: job, | ||
id: jobId, | ||
getMetadata: sinon.stub().yields(null, metadataMock), | ||
on: sinon.stub().returnsThis() | ||
}; | ||
jobMock.on.withArgs('complete').yields(metadataMock); | ||
var tableMock = { | ||
export: sinon.stub().yields(null, jobMock), | ||
copy: sinon.stub().yields(null, jobMock), | ||
delete: sinon.stub().yields(null), | ||
import: sinon.stub().yields(null, jobMock), | ||
insert: sinon.stub().yields(null, errorList) | ||
|
@@ -220,7 +226,6 @@ describe('bigquery:tables', function () { | |
table: table, | ||
file: file | ||
}; | ||
sample.mocks.job.on.withArgs('complete').yields(sample.mocks.metadata); | ||
|
||
sample.program.importFile(options, callback); | ||
|
||
|
@@ -243,7 +248,6 @@ describe('bigquery:tables', function () { | |
bucket: bucket, | ||
format: format | ||
}; | ||
sample.mocks.job.on.withArgs('complete').yields(sample.mocks.metadata); | ||
|
||
sample.program.importFile(options, callback); | ||
|
||
|
@@ -269,6 +273,45 @@ describe('bigquery:tables', function () { | |
}); | ||
}); | ||
|
||
describe('copyTable', function () { | ||
var options = { | ||
srcDataset: srcDataset, | ||
srcTable: srcTable, | ||
destDataset: destDataset, | ||
destTable: destTable | ||
}; | ||
|
||
it('should copy a table', function () { | ||
var sample = getSample(); | ||
var callback = sinon.stub(); | ||
|
||
sample.program.copyTable(options, callback); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to four positional arguments |
||
|
||
assert.equal(sample.mocks.table.copy.calledOnce, true); | ||
assert.deepEqual( | ||
sample.mocks.table.copy.firstCall.args.slice(0, -1), | ||
[sample.mocks.table] | ||
); | ||
assert.equal(callback.calledOnce, true); | ||
assert.deepEqual(callback.firstCall.args, [null, sample.mocks.metadata]); | ||
assert.equal(console.log.calledTwice, true); | ||
assert.equal(console.log.calledWith('Started job: %s', sample.mocks.job.id), true); | ||
assert.equal(console.log.calledWith('Completed job: %s', sample.mocks.job.id), true); | ||
}); | ||
|
||
it('should handle error', function () { | ||
var error = new Error('error'); | ||
var sample = getSample(); | ||
var callback = sinon.stub(); | ||
sample.mocks.table.copy.yields(error); | ||
|
||
sample.program.copyTable(options, callback); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to four positional arguments |
||
|
||
assert.equal(callback.calledOnce, true); | ||
assert.deepEqual(callback.firstCall.args, [error]); | ||
}); | ||
}); | ||
|
||
describe('exportTableToGCS', function () { | ||
it('should export to a table', function () { | ||
var sample = getSample(); | ||
|
@@ -281,7 +324,6 @@ describe('bigquery:tables', function () { | |
gzip: true | ||
}; | ||
var callback = sinon.stub(); | ||
sample.mocks.job.on.withArgs('complete').yields(sample.mocks.metadata); | ||
|
||
sample.program.exportTableToGCS(options, callback); | ||
|
||
|
@@ -389,6 +431,20 @@ describe('bigquery:tables', function () { | |
}]); | ||
}); | ||
|
||
it('should call copyTable', function () { | ||
var program = getSample().program; | ||
program.copyTable = sinon.stub(); | ||
|
||
program.main(['copy', srcDataset, srcTable, destDataset, destTable]); | ||
assert.equal(program.copyTable.calledOnce, true); | ||
assert.deepEqual(program.copyTable.firstCall.args.slice(0, -1), [{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to four positional arguments |
||
srcDataset: srcDataset, | ||
srcTable: srcTable, | ||
destDataset: destDataset, | ||
destTable: destTable | ||
}]); | ||
}); | ||
|
||
it('should call exportTableToGCS', function () { | ||
var program = getSample().program; | ||
program.exportTableToGCS = sinon.stub(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switch to four positional arguments