half-orm-dev 1.0.0a34__tar.gz → 1.0.0a38__tar.gz

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.
Files changed (86) hide show
  1. {half_orm_dev-1.0.0a34/half_orm_dev.egg-info → half_orm_dev-1.0.0a38}/PKG-INFO +1 -1
  2. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/check.py +107 -41
  3. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/restore.py +11 -16
  4. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/main.py +21 -2
  5. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli_extension.py +6 -1
  6. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/file_executor.py +3 -1
  7. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/hgit.py +110 -33
  8. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migration_manager.py +12 -21
  9. half_orm_dev-1.0.0a38/half_orm_dev/migrations/1/0/0/a35/00_drop_stale_release_schemas.py +145 -0
  10. half_orm_dev-1.0.0a38/half_orm_dev/migrations/1/0/0/a36/00_drop_release_schemas.py +123 -0
  11. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/patch_manager.py +166 -342
  12. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/release_file.py +58 -0
  13. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/release_manager.py +155 -88
  14. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/repo.py +113 -181
  15. half_orm_dev-1.0.0a38/half_orm_dev/version.txt +1 -0
  16. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38/half_orm_dev.egg-info}/PKG-INFO +1 -1
  17. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev.egg-info/SOURCES.txt +2 -0
  18. half_orm_dev-1.0.0a34/half_orm_dev/version.txt +0 -1
  19. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/AUTHORS +0 -0
  20. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/LICENSE +0 -0
  21. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/README.md +0 -0
  22. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/__init__.py +0 -0
  23. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/_bootstrap_runner.py +0 -0
  24. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/__init__.py +0 -0
  25. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/__init__.py +0 -0
  26. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/apply.py +0 -0
  27. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/clone.py +0 -0
  28. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/init.py +0 -0
  29. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/migrate.py +0 -0
  30. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/patch.py +0 -0
  31. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/recover.py +0 -0
  32. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/release.py +0 -0
  33. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/revert_migration.py +0 -0
  34. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/rollback.py +0 -0
  35. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/set_git_origin.py +0 -0
  36. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/sync.py +0 -0
  37. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/todo.py +0 -0
  38. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/undo.py +0 -0
  39. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/cli/commands/upgrade.py +0 -0
  40. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/database.py +0 -0
  41. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/decorators.py +0 -0
  42. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/0/17/1/00_move_to_hop.py +0 -0
  43. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/0/17/1/01_txt_to_toml.py +0 -0
  44. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/0/17/4/00_toml_dict_format.py +0 -0
  45. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/0/17/4/02_move_patches_to_subdirs.py +0 -0
  46. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/0/17/5/01_update_pyproject_dependency.py +0 -0
  47. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/0/18/0/00_add_async_support.py +0 -0
  48. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/0/18/0/01_update_default_tests.py +0 -0
  49. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/1/0/0/a20/01_update_gitignore.py +0 -0
  50. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/1/0/0/a33/00_metadata_to_data_sql.py +0 -0
  51. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/migrations/hop/BREAKING_CHANGES-1.0.0.md +0 -0
  52. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/modules.py +0 -0
  53. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/patch_validator.py +0 -0
  54. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/patches/0/1/0/00_half_orm_meta.database.sql +0 -0
  55. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/patches/0/1/0/01_alter_half_orm_meta.hop_release.sql +0 -0
  56. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/patches/0/1/0/02_half_orm_meta.view.hop_penultimate_release.sql +0 -0
  57. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/patches/log +0 -0
  58. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/patches/sql/half_orm_meta.sql +0 -0
  59. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/py.typed +0 -0
  60. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/scripts/repair-metadata.py +0 -0
  61. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/.gitignore +0 -0
  62. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/MANIFEST.in +0 -0
  63. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/README +0 -0
  64. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/conftest_template +0 -0
  65. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/git-hooks/pre-commit +0 -0
  66. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/git-hooks/pre-push +0 -0
  67. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/git-hooks/prepare-commit-msg +0 -0
  68. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/git-hooks/reference-transaction +0 -0
  69. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/init_module_template +0 -0
  70. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/module_template_1 +0 -0
  71. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/module_template_2 +0 -0
  72. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/module_template_3 +0 -0
  73. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/pyproject.toml +0 -0
  74. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/relation_test +0 -0
  75. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/sql_adapter +0 -0
  76. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/templates/warning +0 -0
  77. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/utils.py +0 -0
  78. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev/venv_setup.py +0 -0
  79. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev.egg-info/dependency_links.txt +0 -0
  80. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev.egg-info/entry_points.txt +0 -0
  81. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev.egg-info/requires.txt +0 -0
  82. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/half_orm_dev.egg-info/top_level.txt +0 -0
  83. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/pyproject.toml +0 -0
  84. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/setup.cfg +0 -0
  85. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/setup.py +0 -0
  86. {half_orm_dev-1.0.0a34 → half_orm_dev-1.0.0a38}/tests/test_bootstrap_runner.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: half_orm_dev
3
- Version: 1.0.0a34
3
+ Version: 1.0.0a38
4
4
  Summary: half_orm development Framework.
5
5
  Author-email: Joël Maïzi <joel.maizi@collorg.org>
6
6
  License-Expression: GPL-3.0-or-later
@@ -104,12 +104,28 @@ def _display_check_results(repo, result: dict, dry_run: bool, verbose: bool):
104
104
  click.echo(f"✓ {utils.Color.green(f'half_orm_dev: {current} (latest)')}")
105
105
  click.echo()
106
106
 
107
- # Production version
107
+ # Production version (read on ho-prod)
108
108
  production_version = result.get('production_version')
109
109
  if production_version:
110
110
  click.echo(f"📦 {utils.Color.bold('Production version:')} {utils.Color.green(production_version)}")
111
111
  click.echo()
112
112
 
113
+ # The current branch's .hop/ predates that production version: say so
114
+ # instead of letting the numbers above look like this branch's state.
115
+ behind = result.get('branch_behind_ho_prod')
116
+ if behind:
117
+ click.echo(
118
+ f"⚠️ {utils.Color.bold(behind['branch'])} still records production "
119
+ f"{utils.Color.red(behind['branch_version'])}, "
120
+ f"while ho-prod is on {utils.Color.green(behind['production_version'])}."
121
+ )
122
+ click.echo(
123
+ f" Its .hop/ stopped receiving updates, so the release context "
124
+ f"rebuilt from it is stale."
125
+ )
126
+ click.echo(f" Bring it up to date: {utils.Color.bold('git merge ho-prod')}")
127
+ click.echo()
128
+
113
129
  # Hooks
114
130
  hooks = result.get('hooks', {})
115
131
  if hooks.get('installed'):
@@ -186,54 +202,104 @@ def _display_check_results(repo, result: dict, dry_run: bool, verbose: bool):
186
202
  # Stale branches detection and cleanup
187
203
  stale_branches = result.get('stale_branches', {})
188
204
  candidates = stale_branches.get('candidates', [])
205
+ unmerged = stale_branches.get('unmerged', [])
206
+ current_branch = repo.hgit.branch if repo.hgit else None
189
207
 
190
- if candidates:
208
+ def _label(branch):
209
+ """Mark the branch the developer is standing on."""
210
+ return f"{branch} (current branch)" if branch == current_branch else branch
211
+
212
+ if candidates or unmerged:
191
213
  click.echo()
214
+ click.echo(
215
+ f"⚠️ {utils.Color.bold(f'Found {len(candidates) + len(unmerged)} stale local branch(es)')}"
216
+ f" (no longer on remote):"
217
+ )
218
+ for branch in candidates[:10]:
219
+ click.echo(f" {'○' if dry_run else '•'} {_label(branch)}")
220
+ if len(candidates) > 10:
221
+ click.echo(f" ... and {len(candidates) - 10} more")
222
+
223
+ # Branches carrying commits that are nowhere else are never part
224
+ # of the blanket confirmation: deleting them destroys the work.
225
+ if unmerged:
226
+ click.echo()
227
+ click.echo(
228
+ f" {utils.Color.red('These carry commits that exist nowhere else:')}"
229
+ )
230
+ for branch, count in unmerged[:10]:
231
+ plural = 's' if count != 1 else ''
232
+ click.echo(f" ⚠ {_label(branch)} - {count} unmerged commit{plural}")
233
+ if len(unmerged) > 10:
234
+ click.echo(f" ... and {len(unmerged) - 10} more")
235
+
192
236
  if dry_run:
193
- click.echo(f"⚠️ {utils.Color.bold(f'Found {len(candidates)} stale local branch(es)')} (no longer on remote):")
194
- for branch in candidates[:10]:
195
- click.echo(f" ○ {branch}")
196
- if len(candidates) > 10:
197
- click.echo(f" ... and {len(candidates) - 10} more")
198
237
  click.echo(f"\n Run without --dry-run to be prompted for deletion")
199
- else:
200
- # Show stale branches and prompt for deletion
201
- click.echo(f"⚠️ {utils.Color.bold(f'Found {len(candidates)} stale local branch(es)')} (no longer on remote):")
202
- for branch in candidates[:10]:
203
- click.echo(f" • {branch}")
204
- if len(candidates) > 10:
205
- click.echo(f" ... and {len(candidates) - 10} more")
206
- click.echo()
238
+ return
207
239
 
208
- # Prompt for confirmation
209
- if click.confirm(f"Delete these {len(candidates)} branch(es)?", default=False):
210
- # Get repo instance and actually delete the branches
211
- deleted = []
212
- errors = []
213
- try:
214
- delete_result = repo.hgit.prune_local_branches(
240
+ click.echo()
241
+
242
+ deleted = []
243
+ errors = []
244
+ switched_to = None
245
+
246
+ try:
247
+ if candidates and click.confirm(
248
+ f"Delete these {len(candidates)} branch(es)?", default=False
249
+ ):
250
+ delete_result = repo.hgit.prune_local_branches(
251
+ pattern="ho-*",
252
+ dry_run=False,
253
+ exclude_current=False
254
+ )
255
+ deleted = delete_result.get('deleted', [])
256
+ errors = delete_result.get('errors', [])
257
+ switched_to = delete_result.get('switched_to')
258
+
259
+ if unmerged:
260
+ click.echo()
261
+ click.echo(
262
+ utils.Color.red(
263
+ f"⚠ {len(unmerged)} branch(es) hold work that only exists locally. "
264
+ f"Deleting them loses it."
265
+ )
266
+ )
267
+ click.echo(" To keep that work, merge or push those branches first.")
268
+ if click.confirm(
269
+ f"Delete these {len(unmerged)} branch(es) anyway, losing their commits?",
270
+ default=False
271
+ ):
272
+ force_result = repo.hgit.prune_local_branches(
215
273
  pattern="ho-*",
216
274
  dry_run=False,
217
- exclude_current=True
275
+ exclude_current=False,
276
+ force=True
218
277
  )
219
- deleted = delete_result.get('deleted', [])
220
- errors = delete_result.get('errors', [])
221
-
222
- if deleted:
223
- click.echo(f"\n✓ {utils.Color.green(f'Deleted {len(deleted)} stale branch(es)')}")
224
- if verbose:
225
- for branch in deleted[:10]:
226
- click.echo(f" ✓ {branch}")
227
- if len(deleted) > 10:
228
- click.echo(f" ... and {len(deleted) - 10} more")
229
-
230
- if errors:
231
- click.echo(f"\n⚠ {utils.Color.red('Some errors occurred during cleanup')}")
232
- if verbose:
233
- for branch, error in errors[:3]:
234
- click.echo(f" {branch}: {error}")
235
- except Exception as e:
236
- click.echo(utils.Color.red(f"\n❌ Error deleting branches: {e}"), err=True)
278
+ deleted.extend(force_result.get('deleted', []))
279
+ errors.extend(force_result.get('errors', []))
280
+ switched_to = switched_to or force_result.get('switched_to')
281
+
282
+ if deleted:
283
+ click.echo(f"\n✓ {utils.Color.green(f'Deleted {len(deleted)} stale branch(es)')}")
284
+ if verbose:
285
+ for branch in deleted[:10]:
286
+ click.echo(f" ✓ {branch}")
287
+ if len(deleted) > 10:
288
+ click.echo(f" ... and {len(deleted) - 10} more")
289
+
290
+ if switched_to:
291
+ click.echo(
292
+ f" ℹ You were on a deleted branch - now on "
293
+ f"{utils.Color.bold(switched_to)}"
294
+ )
295
+
296
+ if errors:
297
+ click.echo(f"\n⚠ {utils.Color.red('Some errors occurred during cleanup')}")
298
+ if verbose:
299
+ for branch, error in errors[:3]:
300
+ click.echo(f" {branch}: {error}")
301
+ except Exception as e:
302
+ click.echo(utils.Color.red(f"\n❌ Error deleting branches: {e}"), err=True)
237
303
 
238
304
 
239
305
  def _display_release_branches_grouped(branches: list, verbose: bool):
@@ -6,6 +6,7 @@ from pathlib import Path
6
6
 
7
7
  import click
8
8
  from half_orm_dev.repo import Repo, RepoError
9
+ from half_orm_dev.release_manager import ReleaseManagerError
9
10
  from half_orm import utils
10
11
 
11
12
 
@@ -16,16 +17,17 @@ def restore(release: str) -> None:
16
17
  Restore the database to the state of RELEASE.
17
18
 
18
19
  Drops all user schemas and reloads them, picking the most precise
19
- snapshot available for RELEASE:
20
+ source available for RELEASE:
20
21
 
21
22
  \b
22
- 1. .hop/model/release-RELEASE.sql (production + patches staged
23
- for that release, still in development)
24
- 2. .hop/model/schema-RELEASE.sql (published snapshot of that
25
- exact released version)
23
+ 1. .hop/model/schema-RELEASE.sql + data-RELEASE.sql (published
24
+ snapshot of that exact released version)
25
+ 2. otherwise, for a release still in preparation: the production
26
+ baseline replayed with the validated patches of every release in
27
+ preparation up to RELEASE (patch -> minor -> major)
26
28
 
27
29
  Fails with an error, rather than silently loading a different
28
- version, if neither file exists.
30
+ version, if RELEASE is neither published nor in preparation.
29
31
 
30
32
  \b
31
33
  Examples:
@@ -39,22 +41,15 @@ def restore(release: str) -> None:
39
41
  repo = Repo()
40
42
  click.echo(f"Restoring database to release {utils.Color.bold(release)}...")
41
43
 
42
- release_schema_path = repo.get_release_schema_path(release)
43
44
  schema_path = Path(repo.model_dir) / f"schema-{release}.sql"
44
45
 
45
- if release_schema_path.exists():
46
- repo.restore_database_from_release_schema(release)
47
- elif schema_path.exists():
46
+ if schema_path.exists():
48
47
  repo.restore_database_from_version_schema(release)
49
48
  else:
50
- raise RepoError(
51
- f"No schema found for release '{release}': neither "
52
- f"{release_schema_path.name} nor {schema_path.name} "
53
- f"exists in {repo.model_dir}."
54
- )
49
+ repo.release_manager.restore_to_release(release)
55
50
 
56
51
  click.echo(f"✓ {utils.Color.green('Database restored to')} {utils.Color.bold(release)}")
57
- except RepoError as e:
52
+ except (RepoError, ReleaseManagerError) as e:
58
53
  click.echo(utils.Color.red(f"\n❌ {e}"), err=True)
59
54
  raise click.Abort()
60
55
  except Exception as e:
@@ -7,6 +7,10 @@ import functools
7
7
  import os
8
8
  import subprocess
9
9
  import sys
10
+ from pathlib import Path
11
+
12
+ import half_orm_dev as _half_orm_dev
13
+ from half_orm_dev.utils import hop_version
10
14
  from half_orm_dev.repo import Repo, OutdatedHalfORMDevError
11
15
  from half_orm import utils
12
16
  from .commands import ALL_COMMANDS
@@ -139,7 +143,6 @@ def create_cli_group():
139
143
  return cmd
140
144
  # Unknown command — show a specific message when migration is needed.
141
145
  if hop.repo_checked and hop._Hop__repo.needs_migration():
142
- from half_orm_dev.utils import hop_version
143
146
  installed_version = hop_version()
144
147
  config_version = hop._Hop__repo.repo.config.hop_version
145
148
 
@@ -166,9 +169,26 @@ def create_cli_group():
166
169
  return None
167
170
 
168
171
  @click.group(cls=VersionCheckGroup, invoke_without_command=True)
172
+ @click.version_option(
173
+ version=hop_version(),
174
+ prog_name='half_orm dev',
175
+ # The package path is what settles the recurring question: an
176
+ # editable install runs the working tree, a released wheel does
177
+ # not, and `half_orm --version` answers for the ORM, not for
178
+ # half_orm_dev.
179
+ message=f'%(prog)s %(version)s ({Path(_half_orm_dev.__file__).resolve().parent})',
180
+ )
169
181
  @click.pass_context
170
182
  def dev(ctx):
171
183
  """halfORM development tools - Git-centric patch management and database synchronization"""
184
+ # Which half_orm_dev is running, on every command: a stale
185
+ # install silently answers for the working tree otherwise, and
186
+ # the output looks like a bug in the tool rather than in the
187
+ # environment. `check` is left out - it already opens with the
188
+ # same version plus whether a newer one exists.
189
+ if ctx.invoked_subcommand not in (None, 'check'):
190
+ click.echo(utils.Color.blue(f"half_orm dev {hop_version()}"))
191
+
172
192
  if hop.needs_hop_upgrade:
173
193
  error = hop.hop_upgrade_error
174
194
  required = error.required_version
@@ -195,7 +215,6 @@ def create_cli_group():
195
215
  # Check if migration is needed
196
216
  if hop._Hop__repo.needs_migration():
197
217
  # Propose automatic migration
198
- from half_orm_dev.utils import hop_version
199
218
  from half_orm_dev.repo import RepoError
200
219
  installed_version = hop_version()
201
220
  config_version = hop._Hop__repo.repo.config.hop_version
@@ -35,4 +35,9 @@ def add_commands(main_group):
35
35
  dev.add_command(command)
36
36
 
37
37
  # Copy the callback from the created group
38
- dev.callback = dev_group.callback
38
+ dev.callback = dev_group.callback
39
+
40
+ # ...and the group's own options, which the registration above would
41
+ # otherwise drop: create_cli_group() declares --version there, and a
42
+ # group rebuilt from commands and callback alone would not have it.
43
+ dev.params = list(dev_group.params)
@@ -61,7 +61,9 @@ def execute_sql_file_psql(file_path: Path, database, database_name: str) -> None
61
61
  try:
62
62
  database.execute_pg_command('psql', '-v', 'ON_ERROR_STOP=1', '-d', database_name, '-f', str(file_path))
63
63
  except Exception as e:
64
- raise FileExecutionError(f"psql execution failed for {file_path.name}: {e}") from e
64
+ stderr = getattr(e, 'stderr', None)
65
+ detail = f"{e}\n{stderr.strip()}" if stderr else str(e)
66
+ raise FileExecutionError(f"psql execution failed for {file_path.name}: {detail}") from e
65
67
 
66
68
 
67
69
  def execute_python_file(
@@ -1358,40 +1358,62 @@ class HGit:
1358
1358
 
1359
1359
  def prune_local_branches(
1360
1360
  self,
1361
- pattern: Optional[str] = None,
1361
+ pattern: str = "ho-*",
1362
1362
  dry_run: bool = False,
1363
- exclude_current: bool = True
1363
+ exclude_current: bool = True,
1364
+ force: bool = False
1364
1365
  ) -> dict:
1365
1366
  """
1366
- Delete local branches that no longer exist on remote origin.
1367
+ Delete local hop branches that no longer exist on remote origin.
1367
1368
 
1368
- Compares local branches with remote branches and deletes local branches
1369
- that don't have a corresponding remote branch. This is useful for cleaning
1370
- up after branches are deleted on remote (e.g., after promote_to cleanup).
1369
+ Origin is the source of truth: a branch deleted there has no
1370
+ reason to survive locally. Only ho-* branches are ever considered
1371
+ - a developer's own branches are none of this method's business -
1372
+ and ho-prod is always kept.
1373
+
1374
+ Deletion uses `git branch -d`, so a branch carrying commits that
1375
+ exist nowhere else is refused rather than destroyed. Those are
1376
+ reported under 'unmerged' for the caller to raise explicitly;
1377
+ force=True then deletes them with `git branch -D`. This matters:
1378
+ a patch branch whose remote was deleted before its work was
1379
+ merged is the only remaining copy of that work, and it used to be
1380
+ removed by a single blanket confirmation.
1381
+
1382
+ When exclude_current is False and the branch to delete is the one
1383
+ checked out, this switches to ho-prod first - you cannot delete
1384
+ the branch you stand on, and leaving it behind would defeat the
1385
+ point of the sync.
1371
1386
 
1372
1387
  Args:
1373
- pattern: Optional glob pattern to limit pruning (e.g., "ho-release/*")
1374
- If None, checks all branches except ho-prod
1375
- dry_run: If True, only report what would be deleted without deleting
1376
- exclude_current: If True, never delete currently checked out branch
1388
+ pattern: Glob pattern limiting the branches considered
1389
+ (default "ho-*"). Non-hop branches are skipped
1390
+ whatever the pattern says.
1391
+ dry_run: If True, only report what would be deleted
1392
+ exclude_current: If True, never touch the current branch
1393
+ force: If True, delete even branches carrying unmerged
1394
+ commits (git branch -D)
1377
1395
 
1378
1396
  Returns:
1379
1397
  dict with keys:
1380
1398
  'deleted': List of deleted branch names
1381
- 'skipped': List of (branch_name, reason) tuples for skipped branches
1382
- 'errors': List of (branch_name, error_message) tuples for failed deletions
1399
+ 'unmerged': List of (branch_name, commit_count) for branches
1400
+ kept because they carry unmerged commits
1401
+ 'skipped': List of (branch_name, reason) tuples
1402
+ 'errors': List of (branch_name, error_message) tuples
1403
+ 'switched_to': Branch checked out to allow deleting the
1404
+ current one, or None
1383
1405
 
1384
1406
  Examples:
1385
- # Prune all stale ho-release branches (dry run)
1386
- result = hgit.prune_local_branches(pattern="ho-release/*", dry_run=True)
1407
+ # What would go (dry run)
1408
+ result = hgit.prune_local_branches(dry_run=True)
1387
1409
  print(f"Would delete: {result['deleted']}")
1410
+ print(f"Would keep (unmerged): {result['unmerged']}")
1388
1411
 
1389
- # Actually prune stale ho-release branches
1390
- result = hgit.prune_local_branches(pattern="ho-release/*")
1391
- print(f"Deleted: {result['deleted']}")
1412
+ # Delete, current branch included, work preserved
1413
+ result = hgit.prune_local_branches(exclude_current=False)
1392
1414
 
1393
- # Prune all stale branches except ho-prod
1394
- result = hgit.prune_local_branches()
1415
+ # Delete unmerged ones too, on explicit demand
1416
+ result = hgit.prune_local_branches(exclude_current=False, force=True)
1395
1417
  """
1396
1418
  # Fetch from remote to get up-to-date branch list
1397
1419
  try:
@@ -1399,18 +1421,21 @@ class HGit:
1399
1421
  except GitCommandError as e:
1400
1422
  return {
1401
1423
  'deleted': [],
1424
+ 'unmerged': [],
1402
1425
  'skipped': [],
1403
- 'errors': [('fetch', f"Failed to fetch from origin: {e}")]
1426
+ 'errors': [('fetch', f"Failed to fetch from origin: {e}")],
1427
+ 'switched_to': None
1404
1428
  }
1405
1429
 
1406
- # Get current branch if we need to exclude it
1430
+ # Always needed: to exclude the current branch when asked, and
1431
+ # otherwise to know we must leave it before deleting it - git
1432
+ # refuses to delete a branch used by a worktree.
1407
1433
  current_branch = None
1408
- if exclude_current:
1409
- try:
1410
- current_branch = str(self.__git_repo.active_branch)
1411
- except TypeError:
1412
- # active_branch raises TypeError when HEAD is detached
1413
- pass
1434
+ try:
1435
+ current_branch = str(self.__git_repo.active_branch)
1436
+ except TypeError:
1437
+ # active_branch raises TypeError when HEAD is detached
1438
+ pass
1414
1439
 
1415
1440
  # Get local and remote branches
1416
1441
  local_branches = self.get_local_branches(pattern=pattern)
@@ -1422,8 +1447,10 @@ class HGit:
1422
1447
  }
1423
1448
 
1424
1449
  deleted = []
1450
+ unmerged = []
1425
1451
  skipped = []
1426
1452
  errors = []
1453
+ switched_to = None
1427
1454
 
1428
1455
  for branch in local_branches:
1429
1456
  # Skip ho-prod (always keep)
@@ -1431,6 +1458,12 @@ class HGit:
1431
1458
  skipped.append((branch, 'protected branch'))
1432
1459
  continue
1433
1460
 
1461
+ # Never touch branches outside the hop namespace, whatever
1462
+ # the pattern allows
1463
+ if not branch.startswith('ho-'):
1464
+ skipped.append((branch, 'not a hop branch'))
1465
+ continue
1466
+
1434
1467
  # Skip current branch if requested
1435
1468
  if exclude_current and branch == current_branch:
1436
1469
  skipped.append((branch, 'currently checked out'))
@@ -1441,22 +1474,66 @@ class HGit:
1441
1474
  skipped.append((branch, 'exists on remote'))
1442
1475
  continue
1443
1476
 
1444
- # Branch doesn't exist on remote - delete it
1477
+ # Branch doesn't exist on remote - it should go, unless it
1478
+ # carries commits that exist nowhere else.
1479
+ unmerged_count = self.count_unmerged_commits(branch)
1480
+
1445
1481
  if dry_run:
1446
- deleted.append(branch)
1447
- else:
1448
- try:
1449
- self.__git_repo.git.branch('-D', branch)
1482
+ if unmerged_count and not force:
1483
+ unmerged.append((branch, unmerged_count))
1484
+ else:
1450
1485
  deleted.append(branch)
1486
+ continue
1487
+
1488
+ # Deleting the branch we stand on requires leaving it first.
1489
+ if branch == current_branch:
1490
+ try:
1491
+ self.__git_repo.git.checkout('ho-prod')
1492
+ switched_to = 'ho-prod'
1493
+ current_branch = 'ho-prod'
1451
1494
  except GitCommandError as e:
1495
+ errors.append((branch, f"Could not leave {branch}: {e}"))
1496
+ continue
1497
+
1498
+ try:
1499
+ # -d refuses a branch that is not fully merged: git is
1500
+ # the backstop even when the count above says otherwise
1501
+ self.__git_repo.git.branch('-D' if force else '-d', branch)
1502
+ deleted.append(branch)
1503
+ except GitCommandError as e:
1504
+ if not force and 'not fully merged' in str(e):
1505
+ unmerged.append((branch, unmerged_count))
1506
+ else:
1452
1507
  errors.append((branch, str(e)))
1453
1508
 
1454
1509
  return {
1455
1510
  'deleted': deleted,
1511
+ 'unmerged': unmerged,
1456
1512
  'skipped': skipped,
1457
- 'errors': errors
1513
+ 'errors': errors,
1514
+ 'switched_to': switched_to
1458
1515
  }
1459
1516
 
1517
+ def count_unmerged_commits(self, branch: str, base: str = 'ho-prod') -> int:
1518
+ """
1519
+ Number of commits on branch that are not in base.
1520
+
1521
+ Used to tell a stale branch that can be dropped without loss from
1522
+ one that is the last copy of somebody's work.
1523
+
1524
+ Args:
1525
+ branch: Branch to inspect
1526
+ base: Branch to compare against (default ho-prod)
1527
+
1528
+ Returns:
1529
+ Commit count, 0 when everything is already in base or when
1530
+ the comparison cannot be made
1531
+ """
1532
+ try:
1533
+ return int(self.__git_repo.git.rev_list('--count', f'{base}..{branch}'))
1534
+ except (GitCommandError, ValueError):
1535
+ return 0
1536
+
1460
1537
  def get_active_branches_status(self, stage_files: list = None) -> dict:
1461
1538
  """
1462
1539
  Get status of active Half-ORM branches (branches that can still be modified).
@@ -479,10 +479,11 @@ class MigrationManager:
479
479
  ) -> None:
480
480
  """Regenerate the project modules after a migration on all active branches.
481
481
 
482
- Each branch is regenerated against the DB schema that matches its state:
483
- - ho-prod → production schema (schema.sql)
484
- - ho-release/X.Y.Z → release schema (release-X.Y.Z.sql)
485
- - ho-patch/* → production schema (schema.sql)
482
+ Every branch is regenerated against the production schema
483
+ (schema.sql). Release and patch branches may carry relations that
484
+ only exist once their patches are applied, so modules are not
485
+ regenerated on ho-patch/* at all - the developer re-runs
486
+ `hop patch apply` there.
486
487
 
487
488
  Stale local branches (no longer on remote) are skipped to avoid pre-commit
488
489
  hook failures.
@@ -493,11 +494,8 @@ class MigrationManager:
493
494
  "on a production server.\nModule regeneration (which includes database "
494
495
  "restoration) must never run in production."
495
496
  )
496
- import re as _re
497
497
  from half_orm_dev import modules as _modules
498
498
 
499
- _RELEASE_RE = _re.compile(r'^ho-release/(.+)$')
500
-
501
499
  repo = self._repo
502
500
  git_repo = repo.hgit._HGit__git_repo
503
501
  package_name = repo.name
@@ -541,20 +539,13 @@ class MigrationManager:
541
539
  try:
542
540
  repo.hgit.checkout(branch)
543
541
 
544
- # Restore the DB to the schema appropriate for this branch so
545
- # generate() introspects the right set of relations.
546
- m = _RELEASE_RE.match(branch)
547
- if m:
548
- release_version = m.group(1)
549
- release_schema = repo.get_release_schema_path(release_version)
550
- if release_schema.exists():
551
- repo.restore_database_from_release_schema(release_version)
552
- else:
553
- repo.restore_database_from_schema()
554
- else:
555
- #XXX EST-CE QUE POUR ho-patch/* on ne devrait pas utiliser from_release_schema ?
556
- # ho-prod and ho-patch/*: use production schema
557
- repo.restore_database_from_schema()
542
+ # Restore the DB to the production schema so generate()
543
+ # introspects a known set of relations. Release and patch
544
+ # branches may carry relations that only exist once their
545
+ # patches are applied; the developer re-runs
546
+ # `hop patch apply` there to regenerate with the full
547
+ # release context.
548
+ repo.restore_database_from_schema()
558
549
 
559
550
  if not branch in patch_branches:
560
551
  _modules.generate(repo)