protected void applyConstraints(Metadata metadata) { File tempFile = null; try { tempFile = File.createTempFile("schema", ".sql"); new SchemaExport().setOutputFile(tempFile.getAbsolutePath()) .setFormat(false).createOnly(EnumSet.of(TargetType.SCRIPT), metadata); List<String> sqls = new ArrayList<>(); for (String sql: FileUtils.readLines(tempFile)) { if (isApplyingConstraints(sql)) { sqls.add(sql); } } execute(sqls, true); } catch (IOException e) { throw new RuntimeException(e); } finally { if (tempFile != null) tempFile.delete(); } }
protected void createTables(Metadata metadata) { File tempFile = null; try { tempFile = File.createTempFile("schema", ".sql"); new SchemaExport().setOutputFile(tempFile.getAbsolutePath()) .setFormat(false).createOnly(EnumSet.of(TargetType.SCRIPT), metadata); List<String> sqls = new ArrayList<>(); for (String sql: FileUtils.readLines(tempFile)) { if (shouldInclude(sql) && !isApplyingConstraints(sql)) sqls.add(sql); } execute(sqls, true); } catch (IOException e) { throw new RuntimeException(e); } finally { if (tempFile != null) FileUtils.deleteFile(tempFile); } }
protected void dropConstraints(Metadata metadata) { File tempFile = null; try { tempFile = File.createTempFile("schema", ".sql"); new SchemaExport().setOutputFile(tempFile.getAbsolutePath()) .setFormat(false).drop(EnumSet.of(TargetType.SCRIPT), metadata); List<String> sqls = new ArrayList<>(); for (String sql: FileUtils.readLines(tempFile)) { if (isDroppingConstraints(sql)) sqls.add(sql); } execute(sqls, false); } catch (IOException e) { throw new RuntimeException(e); } finally { if (tempFile != null) tempFile.delete(); } }
protected void cleanDatabase(Metadata metadata) { File tempFile = null; try { tempFile = File.createTempFile("schema", ".sql"); new SchemaExport().setOutputFile(tempFile.getAbsolutePath()) .setFormat(false).drop(EnumSet.of(TargetType.SCRIPT), metadata); List<String> sqls = new ArrayList<>(); for (String sql: FileUtils.readLines(tempFile)) { sqls.add(sql); } execute(sqls, false); } catch (IOException e) { throw new RuntimeException(e); } finally { if (tempFile != null) tempFile.delete(); } }
private void outputDdl(String packageName, String dialect, String fileName) { LocalSessionFactoryBean sfBean = sfBean(packageName, dialect); StandardServiceRegistry serviceRegistry = sfBean.getConfiguration().getStandardServiceRegistryBuilder().build(); try { String outputFile = OutputRoot + fileName; Files.deleteIfExists(Paths.get(outputFile)); MetadataImplementor metadata = metadata(sfBean, serviceRegistry); SchemaExport export = new SchemaExport(); export.setDelimiter(";"); export.setFormat(FormatSql); export.setOutputFile(outputFile); export.create(EnumSet.of(TargetType.SCRIPT), metadata); } catch (Exception e) { throw new InvocationException(e); } finally { StandardServiceRegistryBuilder.destroy( serviceRegistry ); } }
/** * Method that actually creates the file. * * @param dbDialect to use */ private void generate(Dialect dialect) { StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); ssrb.applySetting("hibernate.dialect", dialect.getDialectClass()); StandardServiceRegistry standardServiceRegistry = ssrb.build(); MetadataSources metadataSources = new MetadataSources(standardServiceRegistry); for (Class clzz : jpaClasses) { metadataSources.addAnnotatedClass(clzz); } Metadata metadata = metadataSources.buildMetadata(); SchemaExport export = new SchemaExport(); export.setDelimiter(";"); export.setOutputFile(dialect.name().toLowerCase() + ".ddl"); //export.execute(true, false, false, true); export.execute(EnumSet.of(TargetType.SCRIPT), Action.BOTH, metadata); }
public static void main(String[] args){ String file = "camping-db-creation.sql"; new File(file).delete(); MetadataSources metadata = new MetadataSources( new StandardServiceRegistryBuilder().configure().build()); SchemaExport export = new SchemaExport(); export.setOutputFile(file); export.setDelimiter(";"); export.setFormat(true); export.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata.buildMetadata()); }
private void createSQLSchema(String fileName, String dialect) { File file = new File(fileName); if (file.exists()) { file.delete(); } MetadataSources metadata = new MetadataSources( new StandardServiceRegistryBuilder() .applySetting("hibernate.implicit_naming_strategy", new MidPointImplicitNamingStrategy()) .applySetting("hibernate.physical_naming_strategy", new MidPointPhysicalNamingStrategy()) .applySetting("hibernate.dialect", dialect) .build()); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common.container", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common.any", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common.embedded", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common.enums", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common.id", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common.other", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.common.type", metadata); addAnnotatedClasses("com.evolveum.midpoint.repo.sql.data.audit", metadata); metadata.addPackage("com.evolveum.midpoint.repo.sql.type"); SchemaExport export = new SchemaExport(); export.setOutputFile(fileName); export.setDelimiter(";"); // export.setFormat(true); export.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata.buildMetadata()); }
public void dropAndCreate() { // SchemaExport export = new SchemaExport(HibernateUtil.getConfiguration()); // export.drop(true, true); // export.create(true, true); MetadataSources metadata = new MetadataSources( new StandardServiceRegistryBuilder() .applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect") .applySetting("javax.persistence.schema-generation-connection", this.getConnection()) .build()); SchemaExport export = new SchemaExport(); export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata()); }
public static void writeSqlDiffScript(ConfigurableApplicationContext context, String fileName, String action) { EntityManagerFactory entityManagerFactory = context.getBean(EntityManagerFactory.class); EntityManager entityManager = entityManagerFactory.createEntityManager(); ExecutionOptions executionOptions = new ExecutionOptions() { @Override public boolean shouldManageNamespaces() { return false; } @Override public Map<?, ?> getConfigurationValues() { return Maps.newHashMap(); } @Override public ExceptionHandler getExceptionHandler() { // TODO récupérer et vérifier aucune exception pendant la mise à jour return new ExceptionHandlerCollectingImpl(); } }; ServiceRegistry serviceRegistry = ((SessionImpl) entityManager.getDelegate()).getSessionFactory().getServiceRegistry(); Metadata metadata = MetadataRegistryIntegrator.METADATA; EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT); TargetDescriptor targetDescriptor = SchemaExport.buildTargetDescriptor(targetTypes, fileName, serviceRegistry); if (action.equals("create")) { final SourceDescriptor sourceDescriptor = new SourceDescriptor() { @Override public SourceType getSourceType() { return SourceType.METADATA; } @Override public ScriptSourceInput getScriptSourceInput() { return null; } }; serviceRegistry.getService(SchemaManagementTool.class).getSchemaCreator(Maps.newHashMap()) .doCreation(metadata, executionOptions, sourceDescriptor, targetDescriptor); } else if (action.equals("update")) { serviceRegistry.getService(SchemaManagementTool.class).getSchemaMigrator(Maps.newHashMap()) .doMigration(metadata, executionOptions, targetDescriptor); } }
public void createDatabase() throws HibernateException { // executes a drop and a create! new SchemaExport().create(EnumSet.of(TargetType.DATABASE), metadata); }