소스 검색

db setup and teardown suite

master
nitowa 2 년 전
커밋
883c81b786

BIN
__pycache__/settings.cpython-310.pyc 파일 보기


+ 6
- 0
settings.py 파일 보기

@@ -0,0 +1,6 @@
1
+cassandra_addresses = ['127.0.0.1']
2
+cassandra_port = 9042
3
+cassandra_keyspace = 'distributedunionfind'
4
+
5
+setup_db_dir = 'setup/db'
6
+setup_tables_dir = setup_db_dir+"/tables"

+ 24
- 0
setup.py 파일 보기

@@ -0,0 +1,24 @@
1
+from settings import cassandra_addresses, cassandra_port, setup_db_dir, setup_tables_dir
2
+from cassandra.cluster import Cluster
3
+import os
4
+
5
+print(" == DB SETUP SCRIPT == ")
6
+
7
+print(f"Attempting Cassandra connection @ {cassandra_addresses}:{cassandra_port}")
8
+cluster  = Cluster(cassandra_addresses, port=cassandra_port)
9
+session = cluster.connect()
10
+print(f"Connection OK")
11
+
12
+with open(f"{setup_db_dir}/keyspace/CREATE.sql") as keyspace_create:
13
+    session.execute(keyspace_create.read())
14
+
15
+with open(f"{setup_db_dir}/keyspace/USE.sql") as keyspace_use:
16
+    session.execute(keyspace_use.read())
17
+
18
+for folder_name in os.listdir(setup_tables_dir):
19
+    with open(f'{setup_tables_dir}/{folder_name}/CREATE.sql') as sql_create:
20
+        session.execute(sql_create.read())
21
+
22
+session.execute("INSERT INTO transactions (tx_id,address,value,tx_hash,block_id,timestamp) VALUES(1697,'t1KmCvfPMgfQXeNosFqzAmvYdEoYfdnxnVA',15701,'18c23345908f5097456c5f0014411381fd9866790aa65b863aab24ee17453732',818,1477724947)")
23
+res = session.execute('SELECT * FROM transactions')
24
+print(res.one())

+ 1
- 0
setup/db/keyspace/CREATE.sql 파일 보기

@@ -0,0 +1 @@
1
+CREATE KEYSPACE IF NOT EXISTS distributedunionfind WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};

+ 1
- 0
setup/db/keyspace/DROP.sql 파일 보기

@@ -0,0 +1 @@
1
+DROP KEYSPACE IF EXISTS distributedunionfind

+ 1
- 0
setup/db/keyspace/USE.sql 파일 보기

@@ -0,0 +1 @@
1
+USE distributedunionfind;

+ 9
- 0
setup/db/tables/transactions/CREATE.sql 파일 보기

@@ -0,0 +1,9 @@
1
+CREATE TABLE transactions(
2
+    tx_id INT,
3
+    address TEXT,
4
+    value INT,
5
+    tx_hash TEXT,
6
+    block_id INT,
7
+    timestamp TIMESTAMP,
8
+    PRIMARY KEY (tx_id, address)
9
+) WITH CLUSTERING ORDER BY (address DESC);

+ 1
- 0
setup/db/tables/transactions/DROP.sql 파일 보기

@@ -0,0 +1 @@
1
+DROP TABLE transactions

+ 21
- 0
teardown.py 파일 보기

@@ -0,0 +1,21 @@
1
+from settings import cassandra_addresses, cassandra_port, cassandra_keyspace, setup_tables_dir, setup_db_dir
2
+from cassandra.cluster import Cluster
3
+import os
4
+
5
+print(" == DB TEARDOWN SCRIPT == ")
6
+
7
+print(f"Attempting Cassandra connection @ {cassandra_addresses}:{cassandra_port}")
8
+cluster  = Cluster(cassandra_addresses, port=cassandra_port)
9
+session = cluster.connect(cassandra_keyspace)
10
+print(f"Connection OK")
11
+
12
+for folder_name in os.listdir(setup_tables_dir):
13
+    print(f"Dropping table {folder_name}")
14
+    with open(f'{setup_tables_dir}/{folder_name}/DROP.sql') as sql_drop:
15
+        session.execute(sql_drop.read())
16
+
17
+with open(f"{setup_db_dir}/keyspace/DROP.sql") as keyspace_create:
18
+    print(f"Dropping keyspace {cassandra_keyspace}")
19
+    session.execute(keyspace_create.read())
20
+
21
+print("Done!")

Loading…
취소
저장