Use Pony ORM to create tables.
With the release of Pony 0.8, we will have migration capabilities.
This commit is contained in:
58
lib/dbConfig.py
Normal file
58
lib/dbConfig.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from pony.orm import (
|
||||
Database,
|
||||
LongStr,
|
||||
Optional,
|
||||
PrimaryKey,
|
||||
Required,
|
||||
Set,
|
||||
set_sql_debug,
|
||||
)
|
||||
|
||||
db = Database
|
||||
|
||||
db = Database()
|
||||
|
||||
|
||||
class Cases(db.Entity):
|
||||
case_id = PrimaryKey(int, auto=True)
|
||||
docket_id = Required(str)
|
||||
petitioners = Required(str)
|
||||
respondents = Optional(str)
|
||||
date = Required(date)
|
||||
active = Required(bool)
|
||||
entries = Set("Entries")
|
||||
lhs_id = Set('Cases', column='lhs')
|
||||
rhs_id = Set('Cases', column='rhs')
|
||||
|
||||
class Entries(db.Entity):
|
||||
entry_id = PrimaryKey(int, auto=True)
|
||||
case_id = Required(Cases)
|
||||
date = Required(date)
|
||||
text = Required(LongStr)
|
||||
documents = Set('Documents')
|
||||
|
||||
class Documents(db.Entity):
|
||||
document_id = PrimaryKey(int, auto=True)
|
||||
entry_id = Required(Entries)
|
||||
name = Required(str)
|
||||
url = Required(LongStr)
|
||||
|
||||
class History(db.Entity):
|
||||
history_id = PrimaryKey(int, auto=True)
|
||||
year = Required(str, max_len=3)
|
||||
edocket = Required(bool)
|
||||
number = Required(int)
|
||||
|
||||
set_sql_debug(True)
|
||||
db.bind(
|
||||
provider="mysql",
|
||||
user="scotus",
|
||||
host="ceph5",
|
||||
database="scotus",
|
||||
password="lechOtvirf8Om/",
|
||||
)
|
||||
db.generate_mapping(create_tables=True)
|
||||
db.disconnect()
|
||||
131
scotus-pull.py
131
scotus-pull.py
@@ -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()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user