better-notion 2.1.1__py3-none-any.whl → 2.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -93,10 +93,17 @@ class WorkspaceInitializer:
93
93
  logger.error(error_msg)
94
94
  raise Exception(error_msg) from e
95
95
 
96
- # Check for existing workspace unless skipped
97
- if not skip_detection:
98
- existing = await WorkspaceMetadata.detect_workspace(parent, self._client)
99
- if existing:
96
+ # Check for existing workspace
97
+ existing = await WorkspaceMetadata.detect_workspace(parent, self._client)
98
+
99
+ if existing:
100
+ if skip_detection:
101
+ # --reset flag: Delete all existing databases first
102
+ logger.warning(f"Reset mode: Deleting {len(existing.get('database_ids', {}))} existing databases...")
103
+ await self._delete_existing_databases(parent, existing.get("database_ids", {}))
104
+ logger.info("All existing databases deleted")
105
+ else:
106
+ # Normal mode: Raise error if workspace exists
100
107
  workspace_info = {
101
108
  "workspace_id": existing.get("workspace_id", "unknown"),
102
109
  "workspace_name": existing.get("workspace_name", workspace_name),
@@ -126,20 +133,28 @@ class WorkspaceInitializer:
126
133
  ("Incidents", "incidents", self._create_incidents_db),
127
134
  ]
128
135
 
129
- for display_name, key, create_func in databases_order:
130
- try:
131
- logger.info(f"Creating {display_name} database...")
132
- await create_func(parent)
133
- logger.info(f"✓ {display_name} database created: {self._database_ids[key]}")
134
- except Exception as e:
135
- error_msg = (
136
- f"Failed to create {display_name} database: {str(e)}\n"
137
- f"Databases created so far: {list(self._database_ids.keys())}\n"
138
- f"Parent page: {parent_page_id}\n"
139
- f"Workspace name: {workspace_name}"
140
- )
141
- logger.error(error_msg)
142
- raise Exception(error_msg) from e
136
+ try:
137
+ for display_name, key, create_func in databases_order:
138
+ try:
139
+ logger.info(f"Creating {display_name} database...")
140
+ await create_func(parent)
141
+ logger.info(f"✓ {display_name} database created: {self._database_ids[key]}")
142
+ except Exception as e:
143
+ # Clean up databases created so far
144
+ logger.error(f"Failed to create {display_name} database: {str(e)}")
145
+ logger.warning(f"Cleaning up {len(self._database_ids)} databases that were created...")
146
+ await self._delete_existing_databases(parent, self._database_ids)
147
+ error_msg = (
148
+ f"Failed to create {display_name} database: {str(e)}\n"
149
+ f"All partially created databases have been deleted.\n"
150
+ f"Parent page: {parent_page_id}\n"
151
+ f"Workspace name: {workspace_name}"
152
+ )
153
+ logger.error(error_msg)
154
+ raise Exception(error_msg) from e
155
+ except Exception as e:
156
+ # Re-raise the exception
157
+ raise e
143
158
 
144
159
  # Update cross-database relations that require both databases to exist
145
160
  await self._update_cross_database_relations()
@@ -337,6 +352,41 @@ class WorkspaceInitializer:
337
352
  # This is a no-op but documents the intent
338
353
  pass
339
354
 
355
+ async def _delete_existing_databases(self, parent: Page, database_ids: dict) -> None:
356
+ """Delete all existing databases in the parent page.
357
+
358
+ Args:
359
+ parent: Parent page
360
+ database_ids: Dict of database IDs to delete
361
+ """
362
+ import asyncio
363
+
364
+ deletion_tasks = []
365
+ for db_name, db_id in database_ids.items():
366
+ if db_id:
367
+ logger.info(f"Deleting {db_name} database ({db_id})...")
368
+ deletion_tasks.append(self._delete_database(db_id))
369
+
370
+ # Delete all databases concurrently
371
+ if deletion_tasks:
372
+ await asyncio.gather(*deletion_tasks, return_exceptions=True)
373
+
374
+ async def _delete_database(self, database_id: str) -> None:
375
+ """Delete a single database.
376
+
377
+ Args:
378
+ database_id: ID of the database to delete
379
+ """
380
+ try:
381
+ await self._client._api._request(
382
+ "DELETE",
383
+ f"/blocks/{database_id}"
384
+ )
385
+ logger.info(f"Deleted database {database_id}")
386
+ except Exception as e:
387
+ logger.warning(f"Failed to delete database {database_id}: {str(e)}")
388
+ # Continue with other deletions even if this one fails
389
+
340
390
  async def _update_self_relations(self, database_id: str) -> None:
341
391
  """Update self-referential relations in Tasks database.
342
392
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: better-notion
3
- Version: 2.1.1
3
+ Version: 2.1.2
4
4
  Summary: A high-level Python SDK for the Notion API with developer experience in mind.
5
5
  Project-URL: Homepage, https://github.com/nesalia-inc/better-notion
6
6
  Project-URL: Documentation, https://github.com/nesalia-inc/better-notion#readme
@@ -132,9 +132,9 @@ better_notion/utils/agents/project_context.py,sha256=aJlzy5H2rL4sAfW2jHL_3K2VkBJ
132
132
  better_notion/utils/agents/rbac.py,sha256=8ZA8Y7wbOiVZDbpjpH7iC35SZrZ0jl4fcJ3xWCm3SsE,11820
133
133
  better_notion/utils/agents/schemas.py,sha256=EpAEevoth-DGWepGMubRFjdvHfe7zrQxi9i4CHDvem8,22946
134
134
  better_notion/utils/agents/state_machine.py,sha256=xUBEeDTbU1Xq-rsRo2sbr6AUYpYrV9DTHOtZT2cWES8,6699
135
- better_notion/utils/agents/workspace.py,sha256=-EzPLFPjaD9PCeLkSdu1ddRa9-RlHgFvLJ2qxFou7X8,19713
136
- better_notion-2.1.1.dist-info/METADATA,sha256=5hImQwg_VfumZ1dMkCtz1QEWUtcYr8ywTvlPlREvnKE,11096
137
- better_notion-2.1.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
138
- better_notion-2.1.1.dist-info/entry_points.txt,sha256=D0bUcP7Z00Zyjxw7r2p29T95UrwioDO0aGDoHe9I6fo,55
139
- better_notion-2.1.1.dist-info/licenses/LICENSE,sha256=BAdN3JpgMY_y_fWqZSCFSvSbC2mTHP-BKDAzF5FXQAI,1069
140
- better_notion-2.1.1.dist-info/RECORD,,
135
+ better_notion/utils/agents/workspace.py,sha256=9BRe_qGMjdyR9jaAkm9NCCovqWjeY429LE7mP1fyumo,21894
136
+ better_notion-2.1.2.dist-info/METADATA,sha256=TxRPM7kRzFEcT7oiAZDSmSmtQ0nOOd7Xi5xY4i1Z6sY,11096
137
+ better_notion-2.1.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
138
+ better_notion-2.1.2.dist-info/entry_points.txt,sha256=D0bUcP7Z00Zyjxw7r2p29T95UrwioDO0aGDoHe9I6fo,55
139
+ better_notion-2.1.2.dist-info/licenses/LICENSE,sha256=BAdN3JpgMY_y_fWqZSCFSvSbC2mTHP-BKDAzF5FXQAI,1069
140
+ better_notion-2.1.2.dist-info/RECORD,,