示例#1
0
    def test_get_alembic_revision(self):
        """ Test the `get_alembic_revision` function with a revision."""
        import transaction
        from anuket.models.migration import Migration
        version_table.create(self.engine)
        with transaction.manager:
            alembic_version = Migration()
            alembic_version.version_num = u'revid'
            self.DBSession.add(alembic_version)
        self.DBSession.remove()

        from anuket.lib.alembic_utils import get_alembic_revision
        revision = get_alembic_revision(self.config_uri)
        self.assertEqual(revision[0], u'revid')
示例#2
0
    def initialize_db(self):
        """ Initialize the database schema and insert default values.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        settings = get_appsettings(config_uri)
        engine = engine_from_config(settings, 'sqlalchemy.')
        DBSession.configure(bind=engine)

        # check if there is already a versioned database
        revision = get_alembic_revision(config_uri)
        if revision:
            print("This database is versioned. "
                  "Use the upgrade script instead!")
            return 1

        # create the tables (except alembic_version)
        Base.metadata.create_all(engine)

        # add default user & group values
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            admin_user = AuthUser()
            admin_user.username = u'admin'
            admin_user.password = u'admin'
            admin_user.group = admins_group
            try:
                DBSession.add(admins_group)
                DBSession.add(admin_user)
                DBSession.flush()
            except IntegrityError:
                DBSession.rollback()
                print("There is already a database. "
                      "Use the upgrade script instead!")
                return 1

        # stamp the database with the most recent revision
        # (and create alembic_version table)
        try:
            alembic_cfg = get_alembic_settings(config_uri)
            stamp(alembic_cfg, 'head')
        except (AttributeError, ImportError):  # pragma: no cover
            # alembic is missing or not configured
            pass

        print("Database initialization done.")
        return 0
示例#3
0
    def initialize_db(self):
        """ Initialize the database schema and insert default values.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        settings = get_appsettings(config_uri)
        engine = engine_from_config(settings, 'sqlalchemy.')
        DBSession.configure(bind=engine)

        # check if there is already a versioned database
        revision = get_alembic_revision(config_uri)
        if revision:
            print("This database is versioned. "
                  "Use the upgrade script instead!")
            return 1

        # create the tables (except alembic_version)
        Base.metadata.create_all(engine)

        # add default user & group values
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            admin_user = AuthUser()
            admin_user.username = u'admin'
            admin_user.password = u'admin'
            admin_user.group = admins_group
            try:
                DBSession.add(admins_group)
                DBSession.add(admin_user)
                DBSession.flush()
            except IntegrityError:
                DBSession.rollback()
                print("There is already a database. "
                      "Use the upgrade script instead!")
                return 1

        # stamp the database with the most recent revision
        # (and create alembic_version table)
        try:
            alembic_cfg = get_alembic_settings(config_uri)
            stamp(alembic_cfg, 'head')
        except (AttributeError, ImportError):  # pragma: no cover
            # alembic is missing or not configured
            pass

        print("Database initialization done.")
        return 0
示例#4
0
 def test_get_alembic_revision_empty(self):
     """ Test the `get_alembic_settings` function with an empty revision."""
     from anuket.lib.alembic_utils import get_alembic_revision
     revision = get_alembic_revision(self.config_uri)
     self.assertIsNone(revision)