Search in sources :

Example 6 with Schema

use of org.alfresco.util.schemacomp.model.Schema 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 7 with Schema

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

the class AbstractExportTester method commonPostExportChecks.

/**
 * Common checks that do not need to be coded into every test implementation.
 * May be overridden if required.
 */
protected void commonPostExportChecks() {
    Schema schema = getSchema();
    assertNull("Schema shouldn't have a parent", getSchema().getParent());
    checkResultsFiltered(schema, "export_test_");
}
Also used : Schema(org.alfresco.util.schemacomp.model.Schema)

Example 8 with Schema

use of org.alfresco.util.schemacomp.model.Schema 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 Schema

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

the class DbObjectXMLTransformer method attemptOutput.

private void attemptOutput(DbObject dbObject) throws SAXException {
    // All DbObjects result in an XML element with the DbObject's class name as the tag
    // and a name attribute corresponding to the value of getName(),
    // e.g. For an instance of a Column: <column name="the_column_name"/>
    final AttributesImpl attribs = new AttributesImpl();
    if (dbObject instanceof Schema) {
        // XML Schema (XSD) declarations.
        attribs.addAttribute("", "", "xmlns", "CDATA", "http://www.alfresco.org/repo/db-schema");
        attribs.addAttribute("", "", "xmlns:xsi", "CDATA", "http://www.w3.org/2001/XMLSchema-instance");
        attribs.addAttribute("", "", "xsi:schemaLocation", "CDATA", "http://www.alfresco.org/repo/db-schema db-schema.xsd");
    }
    attribs.addAttribute("", "", XML.ATTR_NAME, "CDATA", dbObject.getName());
    // Add class-specific attributes (after common DbObject attributes).
    addAttributes(dbObject, attribs);
    String tagName = dbObject.getClass().getSimpleName().toLowerCase();
    xmlOut.startElement("", "", tagName, attribs);
    // All DbObjects potentially have validator configuration present in the XML.
    transformValidators(dbObject.getValidators());
    // The element's contents can optionally be populated with class-specific content.
    transformDbObject(dbObject);
    // Provide the end tag, or close an empty element.
    xmlOut.endElement("", "", tagName);
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Schema(org.alfresco.util.schemacomp.model.Schema)

Example 10 with Schema

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

the class SchemaBootstrap method attemptValidateSchema.

/**
 * Validates and compares current DB schema with schema reference definition, specified in <code>referenceResource</code> parameter.<br />
 * <br />
 * The method supports two mechanisms to report validation results:
 * <ol>
 * <li>using an external output stream, specified as <code>out</code>;</li>
 * <li>using specially created {@link FileOutputStream}, which represents temporary file with name, formatted in accordance with <code>outputFileNameTemplate</code> template.</li>
 * </ol>
 * It is necessary to take care about freeing resources of output stream in case of the 1st approach.<br />
 * <b>N.B.:</b> The method only writes messages of the report. And it <b>doesn't flush and doesn't close</b> the specified output stream!<br />
 * <br />
 *
 * @param referenceResource - {@link Resource} instance, which determines file of reference schema
 * @param outputFileNameTemplate - {@link String} value, which determines template of temporary filename for validation report. <b>It can't be <code>null</code> if
 *        <code>out</code> is <code>null</code></b>!
 * @param out - {@link PrintWriter} instance, which represents an external output stream for writing a validation report. This stream is never closed or flushed. <b>It can't be
 *        <code>null</code> if <code>outputFileNameTemplate</code> is <code>null</code></b>!
 * @return {@link Integer} value, which determines amount of errors or warnings that were detected during validation
 */
private int attemptValidateSchema(Resource referenceResource, String outputFileNameTemplate, PrintWriter out) {
    Date startTime = new Date();
    InputStream is = null;
    try {
        is = new BufferedInputStream(referenceResource.getInputStream());
    } catch (IOException e) {
        throw new RuntimeException("Unable to open schema reference file: " + referenceResource);
    }
    XMLToSchema xmlToSchema = new XMLToSchema(is);
    xmlToSchema.parse();
    Schema reference = xmlToSchema.getSchema();
    ExportDb exporter = new ExportDb(dataSource, dialect, descriptorService, databaseMetaDataHelper);
    exporter.setDbSchemaName(dbSchemaName);
    // Ensure that the database objects we're validating are filtered
    // by the same prefix as the reference file.
    exporter.setNamePrefix(reference.getDbPrefix());
    exporter.execute();
    Schema target = exporter.getSchema();
    SchemaComparator schemaComparator = new SchemaComparator(reference, target, dialect);
    schemaComparator.validateAndCompare();
    Results results = schemaComparator.getComparisonResults();
    Object[] outputFileNameParams = new Object[] { dialect.getClass().getSimpleName(), reference.getDbPrefix() };
    PrintWriter pw = null;
    File outputFile = null;
    try {
        if (out == null) {
            String outputFileName = MessageFormat.format(outputFileNameTemplate, outputFileNameParams);
            outputFile = TempFileProvider.createTempFile(outputFileName, ".txt");
            try {
                pw = new PrintWriter(outputFile, SchemaComparator.CHAR_SET);
            } catch (FileNotFoundException error) {
                throw new RuntimeException("Unable to open file for writing: " + outputFile);
            } catch (UnsupportedEncodingException error) {
                throw new RuntimeException("Unsupported char set: " + SchemaComparator.CHAR_SET, error);
            }
        } else {
            pw = out;
        }
        // Populate the file with details of the comparison's results.
        for (Result result : results) {
            pw.print(result.describe());
            pw.print(SchemaComparator.LINE_SEPARATOR);
        }
    } finally {
        // We care only about output streams for reporting, which are created specially for current reference resource...
        if (null == out) {
            pw.close();
        }
    }
    if (results.size() == 0) {
        LogUtil.info(logger, INFO_SCHEMA_COMP_ALL_OK, referenceResource);
    } else {
        int numProblems = results.size();
        if (outputFile == null) {
            LogUtil.warn(logger, WARN_SCHEMA_COMP_PROBLEMS_FOUND_NO_FILE, numProblems);
        } else {
            LogUtil.warn(logger, WARN_SCHEMA_COMP_PROBLEMS_FOUND, numProblems, outputFile);
        }
    }
    Date endTime = new Date();
    long durationMillis = endTime.getTime() - startTime.getTime();
    LogUtil.debug(logger, DEBUG_SCHEMA_COMP_TIME_TAKEN, durationMillis);
    return results.size();
}
Also used : SchemaComparator(org.alfresco.util.schemacomp.SchemaComparator) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Schema(org.alfresco.util.schemacomp.model.Schema) XMLToSchema(org.alfresco.util.schemacomp.XMLToSchema) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Date(java.util.Date) Savepoint(java.sql.Savepoint) Result(org.alfresco.util.schemacomp.Result) XMLToSchema(org.alfresco.util.schemacomp.XMLToSchema) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) BufferedInputStream(java.io.BufferedInputStream) Results(org.alfresco.util.schemacomp.Results) File(java.io.File) ExportDb(org.alfresco.util.schemacomp.ExportDb) PrintWriter(java.io.PrintWriter)

Aggregations

Schema (org.alfresco.util.schemacomp.model.Schema)20 Table (org.alfresco.util.schemacomp.model.Table)11 Test (org.junit.Test)9 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)7 Index (org.alfresco.util.schemacomp.model.Index)6 Sequence (org.alfresco.util.schemacomp.model.Sequence)5 DbValidator (org.alfresco.util.schemacomp.validator.DbValidator)5 Column (org.alfresco.util.schemacomp.model.Column)4 DbObject (org.alfresco.util.schemacomp.model.DbObject)4 BufferedReader (java.io.BufferedReader)3 StringReader (java.io.StringReader)3 ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)3 StringWriter (java.io.StringWriter)2 StreamResult (javax.xml.transform.stream.StreamResult)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1