Skip to content

Commit

Permalink
chore: Add documentation for db creation and rename the testing db
Browse files Browse the repository at this point in the history
  • Loading branch information
alithethird committed Sep 24, 2024
1 parent 4fdacc5 commit 391c367
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
34 changes: 34 additions & 0 deletions testing_database/creating-the-testing-database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Creating the testing database
At the writing this document, the testing database is created using Discourse v3.2.0.

## Creating the database
First of all we need to deploy the Discourse following the [tutorial](https://github.com/canonical/discourse-k8s-operator/blob/main/docs/tutorial.md).

Then, we need to create a 2 new users for testing using the actions:

`juju run discourse-k8s/0 create-user [email protected] admin=true`
`juju run discourse-k8s/0 create-user [email protected]`

Please note that the first user is an admin and the second one is not. Also please not the passwords that are generated automatically by the command.

Now open the Discourse URL in a browser, login with the first user (admin) and create a new topic. Reply to this topic as the admin user again. Then, login with the second user and reply to this topic. Then login with the first user and approve the second users reply.

## Exporting the database

First we need to get the database password:
`juju run postgresql-k8s/0 get-password username=operator`

Ssh into the database
`juju ssh --container postgresql postgresql-k8s/0 bash`

Create a folder to dump the db
`mkdir -p /srv/dump/`

Dump the db. Ip here is the unit ip
`pg_dump -Fc -h 10.1.187.134 -U operator -d discourse > "/srv/dump/testing_database.sql"`

Exit the container
`exit`

Copy the dump into local file system.
`juju scp --container postgresql postgresql-k8s/0:/srv/dump/testing_database.sql./testing_database.sql`
Binary file added testing_database/testing_database.sql
Binary file not shown.
19 changes: 9 additions & 10 deletions tests/integration/test_db_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ async def test_db_migration(model: Model, ops_test: OpsTest, pytestconfig: Confi
)
async with ops_test.fast_forward():
await model.wait_for_idle(apps=[postgres_app.name], status="active")
# configure postgres
await postgres_app.set_config(
{
"plugin_hstore_enable": "true",
Expand All @@ -42,36 +41,37 @@ async def test_db_migration(model: Model, ops_test: OpsTest, pytestconfig: Confi
await model.wait_for_idle(apps=[postgres_app.name], status="active")
db_pass = await run_action(postgres_app.name, "get-password", username="operator")
db_pass = db_pass["password"]
return_code, _, _ = await ops_test.juju(
return_code, _, scp_err = await ops_test.juju(
"scp",
"--container",
"postgresql",
"./mock_db",
"./testing_database/testing_database.sql",
f"{postgres_app.units[0].name}:.",
)
assert return_code == 0

return_code, _, _ = await ops_test.juju(
assert return_code == 0, scp_err

return_code, _, ssh_err = await ops_test.juju(
"ssh",
"--container",
"postgresql",
postgres_app.units[0].name,
"createdb -h localhost -U operator --password discourse",
stdin=str.encode(f"{db_pass}\n"),
)
assert return_code == 0
assert return_code == 0, ssh_err

return_code, _, _ = await ops_test.juju(
return_code, _, ssh_err = await ops_test.juju(
"ssh",
"--container",
"postgresql",
postgres_app.units[0].name,
"pg_restore -h localhost -U operator\
--password -d discourse\
--no-owner --clean --if-exists ./mock_db",
--no-owner --clean --if-exists ./testing_database.sql",
stdin=str.encode(f"{db_pass}\n"),
)
assert return_code == 0
assert return_code == 0, ssh_err
redis_app = await model.deploy("redis-k8s", series="jammy", channel="latest/edge")
await model.wait_for_idle(apps=[redis_app.name], status="active")

Expand All @@ -86,7 +86,6 @@ async def test_db_migration(model: Model, ops_test: OpsTest, pytestconfig: Confi
series="focal",
)
await model.wait_for_idle(apps=[app_name], status="waiting")
# Add required relations
unit = discourse_application.units[0]
assert unit.workload_status == WaitingStatus.name # type: ignore
await model.add_relation(app_name, "postgresql-k8s:database")
Expand Down

0 comments on commit 391c367

Please sign in to comment.