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

Streaming tableA to tableB and updating streamed tableA records #64

Open
iamvanja opened this issue Jun 20, 2017 · 0 comments
Open

Streaming tableA to tableB and updating streamed tableA records #64

iamvanja opened this issue Jun 20, 2017 · 0 comments

Comments

@iamvanja
Copy link
Contributor

iamvanja commented Jun 20, 2017

Hi,

I am wondering about the following scenario in MySQL.

const query = db.query(`SELECT * FROM table_a WHERE is_migrated=?`, [0])

query
  .stream()
  .pipe(
    stream.Transform({
      objectMode: true,
      transform: (row, encoding, callback) => {
        callback(null, processRow(row))
      }
    })
    .on('error', err => console.log('transform pipe error', err))
    .on('end', () => console.log('transform pipe end'))
  )
  .pipe(etl.mysql.upsert(pool, 'db_name', 'table_b'))
  // ... for all migrated records from table_a set is_migrated=1 
  .promise()
  .then(data => console.log('done', data))
  .catch(err => console.error('err:', err))

select from tableA where is_migrated=0
do transformations for each row
insert them to tableB
update tableA for all inserted records to tableB and set is_migrated=1

I have an idea of building an array of IDs and then doing a separate UPDATE, but it seems hacky, especially since the array could grow very quickly due to a big data set. Is there a better way of doing it?

Also, is it possible to use INSERT ... ON DUPLICATE KEY UPDATE for upsert? REPLACE INTO becomes an issue in our use case where it would eat up auto_increment IDs very quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant