Search in sources :

Example 21 with Database

use of org.molgenis.emx2.Database in project molgenis-emx2 by molgenis.

the class MolgenisSessionManager method createSessionListener.

/**
 * takes care of creating and destroying session attributes when Jetty creates/destroys sessions
 */
private EventListener createSessionListener() {
    MolgenisSessionManager _this = this;
    return new HttpSessionListener() {

        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
            logger.info("Initializing session");
            // create private database wrapper to session
            Database database = new SqlDatabase(false);
            // set default use to "anonymous"
            database.setActiveUser("anonymous");
            // create session and add to sessions lists so we can also access all active
            // sessions
            MolgenisSession molgenisSession = new MolgenisSession(database);
            sessions.put(httpSessionEvent.getSession().getId(), molgenisSession);
            logger.info("session created: " + httpSessionEvent.getSession().getId());
            // create listener
            database.setListener(new MolgenisSessionManagerDatabaseListener(_this, molgenisSession));
        }

        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
            // remove from session pool
            sessions.remove(httpSessionEvent.getSession().getId());
            logger.info("session destroyed: " + httpSessionEvent.getSession().getId());
        }
    };
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) Database(org.molgenis.emx2.Database) SqlDatabase(org.molgenis.emx2.sql.SqlDatabase) SqlDatabase(org.molgenis.emx2.sql.SqlDatabase)

Example 22 with Database

use of org.molgenis.emx2.Database in project molgenis-emx2 by molgenis.

the class SqlTableMetadata method alterColumnTransaction.

// static to ensure we don't touch 'this' until transaction succesfull
private static SqlTableMetadata alterColumnTransaction(String schemaName, String tableName, String columnName, Column column, Database db) {
    SqlTableMetadata tm = (SqlTableMetadata) db.getSchema(schemaName).getMetadata().getTableMetadata(tableName);
    Column newColumn = new Column(tm, column);
    Column oldColumn = tm.getColumn(columnName);
    // primary keys by definition are required
    if (newColumn.getKey() == 1) {
        newColumn.setRequired(true);
    }
    validateColumn(newColumn);
    // check if reference and of different size
    if (newColumn.isRefArray() && !newColumn.getName().equals(oldColumn.getName()) && !newColumn.getColumnType().equals(oldColumn.getColumnType()) && newColumn.getRefTable().getPrimaryKeyFields().size() > 1) {
        throw new MolgenisException("Alter column of '" + oldColumn.getName() + " failed: REF_ARRAY is not supported for composite keys of table " + newColumn.getRefTableName());
    }
    // if changing 'ref' then check if not refBack exists
    if (!oldColumn.getColumnType().equals(newColumn.getColumnType())) {
        tm.checkNotRefback(columnName, oldColumn);
    }
    // drop old key, if touched
    if (oldColumn.getKey() > 0 && newColumn.getKey() != oldColumn.getKey()) {
        executeDropKey(tm.getJooq(), oldColumn.getTable(), oldColumn.getKey());
    }
    // drop referential constraints around this column
    executeRemoveRefConstraints(tm.getJooq(), oldColumn);
    // remove refBacks if exist
    executeRemoveRefback(oldColumn, newColumn);
    // add ontology table if needed
    if (newColumn.isOntology()) {
        createOntologyTable(newColumn);
    }
    // rename and retype if needed
    executeAlterType(tm.getJooq(), oldColumn, newColumn);
    executeAlterName(tm.getJooq(), oldColumn, newColumn);
    // only applies to key=1
    if ((oldColumn.isPrimaryKey() || newColumn.isPrimaryKey()) && oldColumn.isRequired() && !oldColumn.isRequired() == newColumn.isRequired()) {
        executeSetRequired(tm.getJooq(), newColumn);
    }
    // update the metadata so we can use it for new keys and references
    if (column.getPosition() == null) {
        column.setPosition(tm.columns.get(columnName).getPosition());
    }
    // remove the old
    tm.columns.remove(columnName);
    // add the new
    tm.columns.put(column.getName(), column);
    // reapply ref constrainst
    executeCreateRefConstraints(tm.getJooq(), newColumn);
    // check if refBack constraints need updating
    reapplyRefbackContraints(oldColumn, newColumn);
    // create/update key, if touched
    if (newColumn.getKey() != oldColumn.getKey()) {
        createOrReplaceKey(tm.getJooq(), tm, newColumn.getKey(), tm.getKeyFields(newColumn.getKey()));
    }
    // delete old column if name changed, then save any other metadata changes
    if (!oldColumn.getName().equals(newColumn.getName()))
        deleteColumn(tm.getJooq(), oldColumn);
    saveColumnMetadata(tm.getJooq(), newColumn);
    return tm;
}
Also used : MetadataUtils.deleteColumn(org.molgenis.emx2.sql.MetadataUtils.deleteColumn)

Example 23 with Database

use of org.molgenis.emx2.Database in project molgenis-emx2 by molgenis.

the class SqlTableMetadata method addTransaction.

// static to ensure we don't touch 'this' until complete
private static SqlTableMetadata addTransaction(Database db, String schemaName, String tableName, Column[] column) {
    SqlTableMetadata tm = (SqlTableMetadata) db.getSchema(schemaName).getMetadata().getTableMetadata(tableName);
    // first per-column actions, then multi-column action such as composite keys/refs
    int position = MetadataUtils.getMaxPosition(tm.getJooq(), schemaName) + 1;
    for (Column c : column) {
        long start = System.currentTimeMillis();
        if (tm.getLocalColumn(c.getName()) != null) {
            tm.alterColumn(c);
        } else {
            Column newColumn = new Column(tm, c);
            if (tm.getInherit() != null && tm.getInheritedTable().getColumn(c.getName()) != null && // this column is replicated in all subclass tables
            !c.getName().equals(MG_TABLECLASS)) {
                throw new MolgenisException("Cannot add column " + tm.getTableName() + "." + c.getName() + ": column exists in inherited class " + tm.getInherit());
            }
            if (!newColumn.isHeading()) {
                validateColumn(newColumn);
                if (newColumn.getPosition() == null) {
                    // positions are asumed to number up in a schema
                    newColumn.setPosition(position++);
                }
                executeCreateColumn(tm.getJooq(), newColumn);
                tm.columns.put(c.getName(), newColumn);
                if (newColumn.getKey() > 0) {
                    createOrReplaceKey(tm.getJooq(), newColumn.getTable(), newColumn.getKey(), newColumn.getTable().getKeyFields(newColumn.getKey()));
                }
                executeCreateRefConstraints(tm.getJooq(), newColumn);
            } else {
                saveColumnMetadata(tm.getJooq(), newColumn);
                tm.columns.put(c.getName(), newColumn);
            }
            log(tm, start, "added column '" + newColumn.getName() + "' to table " + tm.getTableName());
        }
    }
    return tm;
}
Also used : MetadataUtils.deleteColumn(org.molgenis.emx2.sql.MetadataUtils.deleteColumn)

Example 24 with Database

use of org.molgenis.emx2.Database in project molgenis-emx2 by molgenis.

the class SqlTableMetadata method alterNameTransaction.

// ensure the transaction has no side effects on 'this' until completed
private static SqlTableMetadata alterNameTransaction(Database db, String schemaName, String tableName, String newName) {
    SqlTableMetadata tm = (SqlTableMetadata) db.getSchema(schemaName).getMetadata().getTableMetadata(tableName);
    // drop triggers for this table
    for (Column column : tm.getStoredColumns()) {
        SqlColumnExecutor.executeRemoveRefConstraints(tm.getJooq(), column);
    }
    // rename table and triggers
    SqlTableMetadataExecutor.executeAlterName(tm.getJooq(), tm, newName);
    // update metadata
    MetadataUtils.alterTableName(tm.getJooq(), tm, newName);
    tm.tableName = newName;
    // recreate triggers for this table
    for (Column column : tm.getStoredColumns()) {
        SqlColumnExecutor.executeCreateRefConstraints(tm.getJooq(), column);
    }
    return tm;
}
Also used : MetadataUtils.deleteColumn(org.molgenis.emx2.sql.MetadataUtils.deleteColumn)

Example 25 with Database

use of org.molgenis.emx2.Database in project molgenis-emx2 by molgenis.

the class InitTestDatabaseAndRunNonParallelTests method createDatabase.

@BeforeClass
public static void createDatabase() {
    // we want this run only once and NOT parallel for total test suite
    // AND we want run all other tests in parallel
    // so tests are in molgenis-emx2-sql-it ('integration test')
    // and 'init' only happence once, here
    Database db = new SqlDatabase(true);
    assertTrue(db.getDatabaseVersion() > 0);
}
Also used : Database(org.molgenis.emx2.Database) BeforeClass(org.junit.BeforeClass)

Aggregations

Test (org.junit.Test)24 Database (com.google.spanner.admin.database.v1.Database)21 Database (org.molgenis.emx2.Database)12 AbstractMessage (com.google.protobuf.AbstractMessage)10 ArrayList (java.util.ArrayList)10 Schema (org.molgenis.emx2.Schema)9 ByteString (com.google.protobuf.ByteString)8 Operation (com.google.longrunning.Operation)7 RestoreDatabaseRequest (com.google.spanner.admin.database.v1.RestoreDatabaseRequest)7 BeforeClass (org.junit.BeforeClass)7 CreateDatabaseRequest (com.google.spanner.admin.database.v1.CreateDatabaseRequest)4 InstanceName (com.google.spanner.admin.database.v1.InstanceName)4 ListDatabasesPagedResponse (com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListDatabasesPagedResponse)3 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)3 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)3 MetadataUtils.deleteColumn (org.molgenis.emx2.sql.MetadataUtils.deleteColumn)3 BackupName (com.google.spanner.admin.database.v1.BackupName)2 DatabaseName (com.google.spanner.admin.database.v1.DatabaseName)2 GetDatabaseRequest (com.google.spanner.admin.database.v1.GetDatabaseRequest)2 ListDatabasesRequest (com.google.spanner.admin.database.v1.ListDatabasesRequest)2