Skip to content

Commit fc20548

Browse files
committed
Ensure tables and indexes are created only if they do not already exist in the SQLite database
1 parent 5a084eb commit fc20548

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/libinspector/mem_db.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def initialize_db():
7474

7575
# Create the devices table
7676
cursor.execute(f'''
77-
CREATE TABLE devices (
77+
CREATE TABLE IF NOT EXISTS devices (
7878
mac_address TEXT PRIMARY KEY,
7979
ip_address TEXT NOT NULL,
8080
is_inspected INTEGER DEFAULT {1 if inspect_every_device_by_default else 0},
@@ -85,12 +85,12 @@ def initialize_db():
8585
''')
8686

8787
# Create indexes on ip_address and is_inspected separately
88-
cursor.execute('CREATE INDEX idx_devices_ip_address ON devices(ip_address)')
89-
cursor.execute('CREATE INDEX idx_devices_is_inspected ON devices(is_inspected)')
88+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_devices_ip_address ON devices(ip_address)')
89+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_devices_is_inspected ON devices(is_inspected)')
9090

9191
# Create the hostnames table
9292
cursor.execute('''
93-
CREATE TABLE hostnames (
93+
CREATE TABLE IF NOT EXISTS hostnames (
9494
ip_address TEXT PRIMARY KEY,
9595
hostname TEXT NOT NULL,
9696
updated_ts INTEGER DEFAULT 0,
@@ -101,7 +101,7 @@ def initialize_db():
101101

102102
# Create the network flows table, with a compound primary key as the flow_key
103103
cursor.execute('''
104-
CREATE TABLE network_flows (
104+
CREATE TABLE IF NOT EXISTS network_flows (
105105
timestamp INTEGER,
106106
src_ip_address TEXT,
107107
dest_ip_address TEXT,
@@ -126,11 +126,11 @@ def initialize_db():
126126
''')
127127

128128
# Create indexes on src_ip_address, dest_ip_address, src_hostname, dest_hostname and timestamp
129-
cursor.execute('CREATE INDEX idx_network_flows_src_ip_address ON network_flows(src_ip_address)')
130-
cursor.execute('CREATE INDEX idx_network_flows_dest_ip_address ON network_flows(dest_ip_address)')
131-
cursor.execute('CREATE INDEX idx_network_flows_src_hostname ON network_flows(src_hostname)')
132-
cursor.execute('CREATE INDEX idx_network_flows_dest_hostname ON network_flows(dest_hostname)')
133-
cursor.execute('CREATE INDEX idx_network_flows_timestamp ON network_flows(timestamp)')
129+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_network_flows_src_ip_address ON network_flows(src_ip_address)')
130+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_network_flows_dest_ip_address ON network_flows(dest_ip_address)')
131+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_network_flows_src_hostname ON network_flows(src_hostname)')
132+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_network_flows_dest_hostname ON network_flows(dest_hostname)')
133+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_network_flows_timestamp ON network_flows(timestamp)')
134134

135135
# Define a SQLite UDF to parse the OUI from the MAC address
136136
conn.create_function('get_oui_vendor', 1, get_vendor)

0 commit comments

Comments
 (0)