Esempio n. 1
0
def _get_or_create_schema_state(
        txn: Cursor,
        database_engine: BaseDatabaseEngine) -> Optional[_SchemaState]:
    # Bluntly try creating the schema_version tables.
    sql_path = os.path.join(schema_path, "common", "schema_version.sql")
    executescript(txn, sql_path)

    txn.execute("SELECT version, upgraded FROM schema_version")
    row = txn.fetchone()

    if row is None:
        # new database
        return None

    current_version = int(row[0])
    upgraded = bool(row[1])

    compat_version: Optional[int] = None
    txn.execute("SELECT compat_version FROM schema_compat_version")
    row = txn.fetchone()
    if row is not None:
        compat_version = int(row[0])

    txn.execute(
        "SELECT file FROM applied_schema_deltas WHERE version >= ?",
        (current_version, ),
    )
    applied_deltas = tuple(d for d, in txn)

    return _SchemaState(
        current_version=current_version,
        compat_version=compat_version,
        applied_deltas=applied_deltas,
        upgraded=upgraded,
    )
Esempio n. 2
0
 def get_max_as_txn_id(txn: Cursor) -> int:
     logger.warning(
         "Falling back to slow query, you should port to postgres")
     txn.execute(
         "SELECT COALESCE(max(txn_id), 0) FROM application_services_txns"
     )
     return cast(Tuple[int], txn.fetchone())[0]
Esempio n. 3
0
def _get_or_create_schema_state(
    txn: Cursor, database_engine: BaseDatabaseEngine
) -> Optional[Tuple[int, List[str], bool]]:
    # Bluntly try creating the schema_version tables.
    sql_path = os.path.join(schema_path, "common", "schema_version.sql")
    executescript(txn, sql_path)

    txn.execute("SELECT version, upgraded FROM schema_version")
    row = txn.fetchone()

    if row is not None:
        current_version = int(row[0])
        txn.execute(
            "SELECT file FROM applied_schema_deltas WHERE version >= ?",
            (current_version, ),
        )
        applied_deltas = [d for d, in txn]
        upgraded = bool(row[1])
        return current_version, applied_deltas, upgraded

    return None
Esempio n. 4
0
 def get_max_state_group_txn(txn: Cursor):
     txn.execute("SELECT COALESCE(max(id), 0) FROM state_groups")
     return txn.fetchone()[0]
Esempio n. 5
0
 def get_next_id_txn(self, txn: Cursor) -> int:
     txn.execute("SELECT nextval(?)", (self._sequence_name, ))
     fetch_res = txn.fetchone()
     assert fetch_res is not None
     return fetch_res[0]
Esempio n. 6
0
 def get_db_locale(self, txn: Cursor) -> Tuple[str, str]:
     txn.execute(
         "SELECT datcollate, datctype FROM pg_database WHERE datname = current_database()"
     )
     collation, ctype = cast(Tuple[str, str], txn.fetchone())
     return collation, ctype
Esempio n. 7
0
 def get_next_id_txn(self, txn: Cursor) -> int:
     txn.execute("SELECT nextval(?)", (self._sequence_name, ))
     return txn.fetchone()[0]