Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-generated SQL scripts and SQLite DB #77

Merged
merged 8 commits into from
Jun 20, 2024

Conversation

dtemkin-volpe
Copy link
Collaborator

Uses Frictionless Framework to automatically generate SQLite database, then queries it for each tables' schema and outputs it as a .sql file. More details in the documentation: https://framework.frictionlessdata.io/docs/formats/sql.html

@dtemkin-volpe dtemkin-volpe requested a review from a team as a code owner June 18, 2024 14:12
Copy link
Collaborator

@ssmith55 ssmith55 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. If we are not using my original python code (jsontoSQLconverst.py), let's remove it. Question: Is this also generating the SQLite database, in addition to the SQL create table statements?

@dtemkin-volpe
Copy link
Collaborator Author

@ssmith55 yep, it's generating the SQLite database, then creating the SQL files by reading the table schemas from it. The relevant lines of code are below.

# Use https://alpha.sqliteviewer.app/ for verification!
os.remove(db_path / "gmns.sqlite")
create_db = package.publish(
    "sqlite:///" + (db_path / "gmns.sqlite").absolute().as_posix()
)

# Get list of every table and its schema
connection = sqlite3.connect(db_path / "gmns.sqlite")
cursor = connection.cursor()
cursor.execute("SELECT name, sql FROM sqlite_master WHERE type='table';")
list_of_tables = cursor.fetchall()
for table_name, table_sql in list_of_tables:
    with open(db_path / f"{table_name}.sql", "w") as table_file:
        # Add "IF NOT EXISTS" condition
        table_sql = table_sql.replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS")
        table_file.write(table_sql)
cursor.close()
connection.close()

@dtemkin-volpe dtemkin-volpe merged commit 24605cf into zephyr-data-specs:develop Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants