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

Modified "processTable" to add composite primary keys when specified … #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions dbml_sqlite/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ast import IsNot
import re
import os
import uuid
Expand Down Expand Up @@ -82,6 +83,7 @@ def processFile(target, emulationMode, tableExists=True, indexExists=True, idxNa
for table in parsed.tables:
for index in table.indexes:
statements.append(processIndex(table, index, idxNameFunc, indexExists=indexExists, join=False))
break # We just need a single multi-field primary key
statements = list(chain.from_iterable(statements))
if join:
statements = "".join(statements)
Expand Down Expand Up @@ -167,6 +169,11 @@ def processTable(table, emulationMode, tableExists=True, join=True):
segments.append(processRef(ref, False))
if j < len(table.refs) - 1:
segments.append(',\n')
if hasattr(table, 'indexes'):
for k, index in enumerate(table.indexes):
if(index.pk):
segments.append(',\n')
segments.append(index.sql)
segments.append('\n);\n')
segments = list(chain.from_iterable(segments))
if join:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dbml_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_process_enum():
def test_process_file():
p = Path('./tests/abc.dbml')
o = processFile(p, 'full', True, True, MockNameFunc)
assert o == 'CREATE TABLE IF NOT EXISTS mytab (\n name TEXT,\n phone INTEGER\n);\nCREATE INDEX IF NOT EXISTS _mockname ON mytab (name, phone);\n'
assert o == 'CREATE TABLE IF NOT EXISTS mytab (\n name TEXT,\n phone INTEGER,\nPRIMARY KEY ("name", "phone")\n);\nCREATE INDEX IF NOT EXISTS _mockname ON mytab (name, phone);\n'

def test_process_index():
mytab = MockTable('mytab', [], [])
Expand Down