database init if it doesnt exist on background job start

This commit is contained in:
Sam Hadow 2024-02-01 18:42:16 +01:00
parent 17b4224771
commit 9ccf112daf
3 changed files with 36 additions and 7 deletions

View File

@ -7,13 +7,14 @@ from aliexpress import *
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
@app.route('/init') @app.route('/init', methods = ['GET'])
def init_db(): def init_db():
print("init") status = check_schema()
initialize() if not status:
return 'Hello, World!' initialize()
return str(status)
@app.route('/update') @app.route('/update', methods = ['GET'])
def update_hist(): def update_hist():
print("update") print("update")
update_items() update_items()
@ -44,13 +45,13 @@ def add_item():
# item not valid or can't be parsed # item not valid or can't be parsed
return jsonify({'status': 1}), 400 return jsonify({'status': 1}), 400
@app.route('/datahistory',methods = ['POST', 'GET']) @app.route('/datahistory',methods = ['GET'])
def data_history_request(): def data_history_request():
if request.method == 'GET': if request.method == 'GET':
print("fetching data history") print("fetching data history")
return jsonify(get_history()) return jsonify(get_history())
@app.route('/dataitem',methods = ['POST', 'GET']) @app.route('/dataitem',methods = ['GET'])
def data_item_request(): def data_item_request():
if request.method == 'GET': if request.method == 'GET':
print("fetching data item") print("fetching data item")

View File

@ -1,12 +1,18 @@
#!/usr/bin/python #!/usr/bin/python
import requests, re, time, os import requests, re, time, os
def init():
url = "http://127.0.0.1:8080/init"
response = requests.get(url)
print(response)
def update(): def update():
url = "http://127.0.0.1:8080/update" url = "http://127.0.0.1:8080/update"
response = requests.get(url) response = requests.get(url)
print(response) print(response)
if __name__ == '__main__': if __name__ == '__main__':
init()
regex_time = re.compile(r'([1-9][0-9]*)([smhd])') regex_time = re.compile(r'([1-9][0-9]*)([smhd])')
formatted_time = os.environ.get('WAIT_TIME') formatted_time = os.environ.get('WAIT_TIME')
units = { units = {

View File

@ -193,3 +193,25 @@ def initialize():
connection.commit() connection.commit()
connection.close() connection.close()
print("Database initialized") print("Database initialized")
def check_schema():
connection = connect_db()
cursor = connection.cursor()
cursor.execute("""
select
exists(
select *
from pg_tables
where tablename = 'item'
)
and exists(
select *
from pg_tables
where tablename = 'history'
);
""")
result = cursor.fetchall()
cursor.close()
connection.close()
return result[0][0]