示例#1
0
def test_database_fresh():
    db = db_connect(':memory:', 'peewee')

    with DatabaseContext.use(db):
        m = SchemaMigration()

        # Run fresh schema migration
        assert m.run() is True
示例#2
0
def test_database_upgrade():
    db = db_connect(':memory:', 'peewee')

    with DatabaseContext.use(db):
        m = SchemaMigration()

        # Run initial migration
        router = m._build_router()
        router.run('000_initial')

        # Run schema migration
        assert m.run() is True
示例#3
0
    def _get(cls, path, type, **kwargs):
        path = os.path.abspath(path)
        cache = cls._cache[type]

        with cls._lock:
            if path not in cache:
                try:
                    cache[path] = db_connect(path, type, **kwargs)
                except DisabledError:
                    cache[path] = None

            # Return cached connection
            return cache[path]
示例#4
0
    def backup(cls, group, database, tag=None, metadata=None):
        timestamp = datetime.now()

        # Build backup directory/name
        directory, name, path = cls.path(group, timestamp, tag)
        destination_path = path + '.db'

        # Ensure directory exists
        if not os.path.exists(directory):
            os.makedirs(directory)

        log.info('[%s] Backing up database to %r', group, destination_path)

        # Backup database
        destination = db_connect(destination_path, 'raw', name='backup database')

        # Get `database` connection
        source = db_connection(database)

        # Copy `source` database to `destination`
        try:
            cls._copy(group, source, destination)
        finally:
            # Close `destination` database
            destination.close()

        # Ensure path exists
        if not os.path.exists(destination_path):
            log.error('Backup failed (file doesn\'t exist)')
            return False

        # Construct revision
        revision = BackupRevision(
            timestamp, [
                name + '.db'
            ],

            tag=tag,
            attributes=metadata or {}
        )

        # Write backup metadata
        revision.save(path + '.bre')

        return True
示例#5
0
def test_database_corruption():
    db = db_connect(':memory:', 'peewee')

    with DatabaseContext.use(db):
        m = SchemaMigration()

        # Run initial migration
        router = m._build_router()
        router.run('000_initial')

        # Remove table from database (schema corruption)
        db.execute_sql('DROP TABLE "configuration.option"')

        # Ensure migration validation fails
        assert router.validate() is False

        # Run schema migration
        assert m.run() is True
示例#6
0
    def backup(cls, group, database, tag=None, metadata=None):
        timestamp = datetime.now()

        # Build backup directory/name
        directory, name, path = cls.path(group, timestamp, tag)
        destination_path = path + '.db'

        # Ensure directory exists
        if not os.path.exists(directory):
            os.makedirs(directory)

        log.info('[%s] Backing up database to %r', group, destination_path)

        # Backup database
        destination = db_connect(destination_path,
                                 'raw',
                                 name='backup database')

        # Get `database` connection
        source = db_connection(database)

        # Copy `source` database to `destination`
        try:
            cls._copy(group, source, destination)
        finally:
            # Close `destination` database
            destination.close()

        # Ensure path exists
        if not os.path.exists(destination_path):
            log.error('Backup failed (file doesn\'t exist)')
            return False

        # Construct revision
        revision = BackupRevision(timestamp, [name + '.db'],
                                  tag=tag,
                                  attributes=metadata or {})

        # Write backup metadata
        revision.save(path + '.bre')

        return True