Exemplo n.º 1
0
    def upgrade_db(self):
        """ Upgrade the database to the head revision with Alembic.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        force = self.args.force
        settings = get_appsettings(config_uri)
        name = settings['anuket.brand_name']
        directory = settings['anuket.backup_directory']
        today = date.today().isoformat()
        filename = '{0}-{1}.sql.bz2'.format(name, today)
        path = os.path.join(directory, filename)

        # check if there is a database backup
        isfile = os.path.isfile(path)
        if not isfile and not force:
            print("There is no up to date backup for the database. "
                  "Please use the backup script before upgrading!")
            return 1

        # upgrade the database
        alembic_cfg = get_alembic_settings(config_uri)
        upgrade(alembic_cfg, 'head')

        print("Database upgrade done.")
        return 0
Exemplo n.º 2
0
    def upgrade_db(self):
        """ Upgrade the database to the head revision with Alembic.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        force = self.args.force
        settings = get_appsettings(config_uri)
        name = settings["anuket.brand_name"]
        directory = settings["anuket.backup_directory"]
        today = date.today().isoformat()
        filename = "{0}-{1}.sql.bz2".format(name, today)
        path = os.path.join(directory, filename)

        # check if there is a database backup
        isfile = os.path.isfile(path)
        if not isfile and not force:
            print("There is no up to date backup for the database. " "Please use the backup script before upgrading!")
            return 1

        # upgrade the database
        alembic_cfg = get_alembic_settings(config_uri)
        upgrade(alembic_cfg, "head")

        print("Database upgrade done.")
        return 0
Exemplo n.º 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
Exemplo n.º 4
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
Exemplo n.º 5
0
 def test_get_alembic_settings(self):
     """ Test the `get_alembic_settings` function."""
     from alembic.config import Config
     from pyramid.paster import get_appsettings
     from anuket.lib.alembic_utils import get_alembic_settings
     alembic_cfg = get_alembic_settings(self.config_uri)
     # test the config object
     self.assertIsInstance(alembic_cfg, Config)
     # test the script_location option
     script_location = alembic_cfg.get_section_option(
         'alembic',
         'script_location')
     self.assertEqual(script_location, 'anuket:scripts/alembic')
     # test the sqlalchemy.url option
     sqlalchemy_url = alembic_cfg.get_section_option(
         'alembic',
         'sqlalchemy.url')
     pyramid_sqlalchemy_url = self.settings['sqlalchemy.url']
     self.assertEqual(sqlalchemy_url, pyramid_sqlalchemy_url)