db setup and teardown suite
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
cassandra_addresses = ['127.0.0.1']
|
||||
cassandra_port = 9042
|
||||
cassandra_keyspace = 'distributedunionfind'
|
||||
|
||||
setup_db_dir = 'setup/db'
|
||||
setup_tables_dir = setup_db_dir+"/tables"
|
||||
@@ -0,0 +1,24 @@
|
||||
from settings import cassandra_addresses, cassandra_port, setup_db_dir, setup_tables_dir
|
||||
from cassandra.cluster import Cluster
|
||||
import os
|
||||
|
||||
print(" == DB SETUP SCRIPT == ")
|
||||
|
||||
print(f"Attempting Cassandra connection @ {cassandra_addresses}:{cassandra_port}")
|
||||
cluster = Cluster(cassandra_addresses, port=cassandra_port)
|
||||
session = cluster.connect()
|
||||
print(f"Connection OK")
|
||||
|
||||
with open(f"{setup_db_dir}/keyspace/CREATE.sql") as keyspace_create:
|
||||
session.execute(keyspace_create.read())
|
||||
|
||||
with open(f"{setup_db_dir}/keyspace/USE.sql") as keyspace_use:
|
||||
session.execute(keyspace_use.read())
|
||||
|
||||
for folder_name in os.listdir(setup_tables_dir):
|
||||
with open(f'{setup_tables_dir}/{folder_name}/CREATE.sql') as sql_create:
|
||||
session.execute(sql_create.read())
|
||||
|
||||
session.execute("INSERT INTO transactions (tx_id,address,value,tx_hash,block_id,timestamp) VALUES(1697,'t1KmCvfPMgfQXeNosFqzAmvYdEoYfdnxnVA',15701,'18c23345908f5097456c5f0014411381fd9866790aa65b863aab24ee17453732',818,1477724947)")
|
||||
res = session.execute('SELECT * FROM transactions')
|
||||
print(res.one())
|
||||
@@ -0,0 +1 @@
|
||||
CREATE KEYSPACE IF NOT EXISTS distributedunionfind WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};
|
||||
@@ -0,0 +1 @@
|
||||
DROP KEYSPACE IF EXISTS distributedunionfind
|
||||
@@ -0,0 +1 @@
|
||||
USE distributedunionfind;
|
||||
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE transactions(
|
||||
tx_id INT,
|
||||
address TEXT,
|
||||
value INT,
|
||||
tx_hash TEXT,
|
||||
block_id INT,
|
||||
timestamp TIMESTAMP,
|
||||
PRIMARY KEY (tx_id, address)
|
||||
) WITH CLUSTERING ORDER BY (address DESC);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE transactions
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
from settings import cassandra_addresses, cassandra_port, cassandra_keyspace, setup_tables_dir, setup_db_dir
|
||||
from cassandra.cluster import Cluster
|
||||
import os
|
||||
|
||||
print(" == DB TEARDOWN SCRIPT == ")
|
||||
|
||||
print(f"Attempting Cassandra connection @ {cassandra_addresses}:{cassandra_port}")
|
||||
cluster = Cluster(cassandra_addresses, port=cassandra_port)
|
||||
session = cluster.connect(cassandra_keyspace)
|
||||
print(f"Connection OK")
|
||||
|
||||
for folder_name in os.listdir(setup_tables_dir):
|
||||
print(f"Dropping table {folder_name}")
|
||||
with open(f'{setup_tables_dir}/{folder_name}/DROP.sql') as sql_drop:
|
||||
session.execute(sql_drop.read())
|
||||
|
||||
with open(f"{setup_db_dir}/keyspace/DROP.sql") as keyspace_create:
|
||||
print(f"Dropping keyspace {cassandra_keyspace}")
|
||||
session.execute(keyspace_create.read())
|
||||
|
||||
print("Done!")
|
||||
Reference in New Issue
Block a user