Skip to content

Commit 666beff

Browse files
committed
Create an experimental marimo nb for creating relational dbs with a partially dynamic schema.
1 parent 9ff7678 commit 666beff

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

sandbox/sql_geodb.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import marimo
2+
3+
__generated_with = "0.14.10"
4+
app = marimo.App(width="medium")
5+
6+
7+
@app.cell
8+
def _():
9+
import marimo as mo
10+
import polars as pl
11+
from sqlalchemy import create_engine
12+
return create_engine, mo, pl
13+
14+
15+
@app.cell
16+
def _(pl):
17+
# Create DataFrames with one-to-many relationship
18+
customers = pl.DataFrame({
19+
"customer_id": [1, 2, 3],
20+
"name": ["Alice", "Bob", "Charlie"],
21+
22+
})
23+
24+
orders = pl.DataFrame({
25+
"order_id": [101, 102, 103, 104],
26+
"customer_id": [1, 1, 2, 3], # Foreign key reference
27+
"amount": [100.50, 75.25, 200.00, 50.00],
28+
"order_date": ["2024-01-15", "2024-01-16", "2024-01-17", "2024-01-18"]
29+
})
30+
return customers, orders
31+
32+
33+
@app.cell
34+
def _(create_engine, customers, mo, orders):
35+
# Create connection to new SQLite database (file will be created automatically)
36+
db_path = mo.notebook_location() / "database.db"
37+
engine = create_engine(f"sqlite:///{db_path}")
38+
39+
# Write tables - schema will be created automatically
40+
customers.write_database("customers", engine, if_table_exists="replace")
41+
orders.write_database("orders", engine, if_table_exists="replace")
42+
return
43+
44+
45+
@app.cell
46+
def _():
47+
return
48+
49+
50+
@app.cell
51+
def _(create_engine, inspect, pl, text):
52+
class ConfigurableDatabaseManager:
53+
def __init__(self, db_path: str, fk_config: dict[str, list[tuple]]):
54+
self.engine = create_engine(f"sqlite:///{db_path}")
55+
self.fk_config = fk_config # {"table_name": [("col", "ref_table", "ref_col"), ...]}
56+
57+
with self.engine.begin() as conn:
58+
conn.execute(text("PRAGMA foreign_keys = ON"))
59+
60+
def write_data_with_config(self, df: pl.DataFrame, table_name: str):
61+
"""Write data using predefined foreign key configuration"""
62+
foreign_keys = self.fk_config.get(table_name, [])
63+
64+
# Check if table exists and needs recreation for FK constraints
65+
inspector = inspect(self.engine)
66+
if table_name in inspector.get_table_names():
67+
# This class method is not defined
68+
self.recreate_table_with_fks(df, table_name, foreign_keys)
69+
else:
70+
# This class method is not defined
71+
self.create_table_with_fks(df, table_name, foreign_keys)
72+
73+
df.write_database(table_name, self.engine, if_table_exists="append")
74+
75+
# Usage with configuration
76+
# fk_relationships = {
77+
# "orders": [("customer_id", "customers", "customer_id")],
78+
# "order_items": [
79+
# ("order_id", "orders", "order_id"),
80+
# ("product_id", "products", "product_id")
81+
# ],
82+
# "reviews": [
83+
# ("customer_id", "customers", "customer_id"),
84+
# ("product_id", "products", "product_id")
85+
# ]
86+
# }
87+
88+
# db_manager = ConfigurableDatabaseManager("configured_db.db", fk_relationships)
89+
return
90+
91+
92+
if __name__ == "__main__":
93+
app.run()

0 commit comments

Comments
 (0)