Created
March 4, 2020 10:58
-
-
Save jeffmess/4065ec3ec2018755d1c8645fdb58ef11 to your computer and use it in GitHub Desktop.
Migrate Cassandra Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'cassandra' | |
cluster = Cassandra.cluster(port: 9042, consistency: :local_quorum) | |
session = cluster.connect("invoice") | |
# Simple migration script moving one tables data to another with different pk/cks | |
create_table = <<-TABLE_CQL | |
CREATE TABLE IF NOT EXISTS draft_invoices ( | |
invoice_id UUID, | |
user_id UUID, | |
agency_id UUID, | |
portfolio_id UUID, | |
customer_id UUID, | |
customer_tag text, | |
customer text, | |
property text, | |
status text, | |
invoice text, | |
recurrence text, | |
is_history boolean, | |
invoice_number INT, | |
due_date date, | |
PRIMARY KEY ((agency_id), invoice_id) | |
) | |
TABLE_CQL | |
insert = <<-INSERT_CQL | |
INSERT INTO draft_invoices ( | |
invoice_id, agency_id, customer, customer_id, customer_tag, due_date, invoice, | |
is_history, portfolio_id, property, recurrence, status, user_id) | |
VALUES ( | |
:invoice_id, :agency_id, :customer, :customer_id, :customer_tag, :due_date, :invoice, | |
:is_history, :portfolio_id, :property, :recurrence, :status, :user_id) | |
INSERT_CQL | |
session.execute(create_table) | |
data = session.execute("SELECT * FROM invoices_by_owner WHERE status='Draft' allow filtering") | |
insert_statement = session.prepare(insert) | |
data.rows.each do |row| | |
session.execute(insert_statement, arguments: { | |
:invoice_id => row["invoice_id"], | |
:user_id => row["user_id"], | |
:agency_id => row["agency_id"], | |
:portfolio_id => row["portfolio_id"], | |
:customer_id => row["customer_id"], | |
:customer_tag => row["customer_tag"], | |
:customer => row["customer"], | |
:property => row["property"], | |
:status => row["status"], | |
:invoice => row["invoice"], | |
:recurrence => row["recurrence"], | |
:is_history => row["is_history"], | |
:due_date => row["due_date"] | |
}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment