Files
Bubberstation/tools/ezdb/steps/install_database.py
Tom d91555b79e ezdb - A one click script to quickly setting up a development database (#75053) (#21458)
* ezdb - A one click script to quickly setting up a development database (#75053)

https://user-images.githubusercontent.com/35135081/235344815-8e825ba9-52cf-44e8-b8e2-a2aeb5d47276.mp4

- Downloads a portable MariaDB (doesn't pollute your main system)
- Sets up a database with a random password on port 1338 (configurable)
- Installs the initial schema
- Every time after, will run updates

Major versions right now explicitly escape hatch, because those
historically come with something like a Python script, and I do not want
it to pretend to work.

---------

Co-authored-by: san7890 <the@san7890.com>

* touchups

* oh well

---------

Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
Co-authored-by: san7890 <the@san7890.com>
Co-authored-by: Useroth <37159550+Useroth@users.noreply.github.com>
2023-06-03 23:15:41 +02:00

47 lines
1.6 KiB
Python

import argparse
import secrets
import subprocess
from ..ezdb.paths import get_config_path, get_data_path, get_mariadb_bin_path, get_mariadb_daemon_path, get_mariadb_install_db_path
from .step import Step
def create_password() -> str:
return secrets.token_urlsafe(40)
class InstallDatabase(Step):
@staticmethod
def should_run() -> bool:
# If the db folder exists, but the config doesn't, we cancelled
# halfway through and ought to start over by deleting the data folder.
return get_mariadb_bin_path().exists() and (not get_data_path().exists() or not get_config_path().exists())
@staticmethod
def run(args: argparse.Namespace):
data_folder = get_data_path()
if data_folder.exists():
print("Deleting old data folder")
data_folder.rmdir()
password = create_password()
print("Installing database...")
subprocess.run(
[
str(get_mariadb_install_db_path()),
f"--port={args.port}",
f"--password={password}",
],
check = True,
stderr = subprocess.STDOUT,
)
print("Creating config...")
with open(get_config_path(), "w") as file:
file.write("SQL_ENABLED\n")
file.write(f"PORT {args.port}\n")
file.write(f"FEEDBACK_LOGIN root\n")
file.write(f"FEEDBACK_PASSWORD {password}\n")
file.write("FEEDBACK_DATABASE tgstation\n")
file.write("FEEDBACK_TABLEPREFIX\n")
file.write(f"DB_DAEMON {str(get_mariadb_daemon_path())}")