mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 12:54:15 +02:00
black formatting
This commit is contained in:
parent
1c9e21f9a2
commit
3dba2029b5
4 changed files with 84 additions and 62 deletions
|
|
@ -78,18 +78,24 @@ class ForeignKey:
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, ForeignKey):
|
if isinstance(other, ForeignKey):
|
||||||
return all((
|
return all(
|
||||||
self.table == other.table,
|
(
|
||||||
self.column == other.column,
|
self.table == other.table,
|
||||||
self.other_table == other.other_table,
|
self.column == other.column,
|
||||||
self.other_column == other.other_column,
|
self.other_table == other.other_table,
|
||||||
))
|
self.other_column == other.other_column,
|
||||||
|
)
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
if isinstance(other, ForeignKey):
|
if isinstance(other, ForeignKey):
|
||||||
return ((self.table, self.column, self.other_table, self.other_column) <
|
return (self.table, self.column, self.other_table, self.other_column) < (
|
||||||
(other.table, other.column, other.other_table, other.other_column))
|
other.table,
|
||||||
|
other.column,
|
||||||
|
other.other_table,
|
||||||
|
other.other_column,
|
||||||
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|
@ -102,15 +108,16 @@ class ForeignKey:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sql(self):
|
def sql(self):
|
||||||
return "FOREIGN KEY({column}) REFERENCES [{other_table}]({other_column})".format(
|
return (
|
||||||
table=self.table,
|
"FOREIGN KEY({column}) REFERENCES [{other_table}]({other_column})".format(
|
||||||
column=self.column_str,
|
table=self.table,
|
||||||
other_table=self.other_table,
|
column=self.column_str,
|
||||||
other_column=self.other_column_str,
|
other_table=self.other_table,
|
||||||
|
other_column=self.other_column_str,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT = object()
|
DEFAULT = object()
|
||||||
|
|
||||||
COLUMN_TYPE_MAPPING = {
|
COLUMN_TYPE_MAPPING = {
|
||||||
|
|
@ -416,12 +423,8 @@ class Database:
|
||||||
# Soundness check foreign_keys point to existing tables
|
# Soundness check foreign_keys point to existing tables
|
||||||
for fk in foreign_keys:
|
for fk in foreign_keys:
|
||||||
for oc in fk.other_column:
|
for oc in fk.other_column:
|
||||||
if not any(
|
if not any(c for c in self[fk.other_table].columns if c.name == oc):
|
||||||
c for c in self[fk.other_table].columns if c.name == oc
|
raise AlterError("No such column: {}.{}".format(fk.other_table, oc))
|
||||||
):
|
|
||||||
raise AlterError(
|
|
||||||
"No such column: {}.{}".format(fk.other_table, oc)
|
|
||||||
)
|
|
||||||
|
|
||||||
column_defs = []
|
column_defs = []
|
||||||
# ensure pk is a tuple
|
# ensure pk is a tuple
|
||||||
|
|
@ -558,10 +561,7 @@ class Database:
|
||||||
if not self[fk.other_table].exists():
|
if not self[fk.other_table].exists():
|
||||||
raise AlterError("No such other_table: {}".format(fk.other_table))
|
raise AlterError("No such other_table: {}".format(fk.other_table))
|
||||||
for c in fk.other_column:
|
for c in fk.other_column:
|
||||||
if (
|
if c != "rowid" and c not in self[fk.other_table].columns_dict:
|
||||||
c != "rowid"
|
|
||||||
and c not in self[fk.other_table].columns_dict
|
|
||||||
):
|
|
||||||
raise AlterError(
|
raise AlterError(
|
||||||
"No such other_column: {} in {}".format(c, fk.other_table)
|
"No such other_column: {} in {}".format(c, fk.other_table)
|
||||||
)
|
)
|
||||||
|
|
@ -613,9 +613,7 @@ class Database:
|
||||||
def index_foreign_keys(self):
|
def index_foreign_keys(self):
|
||||||
for table_name in self.table_names():
|
for table_name in self.table_names():
|
||||||
table = self[table_name]
|
table = self[table_name]
|
||||||
existing_indexes = {
|
existing_indexes = {tuple(i.columns) for i in table.indexes}
|
||||||
tuple(i.columns) for i in table.indexes
|
|
||||||
}
|
|
||||||
for fk in table.foreign_keys:
|
for fk in table.foreign_keys:
|
||||||
if fk.column not in existing_indexes:
|
if fk.column not in existing_indexes:
|
||||||
table.create_index(fk.column)
|
table.create_index(fk.column)
|
||||||
|
|
@ -769,12 +767,15 @@ class Table(Queryable):
|
||||||
fks[id]["column"].append(from_)
|
fks[id]["column"].append(from_)
|
||||||
fks[id]["other_table"] = table_name
|
fks[id]["other_table"] = table_name
|
||||||
fks[id]["other_column"].append(to_)
|
fks[id]["other_column"].append(to_)
|
||||||
return [ForeignKey(
|
return [
|
||||||
table=fk['table'],
|
ForeignKey(
|
||||||
column=tuple(fk['column']),
|
table=fk["table"],
|
||||||
other_table=fk['other_table'],
|
column=tuple(fk["column"]),
|
||||||
other_column=tuple(fk['other_column']),
|
other_table=fk["other_table"],
|
||||||
) for fk in fks.values()]
|
other_column=tuple(fk["other_column"]),
|
||||||
|
)
|
||||||
|
for fk in fks.values()
|
||||||
|
]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def virtual_table_using(self):
|
def virtual_table_using(self):
|
||||||
|
|
|
||||||
|
|
@ -355,7 +355,10 @@ def test_add_foreign_key(fresh_db):
|
||||||
assert isinstance(t, Table) and t.name == "books"
|
assert isinstance(t, Table) and t.name == "books"
|
||||||
assert [
|
assert [
|
||||||
ForeignKey(
|
ForeignKey(
|
||||||
table="books", column=("author_id",), other_table="authors", other_column=("id",)
|
table="books",
|
||||||
|
column=("author_id",),
|
||||||
|
other_table="authors",
|
||||||
|
other_column=("id",),
|
||||||
)
|
)
|
||||||
] == fresh_db["books"].foreign_keys
|
] == fresh_db["books"].foreign_keys
|
||||||
|
|
||||||
|
|
@ -364,9 +367,9 @@ def test_add_compound_foreign_key(fresh_db):
|
||||||
fresh_db["authors"].insert_all(
|
fresh_db["authors"].insert_all(
|
||||||
[
|
[
|
||||||
{"id": 1, "person_id": 1, "name": "Sally"},
|
{"id": 1, "person_id": 1, "name": "Sally"},
|
||||||
{"id": 2, "person_id": 2, "name": "Asheesh"}
|
{"id": 2, "person_id": 2, "name": "Asheesh"},
|
||||||
],
|
],
|
||||||
pk=("id", "person_id")
|
pk=("id", "person_id"),
|
||||||
)
|
)
|
||||||
fresh_db["books"].insert_all(
|
fresh_db["books"].insert_all(
|
||||||
[
|
[
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,9 @@ def test_extract_single_column(fresh_db, table, fk_column):
|
||||||
" [name] TEXT,\n"
|
" [name] TEXT,\n"
|
||||||
" [{}] INTEGER,\n".format(expected_fk)
|
" [{}] INTEGER,\n".format(expected_fk)
|
||||||
+ " [end] INTEGER,\n"
|
+ " [end] INTEGER,\n"
|
||||||
+ " FOREIGN KEY([{}]) REFERENCES [{}]([id])\n".format(expected_fk, expected_table)
|
+ " FOREIGN KEY([{}]) REFERENCES [{}]([id])\n".format(
|
||||||
|
expected_fk, expected_table
|
||||||
|
)
|
||||||
+ ")"
|
+ ")"
|
||||||
)
|
)
|
||||||
assert fresh_db[expected_table].schema == (
|
assert fresh_db[expected_table].schema == (
|
||||||
|
|
|
||||||
|
|
@ -30,17 +30,25 @@ def test_insert_m2m_list(fresh_db):
|
||||||
assert [{"id": 1, "name": "Natalie D"}, {"id": 2, "name": "Simon W"}] == list(
|
assert [{"id": 1, "name": "Natalie D"}, {"id": 2, "name": "Simon W"}] == list(
|
||||||
humans.rows
|
humans.rows
|
||||||
)
|
)
|
||||||
assert sorted([
|
assert (
|
||||||
ForeignKey(
|
sorted(
|
||||||
table="dogs_humans", column="dogs_id", other_table="dogs", other_column="id"
|
[
|
||||||
),
|
ForeignKey(
|
||||||
ForeignKey(
|
table="dogs_humans",
|
||||||
table="dogs_humans",
|
column="dogs_id",
|
||||||
column="humans_id",
|
other_table="dogs",
|
||||||
other_table="humans",
|
other_column="id",
|
||||||
other_column="id",
|
),
|
||||||
),
|
ForeignKey(
|
||||||
]) == sorted(dogs_humans.foreign_keys)
|
table="dogs_humans",
|
||||||
|
column="humans_id",
|
||||||
|
other_table="humans",
|
||||||
|
other_column="id",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
== sorted(dogs_humans.foreign_keys)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_insert_m2m_iterable(fresh_db):
|
def test_insert_m2m_iterable(fresh_db):
|
||||||
|
|
@ -103,17 +111,25 @@ def test_m2m_lookup(fresh_db):
|
||||||
tags = fresh_db["tags"]
|
tags = fresh_db["tags"]
|
||||||
assert people_tags.exists()
|
assert people_tags.exists()
|
||||||
assert tags.exists()
|
assert tags.exists()
|
||||||
assert sorted([
|
assert (
|
||||||
ForeignKey(
|
sorted(
|
||||||
table="people_tags",
|
[
|
||||||
column="people_id",
|
ForeignKey(
|
||||||
other_table="people",
|
table="people_tags",
|
||||||
other_column="id",
|
column="people_id",
|
||||||
),
|
other_table="people",
|
||||||
ForeignKey(
|
other_column="id",
|
||||||
table="people_tags", column="tags_id", other_table="tags", other_column="id"
|
),
|
||||||
),
|
ForeignKey(
|
||||||
]) == sorted(people_tags.foreign_keys)
|
table="people_tags",
|
||||||
|
column="tags_id",
|
||||||
|
other_table="tags",
|
||||||
|
other_column="id",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
== sorted(people_tags.foreign_keys)
|
||||||
|
)
|
||||||
assert [{"people_id": 1, "tags_id": 1}] == list(people_tags.rows)
|
assert [{"people_id": 1, "tags_id": 1}] == list(people_tags.rows)
|
||||||
assert [{"id": 1, "name": "Wahyu"}] == list(people.rows)
|
assert [{"id": 1, "name": "Wahyu"}] == list(people.rows)
|
||||||
assert [{"id": 1, "tag": "Coworker"}] == list(tags.rows)
|
assert [{"id": 1, "tag": "Coworker"}] == list(tags.rows)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue