MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / _init_database

Method _init_database

db_manager.py:156–448  ·  view source on GitHub ↗

Initialize database schema and perform migrations. Creates tables if they don't exist and migrates CSV data if needed.

(self)

Source from the content-addressed store, hash-verified

154 conn.execute("PRAGMA foreign_keys = ON") # Enable foreign keys
155 yield conn
156 conn.commit()
157 except Exception as e:
158 if conn:
159 conn.rollback()
160 logger.error(f"Database error: {e}")
161 raise
162 finally:
163 if conn:
164 conn.close()
165
166 def _init_database(self):
167 """
168 Initialize database schema and perform migrations.
169 Creates tables if they don't exist and migrates CSV data if needed.
170 """
171 with self.get_connection() as conn:
172 cursor = conn.cursor()
173
174 # Create hosts table
175 cursor.execute("""
176 CREATE TABLE IF NOT EXISTS hosts (
177 mac TEXT PRIMARY KEY,
178 ip TEXT,
179 hostname TEXT,
180 vendor TEXT,
181 ports TEXT,
182 services TEXT,
183 vulnerabilities TEXT,
184 first_seen TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
185 last_seen TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
186 last_ping_success TIMESTAMP,
187 failed_ping_count INTEGER DEFAULT 0,
188 status TEXT DEFAULT 'alive',
189 alive_count INTEGER DEFAULT 0,
190 network_profile TEXT,
191 scanner_status TEXT,
192 ssh_connector TEXT,
193 rdp_connector TEXT,
194 ftp_connector TEXT,
195 smb_connector TEXT,
196 telnet_connector TEXT,
197 sql_connector TEXT,
198 steal_files_ssh TEXT,
199 steal_files_rdp TEXT,
200 steal_files_ftp TEXT,
201 steal_files_smb TEXT,
202 steal_files_telnet TEXT,
203 steal_data_sql TEXT,
204 nmap_vuln_scanner TEXT,
205 notes TEXT,
206 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
207 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
208 )
209 """)
210
211 # Create indexes for fast lookups
212 cursor.execute("""
213 CREATE INDEX IF NOT EXISTS idx_hosts_ip ON hosts(ip)

Callers 2

__init__Method · 0.95
configure_storageMethod · 0.95

Calls 9

get_connectionMethod · 0.95
_migrate_from_csvMethod · 0.95
cursorMethod · 0.80
infoMethod · 0.80
commitMethod · 0.80
warningMethod · 0.80
executeMethod · 0.45

Tested by

no test coverage detected