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

extract DBML code #5585

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions tools/download-database.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,54 @@ def main():
inserter.insert_all(defs)


# Extract DBML:
#
# Table NAME {
# following_user_id integer
# followed_user_id integer
# created_at timestamp
# }

export_DBML = True


class TableInserter:
def __init__(self, dbfile, jsondirectory):
self.db = sqlite3.connect(dbfile)
self.jsondirectory = jsondirectory

def insert_all(self, defs: 'TableDefinitions'):
if export_DBML:
print("\n\n\n\n----------\n")
for table_name in defs.table_names:
self.insert_table(table_name)
self.insert_table(table_name, export_DBML)

def insert_table(self, table_name, export_DBML):

if not export_DBML:
print(table_name)

def insert_table(self, table_name):
print(table_name)
with open(path.join(self.jsondirectory, f'{table_name}.json'), 'r', encoding='utf-8') as f:
table_data = json.load(f)
if not table_data['rows']:
return

table_data['rows'] = restore_types(table_data['rows'])

columns = determine_columns(table_data['rows'])

scalar_columns = [col for col in columns if not col.type.is_collection]
keycolumns = [find_col(scalar_columns, k) for k in table_data['key']]

if export_DBML:
if not len(scalar_columns) == 0:
print(f"Table {table_name.replace('-', '_')}" + "{") # DBML does not like dashes!

for c in columns:
if not c.type.is_collection:
print(" " + c.name, c.type.sql_def)

print("}")

table = SqlTableDef(table_name, scalar_columns, keycolumns)

cursor = self.db.cursor()
Expand All @@ -95,14 +120,19 @@ def insert_table(self, table_name):
f'{table.table_name}_{onetomanycol.name}',
table.key_columns + [onetomanycol],
table.key_columns + [onetomanycol] if listcol.type.is_set else [])
print(onetomanytable.table_name)

if not export_DBML:
print(onetomanytable.table_name)

cursor.execute(onetomanytable.drop_statement)
try:
# The `classes` table contains a list of objects, which this script can't
# deal with. Catch the error, but continue.
cursor.execute(onetomanytable.create_statement)
except Exception as e:
print(f'Dropping column {listcol.name} (running \'{onetomanytable.create_statement}\' leads to {e})')
if not export_DBML:
print(f'Dropping column {listcol.name} (running \'{
onetomanytable.create_statement}\' leads to {e})')
continue

one_to_many_data = []
Expand All @@ -117,7 +147,8 @@ def insert_table(self, table_name):

# Maps (not implemented yet)
for mapcol in (col for col in columns if col.type.is_map):
print(f'Dropping column: {table.original_name}.{mapcol.original_name} (no support for map columns)')
if not export_DBML:
print(f'Dropping column: {table.original_name}.{mapcol.original_name} (no support for map columns)')

self.db.commit()

Expand Down Expand Up @@ -183,7 +214,8 @@ def determine_columns(rows):
try:
existing_col.widen_type(SqlType.of(value))
except RuntimeError:
print(f'Issue in widening {existing_col.name}')
if not export_DBML:
print(f'Issue in widening {existing_col.name}')
else:
columns[key] = SqlColumn(key, SqlType.of(value))
return list(columns.values())
Expand Down
Loading