def testUpdatingSchemaVersion(mysqlBackendConfig, mySQLBackendConfigFile):
    with cleanDatabase(MySQL(**mysqlBackendConfig)) as db:
        createSchemaVersionTable(db)

        version = readSchemaVersion(db)
        assert version is None

        with updateSchemaVersion(db, version=2):
            pass  # NOOP

        version = readSchemaVersion(db)
        assert version == 2
def testReadingSchemaVersionFailsOnUnfinishedUpdate(mysqlBackendConfig,
                                                    mySQLBackendConfigFile):
    with cleanDatabase(MySQL(**mysqlBackendConfig)) as db:
        createSchemaVersionTable(db)

        try:
            with updateSchemaVersion(db, version=1):
                raise RuntimeError("For testing.")
        except RuntimeError:
            pass

        with pytest.raises(DatabaseMigrationUnfinishedError):
            readSchemaVersion(db)
def testUpdatingCurrentBackendDoesBreakNothing(mysqlBackendConfig,
                                               mySQLBackendConfigFile):
    with cleanDatabase(MySQL(**mysqlBackendConfig)):
        with MySQLBackend(**mysqlBackendConfig) as freshBackend:
            freshBackend.backend_createBase()

        updateMySQLBackend(backendConfigFile=mySQLBackendConfigFile)
        # Updating again. Should break nothing.
        updateMySQLBackend(backendConfigFile=mySQLBackendConfigFile)

        with MySQLBackend(**mysqlBackendConfig) as anotherBackend:
            # We want to have the latest schema version
            assert DATABASE_SCHEMA_VERSION == readSchemaVersion(
                anotherBackend._sql)
def testReadingSchemaVersionOnlyReturnsNewestValue(mysqlBackendConfig,
                                                   mySQLBackendConfigFile):
    with cleanDatabase(MySQL(**mysqlBackendConfig)) as db:
        createSchemaVersionTable(db)

        with updateSchemaVersion(db, version=1):
            pass

        with updateSchemaVersion(db, version=15):
            pass

        for number in range(1, 4):
            with updateSchemaVersion(db, version=number * 2):
                pass

        with updateSchemaVersion(db, version=3):
            pass

        assert readSchemaVersion(db) == 15
def testCreatingBackendSetsTheLatestSchemaVersion(mysqlBackendConfig,
                                                  mySQLBackendConfigFile):
    with cleanDatabase(MySQL(**mysqlBackendConfig)) as db:
        with MySQLBackend(**mysqlBackendConfig) as freshBackend:
            freshBackend.backend_createBase()
            assert readSchemaVersion(db) == DATABASE_SCHEMA_VERSION
def testReadingSchemaVersionFromEmptyTable(mysqlBackendConfig,
                                           mySQLBackendConfigFile):
    with cleanDatabase(MySQL(**mysqlBackendConfig)) as db:
        createSchemaVersionTable(db)

        assert readSchemaVersion(db) is None
def testReadingSchemaVersionIfTableIsMissing(mysqlBackendConfig,
                                             mySQLBackendConfigFile):
    with cleanDatabase(MySQL(**mysqlBackendConfig)) as db:
        assert readSchemaVersion(db) is None