Setup the configuration database.
(app: Annotated[str, typer.Argument(
help="This argument doesn't require in CLI mode.")] = None)
| 789 | |
| 790 | @app.command() |
| 791 | def setup_db(app: Annotated[str, typer.Argument( |
| 792 | help="This argument doesn't require in CLI mode.")] = None): |
| 793 | """Setup the configuration database.""" |
| 794 | |
| 795 | app = app or create_app() |
| 796 | create_app_data_directory(config) |
| 797 | |
| 798 | print("pgAdmin 4 - Application Initialisation") |
| 799 | print("======================================\n") |
| 800 | |
| 801 | def run_migration_for_sqlite(): |
| 802 | # See pgadmin/__init__.py:run_migration_for_sqlite — tighten the |
| 803 | # umask so SQLite creates the DB file at 0o600 directly rather |
| 804 | # than relying on the post-hoc chmod (which has a TOCTOU window |
| 805 | # between create and chmod). |
| 806 | _saved_umask = os.umask(0o077) |
| 807 | try: |
| 808 | with app.app_context(): |
| 809 | # Run migration for the first time i.e. create database |
| 810 | from config import SQLITE_PATH |
| 811 | if not os.path.exists(SQLITE_PATH): |
| 812 | db_upgrade(app) |
| 813 | else: |
| 814 | version = Version.query.filter_by(name='ConfigDB').first() |
| 815 | schema_version = version.value |
| 816 | |
| 817 | # Run migration if current schema version is greater |
| 818 | # than the schema version stored in version table |
| 819 | if CURRENT_SCHEMA_VERSION >= schema_version: |
| 820 | db_upgrade(app) |
| 821 | |
| 822 | # Update schema version to the latest |
| 823 | if CURRENT_SCHEMA_VERSION > schema_version: |
| 824 | version = Version.query.filter_by( |
| 825 | name='ConfigDB').first() |
| 826 | version.value = CURRENT_SCHEMA_VERSION |
| 827 | db.session.commit() |
| 828 | finally: |
| 829 | os.umask(_saved_umask) |
| 830 | |
| 831 | if os.name != 'nt': |
| 832 | os.chmod(config.SQLITE_PATH, 0o600) |
| 833 | |
| 834 | def run_migration_for_others(): |
| 835 | with app.app_context(): |
| 836 | version = Version.query.filter_by(name='ConfigDB').first() |
| 837 | if version == -1: |
| 838 | db_upgrade(app) |
| 839 | else: |
| 840 | schema_version = version.value |
| 841 | |
| 842 | # Run migration if current schema version is greater than the |
| 843 | # schema version stored in version table |
| 844 | if CURRENT_SCHEMA_VERSION >= schema_version: |
| 845 | db_upgrade(app) |
| 846 | |
| 847 | # Update schema version to the latest |
| 848 | if CURRENT_SCHEMA_VERSION > schema_version: |
nothing calls this directly
no test coverage detected