Package nexj.core.meta.upgrade

Examples of nexj.core.meta.upgrade.Upgrade


      Metadata metadata = m_context.getMetadata();
      boolean bCommit = false;

      try
      {
         Upgrade upgrade = metadata.getUpgrade("Main");

         upgrade.validate(metadata, null);
         new DataLoader(m_context).upgrade(getDataSourceSet(), upgrade, bForce);
         metadata.getMetaclass("SysUpgrade").invoke("upgrade");
         bCommit = true;
      }
      finally
View Full Code Here


      Lookup2D/*<String, Integer, List<Table>>*/ tableMap =
         new HashTab2D/*<String, Integer, List<Table>>*/(schema.getTableCount());

      if (metadata.isTestEnvironment())
      {
         Upgrade upgrade = metadata.getUpgrade("Main");
         VersionUpgrade version = upgrade.getFirstVersion();
         Lookup/*<Object, UpgradeState>*/ stateMap = Upgrade.getInitialState(version);

         version = findFirstVersion(version, stateMap, (RelationalDatabase)schema.getDataSource());

         for (VersionUpgrade u = version; u != null; u = u.getNext())
View Full Code Here

      SchemaVersion version = new SchemaVersion();
      RelationalSchemaUpgrade upgrade = new RelationalSchemaUpgrade();
      SQLAppender origAppender = m_manager.getSQLAppender();

      upgrade.addStep(step);
      upgrade.setUpgrade(new Upgrade(null));
      m_manager.setSQLAppender(
         (writer == null) ? new SQLSchemaManager.SQLConnectionAppender(m_connection.getConnection())
                          : (SQLAppender)m_manager.new SQLWriterAppender(writer));

      try
View Full Code Here

    * @param schema The modifiable current schema state (not null and getVersionTable() not null).
    * @return The SQL generated during the execution of the upgrade.
    */
   protected String upgrade(VersionUpgrade version, RelationalSchema schema)
   {
      Upgrade upgrade = new Upgrade(null);
      SQLAppender origAppender = m_manager.getSQLAppender();
      StringWriter sql = new StringWriter();

      upgrade.setMetadata(getMetadata());
      upgrade.addVersion(version);
      m_manager.setSQLAppender(m_manager.new SQLWriterAppender(sql));

      try
      {
         m_manager.upgrade(schema, upgrade, upgrade.getFirstVersion().getName());
      }
      finally
      {
         m_manager.setSQLAppender(origAppender);
      }
View Full Code Here

      assertEquals("[/test/path d:/ghi/jkl ab:/xy]", output.toString());
   }

   public void testViewScriptUpgradeMismtch()
   {
      Upgrade upgrade = new Upgrade(null);
      RelationalSchemaUpgrade version = new RelationalSchemaUpgrade("test");
      XMLMetadata metadata = new XMLMetadata(null, null, null, null, null);
      RelationalDatabase ds = new RelationalDatabase(null);
      RelationalSchema schema = new RelationalSchema();
      CreateTableStep step = new CreateTableStep(); // only step that can populate ViewScript
      Table table = new Table(schema);
      SQLScript finalScript = new SQLScript();
      SQLScript upgradeScript = new SQLScript();
      SQLStatement finalStmt = new SQLStatement();
      SQLStatement upgradeStmt = new SQLStatement();

      upgradeStmt.addAdapter("*", m_database.getSchema().getDataSource().getType());
      finalStmt.addAdapter("*", m_database.getSchema().getDataSource().getType());
      upgradeStmt.setSQL("upgrade view SQL");
      finalStmt.setSQL("final view SQL");
      upgradeScript.addStatement(upgradeStmt);
      finalScript.addStatement(finalStmt);
      table.setName("testTable");
      table.setType(Table.VIEW);
      step.setName(table.getName());
      step.setType(table.getType());
      step.setViewScript(upgradeScript);
      schema.addTable(table);
      schema.setDataSource(ds);
      ds.setAdapter(m_database.getSchema().getDataSource().getAdapter());
      ds.setSchema(schema);
      ds.setType(new DataSourceType(null));
      ds.getType().setMetadata(metadata);
      version.setDataSource(ds);
      version.addStep(step);
      metadata.setVersion(version.getName());
      upgrade.setMetadata(metadata);
      upgrade.addVersion(version);
      table.setViewScript(finalScript);

      try
      {
         upgrade.validate(metadata, null);
         fail(); // exception expected
      }
      catch (MetadataException e)
      {
         assertEquals("err.meta.upgrade.sql.viewTableMismatch",
                      ((UncheckedException)e.getCause()).getErrorCode());
      }

      upgradeStmt.setSQL(finalStmt.getSQL()); // different object with same value
      upgrade.validate(metadata, null); // this must pass without exception
   }
View Full Code Here

      StringWriter buf = new StringWriter();
      DataSourceAdapter invalid = new DataSourceAdapter("InvalidAdapter");
      DataSourceAdapter valid = new DataSourceAdapter("ValidAdapter");
      RelationalDatabase ds = new RelationalDatabase("DataSource");
      RelationalSchema schema = new RelationalSchema();
      final Upgrade upgrade = new Upgrade(null);
      XMLMetadata metadata = new XMLMetadata(null, null, null, null, null)
      {
         public Upgrade getUpgrade(String sName) { return upgrade; }
      };
      SQLSchemaManager manager = m_adapter.createSchemaManager();
      RelationalSchemaUpgrade upgradeVersion;

      ds.setAdapter(valid);
      ds.setComponent(new Component("Component"));
      ds.setSchema(schema);
      ds.setType(new DataSourceType("DataSourceType"));
      ds.getType().setMetadata(metadata);
      ds.getType().addAdapter(ds.getAdapter());
      ds.getType().addAdapter(invalid);
      ((RelationalDatabaseFragment)ds.getDefaultFragment()).setDatabase("Database");
      metadata.addDataSource(ds);
      manager.setSQLAppender(manager.new SQLWriterAppender(buf));
      schema.setVersionTable(new Table(schema));
      schema.getVersionTable().setName("VersionTable");
      schema.addTable(schema.getVersionTable());
      upgrade.setMetadata(metadata);

      // setup for first upgrade version
      ExecStep execStep = new ExecStep(); // step lacking match for current adapter
      SQLScript stepScript = new SQLScript();
      SQLStatement stepStmt = new SQLStatement();

      stepStmt.addAdapter(invalid.getName(), ds.getType());
      stepStmt.setSQL("SQLStatement SQL");
      stepScript.addStatement(stepStmt);
      execStep.getScriptHolder().addScript(stepScript);

      // single incompatible step
      metadata.setVersion("1-step");
      upgradeVersion = new RelationalSchemaUpgrade(metadata.getVersion());
      upgradeVersion.addStep(execStep);
      upgradeVersion.setDataSource(ds);
      upgrade.addVersion(upgradeVersion);

      try
      {
         upgrade.validate(null, null); // must fail since an incompatible step "1-step" exists
         fail(); // exception expected
      }
      catch (MetadataException e)
      {
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertEquals("err.meta.sql.statementAdapter", e.getErrorCode());
      }

      try
      {
         manager.upgrade(schema, null); //must fail since no compatible versions found to start with
         fail(); // exception expected
      }
      catch (MetadataException e)
      {
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertEquals("err.meta.sql.statementAdapter", e.getErrorCode());
      }

      try
      {
         manager.upgrade(schema, "1-step"); // must fail since step "1-step" is incompatible
         fail(); // exception expected
      }
      catch (MetadataException e)
      {
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertEquals("err.meta.sql.statementAdapter", e.getErrorCode());
      }

      // setup for second upgrade version
      SupportAdapterStep adapterStep = new SupportAdapterStep();
      CreateTableStep createTableStep = new CreateTableStep(); // step has match for current adapter
      Table table = new Table(schema);
      SQLScript tableScript = new SQLScript();
      SQLStatement tableStmtInvalid = new SQLStatement();
      SQLStatement tableStmtValid = new SQLStatement();

      tableStmtInvalid.addAdapter(invalid.getName(), ds.getType());
      tableStmtInvalid.setSQL("SQLStatement Table SQL Invalid");
      tableStmtValid.addAdapter(ds.getAdapter().getName(), ds.getType());
      tableStmtValid.setSQL("SQLStatement Table SQL Valid");
      tableScript.addStatement(tableStmtValid);
      tableScript.addStatement(tableStmtInvalid);
      tableScript.addStatement(stepStmt);
      table.setName("Table");
      table.setType(Table.VIEW);
      table.setViewScript(tableScript);
      schema.addTable(table);
      createTableStep.setName(table.getName());
      createTableStep.setType(table.getType());
      createTableStep.setViewScript(tableScript);
      adapterStep.setAdapter(invalid);

      // incompatible step -> compatible step
      metadata.setVersion("2-step");
      upgradeVersion = new RelationalSchemaUpgrade(metadata.getVersion());
      upgradeVersion.addStep(adapterStep);
      upgradeVersion.addStep(createTableStep);
      upgradeVersion.setDataSource(ds);
      upgrade.addVersion(upgradeVersion);

      try
      {
         upgrade.validate(null, null); // must fail since an incompatible step "1-step" exists
         fail(); // exception expected
      }
      catch (MetadataException e)
      {
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertEquals("err.meta.sql.statementAdapter", e.getErrorCode());
      }

      try
      {
         manager.upgrade(schema, null); // must fail since an incompatible step "1-step" exists
         fail(); // exception expected
      }
      catch (MetadataException e)
      {
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertEquals("err.meta.sql.statementAdapter", e.getErrorCode());
      }

      buf.getBuffer().setLength(0);
      ds.setAdapter(invalid);
      manager.upgrade(schema, "2-step"); // validation of invalid adapter should not be prevented
      AssertUtil.assertContained(tableStmtInvalid.getSQL(), buf.toString());
      ds.setAdapter(valid);

      try
      {
         manager.upgrade(schema, "2-step"); // must fail since validating current adapter from start
         fail(); // exception expected
      }
      catch (MetadataException e)
      {
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertTrue(e.getCause() instanceof MetadataException);
         e = (MetadataException)e.getCause();
         assertEquals("err.meta.sql.statementAdapter", e.getErrorCode());
      }

      adapterStep.setAdapter(ds.getAdapter());
      upgrade.validate(null, null); // must pass since adapter validation from "2-step"
      buf.getBuffer().setLength(0);
      manager.upgrade(schema, null); // must upgrade starting from "2-step"
      AssertUtil.assertContained(tableStmtValid.getSQL(), buf.toString());

      // setup for third upgrade version
      CreateColumnStep createColumnStep = new CreateColumnStep(); // step lacking match for adapter
      Column column = new Column("Column", table);
      ColumnOutline outline = new ColumnOutline(column.getName());

      column.setType(Primitive.INTEGER);
      table.addColumn(column);
      outline.setType(column.getType());
      createColumnStep.setOutline(outline);
      createColumnStep.setTableName(table.getName());
      createColumnStep.getScriptHolder().addScript(stepScript);

      // incompatible step -> compatible step -> incompatible step
      metadata.setVersion("3-step");
      upgradeVersion = new RelationalSchemaUpgrade(metadata.getVersion());
      upgradeVersion.addStep(createColumnStep);
      upgradeVersion.setDataSource(ds);
      upgrade.addVersion(upgradeVersion);

      try
      {
         upgrade.validate(null, null);//must fail since an incompatible step exists after compatible
         fail(); // exception expected
      }
         catch (MetadataException e)
      {
         assertTrue(e.getCause() instanceof MetadataException);
View Full Code Here

      }
   }

   public void testUpgrade()
   {
      Upgrade upgrade = getMetadata().getUpgrade("Main");

      upgrade.validate(getMetadata(), null);

      VersionUpgrade firstVersion = upgrade.getFirstVersion();
      Lookup stateMap = Upgrade.getInitialState(firstVersion);
      SchemaVersion version = new SchemaVersion();

      version.setNamespace(getMetadata().getNamespace());
      version.setUpgradable(true);
View Full Code Here

TOP

Related Classes of nexj.core.meta.upgrade.Upgrade

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.