Search in sources :

Example 6 with Table

use of org.alfresco.util.schemacomp.model.Table in project alfresco-repository by Alfresco.

the class SchemaComparatorTest method columnOrderingIgnoredWhenDisabled.

@Test
public void columnOrderingIgnoredWhenDisabled() {
    reference = new Schema("schema", "alf_", 590, false);
    target = new Schema("schema", "alf_", 590, false);
    // Reference schema's database objects.
    reference.add(new Table(reference, "table_name", columns(false, "id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"), new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)), fkeys(), indexes()));
    // Target schema's database objects - note different column order
    target.add(new Table(target, "table_name", columns(false, "id NUMBER(10)", "name VARCHAR2(150)", "nodeRef VARCHAR2(200)"), new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)), fkeys(), indexes()));
    comparator = new SchemaComparator(reference, target, dialect);
    comparator.validateAndCompare();
    // See stdout for diagnostics dump...
    dumpDiffs(comparator.getComparisonResults(), false);
    dumpValidation(comparator.getComparisonResults());
    Results results = comparator.getComparisonResults();
    // There are no logical differences
    assertEquals(0, results.size());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Test(org.junit.Test)

Example 7 with Table

use of org.alfresco.util.schemacomp.model.Table in project alfresco-repository by Alfresco.

the class DbObjectXMLTransformerTest method transformSchemaWithColumnOrderCheck.

@Test
public void transformSchemaWithColumnOrderCheck() throws IOException {
    Collection<Column> columns = columns("one VARCHAR2(100)", "two NUMBER(10)");
    PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1));
    Collection<ForeignKey> fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc"));
    Collection<Index> indexes = indexes("index_one col1 col2", "index_two col3 col4");
    Table tableOne = new Table(null, "table_one", columns, pk, fks, indexes);
    Table tableTwo = new Table(null, "table_two", columns, pk, fks, indexes);
    Schema schema = new Schema("my_schema", "alf_", 132, true);
    schema.add(tableOne);
    schema.add(tableTwo);
    schema.add(new Sequence(null, "sequence_one"));
    schema.add(new Sequence(null, "sequence_two"));
    schema.add(new Sequence(null, "sequence_three"));
    schema.setValidators(new ArrayList<DbValidator>());
    transformer.output(schema);
    BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
    dumpOutput();
    assertHasPreamble(reader);
    assertEquals("<schema " + "xmlns=\"http://www.alfresco.org/repo/db-schema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://www.alfresco.org/repo/db-schema db-schema.xsd\" " + "name=\"my_schema\" dbprefix=\"alf_\" version=\"132\" tablecolumnorder=\"true\">", reader.readLine());
    assertEquals("  <objects>", reader.readLine());
    skipUntilEnd("       {table}", reader);
    skipUntilEnd("       {table}", reader);
    skipUntilEnd("       {sequence}", reader, true);
    skipUntilEnd("       {sequence}", reader, true);
    skipUntilEnd("       {sequence}", reader, true);
    assertEquals("  </objects>", reader.readLine());
    assertEquals("</schema>", reader.readLine());
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) Sequence(org.alfresco.util.schemacomp.model.Sequence) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Column(org.alfresco.util.schemacomp.model.Column) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator) Test(org.junit.Test)

Example 8 with Table

use of org.alfresco.util.schemacomp.model.Table in project alfresco-repository by Alfresco.

the class MySQLDialectExportTester method doExportTest.

@Override
protected void doExportTest() throws Exception {
    Schema schema = getSchema();
    Table exampleTable = null;
    Table otherTable = null;
    for (DbObject dbo : schema) {
        if (dbo.getName().equals("export_test_example")) {
            exampleTable = (Table) dbo;
        }
        if (dbo.getName().equals("export_test_other")) {
            otherTable = (Table) dbo;
        }
    }
    checkExampleTable(schema, exampleTable);
    checkOtherTable(schema, otherTable);
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) DbObject(org.alfresco.util.schemacomp.model.DbObject) Schema(org.alfresco.util.schemacomp.model.Schema)

Example 9 with Table

use of org.alfresco.util.schemacomp.model.Table in project alfresco-repository by Alfresco.

the class XMLToSchema method endElement.

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.equals(XML.EL_TABLE)) {
        Table table = (Table) stack.pop();
        schema.add(table);
    } else if (qName.equals(XML.EL_COLUMN)) {
        Column column = (Column) stack.pop();
        Table table = (Table) stack.peek();
        column.setParent(table);
        table.getColumns().add(column);
    } else if (qName.equals(XML.EL_PRIMARY_KEY)) {
        PrimaryKey pk = (PrimaryKey) stack.pop();
        Table table = (Table) stack.peek();
        table.setPrimaryKey(pk);
    } else if (qName.equals(XML.EL_FOREIGN_KEY)) {
        ForeignKey fk = (ForeignKey) stack.pop();
        Table table = (Table) stack.peek();
        fk.setParent(table);
        table.getForeignKeys().add(fk);
    } else if (qName.equals(XML.EL_INDEX)) {
        Index index = (Index) stack.pop();
        Table table = (Table) stack.peek();
        index.setParent(table);
        table.getIndexes().add(index);
    } else if (qName.equals(XML.EL_SEQUENCE)) {
        Sequence seq = (Sequence) stack.pop();
        seq.setParent(schema);
        schema.add(seq);
    } else if (qName.equals(XML.EL_VALIDATOR)) {
        DbValidator validator = (DbValidator) stack.pop();
        DbObject dbo = (DbObject) stack.peek();
        dbo.getValidators().add(validator);
    }
    // Deal with elements that contain text.
    if (lastText.length() != 0) {
        if (qName.equals(XML.EL_TYPE)) {
            Column column = (Column) stack.peek();
            column.setType(lastText.toString());
        } else if (qName.equals(XML.EL_NULLABLE)) {
            Column column = (Column) stack.peek();
            column.setNullable(Boolean.parseBoolean(lastText.toString()));
        } else if (qName.equals(XML.EL_AUTOINCREMENT)) {
            Column column = (Column) stack.peek();
            column.setAutoIncrement(Boolean.parseBoolean(lastText.toString()));
        } else if (qName.equals(XML.EL_COLUMN_NAME)) {
            if (stack.peek() instanceof PrimaryKey) {
                PrimaryKey pk = (PrimaryKey) stack.peek();
                pk.getColumnNames().add(lastText.toString());
            } else if (stack.peek() instanceof Index) {
                Index index = (Index) stack.peek();
                index.getColumnNames().add(lastText.toString());
            }
        } else if (qName.equals(XML.EL_LOCAL_COLUMN)) {
            ForeignKey fk = (ForeignKey) stack.peek();
            fk.setLocalColumn(lastText.toString());
        } else if (qName.equals(XML.EL_TARGET_TABLE)) {
            ForeignKey fk = (ForeignKey) stack.peek();
            fk.setTargetTable(lastText.toString());
        } else if (qName.equals(XML.EL_TARGET_COLUMN)) {
            ForeignKey fk = (ForeignKey) stack.peek();
            fk.setTargetColumn(lastText.toString());
        } else if (qName.equals(XML.EL_PROPERTY)) {
            String propValue = lastText.toString();
            String propName = (String) stack.pop();
            if (stack.peek() instanceof DbValidator) {
                @SuppressWarnings("unchecked") DbValidator validator = (DbValidator) stack.peek();
                validator.setProperty(propName, propValue);
            }
        }
    }
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Column(org.alfresco.util.schemacomp.model.Column) DbObject(org.alfresco.util.schemacomp.model.DbObject) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) Sequence(org.alfresco.util.schemacomp.model.Sequence) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator)

Example 10 with Table

use of org.alfresco.util.schemacomp.model.Table in project alfresco-repository by Alfresco.

the class ExportDb method processTables.

private void processTables(DatabaseMetaData dbmd, ResultSet tables) throws SQLException, IllegalArgumentException, IllegalAccessException {
    if (tables == null) {
        return;
    }
    while (tables.next()) {
        final String tableName = tables.getString("TABLE_NAME");
        if (log.isDebugEnabled()) {
            log.debug("Examining table tableName=[" + tableName + "]");
        }
        // ALF-14129 fix, check whether schema already contains object with provided name
        if (tableName.startsWith("BIN$") || schema.containsByName(tableName)) {
            continue;
        }
        if (tables.getString("TABLE_TYPE").equals("SEQUENCE")) {
            Sequence sequence = new Sequence(tableName);
            schema.add(sequence);
            continue;
        }
        Table table = new Table(tableName);
        schema.add(table);
        // Table columns
        final ResultSet columns = dbmd.getColumns(null, tables.getString("TABLE_SCHEM"), tableName, "%");
        while (columns.next()) {
            String columnName = columns.getString("COLUMN_NAME");
            Column column = new Column(columnName);
            String dbType = columns.getString("TYPE_NAME");
            int colSize = columns.getInt("COLUMN_SIZE");
            int scale = columns.getInt("DECIMAL_DIGITS");
            int jdbcType = columns.getInt("DATA_TYPE");
            String type = generateType(dbType, colSize, scale, jdbcType);
            column.setType(type);
            String nullableString = columns.getString("IS_NULLABLE");
            column.setNullable(parseBoolean(nullableString));
            column.setOrder(columns.getInt("ORDINAL_POSITION"));
            try {
                String autoIncString = columns.getString("IS_AUTOINCREMENT");
                column.setAutoIncrement(parseBoolean(autoIncString));
            } catch (SQLException jtdsDoesNOtHAveIsUatoincrement) {
                column.setAutoIncrement((dbType.endsWith("identity")));
            }
            column.setParent(table);
            table.getColumns().add(column);
        }
        columns.close();
        // Primary key
        final ResultSet primarykeycols = dbmd.getPrimaryKeys(null, tables.getString("TABLE_SCHEM"), tableName);
        PrimaryKey pk = null;
        while (primarykeycols.next()) {
            if (pk == null) {
                String pkName = primarykeycols.getString("PK_NAME");
                pk = new PrimaryKey(pkName);
            }
            String columnName = primarykeycols.getString("COLUMN_NAME");
            pk.getColumnNames().add(columnName);
            int columnOrder = primarykeycols.getInt("KEY_SEQ");
            pk.getColumnOrders().add(columnOrder);
        }
        primarykeycols.close();
        // If this table has a primary key, add it.
        if (pk != null) {
            pk.setParent(table);
            table.setPrimaryKey(pk);
        }
        // Indexes
        final ResultSet indexes = dbmd.getIndexInfo(null, tables.getString("TABLE_SCHEM"), tableName, false, true);
        String lastIndexName = "";
        Index index = null;
        while (indexes.next()) {
            final String indexName = indexes.getString("INDEX_NAME");
            if (indexName == null) {
                // Oracle seems to have some dummy index entries
                continue;
            } else // Skip the index corresponding to the PK if it is mentioned
            if (indexName.equals(table.getPrimaryKey().getName())) {
                continue;
            }
            if (!indexName.equals(lastIndexName)) {
                index = new Index(indexName);
                index.setUnique(!indexes.getBoolean("NON_UNIQUE"));
                index.setParent(table);
                table.getIndexes().add(index);
                lastIndexName = indexName;
            }
            if (index != null) {
                String columnName = indexes.getString("COLUMN_NAME");
                index.getColumnNames().add(columnName);
            }
        }
        indexes.close();
        final ResultSet foreignkeys = dbmd.getImportedKeys(null, tables.getString("TABLE_SCHEM"), tableName);
        String lastKeyName = "";
        ForeignKey fk = null;
        while (foreignkeys.next()) {
            final String keyName = foreignkeys.getString("FK_NAME");
            if (!keyName.equals(lastKeyName)) {
                fk = new ForeignKey(keyName);
                fk.setParent(table);
                table.getForeignKeys().add(fk);
                lastKeyName = keyName;
            }
            if (fk != null) {
                fk.setLocalColumn(foreignkeys.getString("FKCOLUMN_NAME"));
                fk.setTargetTable(foreignkeys.getString("PKTABLE_NAME"));
                fk.setTargetColumn(foreignkeys.getString("PKCOLUMN_NAME"));
            }
        }
        foreignkeys.close();
    }
    tables.close();
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Column(org.alfresco.util.schemacomp.model.Column) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) Sequence(org.alfresco.util.schemacomp.model.Sequence) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey)

Aggregations

Table (org.alfresco.util.schemacomp.model.Table)19 Test (org.junit.Test)13 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)11 Schema (org.alfresco.util.schemacomp.model.Schema)11 Index (org.alfresco.util.schemacomp.model.Index)10 DbValidator (org.alfresco.util.schemacomp.validator.DbValidator)8 Column (org.alfresco.util.schemacomp.model.Column)7 ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)7 Sequence (org.alfresco.util.schemacomp.model.Sequence)7 BufferedReader (java.io.BufferedReader)5 StringReader (java.io.StringReader)5 DbObject (org.alfresco.util.schemacomp.model.DbObject)5 ArrayList (java.util.ArrayList)2 NameValidator (org.alfresco.util.schemacomp.validator.NameValidator)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 MySQLInnoDBDialect (org.alfresco.repo.domain.dialect.MySQLInnoDBDialect)1 Before (org.junit.Before)1