Add support for automatic (Postgres -> SQLite) migration to mx-puppet-instagram

This commit is contained in:
Slavi Pantaleev 2020-12-22 13:30:52 +02:00
parent e64758c119
commit 44c9f4daca
4 changed files with 41 additions and 31 deletions

View file

@ -47,18 +47,17 @@ matrix_mx_puppet_instagram_homeserver_token: ''
matrix_mx_puppet_instagram_login_shared_secret: ''
matrix_mx_puppet_instagram_database_engine: sqlite
matrix_mx_puppet_instagram_sqlite_database_path_local: "{{ matrix_mx_puppet_instagram_data_path }}/database.db"
matrix_mx_puppet_instagram_sqlite_database_path_in_container: "/data/database.db"
matrix_mx_puppet_instagram_database_username: matrix_mx_puppet_instagram
matrix_mx_puppet_instagram_database_password: ~
matrix_mx_puppet_instagram_database_hostname: 'matrix-postgres'
matrix_mx_puppet_instagram_database_port: 5432
matrix_mx_puppet_instagram_database_name: matrix_mx_puppet_instagram
matrix_mx_puppet_instagram_database_file: /data/database.db
matrix_mx_puppet_instagram_database_connString: >-2
{%- if matrix_mx_puppet_instagram_database_engine == 'postgres' -%}
postgresql://{{ matrix_mx_puppet_instagram_database_username }}:{{ matrix_mx_puppet_instagram_database_password }}@{{ matrix_mx_puppet_instagram_database_hostname }}:{{ matrix_mx_puppet_instagram_database_port }}/{{ matrix_mx_puppet_instagram_database_name }}?sslmode=disable
{%- elif matrix_mx_puppet_instagram_database_engine == 'sqlite' -%}
{{ matrix_mx_puppet_instagram_database_engine }}://{{ matrix_mx_puppet_instagram_database_file }}
{%- endif -%}
matrix_mx_puppet_instagram_database_db_name: matrix_mx_puppet_instagram
matrix_mx_puppet_instagram_database_connection_string: 'postgresql://{{ matrix_mx_puppet_instagram_database_username }}:{{ matrix_mx_puppet_instagram_database_password }}@{{ matrix_mx_puppet_instagram_database_hostname }}:{{ matrix_mx_puppet_instagram_database_port }}/{{ matrix_mx_puppet_instagram_database_db_name }}?sslmode=disable'
# Default configuration template which covers the generic use case.
# You can customize it by controlling the various variables inside it.

View file

@ -8,6 +8,33 @@
The matrix-bridge-mx-puppet-instagram role needs to execute before the matrix-synapse role.
when: "matrix_synapse_role_executed|default(False)"
- set_fact:
matrix_mx_puppet_instagram_requires_restart: false
- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_mx_puppet_instagram_sqlite_database_path_local }}"
register: matrix_mx_puppet_instagram_sqlite_database_path_local_stat_result
- block:
- set_fact:
matrix_postgres_db_migration_request:
src: "{{ matrix_mx_puppet_instagram_sqlite_database_path_local }}"
dst: "{{ matrix_mx_puppet_instagram_database_connection_string }}"
caller: "{{ role_path|basename }}"
engine_variable_name: 'matrix_mx_puppet_instagram_database_engine'
engine_old: 'sqlite'
systemd_services_to_stop: ['matrix-mx-puppet-instagram.service']
- import_tasks: "roles/matrix-postgres/tasks/util/migrate_db_to_postgres.yml"
- set_fact:
matrix_mx_puppet_instagram_requires_restart: true
when: "matrix_mx_puppet_instagram_sqlite_database_path_local_stat_result.stat.exists|bool"
when: "matrix_mx_puppet_instagram_database_engine == 'postgres'"
- name: Ensure mx-puppet-instagram image is pulled
docker_image:
name: "{{ matrix_mx_puppet_instagram_docker_image }}"
@ -77,4 +104,8 @@
daemon_reload: yes
when: "matrix_mx_puppet_instagram_systemd_service_result.changed"
- name: Ensure matrix-mx-puppet-instagram.service restarted, if necessary
service:
name: "matrix-mx-puppet-instagram.service"
state: restarted
when: "matrix_mx_puppet_instagram_requires_restart|bool"

View file

@ -8,23 +8,3 @@
with_items:
- "matrix_mx_puppet_instagram_appservice_token"
- "matrix_mx_puppet_instagram_homeserver_token"
- block:
- name: Check if an SQLite database already exists
stat:
path: "{{ matrix_mx_puppet_instagram_data_path }}/database.db"
register: matrix_mx_puppet_instagram_sqlite_database_path_local_stat_result
- name: Fail if an SQLite database already exists when using Postgres
fail:
msg: >-
matrix_mx_puppet_instagram_database_engine has been set to `postgres` (which is our new default now).
However, we've discovered an existing SQLite database in {{ matrix_mx_puppet_instagram_data_path }}/database.db.
It appears that you've been using this bridge with the SQLite engine until now.
To continue using SQLite, opt into it explicitly: add `matrix_mx_puppet_instagram_database_engine: sqlite` to your vars.yml file and re-run this same command.
Alternatively, to migrate your existing SQLite database to Postgres:
1. Stop all services (`ansible-playbook -i inventory/hosts setup.yml --tags=stop`)
2. Import the SQLite database into Postgres (`ansible-playbook -v -i inventory/hosts setup.yml --tags=import-generic-sqlite-db --extra-vars='sqlite_database_path={{ matrix_mx_puppet_instagram_data_path }}/database.db postgres_connection_string_variable_name=matrix_mx_puppet_instagram_database_connString'`)
3. Re-run the playbook (`ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start`)
when: "matrix_mx_puppet_instagram_sqlite_database_path_local_stat_result.stat.exists"
when: "matrix_mx_puppet_instagram_database_engine == 'postgres'"

View file

@ -50,11 +50,11 @@ database:
# Connection string to connect to the Postgres instance
# with username "user", password "pass", host "localhost" and database name "dbname".
# Modify each value as necessary
connString: {{ matrix_mx_puppet_instagram_database_connString | to_json }}
connString: {{ matrix_mx_puppet_instagram_database_connection_string|to_json }}
{% else %}
# Use SQLite3 as a database backend
# The name of the database file
filename: {{ matrix_mx_puppet_instagram_database_file }}
filename: {{ matrix_mx_puppet_instagram_sqlite_database_path_in_container|to_json }}
{% endif %}
logging: