Skip to content

Build1 publisher3 min readPublished

A read-only Python script previews which production orders a new Delphi check would reject

One engineer maintains 488 units of Object Pascal over 342 MySQL tables, and the tooling layer added beside it sits in a tools/ directory separate from what users install and what Delphi units depend on.

The Engineer · Build desk

Illustration accompanying A read-only Python script previews which production orders a new Delphi check would reject

What happened

  • Its sole maintainer says a rewrite is never happening as a project with a start date, because it would mean running two systems in parallel for years while the business changes under both.
  • Python was added beside the Delphi instead: the Delphi build kept every unit and dependency it already had, and the scripts sit in a tools/ directory that ships to nobody.
  • A read-only script reimplements the export routine's queries, runs them against production data, and prints per order whether a proposed new validation would abort and why.

Compiled by The EngineerSomething wrong?How this is made

Why it matters

  • cost The export logic now exists twice in two languages, so whoever edits the Delphi queries owes a matching edit to the Python copy for the preview to stay honest.
  • decision A job used four times a year no longer has to win a week of Delphi screen work to get built.
  • exposure The full structure of a 342-table production database now sits in the repository, and the read-only scripts need production database credentials to run.

The schema dumper is the part worth copying. It connects to MySQL and writes the whole structure, tables, columns, types and indexes, into a single JSON file committed to the repository [6]. The maintainer calls it "the highest-leverage hundred lines in the project" [7]. The gain comes from committing it. Before the file existed, nobody could answer what a table looked like in March; now a diff on that file shows what the database did between two releases [8]. That file is also what changed the AI story here. An assistant with no schema in front of it guesses column names across 342 tables and produces plausible SQL that fails at runtime [9]. The repo instruction is blunt: check the schema file before touching any SQL, do not recall table names from memory [10]. The rule only holds while the dump matches the database, so a stale file would be worse than no rule. In this codebase the SQL is assembled at runtime, `Query.SQL.Add('SELECT ...')` spread across dozens of DAO units, and there is no way to ask the Delphi compiler which queries touch a given table [11]. A Python script parses those units with regular expressions, pulls every `SQL.Add` and `SQL.Text` assignment, reassembles the fragments and produces an inventory of every query in the application [12]. "Regex parsing of a real language is normally a bad idea," the maintainer wrote, adding that for finding string literals in a consistent pattern it is completely adequate and took an afternoon instead of a semester [13][14]. What comes back is an inventory of literals in the source. Where a unit branches between fragments, someone still reads the unit. The same approach on Delphi's form files takes 44 lines. It decodes the bitmask integers a VCL `TActionManager` uses for keyboard shortcuts back into `Ctrl+Shift+F4` and prints the whole shortcut map, a document that did not exist before [15]. "The useful reframe: the legacy source code is itself a dataset. You do not need the legacy language to query it," the post says [24]. The export routine pushes orders to the vendor ERP the company also runs. It needed a new validation that aborts on inconsistent data, and the open question was how many orders sitting in production the check would reject [16]. "In the old workflow, I would find out after deploying, from the people whose day I had just ruined," the maintainer wrote [26]. The answer now comes from a read-only script. It reimplements the exact queries the Delphi routine runs, executes them against production data, and prints per order whether the validation would abort and why, with no rebuild, no deploy and no write path [17]. "It is a deliberately redundant implementation, and the redundancy is the point," the maintainer wrote [18]. Two implementations of the same logic now exist in two languages, one of them cheap to run against real data and unable to write anything [19]. Every future edit to those Delphi queries needs a matching edit in the Python copy, or the preview describes a routine that no longer runs. The post does not say how the two are kept in step [25]. The layer is small. Roughly 100 lines for the schema dumper, 44 for the shortcut decoder and 260 for the spreadsheet importer come to about 404 lines, a quarter of one percent of the 160,000 lines of Object Pascal [23]. That tally leaves out the query parser, whose size the post never gives. That 260-line importer, openpyxl in and pymysql out, covers a job the maintainer priced at a week of Delphi screen work for something used four times a year [21][22]. For this to transfer, the generated artefacts have to stay authoritative. That means regenerating the schema file per release [8].

What to watch

  • Whether the schema JSON is regenerated automatically at release or by hand. The assistant rule depends on the dump matching the live database.
  • Whether the Python reimplementation of the export queries ever disagrees with the Delphi routine without anyone noticing.
  • Whether the query inventory gets used to drive a change, such as a table rename checked off against it, rather than only to document.
Loading claim ledger
Loading source directory links
Loading share composer
Loading topic controls
Loading related stories