Switch migrate.py to structlog, remove unused old testing code

This commit is contained in:
2024-11-01 18:37:10 -05:00
parent 3b3f3ba784
commit 75e875e61d
2 changed files with 37 additions and 55 deletions
+7 -2
View File
@@ -17,6 +17,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A `get_db` utility function to retrieve a reference to the database (with type hinting) - A `get_db` utility function to retrieve a reference to the database (with type hinting)
- Minor `DATABASE_URL` check in `models.py` to prevent cryptic connection issues - Minor `DATABASE_URL` check in `models.py` to prevent cryptic connection issues
## Changed
- Migration script now uses `structlog` instead of `print`
- Migration script output is tuned to structlog as well.
- Unspecified IPv6 addresses are returned without hiding in `utilities.hide_ip`
- Applied `get_db` utility function in all applicable areas.
### Fixed ### Fixed
- Raised level for `apscheduler.scheduler` logger to `WARNING` to prevent excessive logging - Raised level for `apscheduler.scheduler` logger to `WARNING` to prevent excessive logging
@@ -29,8 +36,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Mildly reformatted `README.md` - Mildly reformatted `README.md`
- A development mode check for the `app.state.ip_pool`'s initialization (caused application failure in production only) - A development mode check for the `app.state.ip_pool`'s initialization (caused application failure in production only)
- Applied `get_db` utility function in all applicable areas.
- Unspecified IPv6 addresses are returned without hiding in `utilities.hide_ip`
### Fixed ### Fixed
+30 -53
View File
@@ -4,9 +4,11 @@ import sys
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
import questionary import questionary
import structlog
from dotenv import load_dotenv from dotenv import load_dotenv
from peewee_migrate import Router, router from peewee_migrate import Router, router
logger = structlog.get_logger()
load_dotenv(dotenv_path=".env") load_dotenv(dotenv_path=".env")
@@ -89,23 +91,23 @@ def main(*args: str) -> None:
diff = router.diff diff = router.diff
if len(diff) == 0: if len(diff) == 0:
print( logger.info(
"No migrations found, no pending migrations to apply. Creating initial migration." "No migrations found, no pending migrations to apply. Creating initial migration."
) )
migration = router.create("initial", auto=target_models) migration = router.create("initial", auto=target_models)
if not migration: if not migration:
print("No changes detected. Something went wrong.") logger.error("No changes detected. Something went wrong.")
else: else:
print(f"Migration created: {migration}") logger.info(f"Migration created: {migration}")
router.run(migration) router.run(migration)
diff = router.diff diff = router.diff
if len(diff) > 0: if len(diff) > 0:
print( logger.info(
"Note: Selecting a migration will apply all migrations up to and including the selected migration." "Note: Selecting a migration will apply all migrations up to and including the selected migration."
) )
print( logger.info(
"e.g. Applying 004 while only 001 is applied would apply 002, 003, and 004." "e.g. Applying 004 while only 001 is applied would apply 002, 003, and 004."
) )
@@ -113,39 +115,35 @@ def main(*args: str) -> None:
"Select highest migration to apply:", choices=diff "Select highest migration to apply:", choices=diff
).ask() ).ask()
if choice is None: if choice is None:
print( logger.warning(
"For safety reasons, you won't be able to create migrations without applying the pending ones." "For safety reasons, you won't be able to create migrations without applying the pending ones."
) )
if len(current) == 0: if len(current) == 0:
print( logger.warning(
"Warn: No migrations have been applied globally, which is dangerous. Something may be wrong." "Warn: No migrations have been applied globally, which is dangerous. Something may be wrong."
) )
return return
result = router.run(choice) result = router.run(choice)
print(f"Done. Applied migrations: {result}") logger.info(f"Done. Applied migrations: {result}")
print("Warning: You should commit and push any new migrations immediately!") logger.warning("You should commit and push any new migrations immediately!")
else: else:
print("No pending migrations to apply.") logger.info("No pending migrations to apply.")
migration_available = router.show(target_models) migration_available = router.show(target_models)
if migration_available is not None: if migration_available is not None:
print("A migration is available to be applied:") logger.info("A migration is available to be applied:")
migrate_text, rollback_text = migration_available migrate_text, rollback_text = migration_available
print("MIGRATION:") def _reformat_text(text: str) -> str:
for line in migrate_text.split("\n"): text = [line for line in text.split("\n") if line.strip() != ""]
if line.strip() == "": return "\n" + "\n".join([f"{i:02}:\t{line}" for i, line in enumerate(text)])
continue
print("\t" + line) logger.info("Migration Content", content=_reformat_text(migrate_text))
print("ROLLBACK:") logger.info("Rollback Content", content=_reformat_text(rollback_text))
for line in rollback_text.split("\n"):
if line.strip() == "":
continue
print("\t" + line)
if questionary.confirm("Do you want to create this migration?").ask(): if questionary.confirm("Do you want to create this migration?").ask():
print( logger.info(
'Lowercase letters and underscores only (e.g. "create_table", "remove_ipaddress_count").' 'Lowercase letters and underscores only (e.g. "create_table", "remove_ipaddress_count").'
) )
migration_name: Optional[str] = questionary.text( migration_name: Optional[str] = questionary.text(
@@ -158,50 +156,29 @@ def main(*args: str) -> None:
migration = router.create(migration_name, auto=target_models) migration = router.create(migration_name, auto=target_models)
if migration: if migration:
print(f"Migration created: {migration}") logger.info(f"Migration created: {migration}")
if len(router.diff) == 1: if len(router.diff) == 1:
if questionary.confirm( if questionary.confirm(
"Do you want to apply this migration immediately?" "Do you want to apply this migration immediately?"
).ask(): ).ask():
router.run(migration) router.run(migration)
print("Done.") logger.info("Done.")
print("!!! Commit and push this migration file immediately!") logger.warning(
"!!! Commit and push this migration file immediately!"
)
else: else:
print("No changes detected. Something went wrong.") logger.error("No changes detected. Something went wrong.")
return return
else: else:
print("No database changes detected.") logger.info("No database changes detected.")
if len(current) > 5: if len(current) > 5:
if questionary.confirm( if questionary.confirm(
"There are more than 5 migrations applied. Do you want to merge them?", "There are more than 5 migrations applied. Do you want to merge them?",
default=False, default=False,
).ask(): ).ask():
print("Merging migrations...") logger.info("Merging migrations...")
router.merge(name="initial") router.merge(name="initial")
print("Done.") logger.info("Done.")
print("!!! Commit and push this merged migration file immediately!") logger.warning("Commit and push this merged migration file immediately!")
# Testing Code:
"""
print(router.print('linkpulse.models'))
# Create migration
print("Creating migration")
migration = router.create('test', auto='linkpulse.models')
if migration is None:
print("No changes detected")
else:
print(f"Migration Created: {migration}")
# Run migration/migrations
router.run(migration)
Run all unapplied migrations
print("Running all unapplied migrations")
applied = router.run()
print(f"Applied migrations: {applied}")
"""