コード例 #1
0
def enable_longer_zodb_paths(context):
    session = Session()
    session.execute(
        "ALTER TABLE %s ALTER COLUMN zodb_path TYPE varchar(512);" %
        model.SurveyTreeItem.__table__.name
    )
    datamanager.mark_changed(session)
コード例 #2
0
    def _migrate_sql_column(self, table, column_name, old_userid, new_userid):  # noqa
        moved = []

        rows_to_fix = self._get_sql_rows_with_old_userid(
            table, column_name, old_userid)

        if rows_to_fix:
            logger.info(
                "Migrating '{}.{}' ({} -> {})".format(
                    table.name, column_name, old_userid, new_userid))
            self._verify_user(new_userid)

            column = getattr(table.c, column_name)
            self.session.execute(
                table.update().
                where(column == old_userid).
                values(**{column_name: new_userid}))

            mark_changed(self.session)

            # Use primary key (possibly compound) as unique row identifier
            pk_cols = [c.name for c in table.primary_key]
            for row in rows_to_fix:
                pk = ['%s=%s' % (cn, getattr(row, cn, None)) for cn in pk_cols]
                row_id = '%s: ' % table.name + ','.join(pk)
                moved.append((row_id, old_userid, new_userid))
        return moved
コード例 #3
0
def migrateCompanyTable(context):
    from z3c.saconfig import Session
    from euphorie.deployment.upgrade.utils import ColumnExists
    from euphorie.deployment.upgrade.utils import TableExists
    from euphorie.client import model
    from zope.sqlalchemy import datamanager
    import transaction

    session = Session()
    if ColumnExists(session, "company", "referer"):
        return

    if TableExists(session, "company"):
        log.info("Moving company table to dutch_company")
        session.execute("ALTER TABLE company RENAME TO dutch_company")
        session.execute(
                "ALTER SEQUENCE company_id_seq RENAME TO dutch_company_id_seq")
        session.execute(
                "ALTER INDEX ix_company_session_id RENAME TO "
                        "ix_dutch_company_session_id")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()

    log.info("Creating new company table")
コード例 #4
0
    def _migrate_sql_column(self, table, column_name, old_userid,
                            new_userid):  # noqa
        moved = []

        rows_to_fix = self._get_sql_rows_with_old_userid(
            table, column_name, old_userid)

        if rows_to_fix:
            logger.info("Migrating '{}.{}' ({} -> {})".format(
                table.name, column_name, old_userid, new_userid))
            self._verify_user(new_userid)

            column = getattr(table.c, column_name)
            self.session.execute(table.update().where(
                column == old_userid).values(**{column_name: new_userid}))

            mark_changed(self.session)

            # Use primary key (possibly compound) as unique row identifier
            pk_cols = [c.name for c in table.primary_key]
            for row in rows_to_fix:
                pk = ['%s=%s' % (cn, getattr(row, cn, None)) for cn in pk_cols]
                row_id = '%s: ' % table.name + ','.join(pk)
                moved.append((row_id, old_userid, new_userid))
        return moved
コード例 #5
0
def sql_create_all(context):
    """Add all missing SQL tables and indices.
    """
    session = Session()
    transaction.get().commit()
    model.metadata.create_all(session.bind, checkfirst=True)
    datamanager.mark_changed(session)
コード例 #6
0
ファイル: statistics.py プロジェクト: euphorie/osha.oira
    def render(self):
        log.info("Called: write_statistics")
        dbtable_surveys = "statistics_surveys"
        info_surveys = self.getSurveysInfo()
        # write to db
        session = Session()
        session.execute("""DELETE FROM %s;""" % dbtable_surveys)

        def clean(value):
            if isinstance(value, basestring):
                return safe_unicode(value).strip().encode("utf-8")
            return value

        def pg_format(value):
            if value is None:
                return "NULL"
            if isinstance(value, datetime):
                return "TIMESTAMP '%s'" % value.isoformat()
            return "'%s'" % value

        for line in info_surveys:
            insert = """INSERT INTO %s VALUES %s;""" % (
                dbtable_surveys,
                "(%s)" % ", ".join(map(pg_format, map(clean, line))),
            )
            session.execute(insert)
        datamanager.mark_changed(session)
        transaction.get().commit()
        log.info("Exported statistics on {0} surveys to the DB.".format(
            len(info_surveys)))
        from pprint import pformat

        return "Written:\n" + pformat(info_surveys)
コード例 #7
0
    def render(self):
        dbtable_surveys = 'statistics_surveys'
        info_surveys = self.getSurveysInfo()
        # write to db
        session = Session()
        session.execute('''DELETE FROM %s;''' % dbtable_surveys)

        def clean(value):
            if isinstance(value, basestring):
                return safe_unicode(value).strip().encode('utf-8')
            return value

        def pg_format(value):
            if value is None:
                return 'NULL'
            if isinstance(value, datetime):
                return "TIMESTAMP '%s'" % value.isoformat()
            return "'%s'" % value

        for line in info_surveys:
            insert = '''INSERT INTO %s VALUES %s;''' % \
                     (dbtable_surveys, '(%s)' % ', '.join(map(pg_format,
                      map(clean, line))))
            session.execute(insert)
        datamanager.mark_changed(session)
        transaction.get().commit()
        from pprint import pformat
        return "Written:\n" + pformat(info_surveys)
コード例 #8
0
ファイル: SubmissionInfoDao.py プロジェクト: vabc3/zojstat
def savesinfo(data):
    if type(data)==list:
	    for item in data:
	    	_savesinfo(item)
    else:
        _savesinfo(data)
    mark_changed(DBSession())
コード例 #9
0
ファイル: upgrade.py プロジェクト: euphorie/osha.oira
def sql_create_all(context):
    """Add all missing SQL tables and indices.
    """
    session = Session()
    transaction.get().commit()
    model.metadata.create_all(session.bind, checkfirst=True)
    datamanager.mark_changed(session)
コード例 #10
0
    def render(self):
        log.info('Called: write_statistics')
        dbtable_surveys = 'statistics_surveys'
        info_surveys = self.getSurveysInfo()
        # write to db
        session = Session()
        session.execute('''DELETE FROM %s;''' % dbtable_surveys)

        def clean(value):
            if isinstance(value, basestring):
                return safe_unicode(value).strip().encode('utf-8')
            return value

        def pg_format(value):
            if value is None:
                return 'NULL'
            if isinstance(value, datetime):
                return "TIMESTAMP '%s'" % value.isoformat()
            return "'%s'" % value

        for line in info_surveys:
            insert = '''INSERT INTO %s VALUES %s;''' % \
                     (dbtable_surveys, '(%s)' % ', '.join(map(pg_format,
                      map(clean, line))))
            session.execute(insert)
        datamanager.mark_changed(session)
        transaction.get().commit()
        log.info('Exported statistics on {0} surveys to the DB.'.format(
            len(info_surveys)))
        from pprint import pformat
        return "Written:\n" + pformat(info_surveys)
コード例 #11
0
def drop_constraint_no_duplicates_in_tree(context):
    session = Session()
    if TableExists(session, "tree"):
        session.execute("ALTER TABLE tree DROP CONSTRAINT no_duplicates")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()
    log.info("Removed the constraint `no_duplicates` from table tree.")
コード例 #12
0
ファイル: upgrade.py プロジェクト: euphorie/osha.oira
def increase_sessions_path_column(context):
    session = Session()
    if TableExists(session, "session"):
        session.execute("ALTER TABLE session ALTER COLUMN zodb_path TYPE varchar(512)")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()
    log.info("Increased the size of column zodb_path in table session.")
コード例 #13
0
ファイル: upgrade.py プロジェクト: euphorie/osha.oira
def enable_custom_risks_on_all_modules(context):
    """ """
    if not api.portal.get_registry_record("euphorie.allow_user_defined_risks"):
        log.warning(
            "Custom risks are not enabled. Set 'allow_user_defined_risks' to "
            "true in euphorie.ini for enabling them."
        )
        return
    portal = api.portal.get()
    client = portal.client
    count = 0
    for country in client.objectValues():
        if IClientCountry.providedBy(country):
            for sector in country.objectValues():
                if IClientSector.providedBy(sector):
                    for survey in sector.objectValues():
                        try:
                            is_new = EnableCustomRisks(survey)
                            count += 1
                            custom = getattr(survey, "custom-risks", None)
                            if custom:
                                custom.title = _(
                                    "title_other_risks",
                                    default="Added risks (by you)",
                                )
                                custom.description = _(
                                    "description_other_risks",
                                    default="In case you have identified risks not included in "  # noqa: E501
                                    "the tool, you are able to add them now:",
                                )
                                custom.question = _(
                                    "question_other_risks",
                                    default="<p>Would you now like to add your own defined risks "  # noqa: E501
                                    "to this tool?</p><p><strong>Important:</strong> In "  # noqa: E501
                                    "order to avoid duplicating risks, we strongly recommend you "  # noqa: E501
                                    "to go first through all the previous modules, if you have not "  # noqa: E501
                                    "done it yet.</p><p>If you don't need to add risks, please select 'No.'</p>",  # noqa: E501
                                )
                            if is_new:
                                survey.published = (
                                    survey.id,
                                    survey.title,
                                    datetime.datetime.now(),
                                )
                        except Exception as e:
                            log.error(
                                "Could not enable custom risks for module. %s" % e
                            )
    log.info("All %d published surveys can now have custom risks." % count)
    session = Session()
    if TableExists(session, "tree"):
        session.execute(
            "UPDATE tree SET title = 'title_other_risks' WHERE zodb_path ='custom-risks'"  # noqa: E501
        )
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()
        log.info("Set correct title on all exisiting sessions for custom risks module.")
コード例 #14
0
ファイル: testing.py プロジェクト: braegelno5/opengever.core
 def testSetUp(self):
     super(GEVERIntegrationTesting, self).testSetUp()
     # In order to let the SQL transaction manager make a savepoint of no
     # changes we need to mark the session as changed first.
     mark_changed(create_session())
     self.savepoint = transaction.savepoint()
     self.interceptor.intercept(self.interceptor.BEGIN
                                | self.interceptor.COMMIT
                                | self.interceptor.ABORT)
コード例 #15
0
ファイル: upgrade.py プロジェクト: euphorie/osha.oira
def increase_sessions_path_column(context):
    session = Session()
    if TableExists(session, "session"):
        session.execute(
            "ALTER TABLE session ALTER COLUMN zodb_path TYPE varchar(512)")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()
    log.info("Increased the size of column zodb_path in table session.")
コード例 #16
0
ファイル: v2.py プロジェクト: euphorie/tno.euphorie
def add_od_version_column(context):
    session = Session()
    if column_exists(session, 'od_link', 'version'):
        return
    transaction.get().commit()
    session.execute('ALTER TABLE od_link ADD COLUMN version INT DEFAULT 0 NOT NULL')
    datamanager.mark_changed(session)
    transaction.get().commit()
    log.info("Added new column 'version' to table 'od_link'")
コード例 #17
0
ファイル: upgrade.py プロジェクト: euphorie/osha.oira
def alter_time_column(context):
    session = Session()
    if TableExists(session, "statistics_login"):
        session.execute(
            "ALTER TABLE statistics_login ALTER COLUMN time SET DEFAULT CURRENT_TIMESTAMP")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()
    log.info("Changed default for column 'time' to current timestamp")
コード例 #18
0
def drop_constraint_no_duplicates_in_tree(context):
    session = Session()
    if TableExists(session, "tree"):
        session.execute(
            "ALTER TABLE tree DROP CONSTRAINT no_duplicates")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()
    log.info("Removed the constraint `no_duplicates` from table tree.")
コード例 #19
0
ファイル: v2.py プロジェクト: euphorie/tno.euphorie
def add_od_version_column(context):
    session = Session()
    if column_exists(session, "od_link", "version"):
        return
    transaction.get().commit()
    session.execute(
        "ALTER TABLE od_link ADD COLUMN version INT DEFAULT 0 NOT NULL")
    datamanager.mark_changed(session)
    transaction.get().commit()
    log.info("Added new column 'version' to table 'od_link'")
コード例 #20
0
def allow_empty_password(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = inspector.get_columns(Account.__table__.name)
    password = [c for c in columns if c['name'] == 'password'][0]
    if not password['nullable']:
        log.info('Dropping NOT NULL constraint for account.password')
        session.execute(
            'ALTER TABLE account ALTER COLUMN password DROP NOT NULL')
        datamanager.mark_changed(session)
コード例 #21
0
def alter_time_column(context):
    session = Session()
    if TableExists(session, "statistics_login"):
        session.execute(
            "ALTER TABLE statistics_login ALTER COLUMN time SET DEFAULT CURRENT_TIMESTAMP"
        )
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()
    log.info("Changed default for column 'time' to current timestamp")
コード例 #22
0
def allow_empty_password(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = inspector.get_columns(Account.__table__.name)
    password = [c for c in columns if c['name'] == 'password'][0]
    if not password['nullable']:
        log.info('Dropping NOT NULL constraint for account.password')
        session.execute(
                'ALTER TABLE account ALTER COLUMN password DROP NOT NULL')
        datamanager.mark_changed(session)
コード例 #23
0
def add_wp_column_to_company(context):
    session = Session()
    if TableExists(session, "company"):
        session.execute(
            "ALTER TABLE company ADD workers_participated bool DEFAULT NULL")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()

    log.info("Added new column 'workers_participated' to table 'company'")
コード例 #24
0
ファイル: v3.py プロジェクト: EU-OSHA/Euphorie
def add_wp_column_to_company(context):
    session = Session()
    if TableExists(session, "company"):
        session.execute(
            "ALTER TABLE company ADD workers_participated bool DEFAULT NULL")
        model.metadata.create_all(session.bind, checkfirst=True)
        datamanager.mark_changed(session)
        transaction.get().commit()

    log.info("Added new column 'workers_participated' to table 'company'")
コード例 #25
0
    def mark_as_installed_in_sql(self, target_version):
        if not self.is_schema_migration(target_version):
            return False  # we only track SchemaMigration upgrade steps in SQL

        if self.is_marked_installed_in_sql(target_version):
            return False  # Version is already marked as installed.

        self._setup_db_connection()
        mark_changed(self.session)
        self.session.execute(self._get_tracking_table().insert().values(
            profileid=self.profile, upgradeid=target_version))
コード例 #26
0
ファイル: v6.py プロジェクト: EU-OSHA/Euphorie
def add_skip_evaluation_to_model(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = [c['name']
               for c in inspector.get_columns(model.Risk.__table__.name)]
    if 'skip_evaluation' not in columns:
        log.info('Adding skip_evaluation column for risks')
        session.execute(
            "ALTER TABLE %s ADD skip_evaluation BOOL DEFAULT 'f' NOT NULL" %
            model.Risk.__table__.name)
        datamanager.mark_changed(session)
コード例 #27
0
 def _get_tracking_table(self):
     """Fetches the tracking table from the DB schema metadata if present,
     or creates it if necessary.
     """
     table = MetaData(self.connection, reflect=True).tables.get(TRACKING_TABLE_NAME)
     if table is not None:
         self._migrate_tracking_table(table)
         return table
     else:
         mark_changed(self.session)
         return self.operations.create_table(*TRACKING_TABLE_DEFINITION)
コード例 #28
0
ファイル: upgrade.py プロジェクト: lukasgraf/opengever.core
 def _get_tracking_table(self):
     """Fetches the tracking table from the DB schema metadata if present,
     or creates it if necessary.
     """
     table = MetaData(self.connection, reflect=True).tables.get(TRACKING_TABLE_NAME)
     if table is not None:
         self._migrate_tracking_table(table)
         return table
     else:
         mark_changed(self.session)
         return self.operations.create_table(*TRACKING_TABLE_DEFINITION)
コード例 #29
0
ファイル: v3.py プロジェクト: EU-OSHA/Euphorie
def add_has_description_column(context):
    session = Session()
    if ColumnExists(session, 'tree', 'has_description'):
        return

    transaction.get().commit()
    session.execute("ALTER TABLE tree ADD has_description bool DEFAULT 'f'")
    model.metadata.create_all(session.bind, checkfirst=True)
    datamanager.mark_changed(session)
    transaction.get().commit()
    log.info("Added new column 'has_description' to table 'tree'")
コード例 #30
0
ファイル: v10.py プロジェクト: EU-OSHA/Euphorie
def add_column_for_existing_measures(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = [
        c['name'] for c in inspector.get_columns(model.Risk.__table__.name)
    ]
    if 'existing_measures' not in columns:
        log.info('Adding existing_measures column for risks')
        session.execute("ALTER TABLE %s ADD existing_measures TEXT" %
                        model.Risk.__table__.name)
        datamanager.mark_changed(session)
コード例 #31
0
ファイル: v20.py プロジェクト: euphorie/Euphorie
def add_column_for_training_notes(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = [
        c["name"] for c in inspector.get_columns(model.Risk.__table__.name)
    ]
    if "training_notes" not in columns:
        log.info("Adding training_notes column for risks")
        session.execute("ALTER TABLE %s ADD training_notes TEXT" %
                        model.Risk.__table__.name)
        datamanager.mark_changed(session)
コード例 #32
0
def add_column_for_custom_risks(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = [c['name']
               for c in inspector.get_columns(model.Risk.__table__.name)]
    if 'is_custom_risk' not in columns:
        log.info('Adding is_custom_risk column for risks')
        session.execute(
            "ALTER TABLE %s ADD is_custom_risk BOOL NOT NULL DEFAULT FALSE" %
            model.Risk.__table__.name)
        datamanager.mark_changed(session)
コード例 #33
0
def addAccountChangeTable(context):
    from z3c.saconfig import Session
    from euphorie.client import model
    from zope.sqlalchemy import datamanager
    import transaction
    transaction.get().commit()  # Clean current connection to prevent hangs
    session = Session()
    model.AccountChangeRequest.__table__.create(
            bind=session.bind, checkfirst=True)
    datamanager.mark_changed(session)
    transaction.get().commit()
コード例 #34
0
ファイル: v1.py プロジェクト: euphorie/tno.euphorie
def updateAbsenteePercentage(context):
    from z3c.saconfig import Session
    from euphorie.deployment.upgrade.utils import ColumnType
    from zope.sqlalchemy import datamanager

    session=Session()
    if ColumnType(session, "dutch_company", "absentee_percentage")=="numeric(5,2)":
        return

    log.info("Changing type for dutch_company.absentee_percentage to NUMERIC(5,2)")
    session.execute("ALTER TABLE dutch_company ALTER COLUMN absentee_percentage TYPE NUMERIC(5,2);")
    datamanager.mark_changed(session)
コード例 #35
0
ファイル: model.py プロジェクト: pombredanne/Euphorie
 def removeChildren(self):
     session = Session()
     if self.path:
         filter = sql.and_(SurveyTreeItem.session_id == self.session_id,
                           SurveyTreeItem.path.like(self.path + "%"),
                           SurveyTreeItem.id != self.id)
     else:
         filter = sql.and_(SurveyTreeItem.session_id == self.session_id,
                           SurveyTreeItem.id != self.id)
     session.execute(SurveyTreeItem.__table__.delete().where(filter))
     self.session.touch()
     datamanager.mark_changed(session)
コード例 #36
0
def add_has_description_column(context):
    session = Session()
    if ColumnExists(session, 'tree', 'has_description'):
        return

    transaction.get().commit()
    session.execute(
            "ALTER TABLE tree ADD has_description bool DEFAULT 'f'")
    model.metadata.create_all(session.bind, checkfirst=True)
    datamanager.mark_changed(session)
    transaction.get().commit()
    log.info("Added new column 'has_description' to table 'tree'")
コード例 #37
0
ファイル: upgrade.py プロジェクト: lukasgraf/opengever.core
    def mark_as_installed_in_sql(self, target_version):
        if not self.is_schema_migration(target_version):
            return False  # we only track SchemaMigration upgrade steps in SQL

        if self.is_marked_installed_in_sql(target_version):
            return False  # Version is already marked as installed.

        self._setup_db_connection()
        mark_changed(self.session)
        self.session.execute(self._get_tracking_table().insert().values(
            profileid=self.profile,
            upgradeid=target_version))
コード例 #38
0
ファイル: v8.py プロジェクト: EU-OSHA/Euphorie
def add_column_for_custom_risks(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = [
        c['name'] for c in inspector.get_columns(model.Risk.__table__.name)
    ]
    if 'is_custom_risk' not in columns:
        log.info('Adding is_custom_risk column for risks')
        session.execute(
            "ALTER TABLE %s ADD is_custom_risk BOOL NOT NULL DEFAULT FALSE" %
            model.Risk.__table__.name)
        datamanager.mark_changed(session)
コード例 #39
0
ファイル: v8.py プロジェクト: EU-OSHA/Euphorie
def make_risk_id_column_nullable(context):
    """ Make the risk_id column of the Risk table nullable.
        This is so that the user can create custom risks. These risks don't
        have dexterity counterparts in the survey, so we don't have a value for
        risk_id.
    """
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    log.info('Making the risk_id column of Risk table nullable')
    session.execute("ALTER TABLE %s ALTER COLUMN risk_id DROP NOT NULL;" %
                    model.Risk.__table__.name)
    datamanager.mark_changed(session)
コード例 #40
0
def add_columns_to_company_survey(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = [c["name"] for c in inspector.get_columns(model.Company.__table__.name)]
    if "needs_met" not in columns:
        log.info("Adding needs_met column for company")
        session.execute("ALTER TABLE %s ADD needs_met BOOL " % model.Company.__table__.name)
        datamanager.mark_changed(session)
    if "recommend_tool" not in columns:
        log.info("Adding recommend_tool column for company")
        session.execute("ALTER TABLE %s ADD recommend_tool BOOL " % model.Company.__table__.name)
        datamanager.mark_changed(session)
コード例 #41
0
def addTermsAndConditionsColumn(context):
    from z3c.saconfig import Session
    from euphorie.deployment.upgrade.utils import ColumnExists
    from zope.sqlalchemy import datamanager
    import transaction
    session = Session()
    if ColumnExists(session, "user", "tc_approved"):
        return

    log.info("Adding tc_approved column to account table")
    session.execute("ALTER TABLE account ADD COLUMN tc_approved INT")
    datamanager.mark_changed(session)
    transaction.get().commit()
コード例 #42
0
 def __call__(self):
     self._assert_configuration()
     self._setup_db_connection()
     self._insert_initial_version()
     if self._has_upgrades_to_install():
         self._log_do_migration()
         self.migrate()
         self._update_migrated_version()
         # If the transaction contains only DDL statements, the transaction
         # isn't automatically marked as changed, so we do it ourselves
         mark_changed(self.session)
     else:
         self._log_skipping_migration()
コード例 #43
0
 def __call__(self):
     self._assert_configuration()
     self._setup_db_connection()
     self._insert_initial_version()
     if self._has_upgrades_to_install():
         self._log_do_migration()
         self.migrate()
         self._update_migrated_version()
         # If the transaction contains only DDL statements, the transaction
         # isn't automatically marked as changed, so we do it ourselves
         mark_changed(self.session)
     else:
         self._log_skipping_migration()
コード例 #44
0
ファイル: update.py プロジェクト: aytsai/ricebowl
def update():
    """
    Update the Loom database.  By default this method only executes the update.sql script which should update
    the database from a previous version to the current version.  Any database changes that cannot be programmed
    in pure SQL should be added to this method.  As an example, recalculation of compound properties is shown 
    in the body of this method.
    
    After each tagged release, this script should be restored--on the development trunk--to executing only the
    update.sql script and granting select on each table to the backup role.  This script and update.sql should
    then accumulate any database changes until the next tagged release.
    
    The final step in updating the database is to validate the database for consistency by dumping the structure of the
    database and doing a checksum against an expected target.
    """
    executeScript('sql/update.sql')
    executeScript('sql/prepare_update.sql')

    from paste.deploy import loadapp
    app=loadapp('config:%s' % cname, name='main', relative_to='../local/') #@UnusedVariable

    from app.model import DBSession, metadata
    DBSession().execute('grant select on %s to backup ' % ','.join(metadata.tables)) #@UndefinedVariable
    DBSession().execute('grant select on demo.demo_events, demo.demo_user to backup')
    DBSession().execute('grant usage on schema demo to backup')
    seqnames = []
    for r in DBSession().execute("select relname from pg_class c, pg_namespace n where c.relkind = 'S' and c.relnamespace = n.oid and n.nspname = 'app'"):
        seqnames.append(r.relname)
    if seqnames:
        DBSession().execute('grant select on %s to backup ' % ','.join(seqnames))
    mark_changed(DBSession())
    # Example script to update the properties for a reagent mol, if e.g. a new one is added
    '''
    from app.model.compounds import ReagentMol
    
    for m in DBSession().query(ReagentMol):
        m._setProps()
    
    import transaction
    transaction.commit()
    '''
    '''
    BEGIN CUSTOM UPDATE CODE
    AFTER MAKING A TAG DELETE ALL LINES FROM THIS COMMENT UNTIL THE "END CUSTOM UPDATE CODE" COMMENT BELOW
    '''
    '''
    END CUSTOM UPDATE CODE
    '''

    import transaction
    transaction.commit()
    validate()
コード例 #45
0
def make_risk_id_column_nullable(context):
    """ Make the risk_id column of the Risk table nullable.
        This is so that the user can create custom risks. These risks don't
        have dexterity counterparts in the survey, so we don't have a value for
        risk_id.
    """
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    log.info('Making the risk_id column of Risk table nullable')
    session.execute(
        "ALTER TABLE %s ALTER COLUMN risk_id DROP NOT NULL;" %
        model.Risk.__table__.name
    )
    datamanager.mark_changed(session)
コード例 #46
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)

    from gengine.base.settings import set_settings
    set_settings(settings)

    durl = os.environ.get("DATABASE_URL")  # heroku
    if durl:
        settings['sqlalchemy.url'] = durl

    murl = os.environ.get("MEMCACHED_URL")
    if murl:
        settings['urlcache_url'] = murl

    engine = engine_from_config(settings, 'sqlalchemy.')

    config = Configurator(settings=settings)
    pyramid_dogpile_cache.includeme(config)

    from gengine.metadata import (
        init_session,
        init_declarative_base,
        init_db
    )
    init_session()
    init_declarative_base()
    init_db(engine)
    init_caches()

    from gengine.metadata import (
        DBSession
    )
    sess = DBSession()
    init_session(override_session=sess, replace=True)

    import gengine.app.model as m
    with transaction.manager:
        mark_changed(sess, transaction.manager, True)

        messages = sess.execute(m.t_user_messages.select().where(m.t_user_messages.c.has_been_pushed == False))
        for msg in messages:
            m.UserMessage.deliver(msg)
        sess.flush()
        sess.commit()
コード例 #47
0
def updateAbsenteePercentage(context):
    from euphorie.deployment.upgrade.utils import ColumnType
    from z3c.saconfig import Session
    from zope.sqlalchemy import datamanager

    session = Session()
    if ColumnType(session, "dutch_company",
                  "absentee_percentage") == "numeric(5,2)":
        return

    log.info(
        "Changing type for dutch_company.absentee_percentage to NUMERIC(5,2)")
    session.execute(
        "ALTER TABLE dutch_company ALTER COLUMN absentee_percentage TYPE NUMERIC(5,2);"
    )
    datamanager.mark_changed(session)
コード例 #48
0
def add_columns_to_company_survey(context):
    session = Session()
    inspector = Inspector.from_engine(session.bind)
    columns = [
        c['name'] for c in inspector.get_columns(model.Company.__table__.name)
    ]
    if 'needs_met' not in columns:
        log.info('Adding needs_met column for company')
        session.execute("ALTER TABLE %s ADD needs_met BOOL " %
                        model.Company.__table__.name)
        datamanager.mark_changed(session)
    if 'recommend_tool' not in columns:
        log.info('Adding recommend_tool column for company')
        session.execute("ALTER TABLE %s ADD recommend_tool BOOL " %
                        model.Company.__table__.name)
        datamanager.mark_changed(session)
コード例 #49
0
 def _migrate_tracking_table(self, table):
     """Verify tracking table state and apply migrations on the fly when necessary.
     We cannot do that in a schema migration because the migration mechanism relies
     on it - thus we might need to run other schema migrations before running the
     migration updating the table to the state the code expects.
     """
     if not table.columns.get('upgradeid').primary_key:
         # We need a primarykey over both columns (profileid and upgradeid) in order
         # to track / record each upgrade step for a profile; we used to only store
         # the newest version.
         mark_changed(self.session)
         for constraint in table.constraints:
             self.operations.drop_constraint(constraint.name,
                                             TRACKING_TABLE_NAME)
         self.operations.create_primary_key(
             'opengever_upgrade_version_pkey', TRACKING_TABLE_NAME,
             ['profileid', 'upgradeid'])
コード例 #50
0
ファイル: upgrade.py プロジェクト: lukasgraf/opengever.core
 def _migrate_tracking_table(self, table):
     """Verify tracking table state and apply migrations on the fly when necessary.
     We cannot do that in a schema migration because the migration mechanism relies
     on it - thus we might need to run other schema migrations before running the
     migration updating the table to the state the code expects.
     """
     if not table.columns.get('upgradeid').primary_key:
         # We need a primarykey over both columns (profileid and upgradeid) in order
         # to track / record each upgrade step for a profile; we used to only store
         # the newest version.
         mark_changed(self.session)
         for constraint in table.constraints:
             self.operations.drop_constraint(constraint.name,
                                             TRACKING_TABLE_NAME)
         self.operations.create_primary_key('opengever_upgrade_version_pkey',
                                            TRACKING_TABLE_NAME,
                                            ['profileid', 'upgradeid'])
コード例 #51
0
def update_favorites_title(context, event):
    """Event handler which updates the titles of all existing favorites for the
    current context, unless the title is personalized.
    """
    if IContainerModifiedEvent.providedBy(event):
        return

    if ILocalrolesModifiedEvent.providedBy(event):
        return

    if is_title_changed(event.descriptions):
        query = Favorite.query.filter(
            and_(Favorite.oguid == Oguid.for_object(context),
                 Favorite.is_title_personalized == False))  # noqa
        query.update_title(context.title)

        mark_changed(create_session())
コード例 #52
0
ファイル: handlers.py プロジェクト: 4teamwork/opengever.core
def update_favorites_title(context, event):
    """Event handler which updates the titles of all existing favorites for the
    current context, unless the title is personalized.
    """
    if IContainerModifiedEvent.providedBy(event):
        return

    if ILocalrolesModifiedEvent.providedBy(event):
        return

    if is_title_changed(event.descriptions):
        query = Favorite.query.filter(
            and_(Favorite.oguid == Oguid.for_object(context),
                 Favorite.is_title_personalized == False))  # noqa
        query.update_title(context.title)

        mark_changed(create_session())
コード例 #53
0
    def __call__(self):
        logger.info('Start syncing {}'.format(self.model.__tablename__))

        existing_id_lookup = self.get_existing_id_lookup()
        result = self.source_session.execute(self.query)
        to_insert, to_update = self.prepare_values(result, existing_id_lookup)

        self.db_session.bulk_insert_mappings(
            self.model, to_insert, return_defaults=True)
        logger.info('{} new {} added'.format(
            len(to_insert), self.model.__tablename__))

        self.db_session.bulk_update_mappings(self.model, to_update)
        logger.info('{} {} updated'.format(
            len(to_update), self.model.__tablename__))

        if to_insert or to_update:
            mark_changed(self.db_session)
コード例 #54
0
    def __call__(self):
        logger.info('Start syncing {}'.format(self.model.__tablename__))

        existing_id_lookup = self.get_existing_id_lookup()
        result = self.source_session.execute(self.query)
        to_insert, to_update = self.prepare_values(result, existing_id_lookup)

        self.db_session.bulk_insert_mappings(self.model,
                                             to_insert,
                                             return_defaults=True)
        logger.info('{} new {} added'.format(len(to_insert),
                                             self.model.__tablename__))

        self.db_session.bulk_update_mappings(self.model, to_update)
        logger.info('{} {} updated'.format(len(to_update),
                                           self.model.__tablename__))

        if to_insert or to_update:
            mark_changed(self.db_session)
コード例 #55
0
 def removeChildren(self, excluded=[]):
     if self.id not in excluded:
         excluded.append(self.id)
     session = Session()
     if self.path:
         filter = sql.and_(
                     SurveyTreeItem.session_id == self.session_id,
                     SurveyTreeItem.path.like(self.path + "%"),
                     sql.not_(SurveyTreeItem.id.in_(excluded))
                 )
     else:
         filter = sql.and_(
                     SurveyTreeItem.session_id == self.session_id,
                     sql.not_(SurveyTreeItem.id.in_(excluded))
                 )
     removed = session.query(SurveyTreeItem).filter(filter).all()
     session.execute(SurveyTreeItem.__table__.delete().where(filter))
     self.session.touch()
     datamanager.mark_changed(session)
     return removed
コード例 #56
0
ファイル: audit.py プロジェクト: aytsai/ricebowl
 def clearHistory(cls):
     DBSession().execute('delete from aud_login')
     mark_changed(DBSession())