From cbc298a629f5c14b336d9cbfbef4287ca480f539 Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 8 Jan 2020 01:47:12 +0100 Subject: [PATCH] Fix duplicate 'users' table insertion --- Pipfile | 11 +++++++++++ code/database/db_setup.py | 11 +++++++---- code/database/{storage.py => export.py} | 8 ++++---- code/database/parser.py | 14 +++++++------- data/{users.csv => user.csv} | 0 5 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 Pipfile rename code/database/{storage.py => export.py} (84%) rename data/{users.csv => user.csv} (100%) diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..b5846df --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] + +[requires] +python_version = "3.8" diff --git a/code/database/db_setup.py b/code/database/db_setup.py index 69d0655..397fa27 100644 --- a/code/database/db_setup.py +++ b/code/database/db_setup.py @@ -1,7 +1,7 @@ from subprocess import run from flask_sqlalchemy import SQLAlchemy -from flask import Flask from datetime import datetime +from flask import Flask from constants import DB_NAME, DB_USER, DB_PW @@ -42,6 +42,7 @@ class Glacier(db.Model): class Annual_Data(db.Model): + __tablename__ = "annual_data" year = db.Column(db.Integer, primary_key=True) uid = db.Column(db.String(5), db.ForeignKey("glacier.uid"), primary_key=True) surface = db.Column(db.Integer) @@ -56,7 +57,8 @@ class Annual_Data(db.Model): class Annual_Change(db.Model): - year = db.Column(db.Integer, db.ForeignKey("annual__data.year"), primary_key=True) + __tablename__ = "annual_change" + year = db.Column(db.Integer, db.ForeignKey("annual_data.year"), primary_key=True) uid = db.Column(db.String(5), db.ForeignKey("glacier.uid"), primary_key=True) surface_change = db.Column(db.Integer) length_change = db.Column(db.Integer) @@ -70,13 +72,14 @@ class Annual_Change(db.Model): class User(db.Model): uid = db.Column(db.Integer, primary_key=True) - registration_date = db.Column(db.DateTime, nullable=False) + registration_date = db.Column( + db.DateTime, nullable=False, server_default=db.func.now() + ) username = db.Column(db.String(20), nullable=False, unique=True) password = db.Column(db.String(60)) def __init__(self, uid, username, password): self.uid = uid - self.registration_date = datetime.today() self.username = username self.password = password diff --git a/code/database/storage.py b/code/database/export.py similarity index 84% rename from code/database/storage.py rename to code/database/export.py index 019105b..f77e4c9 100644 --- a/code/database/storage.py +++ b/code/database/export.py @@ -15,9 +15,9 @@ def create_connection() -> engine: def create_dataframes() -> DataFrame: files = { "glacier": "../data/glacier.csv", - "annual__data": "../data/annual_data.csv", - "annual__change": "../data/annual_change.csv", - "users": "../data/users.csv", + "annual_data": "../data/annual_data.csv", + "annual_change": "../data/annual_change.csv", + "user": "../data/user.csv", } df_list = {} for csv in files.keys(): @@ -27,7 +27,7 @@ def create_dataframes() -> DataFrame: def insert_data(df_list, conn): for key, value in df_list.items(): - value.to_sql(key, con=conn, index=False, if_exists="append", chunksize=1) + value.to_sql(key, con=conn, index=False, if_exists="append") def main(): diff --git a/code/database/parser.py b/code/database/parser.py index 9415465..29df0cc 100644 --- a/code/database/parser.py +++ b/code/database/parser.py @@ -51,18 +51,18 @@ def rename_fields(df_list): def create_databases(df): - users = {"uid": [7843], "username": ["admin"], "password": [ADMIN_PW]} + user = {"uid": [7843], "username": ["admin"], "password": [ADMIN_PW]} files = { "glacier": "../data/glacier.csv", - "annual__data": "../data/annual_data.csv", - "annual__change": "../data/annual_change.csv", - "users": "../data/users.csv", + "annual_data": "../data/annual_data.csv", + "annual_change": "../data/annual_change.csv", + "user": "../data/user.csv", } dataframes = { "glacier": df[["POLITICAL_UNIT", "NAME", "WGMS_ID"]].drop_duplicates(), - "annual__data": df[["WGMS_ID", "YEAR", "AREA", "MEDIAN_ELEVATION", "LENGTH"]], - "annual__change": df[["WGMS_ID", "YEAR"]], - "users": DataFrame(users), + "annual_data": df[["WGMS_ID", "YEAR", "AREA", "MEDIAN_ELEVATION", "LENGTH"]], + "annual_change": df[["WGMS_ID", "YEAR"]], + "user": DataFrame(user), } renamed_dfs = rename_fields(dataframes) for key, val in renamed_dfs.items(): diff --git a/data/users.csv b/data/user.csv similarity index 100% rename from data/users.csv rename to data/user.csv