Skip to content

Commit a9e2946

Browse files
Merge pull request #80 from goldlabelapps/staging
This pull request introduces a new PATCH endpoint to factory reset all prospects' `flag` and `hide` fields, updates the project version, and adds a utility script for printing the current SQL schema for the `prospects` table. Additionally, the SQL file for creating the `prospects` table has been removed, possibly in favor of the new dynamic schema printer.
2 parents 1f0883e + d6c1235 commit a9e2946

5 files changed

Lines changed: 74 additions & 41 deletions

File tree

app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Python - FastAPI, Postgres, tsvector"""
22

33
# Current Version
4-
__version__ = "2.1.9"
4+
__version__ = "2.2.0"

app/api/prospects/prospects.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ def prospects_read_one(id: int = Path(..., description="ID of the prospect to re
135135
return {"meta": meta, "data": data}
136136

137137

138+
# PATCH endpoint to factory reset all prospects (ensure only one definition, at end of file)
139+
@router.patch("/prospects/factoryreset")
140+
def factory_reset_prospects() -> dict:
141+
"""Reset all prospects' flag and hide fields to False."""
142+
meta = make_meta("success", "Factory reset all prospects (flag, hide)")
143+
conn_gen = get_db_connection()
144+
conn = next(conn_gen)
145+
cur = conn.cursor()
146+
try:
147+
cur.execute("UPDATE prospects SET flag = FALSE, hide = FALSE;")
148+
conn.commit()
149+
data = {"reset": True}
150+
except Exception as e:
151+
meta = make_meta("error", f"Failed to factory reset: {str(e)}")
152+
data = {"reset": False}
153+
finally:
154+
cur.close()
155+
conn.close()
156+
return {"meta": meta, "data": data}
157+
138158
# PATCH endpoint to update flag/hide
139159
@router.patch("/prospects/{id}")
140160
def update_prospect(id: int = Path(..., description="ID of the prospect to update"), update: ProspectUpdate = Body(...)) -> dict:
@@ -174,4 +194,26 @@ def update_prospect(id: int = Path(..., description="ID of the prospect to updat
174194
finally:
175195
cur.close()
176196
conn.close()
197+
return {"meta": meta, "data": data}
198+
199+
200+
201+
# PATCH endpoint to factory reset all prospects (ensure only one definition, at end of file)
202+
@router.patch("/prospects/factoryreset")
203+
def factory_reset_prospects() -> dict:
204+
"""Reset all prospects' flag and hide fields to False."""
205+
meta = make_meta("success", "Factory reset all prospects (flag, hide)")
206+
conn_gen = get_db_connection()
207+
conn = next(conn_gen)
208+
cur = conn.cursor()
209+
try:
210+
cur.execute("UPDATE prospects SET flag = FALSE, hide = FALSE;")
211+
conn.commit()
212+
data = {"reset": True}
213+
except Exception as e:
214+
meta = make_meta("error", f"Failed to factory reset: {str(e)}")
215+
data = {"reset": False}
216+
finally:
217+
cur.close()
218+
conn.close()
177219
return {"meta": meta, "data": data}

app/api/prospects/sql/create_table.sql

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import sys
3+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../..')))
4+
from app.utils.db import get_db_connection_direct
5+
6+
def get_create_table_sql(table_name):
7+
conn = get_db_connection_direct()
8+
cur = conn.cursor()
9+
cur.execute("""
10+
SELECT column_name, data_type, is_nullable, column_default
11+
FROM information_schema.columns
12+
WHERE table_name = %s
13+
ORDER BY ordinal_position;
14+
""", (table_name,))
15+
columns = cur.fetchall()
16+
cur.close()
17+
conn.close()
18+
19+
lines = []
20+
for name, dtype, nullable, default in columns:
21+
line = f' {name} {dtype.upper()}'
22+
if nullable == 'NO':
23+
line += ' NOT NULL'
24+
if default:
25+
line += f' DEFAULT {default}'
26+
lines.append(line)
27+
create_sql = f"CREATE TABLE {table_name} (\n" + ",\n".join(lines) + "\n);"
28+
return create_sql
29+
30+
if __name__ == "__main__":
31+
print(get_create_table_sql('prospects'))

app/static/python copy.png

333 KB
Loading

0 commit comments

Comments
 (0)