-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_postgres_populate.py
executable file
·85 lines (61 loc) · 1.88 KB
/
python_postgres_populate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
import psycopg2
import time
import os
import os.path
import sys
userid = os.getenv('PGUSERID')
password = os.getenv('PGPASSWORD')
source_ip = os.getenv('PGSOURCEIP')
db_status = sys.argv[1]
conn = psycopg2.connect("dbname=ndb_test host=" + source_ip + " user=" + userid + " password=" + password)
cur = conn.cursor()
test_for_table_sql = "select * from information_schema.tables where table_name='ndb_test_table';"
cur.execute(test_for_table_sql)
if cur.rowcount == 0:
sql_create_string = """CREATE TABLE ndb_test_table (
sequence_id INTEGER NOT NULL,
string1 VARCHAR,
string2 VARCHAR,
string3 VARCHAR,
PRIMARY KEY (sequence_id))
"""
cur.execute(sql_create_string)
conn.commit()
#
# If Standby node then wait for replication
if db_status == "STANDBY":
if os.path.exists("/tmp/postgres/ERROR"):
os.remove("/tmp/postgres/ERROR")
# End If
if os.path.exists("/tmp/postgres/DONE"):
os.remove("/tmp/postgres/DONE")
# End If
replication_wait_status = 1
while replication_wait_status == 1:
print("Waiting for replication...")
time.sleep(1)
if os.path.exists("/tmp/postgres/ERROR"):
os.system("cat /tmp/postgres/ERROR")
exit(1)
elif os.path.exists("/tmp/postgres/DONE"):
os.system("cat /tmp/postgres/DONE")
replication_wait_status = 0
# End If
# End While
# End If
###
start_index = 0
get_max_sequence_id_sql_string = "select max(sequence_id) from ndb_test_table;"
cur.execute(get_max_sequence_id_sql_string)
try:
start_index = cur.fetchone()[0] + 1
except:
pass
while (True):
time.sleep(5)
sql_insert_string = "insert into ndb_test_table (sequence_id, string1, string2, string3) values ('" + str(start_index) + "', 'string1_" + str(start_index) + "', 'string2_" + str(start_index) + "', 'string3_" + str(start_index) + "');"
cur.execute(sql_insert_string)
conn.commit()
start_index = start_index + 1
#