2020-12-14 00:23:29 +00:00
---
# Pre-checks
- name : Fail if Postgres not enabled
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2020-12-14 00:23:29 +00:00
msg : "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot import."
2022-07-18 08:22:05 +00:00
when : "not matrix_postgres_enabled | bool"
2020-12-14 00:23:29 +00:00
- name : Fail if playbook called incorrectly
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2020-12-14 00:23:29 +00: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 08:22:05 +00:00
ansible.builtin.stat :
2020-12-14 00:23:29 +00:00
path : "{{ sqlite_database_path }}"
register : sqlite_database_path_stat_result
- name : Fail if provided SQLite database file doesn't exist
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2020-12-14 00:23:29 +00: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 09:47:00 +00: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.
2022-09-27 08:38:33 +00:00
- when : 'postgres_connection_string_variable_name is defined'
block :
2020-12-14 09:47:00 +00:00
- name : Fail if postgres_connection_string_variable_name points to an undefined variable
2022-10-25 03:43:57 +00: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 09:47:00 +00:00
when : "postgres_connection_string_variable_name not in vars"
- name : Get Postgres connection string from variable
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2020-12-14 09:47:00 +00:00
postgres_db_connection_string : "{{ lookup('vars', postgres_connection_string_variable_name) }}"
- name : Fail if playbook called incorrectly
2022-07-18 07:39:08 +00:00
ansible.builtin.fail :
2020-12-14 09:47:00 +00: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 00:23:29 +00:00
# Defaults
- name : Set postgres_start_wait_time, if not provided
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2020-12-14 00:23:29 +00:00
postgres_start_wait_time : 15
2022-07-18 08:22:05 +00:00
when : "postgres_start_wait_time | default('') == ''"
2020-12-14 00:23:29 +00:00
# Actual import work
- name : Ensure matrix-postgres is started
2022-07-18 07:39:08 +00:00
ansible.builtin.service :
2020-12-14 00:23:29 +00:00
name : matrix-postgres
state : started
2022-02-05 20:32:54 +00:00
daemon_reload : true
2020-12-14 00:23:29 +00:00
register : matrix_postgres_service_start_result
- name : Wait a bit, so that Postgres can start
2022-07-18 08:22:05 +00:00
ansible.builtin.wait_for :
2020-12-14 00:23:29 +00:00
timeout : "{{ postgres_start_wait_time }}"
delegate_to : 127.0 .0 .1
become : false
2022-07-18 08:22:05 +00:00
when : "matrix_postgres_service_start_result.changed | bool"
2020-12-14 00:23:29 +00:00
2022-10-09 17:42:20 +00:00
- name : Import SQLite database from {{ sqlite_database_path }} into Postgres # noqa name[template]
2022-07-18 07:39:08 +00:00
ansible.builtin.command :
2020-12-14 00:23:29 +00:00
cmd : >-
2022-11-04 14:39:35 +00:00
{{ devture_systemd_docker_base_host_command_docker }} run
2020-12-14 00:23:29 +00:00
--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 12:08:10 +00: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 00:23:29 +00:00
2022-10-09 17:42:20 +00:00
- name : Archive SQLite database ({{ sqlite_database_path }} -> {{ sqlite_database_path }}.backup) # noqa name[template]
2022-07-18 07:39:08 +00:00
ansible.builtin.command :
2020-12-14 00:23:29 +00:00
cmd : "mv {{ sqlite_database_path }} {{ sqlite_database_path }}.backup"
2022-07-18 12:08:10 +00: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 00:23:29 +00:00
- name : Inject result
2022-07-18 07:39:08 +00:00
ansible.builtin.set_fact :
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list : |
2020-12-14 00:23:29 +00:00
{{
2022-11-04 14:19:17 +00:00
devture_playbook_runtime_messages_list | default([])
2020-12-14 00:23:29 +00: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."
]
}}