2020-12-14 02:23:29 +02:00
---
# Pre-checks
- name : Fail if Postgres not enabled
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2020-12-14 02:23:29 +02:00
msg : "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot import."
2022-07-18 10:22:05 +02:00
when : "not matrix_postgres_enabled | bool"
2020-12-14 02:23:29 +02:00
- name : Fail if playbook called incorrectly
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2020-12-14 02:23:29 +02:00
msg : "The `sqlite_database_path` variable needs to be provided to this playbook, via --extra-vars"
when : "sqlite_database_path is not defined or sqlite_database_path.startswith('<')"
- name : Check if the provided SQLite database file exists
2022-07-18 10:22:05 +02:00
ansible.builtin.stat :
2020-12-14 02:23:29 +02:00
path : "{{ sqlite_database_path }}"
register : sqlite_database_path_stat_result
- name : Fail if provided SQLite database file doesn't exist
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2020-12-14 02:23:29 +02:00
msg : "File cannot be found on the server at {{ sqlite_database_path }}"
when : "not sqlite_database_path_stat_result.stat.exists"
2020-12-14 11:47:00 +02:00
# We either expect `postgres_db_connection_string` specifying a full Postgres database connection string,
# or `postgres_connection_string_variable_name`, specifying a name of a variable, which contains a valid connection string.
- block :
- name : Fail if postgres_connection_string_variable_name points to an undefined variable
2022-07-18 09:39:08 +02:00
ansible.builtin.fail : msg="postgres_connection_string_variable_name is defined, but there is no variable with the name `{{ postgres_connection_string_variable_name }}`"
2020-12-14 11:47:00 +02:00
when : "postgres_connection_string_variable_name not in vars"
- name : Get Postgres connection string from variable
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2020-12-14 11:47:00 +02:00
postgres_db_connection_string : "{{ lookup('vars', postgres_connection_string_variable_name) }}"
when : 'postgres_connection_string_variable_name is defined'
- name : Fail if playbook called incorrectly
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2020-12-14 11:47:00 +02:00
msg : >-
Either a `postgres_db_connection_string` variable or a `postgres_connection_string_variable_name` needs to be provided to this playbook, via `--extra-vars`.
Example : `--extra-vars="postgres_db_connection_string=postgresql://username:password@localhost:<port>/database_name"` or `--extra-vars="postgres_connection_string_variable_name=matrix_appservice_discord_database_connString"`
when : "postgres_db_connection_string is not defined or not postgres_db_connection_string.startswith('postgresql://')"
2020-12-14 02:23:29 +02:00
# Defaults
- name : Set postgres_start_wait_time, if not provided
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2020-12-14 02:23:29 +02:00
postgres_start_wait_time : 15
2022-07-18 10:22:05 +02:00
when : "postgres_start_wait_time | default('') == ''"
2020-12-14 02:23:29 +02:00
# Actual import work
- name : Ensure matrix-postgres is started
2022-07-18 09:39:08 +02:00
ansible.builtin.service :
2020-12-14 02:23:29 +02:00
name : matrix-postgres
state : started
2022-02-05 22:32:54 +02:00
daemon_reload : true
2020-12-14 02:23:29 +02:00
register : matrix_postgres_service_start_result
- name : Wait a bit, so that Postgres can start
2022-07-18 10:22:05 +02:00
ansible.builtin.wait_for :
2020-12-14 02:23:29 +02:00
timeout : "{{ postgres_start_wait_time }}"
delegate_to : 127.0 .0 .1
become : false
2022-07-18 10:22:05 +02:00
when : "matrix_postgres_service_start_result.changed | bool"
2020-12-14 02:23:29 +02:00
- name : Import SQLite database from {{ sqlite_database_path }} into Postgres
2022-07-18 09:39:08 +02:00
ansible.builtin.command :
2020-12-14 02:23:29 +02:00
cmd : >-
{{ matrix_host_command_docker }} run
--rm
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
--cap-drop=ALL
--network={{ matrix_docker_network }}
--mount type=bind,src={{ sqlite_database_path }},dst=/in.db,ro
--entrypoint=/bin/sh
{{ matrix_postgres_pgloader_docker_image }}
-c
'pgloader /in.db {{ postgres_db_connection_string }}'
2022-07-18 14:08:10 +02:00
register : matrix_postgres_import_generic_sqlite_db_import_result
changed_when : matrix_postgres_import_generic_sqlite_db_import_result.rc == 0
2020-12-14 02:23:29 +02:00
- name : Archive SQLite database ({{ sqlite_database_path }} -> {{ sqlite_database_path }}.backup)
2022-07-18 09:39:08 +02:00
ansible.builtin.command :
2020-12-14 02:23:29 +02:00
cmd : "mv {{ sqlite_database_path }} {{ sqlite_database_path }}.backup"
2022-07-18 14:08:10 +02:00
register : matrix_postgres_import_generic_sqlite_db_move_result
changed_when : matrix_postgres_import_generic_sqlite_db_move_result.rc == 0
2020-12-14 02:23:29 +02:00
- name : Inject result
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2020-12-14 02:23:29 +02:00
matrix_playbook_runtime_results : |
{{
2022-07-18 10:22:05 +02:00
matrix_playbook_runtime_results | default([])
2020-12-14 02:23:29 +02:00
+
[
"NOTE: Your SQLite database file has been imported into Postgres. The original file has been moved from `{{ sqlite_database_path }}` to `{{ sqlite_database_path }}.backup`. When you've confirmed that the import went well and everything works, you should be able to safely delete this file."
]
}}