Repository: Mailu/Mailu

Issue:
[Setup utility] Cannot generate files when database flavors have been switched
## Before you open your issue
- [x] Check if no issue or pull-request for this already exists.
- [x] Check [documentation](https://mailu.io/master/) and [FAQ](https://mailu.io/master/faq.html). (Tip, use the search function on the documentation page)
- [x] You understand `Mailu` is made by volunteers in their **free time** — be conscise, civil and accept that delays can occur.
- [x] The title of the issue should be short and simple. It should contain specific terms related to the actual issue. Be specific while writing the title.

## Environment & Versions
### Environment
 - [x] docker-compose
 - [ ] kubernetes
 - [x] docker swarm

### Versions
All versions

## Description
In the setup utility when you select a different database flavor and then switch back to SQLite, then you can not generate the files. The reason is that it still expects the fields to be filled of the alternative database flavor you previously selected. 
When you select an alternative database flavor, the fields (e.g. hostname, database name) are mandatory. These fields are still mandatory when you switch back to SQLlite as database flavor.

As a workaround you can fill in mandatory fields and then switch back to SQLite again. Or you could refresh the page and do not switch from SQLite. 

The problem is in https://github.com/Mailu/Mailu/blob/master/setup/templates/steps/database.html . 
I didn't check yet how to resolve this.

## Replication Steps
1) Go to https://setup.mailu.io/master/ and click next.
2). Enter a value for main domain server and public hostname.
3). Select roundcube as webmail.
4). At the bottom switch to postgresql or mysql as database
5). Switch back to SQLite as database.
6). Click Setup mailu. Note that the button does not work.

## Expected behaviour
The Setup Mailu button works after following about steps.
[Setup utility] Cannot generate files when database flavors have been switched
## Before you open your issue
- [x] Check if no issue or pull-request for this already exists.
- [x] Check [documentation](https://mailu.io/master/) and [FAQ](https://mailu.io/master/faq.html). (Tip, use the search function on the documentation page)
- [x] You understand `Mailu` is made by volunteers in their **free time** — be conscise, civil and accept that delays can occur.
- [x] The title of the issue should be short and simple. It should contain specific terms related to the actual issue. Be specific while writing the title.

## Environment & Versions
### Environment
 - [x] docker-compose
 - [ ] kubernetes
 - [x] docker swarm

### Versions
All versions

## Description
In the setup utility when you select a different database flavor and then switch back to SQLite, then you can not generate the files. The reason is that it still expects the fields to be filled of the alternative database flavor you previously selected. 
When you select an alternative database flavor, the fields (e.g. hostname, database name) are mandatory. These fields are still mandatory when you switch back to SQLlite as database flavor.

As a workaround you can fill in mandatory fields and then switch back to SQLite again. Or you could refresh the page and do not switch from SQLite. 

The problem is in https://github.com/Mailu/Mailu/blob/master/setup/templates/steps/database.html . 
I didn't check yet how to resolve this.

## Replication Steps
1) Go to https://setup.mailu.io/master/ and click next.
2). Enter a value for main domain server and public hostname.
3). Select roundcube as webmail.
4). At the bottom switch to postgresql or mysql as database
5). Switch back to SQLite as database.
6). Click Setup mailu. Note that the button does not work.

## Expected behaviour
The Setup Mailu button works after following about steps.


Discussion:

Resolving patch:
diff --git a/optional/postgresql/start.py b/optional/postgresql/start.py
deleted file mode 100755
--- a/optional/postgresql/start.py
+++ /dev/null
@@ -1,65 +0,0 @@
-#!/usr/bin/python3
-
-import anosql
-import psycopg2
-import glob
-import os
-import subprocess
-from socrate import conf
-
-def setup():
-    conn =  psycopg2.connect(user='postgres')
-    queries = anosql.load_queries('postgres', '/conf/queries.sql')
-    # Mailu user
-    queries.create_mailu_user(conn)
-    queries.update_pw(conn, pw=os.environ.get("DB_PW"))
-    # Healthcheck user
-    queries.create_health_user(conn)
-    queries.grant_health(conn)
-    conn.commit()
-    # create db cannot be atomic. But this script is the only active connection, this is kinda safe.
-    if not queries.check_db(conn):
-        conn.set_isolation_level(0)
-        queries.create_db(conn)
-        conn.set_isolation_level(1)
-    conn.close()
-
-# Check if /data is empty
-if not os.listdir("/data"):
-    os.system("chown -R postgres:postgres /data")
-    os.system("chmod 0700 /data")
-    base_backups=sorted(glob.glob("/backup/base-*"))
-    if base_backups:
-        # Restore the latest backup
-        subprocess.call(["tar", "--same-owner", "-zpxf", base_backups[-1] + "/base.tar.gz" , "-C", "/data"])
-        if os.listdir("/backup/wal_archive"):
-            with open("/data/recovery.conf", "w") as rec:
-                rec.write("restore_command = 'gunzip < /backup/wal_archive/%f > %p'\n")
-                rec.write("standby_mode = off\n")
-            os.system("chown postgres:postgres /data/recovery.conf")
-    else:
-        # Bootstrap the database
-        os.system("sudo -u postgres initdb -D /data")
-
-# Create backup directory structure, if it does not yet exist
-os.system("mkdir -p /backup/wal_archive")
-os.system("chown -R postgres:postgres /backup")
-
-# Render config files
-for pg_file in glob.glob("/conf/*.conf"):
-    conf.jinja(pg_file, os.environ, os.path.join("/data", os.path.basename(pg_file)))
-
-# (Re)start postgresql locally for DB and user creation
-os.system("sudo -u postgres pg_ctl start -D /data -o '-h \"''\" '")
-while os.path.isfile("recovery.conf"):
-    pass
-os.system("sudo -u postgres pg_ctl -D /data promote")
-setup()
-os.system("sudo -u postgres pg_ctl stop -m smart -w -D /data")
-
-out=open("/proc/1/fd/1", "w")
-err=open("/proc/1/fd/2", "w")
-# Run the cron deamon
-subprocess.Popen(["crond", "-f"], stdout=out, stderr=err)
-# Run postgresql service
-os.system("sudo -u postgres postgres -D /data -h \*")


Test patch:
diff --git a/tests/build.yml b/tests/build.yml
--- a/tests/build.yml
+++ b/tests/build.yml
@@ -65,13 +65,6 @@ services:
       args:
         VERSION: ${PINNED_MAILU_VERSION:-local}
 
-  postgresql:
-    image: ${DOCKER_ORG:-mailu}/postgresql:${PINNED_MAILU_VERSION:-local}
-    build:
-      context: ../optional/postgresql
-      args:
-        VERSION: ${PINNED_MAILU_VERSION:-local}
-
   roundcube:
     image: ${DOCKER_ORG:-mailu}/${DOCKER_PREFIX:-}roundcube:${PINNED_MAILU_VERSION:-local}
     build: