from textwrap import dedent


def base_spec(cursor):
    """ A spec with the existing state of the test database before anything has been done """
    spec = dedent("""
        postgres:
            attributes:
                - BYPASSRLS
                - CREATEDB
                - CREATEROLE
                - REPLICATION
            can_login: true
            is_superuser: true
            owns:
                schemas:
                    - information_schema
                    - pg_catalog
                    - public
                tables:
                    - information_schema.*
                    - pg_catalog.*
            privileges:
                schemas:
                    write:
                        - information_schema
                        - pg_catalog
                        - public

        test_user:
            attributes:
                - PASSWORD "test_password"
            can_login: yes
            is_superuser: yes
        """)

    # Postgres 10 introduces several new roles that we have to account for
    cursor.execute("SELECT substring(version from 'PostgreSQL ([0-9.]*) ') FROM version()")
    pg_version = cursor.fetchone()[0]
    if pg_version.startswith('10.'):
        spec += dedent("""

            pg_read_all_settings:

            pg_stat_scan_tables:

            pg_read_all_stats:

            pg_monitor:
                member_of:
                    - pg_read_all_settings
                    - pg_stat_scan_tables
                    - pg_read_all_stats
            """)

    return spec