Use Pony ORM to create tables.

With the release of Pony 0.8, we will have migration capabilities.
This commit is contained in:
Christopher T. Johnson
2025-02-12 10:22:33 -05:00
parent a1eb44f6cb
commit c7eff99cf6
2 changed files with 66 additions and 123 deletions

View File

@@ -215,132 +215,17 @@ class MainWindow(QMainWindow, Ui_MainWindow):
model.setQuery(query)
return
SQL_CMDS = [
# "PRAGMA foreign_keys=ON",
"CREATE TABLE IF NOT EXISTS cases "
"(case_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"docket_id TEXT, "
"linked INTEGER, "
"petitioners TEXT, respondents TEXT, date INTEGER, "
"active INTEGER, "
"FOREIGN KEY(linked) REFERENCES cases(case_id))",
#
"CREATE TABLE IF NOT EXISTS entries ("
"entry_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"case_id INTEGER, "
"date INTEGER, "
"text TEXT, "
"FOREIGN KEY(case_id) REFERENCES cases(case_id))",
#
"CREATE TABLE IF NOT EXISTS documents ("
"document_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"entry_id INTEGER, "
"name TEXT, "
"url TEXT, "
"FOREIGN KEY(entry_id) REFERENCES entries(entry_id))",
#
"CREATE TABLE IF NOT EXISTS history ("
"history_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"year TEXT, "
"edocket INTEGER, "
"number INTEGER)",
]
def schema_update(db: QSqlDatabase) -> None:
query = QSqlQuery()
for sql in SQL_CMDS:
inlower = sql.lower().strip()
if not inlower.startswith("create table "):
if not query.exec(sql):
query_error(query)
continue
create_cmd = re.sub(r"IF NOT EXISTS ", "", sql.strip())
create_cmd = re.sub(r"\s\s*", " ", create_cmd)
matches = re.search(r"^(CREATE TABLE )([^ ]+)( \(.+)$", create_cmd)
if matches:
table_name = matches.group(2)
create_cmd = (
matches.group(1)
+ '"'
+ matches.group(2)
+ '"'
+ matches.group(3)
)
else:
raise AttributeError(f"No match found: {create_cmd}")
print(f"Table name = {table_name}")
query.prepare("SELECT sql FROM sqlite_schema WHERE tbl_name = :tbl")
query.bindValue(":tbl", table_name)
if not query.exec():
query_error(query)
if not query.next():
print(sql)
if not query.exec(sql):
query_error(query)
continue
old = query.value(0)
if old.lower() == create_cmd.lower():
continue
print(old.lower())
print(create_cmd.lower())
print(translate("MainWindow", "Updating: ") + f"{table_name}")
# Step 1 turn off foreign key constraints
if not query.exec("PRAGMA foreign_keys=OFF"):
query_error(query)
# Step 2 start a transaction
db.transaction()
# Step 3 remember old indexes, triggers, and views
# Step 4 create new table
new_table_name = table_name + "_new"
sql = matches.group(1) + new_table_name + matches.group(3)
if not query.exec(sql):
query_error(query)
# step 5 transfer content
coldefs = re.search(r"\((.+)\)", old).group(1).split(", ") # type: ignore[union-attr]
cols = [
x.split(" ")[0]
for x in filter(lambda s: not s.startswith("FOREIGN "), coldefs)
]
cols_str = ", ".join(cols)
sql = f"INSERT INTO {new_table_name} ({cols_str}) SELECT {cols_str} FROM {table_name}"
query.prepare(sql)
if not query.exec():
query_error(query)
# step 6 Drop old table
query.prepare("DROP TABLE " + table_name)
if not query.exec():
query_error(query)
# step 6 rename new table to old table
query.prepare(
"ALTER TABLE " + new_table_name + " RENAME TO " + table_name
)
if not query.exec():
query_error(query)
# step 8 create indexes, triggers, and views
# step 9 rebuild affected views
# step 10 turn foreign key constrants back on
# if not query.exec("PRAGMA foreign_keys=ON"):
# query_error(query)
# step 11 commit the changes
db.commit()
return
def main() -> int:
#
app = QApplication(sys.argv)
db = QSqlDatabase.addDatabase("QSQLITE")
# db.setConnectOptions("PRAGMA foreign_keys = ON")
db.setDatabaseName("scotus.db")
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("ceph5")
db.setDatabaseName("scotus")
db.setUserName("scotus")
db.setPassword("lechOtvirf8Om/")
db.open()
schema_update(db)
MainWindow()
import lib.dbConfig
window = MainWindow()
return app.exec()