Coverage for dak/update_db.py: 67%
167 statements
« prev ^ index » next coverage.py v7.6.0, created at 2026-11-21 08:51 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2026-11-21 08:51 +0000
1"""Database Update Main Script
3@contact: Debian FTP Master <ftpmaster@debian.org>
4# Copyright (C) 2008 Michael Casadevall <mcasadevall@debian.org>
5@license: GNU General Public License version 2 or later
6"""
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22################################################################################
24# <Ganneff> when do you have it written?
25# <NCommander> Ganneff, after you make my debian account
26# <Ganneff> blackmail wont work
27# <NCommander> damn it
29################################################################################
31import errno
32import fcntl
33import importlib
34import os
35import pkgutil
36import re
37import sys
38import time
39from types import ModuleType
40from typing import Literal, NoReturn
42import apt_pkg
43import psycopg2
45import dak.dakdb
46from daklib import utils
47from daklib.config import Config
48from daklib.dak_exceptions import DBUpdateError
49from daklib.daklog import Logger
51################################################################################
54class UpdateDB:
55 def usage(self, exit_code=0) -> NoReturn:
56 print(
57 """Usage: dak update-db
58Updates dak's database schema to the lastest version. You should disable crontabs while this is running
60 -h, --help show this help and exit.
61 -y, --yes do not ask for confirmation"""
62 )
63 sys.exit(exit_code)
65 ################################################################################
67 def update_db_to_zero(self) -> None:
68 """This function will attempt to update a pre-zero database schema to zero"""
70 # First, do the sure thing, and create the configuration table
71 try:
72 print("Creating configuration table ...")
73 c = self.db.cursor()
74 c.execute(
75 """CREATE TABLE config (
76 id SERIAL PRIMARY KEY NOT NULL,
77 name TEXT UNIQUE NOT NULL,
78 value TEXT
79 );"""
80 )
81 c.execute(
82 "INSERT INTO config VALUES ( nextval('config_id_seq'), 'db_revision', '0')"
83 )
84 self.db.commit()
86 except psycopg2.ProgrammingError:
87 self.db.rollback()
88 print("Failed to create configuration table.")
89 print("Can the projectB user CREATE TABLE?")
90 print()
91 print("Aborting update.")
92 sys.exit(-255)
94 ################################################################################
96 def get_db_rev(self) -> str | Literal[-1]:
97 # We keep database revision info the config table
98 # Try and access it
100 try:
101 c = self.db.cursor()
102 c.execute("SELECT value FROM config WHERE name = 'db_revision';")
103 row = c.fetchone()
104 assert row is not None
105 return row[0]
107 except psycopg2.ProgrammingError:
108 # Whoops .. no config table ...
109 self.db.rollback()
110 print(
111 "No configuration table found, assuming dak database revision to be pre-zero"
112 )
113 return -1
115 ################################################################################
117 def get_transaction_id(self) -> str:
118 """
119 Returns the current transaction id as a string.
120 """
121 cursor = self.db.cursor()
122 cursor.execute("SELECT txid_current();")
123 row = cursor.fetchone()
124 assert row is not None
125 id = row[0]
126 cursor.close()
127 return id
129 ################################################################################
131 def apply_update(self, update_module: ModuleType, revision: int) -> None:
132 print(update_module.__doc__)
133 try:
134 c = self.db.cursor()
136 do_update = getattr(update_module, "do_update", None)
137 if do_update is not None:
138 do_update(c)
139 else:
140 for stmt in update_module.statements:
141 c.execute(stmt)
143 c.execute(
144 "UPDATE config SET value = %s WHERE name = 'db_revision'",
145 (str(revision),),
146 )
147 self.db.commit()
149 except psycopg2.ProgrammingError as msg:
150 self.db.rollback()
151 raise DBUpdateError(
152 f"Unable to apply sick update {revision}, rollback issued. Error message: {msg}"
153 )
154 except Exception:
155 self.db.rollback()
156 raise
158 ################################################################################
160 def update_db(self) -> None:
161 # Ok, try and find the configuration table
162 print("Determining dak database revision ...")
163 cnf = Config()
164 logger = Logger("update-db")
165 modules = []
167 try:
168 # Build a connect string
169 if "DB::Service" in cnf: 169 ↛ 170line 169 didn't jump to line 170 because the condition on line 169 was never true
170 connect_str = "service=%s" % cnf["DB::Service"]
171 else:
172 connect_str = "dbname=%s" % (cnf["DB::Name"])
173 if "DB::Host" in cnf and cnf["DB::Host"] != "": 173 ↛ 174line 173 didn't jump to line 174 because the condition on line 173 was never true
174 connect_str += " host=%s" % (cnf["DB::Host"])
175 if "DB::Port" in cnf and cnf["DB::Port"] != "-1": 175 ↛ 176line 175 didn't jump to line 176 because the condition on line 175 was never true
176 connect_str += " port=%d" % (int(cnf["DB::Port"]))
178 self.db = psycopg2.connect(connect_str)
180 db_role = cnf.get("DB::Role")
181 if db_role: 181 ↛ 190line 181 didn't jump to line 190 because the condition on line 181 was always true
182 self.db.cursor().execute(
183 "SELECT set_config('role', %s, false)", (db_role,)
184 )
186 except Exception as e:
187 print("FATAL: Failed connect to database (%s)" % str(e))
188 sys.exit(1)
190 database_revision = int(self.get_db_rev())
191 logger.log(["transaction id before update: %s" % self.get_transaction_id()])
193 if database_revision == -1: 193 ↛ 194line 193 didn't jump to line 194 because the condition on line 193 was never true
194 print("dak database schema predates update-db.")
195 print()
196 print(
197 "This script will attempt to upgrade it to the lastest, but may fail."
198 )
199 print(
200 "Please make sure you have a database backup handy. If you don't, press Ctrl-C now!"
201 )
202 print()
203 print("Continuing in five seconds ...")
204 time.sleep(5)
205 print()
206 print("Attempting to upgrade pre-zero database to zero")
208 self.update_db_to_zero()
209 database_revision = 0
211 re_update_module = re.compile(r"update(\d+)")
212 required_database_schema = max(
213 int(match.group(1))
214 for module_info in pkgutil.iter_modules(dak.dakdb.__path__)
215 if (match := re_update_module.fullmatch(module_info.name))
216 )
218 print("dak database schema at %d" % database_revision)
219 print("dak version requires schema %d" % required_database_schema)
221 if database_revision < required_database_schema: 221 ↛ 235line 221 didn't jump to line 235 because the condition on line 221 was always true
222 print("\nUpdates to be applied:")
223 for i in range(database_revision + 1, required_database_schema + 1):
224 update_module = importlib.import_module(f"dak.dakdb.update{i}")
225 doc = update_module.__doc__
226 assert doc is not None
227 print("Update %d: %s" % (i, next(s for s in doc.split("\n") if s))) 227 ↛ exitline 227 didn't finish the generator expression on line 227
228 modules.append((update_module, i))
229 if not Config().find_b("Update-DB::Options::Yes", False): 229 ↛ 230line 229 didn't jump to line 230 because the condition on line 229 was never true
230 prompt = "\nUpdate database? (y/N) "
231 answer = utils.input_or_exit(prompt)
232 if answer.upper() != "Y":
233 sys.exit(0)
234 else:
235 print("no updates required")
236 logger.log(["no updates required"])
237 sys.exit(0)
239 for module in modules:
240 (update_module, i) = module
241 try:
242 self.apply_update(update_module, i)
243 message = "updated database schema from %d to %d" % (
244 database_revision,
245 i,
246 )
247 print(message)
248 logger.log([message])
249 except DBUpdateError as e:
250 # Seems the update did not work.
251 print(
252 "Was unable to update database schema from %d to %d."
253 % (database_revision, i)
254 )
255 print("The error message received was %s" % (e))
256 logger.log(["DB Schema upgrade failed"])
257 logger.close()
258 utils.fubar("DB Schema upgrade failed")
259 database_revision += 1
260 logger.close()
262 ################################################################################
264 def init(self) -> None:
265 cnf = Config()
266 arguments = [
267 ("h", "help", "Update-DB::Options::Help"),
268 ("y", "yes", "Update-DB::Options::Yes"),
269 ]
270 for i in ["help"]:
271 key = "Update-DB::Options::%s" % i
272 if key not in cnf: 272 ↛ 270line 272 didn't jump to line 270 because the condition on line 272 was always true
273 cnf[key] = ""
275 arguments = apt_pkg.parse_commandline(cnf.Cnf, arguments, sys.argv) # type: ignore[attr-defined]
277 options = cnf.subtree("Update-DB::Options")
278 if options["Help"]:
279 self.usage()
280 elif arguments: 280 ↛ 281line 280 didn't jump to line 281 because the condition on line 280 was never true
281 utils.warn("dak update-db takes no arguments.")
282 self.usage(exit_code=1)
284 try:
285 if os.path.isdir(cnf["Dir::Lock"]): 285 ↛ 286line 285 didn't jump to line 286 because the condition on line 285 was never true
286 lock_fd = os.open(
287 os.path.join(cnf["Dir::Lock"], "daily.lock"),
288 os.O_RDONLY | os.O_CREAT,
289 )
290 fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
291 else:
292 utils.warn("Lock directory doesn't exist yet - not locking")
293 except OSError as e:
294 if e.errno in (errno.EACCES, errno.EAGAIN):
295 utils.fubar(
296 "Couldn't obtain lock, looks like archive is doing something, try again later."
297 )
298 else:
299 raise
301 self.update_db()
304################################################################################
307def main() -> None:
308 app = UpdateDB()
309 app.init()