synapse/storage/prepare_database.py
def _setup_new_database(cur, database_engine):
    """Sets up the database by finding a base set of "full schemas" and then
    applying any necessary deltas.

    The "full_schemas" directory has subdirectories named after versions. This
    function searches for the highest version less than or equal to
    `SCHEMA_VERSION` and executes all .sql files in that directory.

    The function will then apply all deltas for all versions after the base
    version.

    Example directory structure:

        schema/
            delta/
                ...
            full_schemas/
                3/
                    test.sql
                    ...
                11/
                    foo.sql
                    bar.sql
                ...

    In the example foo.sql and bar.sql would be run, and then any delta files
    for versions strictly greater than 11.
    """
    current_dir = os.path.join(dir_path, "schema", "full_schemas")
    directory_entries = os.listdir(current_dir)

    valid_dirs = []
    pattern = re.compile(r"^\d+(\.sql)?$")
    for filename in directory_entries:
        match = pattern.match(filename)
        abs_path = os.path.join(current_dir, filename)
        if match and os.path.isdir(abs_path):
            ver = int(match.group(0))
            if ver <= SCHEMA_VERSION:
                valid_dirs.append((ver, abs_path))
        else:
            logger.warn("Unexpected entry in 'full_schemas': %s", filename)

    if not valid_dirs:
        raise PrepareDatabaseException(
            "Could not find a suitable base set of full schemas"
        )

    max_current_ver, sql_dir = max(valid_dirs, key=lambda x: x[0])

    logger.debug("Initialising schema v%d", max_current_ver)

    directory_entries = os.listdir(sql_dir)

    for filename in fnmatch.filter(directory_entries, "*.sql"):
        sql_loc = os.path.join(sql_dir, filename)
        logger.debug("Applying schema %s", sql_loc)
        executescript(cur, sql_loc)

    cur.execute(
        database_engine.convert_param_style(
            "INSERT INTO schema_version (version, upgraded)"
            " VALUES (?,?)"
        ),
        (max_current_ver, False,)
    )

    _upgrade_existing_database(
        cur,
        current_version=max_current_ver,
        applied_delta_files=[],
        upgraded=False,
        database_engine=database_engine,
        config=None,
        is_empty=True,
    )
Sets up the database by finding a base set of "full schemas" and then
    applying any necessary deltas.

    The "full_schemas" directory has subdirectories named after versions. This
    function searches for the highest version less than or equal to
    `SCHEMA_VERSION` and executes all .sql files in that directory.

    The function will then apply all deltas for all versions after the base
    version.

    Example directory structure:

        schema/
            delta/
                ...
            full_schemas/
                3/
                    test.sql
                    ...
                11/
                    foo.sql
                    bar.sql
                ...

    In the example foo.sql and bar.sql would be run, and then any delta files
    for versions strictly greater than 11.
    
Sets up the database by finding a base set of "full schemas" and then
applying any necessary deltas.

The "full_schemas" directory has subdirectories named after versions. This
function searches for the highest version less than or equal to
`SCHEMA_VERSION` and executes all .sql files in that directory.

The function will then apply all deltas for all versions after the base
version.

Example directory structure.



In the example foo.sql and bar.sql would be run, and then any delta files
for versions strictly greater than 11.
Sets up the database by finding a base set of " full schemas " and then applying any necessary deltas . The " full_schemas " directory has subdirectories named after versions . This function searches for the highest version less than or equal to ` SCHEMA_VERSION ` and executes all . sql files in that directory . The function will then apply all deltas for all versions after the base version . Example directory structure . In the example foo . sql and bar . sql would be run and then any delta files for versions strictly greater than 11 .
def _setup_new_database(cur, database_engine):
    current_dir = os.path.join(dir_path, "schema", "full_schemas")
    directory_entries = os.listdir(current_dir)
    valid_dirs = []
    pattern = re.compile(r"^\d+(\.sql)?$")
    for filename in directory_entries:
        match = pattern.match(filename)
        abs_path = os.path.join(current_dir, filename)
        if match and os.path.isdir(abs_path):
            ver = int(match.group(0))
            if ver <= SCHEMA_VERSION:
                valid_dirs.append((ver, abs_path))
        else:
            logger.warn("Unexpected entry in 'full_schemas': %s", filename)
    if not valid_dirs:
        raise PrepareDatabaseException(
            "Could not find a suitable base set of full schemas"
        )
    max_current_ver, sql_dir = max(valid_dirs, key=lambda x: x[0])
    logger.debug("Initialising schema v%d", max_current_ver)
    directory_entries = os.listdir(sql_dir)
    for filename in fnmatch.filter(directory_entries, "*.sql"):
        sql_loc = os.path.join(sql_dir, filename)
        logger.debug("Applying schema %s", sql_loc)
        executescript(cur, sql_loc)
    cur.execute(
        database_engine.convert_param_style(
            "INSERT INTO schema_version (version, upgraded)"
            " VALUES (?,?)"
        ),
        (max_current_ver, False,)
    )
    _upgrade_existing_database(
        cur,
        current_version=max_current_ver,
        applied_delta_files=[],
        upgraded=False,
        database_engine=database_engine,
        config=None,
        is_empty=True,
    )
def _setup_new_database ( cur , database_engine ) : current_dir = os . path . join ( dir_path , "schema" , "full_schemas" ) directory_entries = os . listdir ( current_dir ) valid_dirs = [ ] pattern = re . compile ( r"^\d+(\.sql)?$" ) for filename in directory_entries : match = pattern . match ( filename ) abs_path = os . path . join ( current_dir , filename ) if match and os . path . isdir ( abs_path ) : ver = int ( match . group ( 0 ) ) if ver <= SCHEMA_VERSION : valid_dirs . append ( ( ver , abs_path ) ) else : logger . warn ( "Unexpected entry in 'full_schemas': %s" , filename ) if not valid_dirs : raise PrepareDatabaseException ( "Could not find a suitable base set of full schemas" ) max_current_ver , sql_dir = max ( valid_dirs , key = lambda x : x [ 0 ] ) logger . debug ( "Initialising schema v%d" , max_current_ver ) directory_entries = os . listdir ( sql_dir ) for filename in fnmatch . filter ( directory_entries , "*.sql" ) : sql_loc = os . path . join ( sql_dir , filename ) logger . debug ( "Applying schema %s" , sql_loc ) executescript ( cur , sql_loc ) cur . execute ( database_engine . convert_param_style ( "INSERT INTO schema_version (version, upgraded)" " VALUES (?,?)" ) , ( max_current_ver , False , ) ) _upgrade_existing_database ( cur , current_version = max_current_ver , applied_delta_files = [ ] , upgraded = False , database_engine = database_engine , config = None , is_empty = True , )
Sets up the database by finding a base set of "full schemas" and then
applying any necessary deltas.
Sets up the database by finding a base set of " full schemas " and then applying any necessary deltas .
"""Sets up the database by finding a base set of "full schemas" and then
    applying any necessary deltas.

    The "full_schemas" directory has subdirectories named after versions. This
    function searches for the highest version less than or equal to
    `SCHEMA_VERSION` and executes all .sql files in that directory.

    The function will then apply all deltas for all versions after the base
    version.

    Example directory structure:

        schema/
            delta/
                ...
            full_schemas/
                3/
                    test.sql
                    ...
                11/
                    foo.sql
                    bar.sql
                ...

    In the example foo.sql and bar.sql would be run, and then any delta files
    for versions strictly greater than 11.
    """
cur
database_engine