1 /*
2  * Copyright 2010-2020 Redgate Software Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.flywaydb.core.internal.resource;
17
18 import org.flywaydb.core.api.FlywayException;
19 import org.flywaydb.core.api.configuration.Configuration;
20 import org.flywaydb.core.api.logging.Log;
21 import org.flywaydb.core.api.logging.LogFactory;
22 import org.flywaydb.core.internal.util.StringUtils;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 public class ResourceNameValidator {
29     private static final Log LOG = LogFactory.getLog(ResourceNameValidator.class);
30
31     /**
32      * Validates the names of all SQL resources returned by the ResourceProvider
33      * @param provider The ResourceProvider to validate
34      * @param configuration The configuration to use
35      */

36     public void validateSQLMigrationNaming(ResourceProvider provider, Configuration configuration) {
37
38         List<String> errorsFound = new ArrayList<>();
39         ResourceNameParser resourceNameParser = new ResourceNameParser(configuration);
40
41         for (Resource resource : getAllSqlResources(provider, configuration)) {
42             LOG.debug("Validating " + resource.getFilename());
43
44             ResourceName result = resourceNameParser.parse(resource.getFilename());
45             if (!result.isValid()) {
46                 errorsFound.add(result.getValidityMessage());
47             }
48         }
49
50         if (!errorsFound.isEmpty()) {
51             throw new FlywayException("Invalid SQL filenames found:\r\n" + StringUtils.collectionToDelimitedString(errorsFound, "\r\n"));
52         }
53     }
54
55     private Collection<LoadableResource> getAllSqlResources(ResourceProvider provider, Configuration configuration) {
56         return provider.getResources("", configuration.getSqlMigrationSuffixes());
57     }
58 }