Skip to content

Commit

Permalink
batch deploy-cf in 100 querys
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickwasused committed Oct 31, 2024
1 parent 575261e commit bb40e16
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions d1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ def load_json(file):
f = open(file, encoding="utf8")
return load(f)


x=client.d1.database.list(account_id=os.getenv("CF_ACCOUNT_ID"))

station_path = (
Path(__file__)
.parent.absolute()
Expand All @@ -23,10 +20,12 @@ def load_json(file):

def export_stations():
station_data = load_json(station_path)
tmp_statements = ["DELETE FROM stations;"]
tmp_lists = []
tmp_list = []
counter = 0
for station in station_data:
# FIXME: the should be a better way for this
tmp_statements.append("INSERT INTO stations (id,name,lat,lng,address,city,state,postcode,"
tmp_list.append("INSERT INTO stations (id,name,lat,lng,address,city,state,postcode,"
"country_code,telephone,site_brand,watchlist_id,website,fuel,facilities) "
"VALUES ({},\"{}\",{},{},\"{}\",\"{}\",\"{}\",{},\"{}\",\"{}\",\"{}\",{},\"{}\",\"{}\",\"{}\");".format(station["watchlist_id"],
station["name"],
Expand All @@ -43,7 +42,21 @@ def export_stations():
station["website"],
", ".join(station["products"]),
", ".join(station["facilities"])))
client.d1.database.query(account_id=os.getenv("CF_ACCOUNT_ID"), database_id=os.getenv("CF_DATABASE_ID"), sql="".join(tmp_statements))
counter = counter + 1
if len(tmp_list) > 98:
tmp_lists.append(tmp_list)
tmp_list = []
counter = 0

querys = ["DELETE FROM stations;"]
tmp_query = []
for entry in tmp_lists:
for station in entry:
tmp_query.append(station)
querys.append("".join(tmp_query))
tmp_query = []
for query in querys:
client.d1.database.query(account_id=os.getenv("CF_ACCOUNT_ID"), database_id=os.getenv("CF_DATABASE_ID"), sql=query)


if __name__ == "__main__":
Expand Down

0 comments on commit bb40e16

Please sign in to comment.