Script for tj_project template
This commit is contained in:
104
script/script/template.py
Normal file
104
script/script/template.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import psycopg as pg
|
||||
|
||||
sql_create = [
|
||||
"table/create/namespace.sql",
|
||||
"table/create/0.base.sql",
|
||||
"table/create/1.title.sql",
|
||||
"table/create/2.junctions.sql",
|
||||
"table/create/3.reservoirs.sql",
|
||||
"table/create/4.tanks.sql",
|
||||
"table/create/5.pipes.sql",
|
||||
"table/create/6.pumps.sql",
|
||||
"table/create/7.valves.sql",
|
||||
"table/create/8.tags.sql",
|
||||
"table/create/9.demands.sql",
|
||||
"table/create/10.status.sql",
|
||||
"table/create/11.patterns.sql",
|
||||
"table/create/12.curves.sql",
|
||||
"table/create/13.controls.sql",
|
||||
"table/create/14.rules.sql",
|
||||
"table/create/15.energy.sql",
|
||||
"table/create/16.emitters.sql",
|
||||
"table/create/17.quality.sql",
|
||||
"table/create/18.sources.sql",
|
||||
"table/create/19.reactions.sql",
|
||||
"table/create/20.mixing.sql",
|
||||
"table/create/21.times.sql",
|
||||
"table/create/22.report.sql",
|
||||
"table/create/23.options.sql",
|
||||
"table/create/24.coordinates.sql",
|
||||
"table/create/25.vertices.sql",
|
||||
"table/create/26.labels.sql",
|
||||
"table/create/27.backdrop.sql",
|
||||
"table/create/28.end.sql",
|
||||
"table/create/operation.sql"
|
||||
]
|
||||
|
||||
sql_drop = [
|
||||
"table/drop/operation.sql",
|
||||
"table/drop/28.end.sql",
|
||||
"table/drop/27.backdrop.sql",
|
||||
"table/drop/26.labels.sql",
|
||||
"table/drop/25.vertices.sql",
|
||||
"table/drop/24.coordinates.sql",
|
||||
"table/drop/23.options.sql",
|
||||
"table/drop/22.report.sql",
|
||||
"table/drop/21.times.sql",
|
||||
"table/drop/20.mixing.sql",
|
||||
"table/drop/19.reactions.sql",
|
||||
"table/drop/18.sources.sql",
|
||||
"table/drop/17.quality.sql",
|
||||
"table/drop/16.emitters.sql",
|
||||
"table/drop/15.energy.sql",
|
||||
"table/drop/14.rules.sql",
|
||||
"table/drop/13.controls.sql",
|
||||
"table/drop/12.curves.sql",
|
||||
"table/drop/11.patterns.sql",
|
||||
"table/drop/10.status.sql",
|
||||
"table/drop/9.demands.sql",
|
||||
"table/drop/8.tags.sql",
|
||||
"table/drop/7.valves.sql",
|
||||
"table/drop/6.pumps.sql",
|
||||
"table/drop/5.pipes.sql",
|
||||
"table/drop/4.tanks.sql",
|
||||
"table/drop/3.reservoirs.sql",
|
||||
"table/drop/2.junctions.sql",
|
||||
"table/drop/1.title.sql",
|
||||
"table/drop/0.base.sql",
|
||||
"table/drop/namespace.sql"
|
||||
]
|
||||
|
||||
def create_template():
|
||||
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("create database tj_project")
|
||||
with pg.connect(conninfo="dbname=tj_project") as conn:
|
||||
with conn.cursor() as cur:
|
||||
for sql in sql_create:
|
||||
with open(sql, "r") as f:
|
||||
cur.execute(f.read())
|
||||
print(f'executed {sql}')
|
||||
conn.commit()
|
||||
|
||||
def have_template():
|
||||
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("select * from pg_database where datname = 'tj_project'")
|
||||
return cur.rowcount > 0
|
||||
|
||||
def delete_template():
|
||||
with pg.connect(conninfo="dbname=tj_project") as conn:
|
||||
with conn.cursor() as cur:
|
||||
for sql in sql_drop:
|
||||
with open(sql, "r") as f:
|
||||
cur.execute(f.read())
|
||||
print(f'executed {sql}')
|
||||
conn.commit()
|
||||
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("drop database tj_project")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if (have_template()):
|
||||
delete_template()
|
||||
create_template()
|
||||
Reference in New Issue
Block a user