Search in sources :

Example 1 with LoadableResource

use of org.flywaydb.core.api.resource.LoadableResource in project flyway by flyway.

the class SqlMigrationResolver method addMigrations.

private void addMigrations(List<ResolvedMigration> migrations, String prefix, String[] suffixes, boolean repeatable) {
    ResourceNameParser resourceNameParser = new ResourceNameParser(configuration);
    for (LoadableResource resource : resourceProvider.getResources(prefix, suffixes)) {
        String filename = resource.getFilename();
        ResourceName result = resourceNameParser.parse(filename);
        if (!result.isValid() || isSqlCallback(result) || !prefix.equals(result.getPrefix())) {
            continue;
        }
        SqlScript sqlScript = sqlScriptFactory.createSqlScript(resource, configuration.isMixed(), resourceProvider);
        List<LoadableResource> resources = new ArrayList<>();
        resources.add(resource);
        Integer checksum = getChecksumForLoadableResource(repeatable, resources);
        Integer equivalentChecksum = getEquivalentChecksumForLoadableResource(repeatable, resources);
        migrations.add(new ResolvedMigrationImpl(result.getVersion(), result.getDescription(), resource.getRelativePath(), checksum, equivalentChecksum, MigrationType.SQL, resource.getAbsolutePathOnDisk(), new SqlMigrationExecutor(sqlScriptExecutorFactory, sqlScript, false, false)) {

            @Override
            public void validate() {
            }
        });
    }
}
Also used : ResourceName(org.flywaydb.core.internal.resource.ResourceName) ResourceNameParser(org.flywaydb.core.internal.resource.ResourceNameParser) LoadableResource(org.flywaydb.core.api.resource.LoadableResource) ArrayList(java.util.ArrayList) SqlScript(org.flywaydb.core.internal.sqlscript.SqlScript) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)

Example 2 with LoadableResource

use of org.flywaydb.core.api.resource.LoadableResource in project flyway by flyway.

the class ClasspathClassScanner method scanForType.

public List<String> scanForType(String location, Class<?> classType, boolean errorOnNotFound) {
    ClassPathScanner<?> s = new ClassPathScanner<>(classType, classLoader, Charset.defaultCharset(), new Location("classpath:" + location), resourceNameCache, locationScannerCache, errorOnNotFound);
    List<String> discoveredTypes = new ArrayList<>();
    for (LoadableResource resource : s.scanForResources()) {
        if (resource.getAbsolutePath().endsWith(".class")) {
            discoveredTypes.add(toClassName(resource.getAbsolutePath()));
        }
    }
    return discoveredTypes;
}
Also used : ArrayList(java.util.ArrayList) LoadableResource(org.flywaydb.core.api.resource.LoadableResource) ClassPathScanner(org.flywaydb.core.internal.scanner.classpath.ClassPathScanner) Location(org.flywaydb.core.api.Location)

Example 3 with LoadableResource

use of org.flywaydb.core.api.resource.LoadableResource in project flyway by flyway.

the class ClassPathScanner method scanForClasses.

@Override
public Collection<Class<? extends I>> scanForClasses() {
    LOG.debug("Scanning for classes at " + location);
    List<Class<? extends I>> classes = new ArrayList<>();
    for (LoadableResource resource : resources) {
        if (resource.getAbsolutePath().endsWith(".class")) {
            Class<? extends I> clazz;
            try {
                clazz = ClassUtils.loadClass(implementedInterface, toClassName(resource.getAbsolutePath()), classLoader);
            } catch (Throwable e) {
                Throwable rootCause = ExceptionUtils.getRootCause(e);
                LOG.warn("Skipping " + Callback.class + ": " + ClassUtils.formatThrowable(e) + (rootCause == e ? "" : " caused by " + ClassUtils.formatThrowable(rootCause) + " at " + ExceptionUtils.getThrowLocation(rootCause)));
                clazz = null;
            }
            if (clazz != null) {
                classes.add(clazz);
            }
        }
    }
    return classes;
}
Also used : Callback(org.flywaydb.core.api.callback.Callback) LoadableResource(org.flywaydb.core.api.resource.LoadableResource)

Example 4 with LoadableResource

use of org.flywaydb.core.api.resource.LoadableResource in project flyway by flyway.

the class FileSystemScanner method scanForResources.

/**
 * Scans the FileSystem for resources under the specified location, starting with the specified prefix and ending with
 * the specified suffix.
 *
 * @param location The location in the filesystem to start searching. Subdirectories are also searched.
 * @return The resources that were found.
 */
public Collection<LoadableResource> scanForResources(Location location) {
    String path = location.getRootPath();
    LOG.debug("Scanning for filesystem resources at '" + path + "'");
    File dir = new File(path);
    if (!dir.exists()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + ".");
        }
        LOG.error("Skipping filesystem location:" + path + " (not found).");
        return Collections.emptyList();
    }
    if (!dir.canRead()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + " (not readable).");
        }
        LOG.error("Skipping filesystem location:" + path + " (not readable).");
        return Collections.emptyList();
    }
    if (!dir.isDirectory()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + " (not a directory).");
        }
        LOG.error("Skipping filesystem location:" + path + " (not a directory).");
        return Collections.emptyList();
    }
    Set<LoadableResource> resources = new TreeSet<>();
    for (String resourceName : findResourceNamesFromFileSystem(path, new File(path))) {
        boolean detectEncodingForThisResource = detectEncoding;
        if (location.matchesPath(resourceName)) {
            Charset encoding = defaultEncoding;
            String encodingBlurb = "";
            if (new File(resourceName + ".conf").exists()) {
                LoadableResource metadataResource = new FileSystemResource(location, resourceName + ".conf", defaultEncoding, false);
                SqlScriptMetadata metadata = SqlScriptMetadata.fromResource(metadataResource, null);
                if (metadata.encoding() != null) {
                    encoding = Charset.forName(metadata.encoding());
                    detectEncodingForThisResource = false;
                    encodingBlurb = " (with overriding encoding " + encoding + ")";
                }
            }
            resources.add(new FileSystemResource(location, resourceName, encoding, detectEncodingForThisResource, stream));
            LOG.debug("Found filesystem resource: " + resourceName + encodingBlurb);
        }
    }
    return resources;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) TreeSet(java.util.TreeSet) SqlScriptMetadata(org.flywaydb.core.internal.sqlscript.SqlScriptMetadata) LoadableResource(org.flywaydb.core.api.resource.LoadableResource) Charset(java.nio.charset.Charset) FileSystemResource(org.flywaydb.core.internal.resource.filesystem.FileSystemResource) File(java.io.File)

Example 5 with LoadableResource

use of org.flywaydb.core.api.resource.LoadableResource in project flyway by flyway.

the class AwsS3Scanner method getLoadableResources.

private Collection<LoadableResource> getLoadableResources(String bucketName, final ListObjectsV2Response listObjectResult) {
    List<S3Object> objectSummaries = listObjectResult.contents();
    Set<LoadableResource> resources = new TreeSet<>();
    for (S3Object objectSummary : objectSummaries) {
        LOG.debug("Found Amazon S3 resource: " + bucketName.concat("/").concat(objectSummary.key()));
        resources.add(new AwsS3Resource(bucketName, objectSummary, encoding));
    }
    return resources;
}
Also used : AwsS3Resource(org.flywaydb.core.internal.resource.s3.AwsS3Resource) LoadableResource(org.flywaydb.core.api.resource.LoadableResource) S3Object(software.amazon.awssdk.services.s3.model.S3Object)

Aggregations

LoadableResource (org.flywaydb.core.api.resource.LoadableResource)6 ArrayList (java.util.ArrayList)3 File (java.io.File)1 Charset (java.nio.charset.Charset)1 TreeSet (java.util.TreeSet)1 FlywayException (org.flywaydb.core.api.FlywayException)1 Location (org.flywaydb.core.api.Location)1 Callback (org.flywaydb.core.api.callback.Callback)1 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)1 ResourceName (org.flywaydb.core.internal.resource.ResourceName)1 ResourceNameParser (org.flywaydb.core.internal.resource.ResourceNameParser)1 FileSystemResource (org.flywaydb.core.internal.resource.filesystem.FileSystemResource)1 AwsS3Resource (org.flywaydb.core.internal.resource.s3.AwsS3Resource)1 ClassPathScanner (org.flywaydb.core.internal.scanner.classpath.ClassPathScanner)1 SqlScript (org.flywaydb.core.internal.sqlscript.SqlScript)1 SqlScriptMetadata (org.flywaydb.core.internal.sqlscript.SqlScriptMetadata)1 S3Object (software.amazon.awssdk.services.s3.model.S3Object)1