Skip to content

Update schema #274

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

Open
wants to merge 7 commits into
base: master
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
44 changes: 44 additions & 0 deletions notion/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ class Collection(Record):
python_to_api=markdown_to_notion,
)
cover = field_map("cover")
_schema_types = [
"text",
"number",
"select",
"multi_select",
"date",
"person",
"file",
"checkbox",
"url",
"email",
"phone_number",
"created_time",
"created_by",
"last_edited_time",
"last_edited_by",
"formula",
"rollup",
# "relation" # another contract for this type, requires another db at creation time
]


@property
def templates(self):
Expand All @@ -159,6 +180,29 @@ def templates(self):
self._templates = Templates(parent=self)
return self._templates

def update_schema_properties(self, props):
"""
Update current schema with props.
"""
schema = self.get("schema")
# check that props has a valid type
for _id, prop in props.items():
if prop["type"] not in self._schema_types:
logger.error("The type {} is unsupported.".format(prop["type"]))
return

logger.debug("Update current schema: {} with {}".format(schema, props))
schema.update(props)
self._client.submit_transaction(
build_operation(
id=self.id,
path=[],
args=dict(schema=schema),
command="update",
table="collection",
)
)

def get_schema_properties(self):
"""
Fetch a flattened list of all properties in the collection's schema.
Expand Down
14 changes: 14 additions & 0 deletions notion/smoke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ def run_live_smoke_test(token_v2, parent_page_url_or_id):
assert row1.some_date.timezone == timezone
assert row1.some_date.reminder == reminder

# Test update schema with new props
for type_ in ["number", "select", "multi_select", "date", "person", "file", "checkbox", "url", "email",
"phone_number", "created_time", "created_by", "last_edited_time", "last_edited_by", "formula",
"rollup", "text",
# "relation" # another contract for this type, requires another db at creation time
]:
cvb.collection.update_schema_properties({
type_: dict(
name=type_,
type=type_
)
})
assert cvb.collection.get_schema_property(type_)

print(
"Check it out and make sure it looks good, then press any key here to delete it..."
)
Expand Down