Initialize the DB under test for the given scenario.
(
scenario: Scenario,
no_indexes: bool,
db_port: int,
db_host: str,
db_user: str,
db_pass: str | None,
db_require_ssl: bool,
)
| 137 | @click.option("--db-pass", **Opt.db_pass) |
| 138 | @click.option("--db-require-ssl", **Opt.db_require_ssl) |
| 139 | def init( |
| 140 | scenario: Scenario, |
| 141 | no_indexes: bool, |
| 142 | db_port: int, |
| 143 | db_host: str, |
| 144 | db_user: str, |
| 145 | db_pass: str | None, |
| 146 | db_require_ssl: bool, |
| 147 | ) -> None: |
| 148 | """Initialize the DB under test for the given scenario.""" |
| 149 | |
| 150 | info(f'Initializing "{scenario}" as the DB under test') |
| 151 | |
| 152 | try: |
| 153 | # connect to the default database in order to re-create the |
| 154 | # database for the selected scenario |
| 155 | with closing( |
| 156 | sql.Database( |
| 157 | port=db_port, |
| 158 | host=db_host, |
| 159 | user=db_user, |
| 160 | database=None, |
| 161 | password=db_pass, |
| 162 | require_ssl=db_require_ssl, |
| 163 | ) |
| 164 | ) as db: |
| 165 | db.drop_database(scenario) |
| 166 | db.create_database(scenario) |
| 167 | |
| 168 | # re-connect to the database for the selected scenario |
| 169 | with closing( |
| 170 | sql.Database( |
| 171 | port=db_port, |
| 172 | host=db_host, |
| 173 | user=db_user, |
| 174 | database=str(scenario), |
| 175 | password=db_pass, |
| 176 | require_ssl=db_require_ssl, |
| 177 | ) |
| 178 | ) as db: |
| 179 | statements = sql.parse_from_file(scenario.schema_path()) |
| 180 | if no_indexes: |
| 181 | idx_re = re.compile(r"(create|create\s+default|drop)\s+index\s+") |
| 182 | statements = [ |
| 183 | statement |
| 184 | for statement in statements |
| 185 | if not idx_re.match(statement.lower()) |
| 186 | ] |
| 187 | db.execute_all(statements) |
| 188 | except Exception as e: |
| 189 | raise click.ClickException(f"init command failed: {e}") |
| 190 | |
| 191 | |
| 192 | @app.command() |
nothing calls this directly
no test coverage detected