├── .gitattributes ├── .github └── workflows │ ├── ci.yaml │ ├── codeql.yml │ ├── coveralls.yaml │ ├── site.yaml │ ├── sonar.yaml │ └── sonatype.yaml ├── .gitignore ├── .mvn ├── extensions.xml ├── maven.config ├── settings.xml └── wrapper │ ├── MavenWrapperDownloader.java │ └── maven-wrapper.properties ├── Dockerfile ├── LICENSE ├── LICENSE_HEADER ├── NOTICE ├── README.md ├── format.xml ├── mvnw ├── mvnw.cmd ├── pom.xml ├── renovate.json └── src ├── main ├── assembly │ └── bundle.xml ├── java │ └── org │ │ └── apache │ │ └── ibatis │ │ └── migration │ │ ├── BootstrapScript.java │ │ ├── Change.java │ │ ├── CommandLine.java │ │ ├── ConnectionProvider.java │ │ ├── ConsoleColors.java │ │ ├── DataSourceConnectionProvider.java │ │ ├── Environment.java │ │ ├── FileMigrationLoader.java │ │ ├── FileMigrationLoaderFactory.java │ │ ├── JavaMigrationLoader.java │ │ ├── JdbcConnectionProvider.java │ │ ├── MigrationException.java │ │ ├── MigrationLoader.java │ │ ├── MigrationReader.java │ │ ├── MigrationScript.java │ │ ├── Migrator.java │ │ ├── OnAbortScript.java │ │ ├── SimpleScript.java │ │ ├── VariableReplacer.java │ │ ├── commands │ │ ├── BaseCommand.java │ │ ├── BootstrapCommand.java │ │ ├── Command.java │ │ ├── Commands.java │ │ ├── DownCommand.java │ │ ├── InfoCommand.java │ │ ├── InitializeCommand.java │ │ ├── NewCommand.java │ │ ├── PendingCommand.java │ │ ├── RedoCommand.java │ │ ├── ScriptCommand.java │ │ ├── StatusCommand.java │ │ ├── UpCommand.java │ │ ├── VersionCommand.java │ │ └── package-info.java │ │ ├── driver │ │ ├── DriverShim.java │ │ └── package-info.java │ │ ├── hook │ │ ├── FileHookScriptFactory.java │ │ ├── FileMigrationHook.java │ │ ├── HookContext.java │ │ ├── HookScript.java │ │ ├── HookScriptFactory.java │ │ ├── Jsr223HookScript.java │ │ ├── MigrationHook.java │ │ ├── NewHookContext.java │ │ ├── ScriptHookContext.java │ │ ├── SqlHookScript.java │ │ └── package-info.java │ │ ├── io │ │ ├── ClassLoaderWrapper.java │ │ ├── DefaultVFS.java │ │ ├── JBoss6VFS.java │ │ ├── ResolverUtil.java │ │ ├── Resources.java │ │ ├── VFS.java │ │ └── package-info.java │ │ ├── operations │ │ ├── BootstrapOperation.java │ │ ├── ChangelogOperation.java │ │ ├── DatabaseOperation.java │ │ ├── DownOperation.java │ │ ├── PendingOperation.java │ │ ├── ScriptRunner.java │ │ ├── StatusOperation.java │ │ ├── UpOperation.java │ │ ├── VersionOperation.java │ │ └── package-info.java │ │ ├── options │ │ ├── DatabaseOperationOption.java │ │ ├── Options.java │ │ ├── OptionsParser.java │ │ ├── SelectedOptions.java │ │ ├── SelectedPaths.java │ │ └── package-info.java │ │ ├── package-info.java │ │ └── utils │ │ ├── Util.java │ │ └── package-info.java └── resources │ ├── mybatis-migrations.properties │ └── org │ └── apache │ └── ibatis │ └── migration │ ├── template_README │ ├── template_bootstrap.sql │ ├── template_changelog.sql │ ├── template_environment.properties │ └── template_migration.sql ├── site ├── site.xml └── xdoc │ ├── bootstrap.xml │ ├── custom-loader.xml │ ├── hooks.xml │ ├── index.xml │ ├── init.xml │ ├── installation.xml │ ├── migrate.xml │ ├── new.xml │ ├── pending.xml │ ├── redo.xml │ ├── runtime-migration.xml │ ├── script.xml │ ├── shortcuts.xml │ ├── status.xml │ ├── updown.xml │ └── version.xml └── test ├── java ├── databases │ └── blog │ │ └── StoredProcedures.java ├── it │ └── version_without_changelog │ │ └── VersionWithoutChangeLogTest.java └── org │ └── apache │ └── ibatis │ └── migration │ ├── MigrationReaderTest.java │ ├── MigratorTest.java │ ├── commands │ └── BaseCommandTest.java │ ├── hook │ ├── MigrationHookTest.java │ └── NewHookTest.java │ ├── options │ └── OptionsParserTest.java │ ├── runtime_migration │ ├── RuntimeMigrationTest.java │ └── scripts_java │ │ ├── Bootstrap.java │ │ ├── JavaMigrationLoaderTest.java │ │ ├── V001_CreateChangelog.java │ │ ├── V002_CreateFirstTable.java │ │ └── V003_CreateSecondTable.java │ ├── script_hook │ └── ScriptHookTest.java │ ├── system_property │ └── SystemPropertyTest.java │ └── utils │ ├── TestUtil.java │ └── UtilTest.java └── resources ├── it └── version_without_changelog │ ├── environments │ └── development.properties │ └── scripts │ ├── 20000101000000_create_changelog.sql │ ├── 20000101000001_first_migration.sql │ └── 20000101000002_second_migration.sql └── org └── apache └── ibatis └── migration ├── commands └── TestTemplate.sql ├── example ├── README ├── environments │ └── development.properties └── scripts │ ├── 20080827200210_create_changelog.sql │ ├── 20080827200211_create_author.sql │ ├── 20080827200212_create_blog.sql │ ├── 20080827200213_create_tags_posts.sql │ ├── 20080827200214_create_comments.sql │ ├── 20080827200216_create_procs.sql │ ├── 20080827200217_parse_file_with_dot_._in_file_name.sql │ └── bootstrap.sql ├── hook └── testdir │ ├── environments │ └── development.properties │ ├── hooks │ ├── InsertWorklog.js │ ├── InsertWorklog.sql │ ├── NewHook.js │ ├── PrintHello1.js │ └── PrintVar.js │ └── scripts │ ├── 001_create_changelog.sql │ ├── 002_create_person.sql │ ├── 003_create_pet.sql │ └── bootstrap.sql ├── runtime_migration ├── scripts │ ├── 20130707120737_create_changelog.sql │ ├── 20130707120738_create_first_table.sql │ ├── 20130707120739_create_second_table.sql │ └── bootstrap.sql └── scripts_from_other_branch │ └── 20130707120740_create_third_table.sql ├── script_hook └── testdir │ ├── environments │ └── development.properties │ ├── hooks │ ├── After.js │ ├── AfterEach.js │ ├── Before.js │ └── BeforeEach.js │ └── scripts │ ├── 001_create_changelog.sql │ ├── 002_second_migration.sql │ └── 003_third_migration.sql └── system_property └── testdir ├── environments └── development.properties └── scripts └── 001_create_changelog.sql /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behaviour, in case users don't have core.autocrlf set. 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [workflow_dispatch, push, pull_request] 4 | 5 | permissions: read-all 6 | 7 | jobs: 8 | test: 9 | runs-on: ${{ matrix.os }} 10 | strategy: 11 | matrix: 12 | cache: [maven] 13 | distribution: [temurin] 14 | java: [17, 21, 24, 25-ea] 15 | os: [ubuntu-latest, macos-latest, windows-latest] 16 | fail-fast: false 17 | max-parallel: 4 18 | name: Test JDK ${{ matrix.java }}, ${{ matrix.os }} 19 | 20 | steps: 21 | - uses: actions/checkout@v4 22 | - name: Set up JDK ${{ matrix.java }} ${{ matrix.distribution }} 23 | uses: actions/setup-java@v4 24 | with: 25 | java-version: ${{ matrix.java }} 26 | distribution: ${{ matrix.distribution }} 27 | cache: ${{ matrix.cache }} 28 | - name: Test with Maven 29 | run: ./mvnw test -B -V --no-transfer-progress -D"license.skip=true" 30 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | schedule: 9 | - cron: '44 14 * * 4' 10 | 11 | jobs: 12 | analyze: 13 | name: Analyze 14 | runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} 15 | timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }} 16 | permissions: 17 | actions: read 18 | contents: read 19 | security-events: write 20 | 21 | strategy: 22 | fail-fast: false 23 | matrix: 24 | language: [ 'java-kotlin', 'javascript-typescript' ] 25 | 26 | steps: 27 | - name: Checkout 28 | uses: actions/checkout@v4 29 | 30 | - name: Setup Java 31 | uses: actions/setup-java@v4 32 | with: 33 | cache: maven 34 | distribution: 'temurin' 35 | java-version: 21 36 | 37 | - name: Initialize CodeQL 38 | uses: github/codeql-action/init@v3 39 | with: 40 | languages: ${{ matrix.language }} 41 | queries: +security-and-quality 42 | 43 | - name: Autobuild 44 | uses: github/codeql-action/autobuild@v3 45 | 46 | - name: Perform CodeQL Analysis 47 | uses: github/codeql-action/analyze@v3 48 | with: 49 | category: "/language:${{matrix.language}}" 50 | -------------------------------------------------------------------------------- /.github/workflows/coveralls.yaml: -------------------------------------------------------------------------------- 1 | name: Coveralls 2 | 3 | on: [push, pull_request] 4 | 5 | permissions: read-all 6 | 7 | jobs: 8 | build: 9 | if: github.repository_owner == 'mybatis' 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v4 13 | - name: Set up JDK 14 | uses: actions/setup-java@v4 15 | with: 16 | cache: maven 17 | distribution: temurin 18 | java-version: 21 19 | - name: Report Coverage to Coveralls for Pull Requests 20 | if: github.event_name == 'pull_request' 21 | run: ./mvnw -B -V test jacoco:report coveralls:report -q -Dlicense.skip=true -DrepoToken=$GITHUB_TOKEN -DserviceName=github -DpullRequest=$PR_NUMBER --no-transfer-progress 22 | env: 23 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 24 | PR_NUMBER: ${{ github.event.number }} 25 | - name: Report Coverage to Coveralls for General Push 26 | if: github.event_name == 'push' 27 | run: ./mvnw -B -V test jacoco:report coveralls:report -q -Dlicense.skip=true -DrepoToken=$GITHUB_TOKEN -DserviceName=github --no-transfer-progress 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | -------------------------------------------------------------------------------- /.github/workflows/site.yaml: -------------------------------------------------------------------------------- 1 | name: Site 2 | 3 | on: 4 | push: 5 | branches: 6 | - site 7 | 8 | permissions: 9 | contents: write 10 | 11 | jobs: 12 | build: 13 | if: github.repository_owner == 'mybatis' && ! contains(toJSON(github.event.head_commit.message), '[maven-release-plugin]') 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: Set up JDK 18 | uses: actions/setup-java@v4 19 | with: 20 | cache: maven 21 | distribution: temurin 22 | java-version: 21 23 | - name: Build site 24 | run: ./mvnw site site:stage -DskipTests -Dlicense.skip=true -B -V --no-transfer-progress --settings ./.mvn/settings.xml 25 | env: 26 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 27 | NVD_API_KEY: ${{ secrets.NVD_API_KEY }} 28 | - name: Deploy Site to gh-pages 29 | uses: JamesIves/github-pages-deploy-action@v4 30 | with: 31 | branch: gh-pages 32 | folder: target/staging 33 | -------------------------------------------------------------------------------- /.github/workflows/sonar.yaml: -------------------------------------------------------------------------------- 1 | name: SonarCloud 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | permissions: read-all 9 | 10 | jobs: 11 | build: 12 | if: github.repository_owner == 'mybatis' 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | with: 17 | # Disabling shallow clone is recommended for improving relevancy of reporting 18 | fetch-depth: 0 19 | - name: Set up JDK 20 | uses: actions/setup-java@v4 21 | with: 22 | cache: maven 23 | distribution: temurin 24 | java-version: 21 25 | - name: Analyze with SonarCloud 26 | run: ./mvnw verify jacoco:report sonar:sonar -B -V -Dsonar.projectKey=mybatis_migrations -Dsonar.organization=mybatis -Dsonar.host.url=https://sonarcloud.io -Dsonar.token=$SONAR_TOKEN -Dlicense.skip=true --no-transfer-progress 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} 30 | -------------------------------------------------------------------------------- /.github/workflows/sonatype.yaml: -------------------------------------------------------------------------------- 1 | name: Sonatype 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | permissions: read-all 9 | 10 | jobs: 11 | build: 12 | if: github.repository_owner == 'mybatis' && ! contains(toJSON(github.event.head_commit.message), '[maven-release-plugin]') 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Set up JDK 17 | uses: actions/setup-java@v4 18 | with: 19 | cache: maven 20 | distribution: temurin 21 | java-version: 21 22 | - name: Deploy to Sonatype 23 | run: ./mvnw deploy -DskipTests -B -V --no-transfer-progress --settings ./.mvn/settings.xml -Dlicense.skip=true 24 | env: 25 | CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }} 26 | CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }} 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # / 2 | ibderby 3 | *migrations.iml 4 | *migrations.ipr 5 | /migrations.iws 6 | /target 7 | .idea 8 | atlassian-ide-plugin.xml 9 | .classpath 10 | .project 11 | .settings 12 | .mvn/wrapper/maven-wrapper.jar 13 | release.properties 14 | pom.xml.releaseBackup 15 | -------------------------------------------------------------------------------- /.mvn/extensions.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | fr.jcgay.maven 22 | maven-profiler 23 | 3.3 24 | 25 | 26 | -------------------------------------------------------------------------------- /.mvn/maven.config: -------------------------------------------------------------------------------- 1 | -Daether.checksums.algorithms=SHA-512,SHA-256,SHA-1,MD5 2 | -Daether.connector.smartChecksums=false 3 | -Dhsqldb.method_class_names=databases.blog.* 4 | -------------------------------------------------------------------------------- /.mvn/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | central 26 | ${env.CI_DEPLOY_USERNAME} 27 | ${env.CI_DEPLOY_PASSWORD} 28 | 29 | 30 | 31 | 32 | gh-pages-scm 33 | 34 | branch 35 | gh-pages 36 | 37 | 38 | 39 | 40 | 41 | github 42 | ${env.GITHUB_TOKEN} 43 | 44 | 45 | 46 | 47 | nvd 48 | ${env.NVD_API_KEY} 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | wrapperVersion=3.3.2 18 | distributionType=source 19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.10/apache-maven-3.9.10-bin.zip 20 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar 21 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2025 the original author or authors. 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 | # https://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 | 17 | FROM openjdk:21 AS build 18 | RUN mkdir -p /opt/migrations/build 19 | WORKDIR /opt/migrations/build 20 | COPY . . 21 | RUN ./mvnw package -DskipTests 22 | 23 | FROM openjdk:21 24 | RUN ["mkdir", "-p", "/opt/migrations"] 25 | 26 | COPY --from=build /opt/migrations/build/target/appassembler /opt/migrations 27 | 28 | VOLUME ["/migration"] 29 | WORKDIR /migration 30 | 31 | ENTRYPOINT ["/opt/migrations/bin/migrate"] 32 | -------------------------------------------------------------------------------- /LICENSE_HEADER: -------------------------------------------------------------------------------- 1 | Copyright ${license.git.copyrightYears} the original author or authors. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | https://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | MyBatis Migrations 2 | Copyright 2010-2023 3 | 4 | This product includes software developed by 5 | The MyBatis Team (http://www.mybatis.org/). 6 | 7 | iBATIS 8 | This product includes software developed by 9 | The Apache Software Foundation (http://www.apache.org/). 10 | 11 | Copyright 2010 The Apache Software Foundation 12 | 13 | Licensed under the Apache License, Version 2.0 (the "License"); 14 | you may not use this file except in compliance with the License. 15 | You may obtain a copy of the License at 16 | 17 | http://www.apache.org/licenses/LICENSE-2.0 18 | 19 | Unless required by applicable law or agreed to in writing, software 20 | distributed under the License is distributed on an "AS IS" BASIS, 21 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | See the License for the specific language governing permissions and 23 | limitations under the License. 24 | 25 | OGNL 26 | //-------------------------------------------------------------------------- 27 | // Copyright (c) 2004, Drew Davidson and Luke Blanshard 28 | // All rights reserved. 29 | // 30 | // Redistribution and use in source and binary forms, with or without 31 | // modification, are permitted provided that the following conditions are 32 | // met: 33 | // 34 | // Redistributions of source code must retain the above copyright notice, 35 | // this list of conditions and the following disclaimer. 36 | // Redistributions in binary form must reproduce the above copyright 37 | // notice, this list of conditions and the following disclaimer in the 38 | // documentation and/or other materials provided with the distribution. 39 | // Neither the name of the Drew Davidson nor the names of its contributors 40 | // may be used to endorse or promote products derived from this software 41 | // without specific prior written permission. 42 | // 43 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 44 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 45 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 46 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 47 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 48 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 49 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 50 | // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 51 | // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 52 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 53 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 54 | // DAMAGE. 55 | //-------------------------------------------------------------------------- 56 | -------------------------------------------------------------------------------- /format.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /src/main/assembly/bundle.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | bundle 22 | mybatis-migrations-${project.version} 23 | 24 | zip 25 | 26 | 27 | 28 | LICENSE 29 | 666 30 | 31 | 32 | NOTICE 33 | 666 34 | 35 | 36 | README.md 37 | true 38 | 666 39 | 40 | 41 | 42 | 45 | 46 | ${project.build.directory}/appassembler/lib/ 47 | lib 48 | 49 | *.xml 50 | 51 | 52 | 53 | ${project.build.directory}/appassembler/bin/ 54 | bin 55 | 755 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/BootstrapScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | public interface BootstrapScript extends SimpleScript { 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/Change.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | import java.math.BigDecimal; 19 | 20 | public class Change implements Comparable { 21 | 22 | private BigDecimal id; 23 | private String description; 24 | private String appliedTimestamp; 25 | private String filename; 26 | 27 | public Change() { 28 | } 29 | 30 | public Change(BigDecimal id) { 31 | this.id = id; 32 | } 33 | 34 | public Change(BigDecimal id, String appliedTimestamp, String description) { 35 | this.id = id; 36 | this.appliedTimestamp = appliedTimestamp; 37 | this.description = description; 38 | } 39 | 40 | public Change(Change toCopy) { 41 | this(toCopy.getId(), toCopy.getAppliedTimestamp(), toCopy.getDescription()); 42 | this.filename = toCopy.getFilename(); 43 | } 44 | 45 | public BigDecimal getId() { 46 | return id; 47 | } 48 | 49 | public void setId(BigDecimal id) { 50 | this.id = id; 51 | } 52 | 53 | public String getDescription() { 54 | return description; 55 | } 56 | 57 | public void setDescription(String description) { 58 | this.description = description; 59 | } 60 | 61 | public String getAppliedTimestamp() { 62 | return appliedTimestamp; 63 | } 64 | 65 | public void setAppliedTimestamp(String appliedTimestamp) { 66 | this.appliedTimestamp = appliedTimestamp; 67 | } 68 | 69 | public String getFilename() { 70 | return filename; 71 | } 72 | 73 | public void setFilename(String filename) { 74 | this.filename = filename; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return id + " " + (appliedTimestamp == null ? " ...pending... " : appliedTimestamp) + " " + description; 80 | } 81 | 82 | @Override 83 | public boolean equals(Object o) { 84 | if (this == o) { 85 | return true; 86 | } 87 | if (o == null || getClass() != o.getClass()) { 88 | return false; 89 | } 90 | 91 | Change change = (Change) o; 92 | 93 | return id.equals(change.getId()); 94 | } 95 | 96 | @Override 97 | public int hashCode() { 98 | return id.hashCode(); 99 | } 100 | 101 | @Override 102 | public int compareTo(Change change) { 103 | return id.compareTo(change.getId()); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/ConnectionProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | import java.sql.Connection; 19 | import java.sql.SQLException; 20 | 21 | public interface ConnectionProvider { 22 | 23 | Connection getConnection() throws SQLException; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/DataSourceConnectionProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | import java.sql.Connection; 19 | import java.sql.SQLException; 20 | 21 | import javax.sql.DataSource; 22 | 23 | public class DataSourceConnectionProvider implements ConnectionProvider { 24 | 25 | private DataSource dataSource; 26 | 27 | public DataSourceConnectionProvider(DataSource dataSource) { 28 | this.dataSource = dataSource; 29 | } 30 | 31 | @Override 32 | public Connection getConnection() throws SQLException { 33 | return dataSource.getConnection(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/FileMigrationLoaderFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | import org.apache.ibatis.migration.options.SelectedPaths; 19 | 20 | public interface FileMigrationLoaderFactory { 21 | MigrationLoader create(SelectedPaths paths, Environment environment); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/JdbcConnectionProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | import java.sql.Connection; 19 | import java.sql.Driver; 20 | import java.sql.DriverManager; 21 | import java.sql.SQLException; 22 | import java.util.Enumeration; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import org.apache.ibatis.migration.driver.DriverShim; 27 | 28 | public class JdbcConnectionProvider implements ConnectionProvider { 29 | private static final Map registeredDrivers = registeredDrivers(); 30 | 31 | private final String url; 32 | private final String username; 33 | private final String password; 34 | 35 | public JdbcConnectionProvider(String driver, String url, String username, String password) { 36 | this(null, driver, url, username, password); 37 | } 38 | 39 | public JdbcConnectionProvider(ClassLoader classLoader, String driver, String url, String username, String password) { 40 | this.url = url; 41 | this.username = username; 42 | this.password = password; 43 | registerDriver(classLoader, driver); 44 | } 45 | 46 | @Override 47 | public Connection getConnection() throws SQLException { 48 | return DriverManager.getConnection(url, username, password); 49 | } 50 | 51 | private void registerDriver(ClassLoader classLoader, String driver) { 52 | registeredDrivers.computeIfAbsent(driver, d -> createDriverClass(classLoader, d)); 53 | } 54 | 55 | private Driver createDriverClass(ClassLoader classLoader, String driver) { 56 | try { 57 | final Class driverClass = classLoader == null ? Class.forName(driver) 58 | : Class.forName(driver, true, classLoader); 59 | final Driver driverInstance = (Driver) driverClass.getDeclaredConstructor().newInstance(); 60 | final DriverShim driverShim = new DriverShim(driverInstance); 61 | DriverManager.registerDriver(driverShim); 62 | return driverShim; 63 | } catch (final Exception e) { 64 | throw new IllegalStateException("Failed to register driver " + driver, e); 65 | } 66 | } 67 | 68 | private static Map registeredDrivers() { 69 | final Map registeredDrivers = new HashMap<>(); 70 | final Enumeration drivers = DriverManager.getDrivers(); 71 | while (drivers.hasMoreElements()) { 72 | final Driver driver = drivers.nextElement(); 73 | registeredDrivers.put(driver.getClass().getName(), driver); 74 | } 75 | return registeredDrivers; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/MigrationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | public class MigrationException extends RuntimeException { 19 | 20 | private static final long serialVersionUID = 491769430730827896L; 21 | 22 | public MigrationException() { 23 | } 24 | 25 | public MigrationException(String message) { 26 | super(message); 27 | } 28 | 29 | public MigrationException(String message, Throwable cause) { 30 | super(message, cause); 31 | } 32 | 33 | public MigrationException(Throwable cause) { 34 | super(cause); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/MigrationLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | import java.io.Reader; 19 | import java.util.List; 20 | 21 | public interface MigrationLoader { 22 | 23 | /** 24 | * @return A list of migrations (bootstrap should NOT be included). 25 | */ 26 | List getMigrations(); 27 | 28 | /** 29 | * @param change 30 | * identifies the migration to read. 31 | * @param undo 32 | * whether the caller requests UNDO SQL script or not. 33 | * 34 | * @return A {@link Reader} of the specified SQL script. 35 | */ 36 | Reader getScriptReader(Change change, boolean undo); 37 | 38 | /** 39 | * @return A {@link Reader} of the bootstrap SQL script. 40 | */ 41 | Reader getBootstrapReader(); 42 | 43 | /** 44 | * @return A {@link Reader} of the onabort SQL script. 45 | */ 46 | Reader getOnAbortReader(); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/MigrationScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | import java.math.BigDecimal; 19 | 20 | public interface MigrationScript { 21 | /** 22 | * @return ID of this migration script.
23 | * Newer script should have a larger ID number. 24 | */ 25 | BigDecimal getId(); 26 | 27 | /** 28 | * @return Short description of this migration script. 29 | */ 30 | String getDescription(); 31 | 32 | /** 33 | * @return SQL statement(s) executed at runtime schema upgrade. 34 | */ 35 | String getUpScript(); 36 | 37 | String getDownScript(); 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/Migrator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | public class Migrator { 19 | public static void main(String[] args) { 20 | new CommandLine(args).execute(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/OnAbortScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | public interface OnAbortScript extends SimpleScript { 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/SimpleScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | 18 | public interface SimpleScript { 19 | String getScript(); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/BootstrapCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import org.apache.ibatis.migration.operations.BootstrapOperation; 19 | import org.apache.ibatis.migration.options.SelectedOptions; 20 | 21 | public final class BootstrapCommand extends BaseCommand { 22 | public BootstrapCommand(SelectedOptions options) { 23 | super(options); 24 | } 25 | 26 | @Override 27 | public void execute(String... params) { 28 | BootstrapOperation operation = new BootstrapOperation(options.isForce()); 29 | operation.operate(getConnectionProvider(), getMigrationLoader(), getDatabaseOperationOption(), printStream); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/Command.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | public interface Command { 19 | void execute(String... params); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/Commands.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import org.apache.ibatis.migration.MigrationException; 19 | import org.apache.ibatis.migration.options.SelectedOptions; 20 | 21 | public enum Commands { 22 | INFO, 23 | 24 | INIT, 25 | 26 | BOOTSTRAP, 27 | 28 | NEW, 29 | 30 | UP, 31 | 32 | DOWN, 33 | 34 | PENDING, 35 | 36 | SCRIPT, 37 | 38 | VERSION, 39 | 40 | STATUS, 41 | 42 | REDO; 43 | 44 | public static Command resolveCommand(String commandString, SelectedOptions selectedOptions) { 45 | final String upperCasedStr = commandString.toUpperCase(); 46 | for (Commands command : values()) { 47 | if (command.name().startsWith(upperCasedStr)) { 48 | return createCommand(command, selectedOptions); 49 | } 50 | } 51 | 52 | throw new MigrationException("Attempt to execute unknown command: " + commandString); 53 | } 54 | 55 | private static Command createCommand(Commands aResolvedCommand, SelectedOptions selectedOptions) { 56 | switch (aResolvedCommand) { 57 | case INFO: 58 | return new InfoCommand(System.out); 59 | case INIT: 60 | return new InitializeCommand(selectedOptions); 61 | case BOOTSTRAP: 62 | return new BootstrapCommand(selectedOptions); 63 | case NEW: 64 | return new NewCommand(selectedOptions); 65 | case UP: 66 | return new UpCommand(selectedOptions); 67 | case DOWN: 68 | return new DownCommand(selectedOptions); 69 | case PENDING: 70 | return new PendingCommand(selectedOptions); 71 | case SCRIPT: 72 | return new ScriptCommand(selectedOptions); 73 | case VERSION: 74 | return new VersionCommand(selectedOptions); 75 | case STATUS: 76 | return new StatusCommand(selectedOptions); 77 | case REDO: 78 | return new RedoCommand(selectedOptions); 79 | default: 80 | return params -> System.out.println("unknown command"); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/DownCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import org.apache.ibatis.migration.operations.DownOperation; 19 | import org.apache.ibatis.migration.options.SelectedOptions; 20 | 21 | public final class DownCommand extends BaseCommand { 22 | public DownCommand(SelectedOptions options) { 23 | super(options); 24 | } 25 | 26 | @Override 27 | public void execute(String... params) { 28 | DownOperation operation = new DownOperation(getStepCountParameter(1, params)); 29 | operation.operate(getConnectionProvider(), getMigrationLoader(), getDatabaseOperationOption(), printStream, 30 | createDownHook()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/InfoCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import java.io.File; 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | import java.io.PrintStream; 22 | import java.nio.charset.Charset; 23 | import java.util.Locale; 24 | import java.util.Properties; 25 | 26 | public final class InfoCommand implements Command { 27 | private final PrintStream out; 28 | 29 | public InfoCommand(PrintStream out) { 30 | this.out = out; 31 | } 32 | 33 | @Override 34 | public void execute(String... params) { 35 | Properties properties = new Properties(); 36 | try (InputStream is = getClass().getResourceAsStream("/mybatis-migrations.properties")) { 37 | if (is != null) { 38 | properties.load(is); 39 | } 40 | } catch (IOException e) { 41 | // ignore 42 | } 43 | 44 | out.printf("%s %s (%s)%n", properties.getProperty("name"), properties.getProperty("version"), 45 | properties.getProperty("build")); 46 | out.printf("Java version: %s, vendor: %s%n", System.getProperty("java.version"), System.getProperty("java.vendor")); 47 | out.printf("Java home: %s%n", System.getProperty("java.home")); 48 | out.printf("Default locale: %s, platform encoding: %s%n", Locale.getDefault().toLanguageTag(), 49 | Charset.defaultCharset().name()); 50 | out.printf("OS name: \"%s\", version: \"%s\", arch: \"%s\", family: \"%s\"%n", System.getProperty("os.name"), 51 | System.getProperty("os.version"), System.getProperty("os.arch"), getOsFamily()); 52 | } 53 | 54 | private static String getOsFamily() { 55 | String osName = System.getProperty("os.name").toLowerCase(); 56 | String pathSep = File.pathSeparator; 57 | 58 | if (osName.indexOf("windows") != -1) { 59 | return "windows"; 60 | } 61 | 62 | if (osName.indexOf("os/2") != -1) { 63 | return "os/2"; 64 | } 65 | 66 | if (osName.indexOf("z/os") != -1 || osName.indexOf("os/390") != -1) { 67 | return "z/os"; 68 | } 69 | 70 | if (osName.indexOf("os/400") != -1) { 71 | return "os/400"; 72 | } 73 | 74 | if (pathSep.equals(";")) { 75 | return "dos"; 76 | } 77 | 78 | if (osName.indexOf("mac") != -1) { 79 | if (osName.endsWith("x")) { 80 | return "mac"; // MACOSX 81 | } 82 | return "unix"; 83 | } 84 | 85 | if (osName.indexOf("nonstop_kernel") != -1) { 86 | return "tandem"; 87 | } 88 | 89 | if (osName.indexOf("openvms") != -1) { 90 | return "openvms"; 91 | } 92 | 93 | if (pathSep.equals(":")) { 94 | return "unix"; 95 | } 96 | 97 | return "undefined"; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/InitializeCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import java.io.File; 19 | import java.util.Properties; 20 | 21 | import org.apache.ibatis.migration.MigrationException; 22 | import org.apache.ibatis.migration.options.SelectedOptions; 23 | import org.apache.ibatis.migration.utils.Util; 24 | 25 | public final class InitializeCommand extends BaseCommand { 26 | public InitializeCommand(SelectedOptions selectedOptions) { 27 | super(selectedOptions); 28 | } 29 | 30 | @Override 31 | public void execute(String... params) { 32 | final File basePath = paths.getBasePath(); 33 | final File scriptPath = paths.getScriptPath(); 34 | 35 | printStream.println("Initializing: " + basePath); 36 | 37 | createDirectoryIfNecessary(basePath); 38 | ensureDirectoryIsEmpty(basePath); 39 | 40 | createDirectoryIfNecessary(paths.getEnvPath()); 41 | createDirectoryIfNecessary(scriptPath); 42 | createDirectoryIfNecessary(paths.getDriverPath()); 43 | 44 | copyResourceTo("org/apache/ibatis/migration/template_README", Util.file(basePath, "README")); 45 | copyResourceTo("org/apache/ibatis/migration/template_environment.properties", environmentFile()); 46 | copyResourceTo("org/apache/ibatis/migration/template_bootstrap.sql", Util.file(scriptPath, "bootstrap.sql")); 47 | copyResourceTo("org/apache/ibatis/migration/template_changelog.sql", 48 | Util.file(scriptPath, getNextIDAsString() + "_" + DESC_CREATE_CHANGELOG.replace(' ', '_') + ".sql")); 49 | 50 | Properties firstMigration = new Properties(); 51 | firstMigration.setProperty("description", "First migration."); 52 | copyResourceTo("org/apache/ibatis/migration/template_migration.sql", 53 | Util.file(scriptPath, getNextIDAsString() + "_first_migration.sql"), firstMigration); 54 | printStream.println("Done!"); 55 | printStream.println(); 56 | } 57 | 58 | protected void ensureDirectoryIsEmpty(File path) { 59 | String[] list = path.list(); 60 | if (list.length != 0) { 61 | for (String entry : list) { 62 | if (!entry.startsWith(".")) { 63 | throw new MigrationException("Directory must be empty (.svn etc allowed): " + path.getAbsolutePath()); 64 | } 65 | } 66 | } 67 | } 68 | 69 | protected void createDirectoryIfNecessary(File path) { 70 | if (!path.exists()) { 71 | printStream.println("Creating: " + path.getName()); 72 | if (!path.mkdirs()) { 73 | throw new MigrationException( 74 | "Could not create directory path for an unknown reason. Make sure you have access to the directory."); 75 | } 76 | } 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/NewCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import java.io.File; 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | import java.util.Properties; 22 | 23 | import org.apache.ibatis.migration.MigrationException; 24 | import org.apache.ibatis.migration.hook.MigrationHook; 25 | import org.apache.ibatis.migration.hook.NewHookContext; 26 | import org.apache.ibatis.migration.options.SelectedOptions; 27 | import org.apache.ibatis.migration.utils.Util; 28 | 29 | public final class NewCommand extends BaseCommand { 30 | 31 | private static final String CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY = "new_command.template"; 32 | 33 | public NewCommand(SelectedOptions options) { 34 | super(options); 35 | } 36 | 37 | @Override 38 | public void execute(String... params) { 39 | if (paramsEmpty(params)) { 40 | throw new MigrationException("No description specified for new migration."); 41 | } 42 | String description = params[0]; 43 | Properties variables = new Properties(); 44 | variables.setProperty("description", description); 45 | existingEnvironmentFile(); 46 | String filename = getNextIDAsString() + '_' + description.replace(' ', '_') + ".sql"; 47 | 48 | Map hookBindings = new HashMap<>(); 49 | MigrationHook hook = createNewHook(); 50 | if (hook != null) { 51 | hookBindings.put(MigrationHook.HOOK_CONTEXT, new NewHookContext(description, filename)); 52 | hook.before(hookBindings); 53 | } 54 | 55 | if (options.getTemplate() != null) { 56 | copyExternalResourceTo(options.getTemplate(), Util.file(paths.getScriptPath(), filename), variables); 57 | } else { 58 | String customConfiguredTemplate = Util.getPropertyOption(CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY); 59 | if (customConfiguredTemplate != null && !customConfiguredTemplate.isEmpty()) { 60 | copyExternalResourceTo(Util.migrationsHome() + File.separator + customConfiguredTemplate, 61 | Util.file(paths.getScriptPath(), filename), variables); 62 | } else { 63 | printStream 64 | .append("Your migrations configuration did not find your custom template. Using the default template."); 65 | copyDefaultTemplate(variables, filename); 66 | } 67 | if (hook != null) { 68 | hookBindings.put(MigrationHook.HOOK_CONTEXT, new NewHookContext(description, filename)); 69 | hook.after(hookBindings); 70 | } 71 | } 72 | printStream.println("Done!"); 73 | printStream.println(); 74 | } 75 | 76 | private void copyDefaultTemplate(Properties variables, String filename) { 77 | copyResourceTo("org/apache/ibatis/migration/template_migration.sql", Util.file(paths.getScriptPath(), filename), 78 | variables); 79 | } 80 | 81 | private MigrationHook createNewHook() { 82 | String before = environment().getHookBeforeNew(); 83 | String after = environment().getHookAfterNew(); 84 | if (before == null && after == null) { 85 | return null; 86 | } 87 | return createFileMigrationHook(before, null, null, after); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/PendingCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import org.apache.ibatis.migration.operations.PendingOperation; 19 | import org.apache.ibatis.migration.options.SelectedOptions; 20 | 21 | public final class PendingCommand extends BaseCommand { 22 | public PendingCommand(SelectedOptions options) { 23 | super(options); 24 | } 25 | 26 | @Override 27 | public void execute(String... params) { 28 | PendingOperation operation = new PendingOperation(); 29 | operation.operate(getConnectionProvider(), getMigrationLoader(), getDatabaseOperationOption(), printStream, 30 | createUpHook()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/RedoCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import org.apache.ibatis.migration.ConnectionProvider; 19 | import org.apache.ibatis.migration.MigrationLoader; 20 | import org.apache.ibatis.migration.operations.DownOperation; 21 | import org.apache.ibatis.migration.operations.UpOperation; 22 | import org.apache.ibatis.migration.options.DatabaseOperationOption; 23 | import org.apache.ibatis.migration.options.SelectedOptions; 24 | 25 | public final class RedoCommand extends BaseCommand { 26 | public RedoCommand(SelectedOptions options) { 27 | super(options); 28 | } 29 | 30 | @Override 31 | public void execute(String... params) { 32 | int steps = getStepCountParameter(1, params); 33 | ConnectionProvider connectionProvider = getConnectionProvider(); 34 | MigrationLoader migrationLoader = getMigrationLoader(); 35 | DatabaseOperationOption databaseOperationOption = getDatabaseOperationOption(); 36 | 37 | DownOperation downOperation = new DownOperation(steps); 38 | downOperation.operate(connectionProvider, migrationLoader, databaseOperationOption, printStream, createDownHook()); 39 | 40 | UpOperation upOperation = new UpOperation(steps); 41 | upOperation.operate(connectionProvider, migrationLoader, databaseOperationOption, printStream, createUpHook()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/StatusCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import org.apache.ibatis.migration.operations.StatusOperation; 19 | import org.apache.ibatis.migration.options.SelectedOptions; 20 | 21 | public final class StatusCommand extends BaseCommand { 22 | private StatusOperation operation; 23 | 24 | public StatusCommand(SelectedOptions options) { 25 | super(options); 26 | } 27 | 28 | @Override 29 | public void execute(String... params) { 30 | operation = new StatusOperation().operate(getConnectionProvider(), getMigrationLoader(), 31 | getDatabaseOperationOption(), printStream); 32 | } 33 | 34 | public StatusOperation getOperation() { 35 | return operation; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/UpCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import org.apache.ibatis.migration.operations.UpOperation; 19 | import org.apache.ibatis.migration.options.SelectedOptions; 20 | 21 | public final class UpCommand extends BaseCommand { 22 | private final boolean runOneStepOnly; 23 | 24 | public UpCommand(SelectedOptions options) { 25 | this(options, false); 26 | } 27 | 28 | public UpCommand(SelectedOptions options, boolean runOneStepOnly) { 29 | super(options); 30 | this.runOneStepOnly = runOneStepOnly; 31 | } 32 | 33 | @Override 34 | public void execute(String... params) { 35 | final int limit = getStepCountParameter(Integer.MAX_VALUE, params); 36 | UpOperation operation = new UpOperation(runOneStepOnly ? 1 : limit); 37 | operation.operate(getConnectionProvider(), getMigrationLoader(), getDatabaseOperationOption(), printStream, 38 | createUpHook()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/VersionCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | 18 | import java.math.BigDecimal; 19 | 20 | import org.apache.ibatis.migration.MigrationException; 21 | import org.apache.ibatis.migration.operations.VersionOperation; 22 | import org.apache.ibatis.migration.options.SelectedOptions; 23 | 24 | public final class VersionCommand extends BaseCommand { 25 | public VersionCommand(SelectedOptions options) { 26 | super(options); 27 | } 28 | 29 | @Override 30 | public void execute(String... params) { 31 | ensureParamsPassed(params); 32 | ensureNumericParam(params); 33 | 34 | VersionOperation operation = new VersionOperation(new BigDecimal(params[0])); 35 | operation.operate(getConnectionProvider(), getMigrationLoader(), getDatabaseOperationOption(), printStream, 36 | createUpHook(), createDownHook()); 37 | } 38 | 39 | private void ensureParamsPassed(String... params) { 40 | if (paramsEmpty(params)) { 41 | throw new MigrationException("No target version specified for migration."); 42 | } 43 | } 44 | 45 | private void ensureNumericParam(String... params) { 46 | try { 47 | new BigDecimal(params[0]); 48 | } catch (Exception e) { 49 | throw new MigrationException("The version number must be a numeric integer. " + e, e); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/commands/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.commands; 17 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/driver/DriverShim.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.driver; 17 | 18 | import java.sql.Connection; 19 | import java.sql.Driver; 20 | import java.sql.DriverPropertyInfo; 21 | import java.sql.SQLException; 22 | import java.sql.SQLFeatureNotSupportedException; 23 | import java.util.Properties; 24 | import java.util.logging.Logger; 25 | 26 | public class DriverShim implements Driver { 27 | private final Driver delegate; 28 | 29 | public DriverShim(Driver delegate) { 30 | this.delegate = delegate; 31 | } 32 | 33 | @Override 34 | public Connection connect(String url, Properties info) throws SQLException { 35 | return delegate.connect(url, info); 36 | } 37 | 38 | @Override 39 | public boolean acceptsURL(String url) throws SQLException { 40 | return delegate.acceptsURL(url); 41 | } 42 | 43 | @Override 44 | public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { 45 | return delegate.getPropertyInfo(url, info); 46 | } 47 | 48 | @Override 49 | public int getMajorVersion() { 50 | return delegate.getMajorVersion(); 51 | } 52 | 53 | @Override 54 | public int getMinorVersion() { 55 | return delegate.getMinorVersion(); 56 | } 57 | 58 | @Override 59 | public boolean jdbcCompliant() { 60 | return delegate.jdbcCompliant(); 61 | } 62 | 63 | @Override 64 | public Logger getParentLogger() throws SQLFeatureNotSupportedException { 65 | return delegate.getParentLogger(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/driver/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.driver; 17 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/FileHookScriptFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2025 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | import java.io.File; 19 | import java.io.PrintStream; 20 | import java.util.Arrays; 21 | import java.util.Properties; 22 | 23 | import org.apache.ibatis.migration.Environment; 24 | import org.apache.ibatis.migration.MigrationException; 25 | import org.apache.ibatis.migration.options.SelectedPaths; 26 | 27 | public class FileHookScriptFactory implements HookScriptFactory { 28 | 29 | protected final SelectedPaths paths; 30 | protected final Environment environment; 31 | protected final PrintStream printStream; 32 | 33 | public FileHookScriptFactory(SelectedPaths paths, Environment environment, PrintStream printStream) { 34 | this.paths = paths; 35 | this.environment = environment; 36 | this.printStream = printStream; 37 | } 38 | 39 | @Override 40 | public HookScript create(String hookSetting) { 41 | if (hookSetting == null) { 42 | return null; 43 | } 44 | File hooksDir = paths.getHookPath(); 45 | if (hooksDir == null) { 46 | throw new MigrationException("Hooks directory must not be null."); 47 | } 48 | if (!hooksDir.exists()) { 49 | throw new MigrationException("Hooks directory not found : " + hooksDir.getAbsolutePath()); 50 | } 51 | String[] segments = hookSetting.split(":"); 52 | if (segments.length < 2) { 53 | throw new MigrationException( 54 | "Error creating a HookScript. Hook setting must contain 'language' and 'file name' separated by ':' (e.g. SQL:post-up.sql)."); 55 | } 56 | String charset = environment.getScriptCharset(); 57 | Properties variables = environment.getVariables(); 58 | // First segment is language 59 | String scriptLang = segments[0]; 60 | // Second segment is file 61 | File scriptFile = hooksDir.toPath().resolve(segments[1]).toFile(); 62 | // The rest are script dependent options 63 | String[] hookOptions = Arrays.copyOfRange(segments, 2, segments.length); 64 | if (!scriptFile.exists()) { 65 | throw new MigrationException("Hook script not found : " + scriptFile.getAbsolutePath()); 66 | } 67 | if ("sql".equalsIgnoreCase(scriptLang)) { 68 | return new SqlHookScript(scriptFile, charset, hookOptions, variables, printStream); 69 | } 70 | // Assuming it's JSR-223. 71 | return new Jsr223HookScript(scriptLang, scriptFile, charset, hookOptions, paths, variables, printStream); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | import java.util.Map; 19 | 20 | public class FileMigrationHook implements MigrationHook { 21 | 22 | protected final HookScript beforeScript; 23 | protected final HookScript beforeEachScript; 24 | protected final HookScript afterEachScript; 25 | protected final HookScript afterScript; 26 | 27 | public FileMigrationHook(HookScript beforeScript, HookScript beforeEachScript, HookScript afterEachScript, 28 | HookScript afterScript) { 29 | this.beforeScript = beforeScript; 30 | this.beforeEachScript = beforeEachScript; 31 | this.afterEachScript = afterEachScript; 32 | this.afterScript = afterScript; 33 | } 34 | 35 | @Override 36 | public void before(Map bindingMap) { 37 | if (beforeScript != null) { 38 | beforeScript.execute(bindingMap); 39 | } 40 | } 41 | 42 | @Override 43 | public void beforeEach(Map bindingMap) { 44 | if (beforeEachScript != null) { 45 | beforeEachScript.execute(bindingMap); 46 | } 47 | } 48 | 49 | @Override 50 | public void afterEach(Map bindingMap) { 51 | if (afterEachScript != null) { 52 | afterEachScript.execute(bindingMap); 53 | } 54 | } 55 | 56 | @Override 57 | public void after(Map bindingMap) { 58 | if (afterScript != null) { 59 | afterScript.execute(bindingMap); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/HookContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | import java.io.Reader; 19 | import java.io.StringReader; 20 | import java.sql.Connection; 21 | import java.sql.SQLException; 22 | 23 | import org.apache.ibatis.migration.Change; 24 | import org.apache.ibatis.migration.ConnectionProvider; 25 | import org.apache.ibatis.migration.operations.ScriptRunner; 26 | 27 | public class HookContext { 28 | private ConnectionProvider connectionProvider; 29 | private ScriptRunner scriptRunner; 30 | private Change change; 31 | 32 | public HookContext(ConnectionProvider connectionProvider, ScriptRunner scriptRunner, Change change) { 33 | this.connectionProvider = connectionProvider; 34 | this.scriptRunner = scriptRunner; 35 | this.change = change; 36 | } 37 | 38 | /** 39 | * @return A new {@link Connection} to the database. The returned connection must be closed. 40 | * 41 | * @throws SQLException 42 | * If a database access error occurs. 43 | */ 44 | public Connection getConnection() throws SQLException { 45 | return connectionProvider.getConnection(); 46 | } 47 | 48 | /** 49 | * @param reader 50 | * Source of the SQL to execute. 51 | */ 52 | public void executeSql(Reader reader) { 53 | scriptRunner.runScript(reader); 54 | } 55 | 56 | /** 57 | * @param sql 58 | * SQL to execute. 59 | */ 60 | public void executeSql(String sql) { 61 | try (StringReader reader = new StringReader(sql)) { 62 | executeSql(reader); 63 | } 64 | } 65 | 66 | /** 67 | * @return Returns an instance of {@link Change} object for an each hook; null otherwise. 68 | */ 69 | public Change getChange() { 70 | return change; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/HookScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | import java.util.Map; 19 | 20 | public interface HookScript { 21 | 22 | void execute(Map bindingMap); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/HookScriptFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | public interface HookScriptFactory { 19 | 20 | HookScript create(String hookSetting); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/MigrationHook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | import java.util.Map; 19 | 20 | public interface MigrationHook { 21 | 22 | String HOOK_CONTEXT = "hookContext"; 23 | 24 | void before(Map bindingMap); 25 | 26 | void beforeEach(Map bindingMap); 27 | 28 | void afterEach(Map bindingMap); 29 | 30 | void after(Map bindingMap); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/NewHookContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | /** 19 | * Hook context object that is available to before_new and after_new hooks. 20 | */ 21 | public class NewHookContext { 22 | private String description; 23 | private String filename; 24 | 25 | public NewHookContext(String description, String filename) { 26 | this.description = description; 27 | this.filename = filename; 28 | } 29 | 30 | /** 31 | * @return The specified description. 32 | */ 33 | public String getDescription() { 34 | return description; 35 | } 36 | 37 | /** 38 | * @return The name of the file that is created by new command.
39 | * In before_new hook, the file is not created yet. 40 | */ 41 | public String getFilename() { 42 | return filename; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/ScriptHookContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | import org.apache.ibatis.migration.Change; 19 | 20 | /** 21 | * Hook context object that is available to before_new and after_new hooks. 22 | */ 23 | public class ScriptHookContext { 24 | private final Change change; 25 | private final boolean undo; 26 | 27 | public ScriptHookContext(Change change, boolean undo) { 28 | this.change = change; 29 | this.undo = undo; 30 | } 31 | 32 | public Change getChange() { 33 | return change; 34 | } 35 | 36 | public boolean isUndo() { 37 | return undo; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/SqlHookScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2025 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | 18 | import java.io.ByteArrayOutputStream; 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | import java.io.PrintStream; 23 | import java.io.StringReader; 24 | import java.nio.charset.Charset; 25 | import java.nio.file.Files; 26 | import java.util.Map; 27 | import java.util.Properties; 28 | 29 | import org.apache.ibatis.migration.MigrationException; 30 | import org.apache.ibatis.migration.VariableReplacer; 31 | import org.apache.ibatis.migration.utils.Util; 32 | 33 | public class SqlHookScript implements HookScript { 34 | 35 | protected final File scriptFile; 36 | protected final String charset; 37 | protected final Properties variables; 38 | protected final PrintStream printStream; 39 | protected final VariableReplacer replacer; 40 | 41 | public SqlHookScript(File scriptFile, String charset, String[] options, Properties variables, 42 | PrintStream printStream) { 43 | this.scriptFile = scriptFile; 44 | this.charset = charset; 45 | this.variables = variables; 46 | this.printStream = printStream; 47 | // options can be local variables in key=value format. 48 | for (String option : options) { 49 | int sep = option.indexOf('='); 50 | if (sep > -1) { 51 | this.variables.put(option.substring(0, sep), option.substring(sep + 1)); 52 | } 53 | } 54 | replacer = new VariableReplacer(this.variables); 55 | } 56 | 57 | @Override 58 | public void execute(Map bindingMap) { 59 | HookContext context = (HookContext) bindingMap.get(MigrationHook.HOOK_CONTEXT); 60 | printStream.println(Util.horizontalLine("Applying SQL hook: " + scriptFile.getName(), 80)); 61 | 62 | try (InputStream inputStream = Files.newInputStream(scriptFile.toPath()); 63 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { 64 | byte[] buffer = new byte[1024]; 65 | int length; 66 | while ((length = inputStream.read(buffer)) != -1) { 67 | outputStream.write(buffer, 0, length); 68 | } 69 | try (StringReader reader = new StringReader(replacer.replace(outputStream.toString(Charset.forName(charset))))) { 70 | context.executeSql(reader); 71 | } 72 | } catch (IOException e) { 73 | throw new MigrationException("Error occurred while running SQL hook script.", e); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/hook/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.hook; 17 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/io/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.io; 17 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.operations; 17 | 18 | import java.io.PrintStream; 19 | import java.io.Reader; 20 | import java.sql.Connection; 21 | 22 | import org.apache.ibatis.migration.ConnectionProvider; 23 | import org.apache.ibatis.migration.MigrationException; 24 | import org.apache.ibatis.migration.MigrationLoader; 25 | import org.apache.ibatis.migration.options.DatabaseOperationOption; 26 | import org.apache.ibatis.migration.utils.Util; 27 | 28 | public final class BootstrapOperation extends DatabaseOperation { 29 | private final boolean force; 30 | 31 | public BootstrapOperation() { 32 | this(false); 33 | } 34 | 35 | public BootstrapOperation(boolean force) { 36 | this.force = force; 37 | } 38 | 39 | public BootstrapOperation operate(ConnectionProvider connectionProvider, MigrationLoader migrationsLoader, 40 | DatabaseOperationOption option, PrintStream printStream) { 41 | try (Connection con = connectionProvider.getConnection()) { 42 | if (option == null) { 43 | option = new DatabaseOperationOption(); 44 | } 45 | if (changelogExists(con, option) && !force) { 46 | println(printStream, 47 | "For your safety, the bootstrap SQL script will only run before migrations are applied (i.e. before the changelog exists). If you're certain, you can run it using the --force option."); 48 | } else { 49 | try (Reader bootstrapReader = migrationsLoader.getBootstrapReader()) { 50 | if (bootstrapReader != null) { 51 | println(printStream, Util.horizontalLine("Applying: bootstrap.sql", 80)); 52 | ScriptRunner runner = getScriptRunner(con, option, printStream); 53 | runner.runScript(bootstrapReader); 54 | println(printStream); 55 | } else { 56 | println(printStream, "Error, could not run bootstrap.sql. The file does not exist."); 57 | } 58 | } 59 | } 60 | return this; 61 | } catch (Exception e) { 62 | throw new MigrationException("Error running bootstrapper. Cause: " + e, e); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/operations/ChangelogOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.operations; 17 | 18 | import java.math.BigDecimal; 19 | import java.sql.Connection; 20 | import java.sql.PreparedStatement; 21 | import java.sql.ResultSet; 22 | import java.sql.SQLException; 23 | import java.sql.Statement; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | import org.apache.ibatis.migration.Change; 28 | import org.apache.ibatis.migration.options.DatabaseOperationOption; 29 | 30 | public class ChangelogOperation { 31 | private final Connection con; 32 | private final DatabaseOperationOption option; 33 | 34 | public ChangelogOperation(Connection con, DatabaseOperationOption option) { 35 | this.con = con; 36 | this.option = option; 37 | } 38 | 39 | public boolean tableExists() { 40 | try (Statement stmt = con.createStatement(); 41 | ResultSet rs = stmt.executeQuery("select count(1) from " + option.getChangelogTable())) { 42 | return rs.next(); 43 | } catch (SQLException e) { 44 | return false; 45 | } 46 | } 47 | 48 | public List selectAll() throws SQLException { 49 | List changes = new ArrayList<>(); 50 | try ( 51 | PreparedStatement stmt = con 52 | .prepareStatement("select ID, APPLIED_AT, DESCRIPTION from " + option.getChangelogTable() + " order by ID"); 53 | ResultSet rs = stmt.executeQuery()) { 54 | while (rs.next()) { 55 | changes.add(new Change(rs.getBigDecimal(1), rs.getString(2), rs.getString(3))); 56 | } 57 | return changes; 58 | } 59 | } 60 | 61 | public void insert(Change change) throws SQLException { 62 | try (PreparedStatement stmt = con.prepareStatement( 63 | "insert into " + option.getChangelogTable() + " (ID, APPLIED_AT, DESCRIPTION) values (?,?,?)")) { 64 | stmt.setBigDecimal(1, change.getId()); 65 | stmt.setString(2, change.getAppliedTimestamp()); 66 | stmt.setString(3, change.getDescription()); 67 | stmt.execute(); 68 | con.commit(); 69 | } 70 | } 71 | 72 | public void deleteById(BigDecimal id) throws SQLException { 73 | try (PreparedStatement stmt = con.prepareStatement("delete from " + option.getChangelogTable() + " where ID = ?")) { 74 | stmt.setBigDecimal(1, id); 75 | stmt.execute(); 76 | con.commit(); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/operations/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.operations; 17 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.options; 17 | 18 | public class DatabaseOperationOption { 19 | private static final String DEFAULT_CHANGELOG_TABLE = "CHANGELOG"; 20 | 21 | private static final String DEFAULT_DELIMITER = ";"; 22 | 23 | private String changelogTable; 24 | 25 | private boolean stopOnError = true; 26 | 27 | private boolean throwWarning = true; 28 | 29 | private boolean autoCommit; 30 | 31 | private boolean sendFullScript; 32 | 33 | private boolean removeCRs; 34 | 35 | private boolean escapeProcessing = true; 36 | 37 | private boolean fullLineDelimiter = false; 38 | 39 | private String delimiter; 40 | 41 | public String getChangelogTable() { 42 | return changelogTable == null ? DEFAULT_CHANGELOG_TABLE : changelogTable; 43 | } 44 | 45 | public void setChangelogTable(String changelogTable) { 46 | this.changelogTable = changelogTable; 47 | } 48 | 49 | public boolean isStopOnError() { 50 | return stopOnError; 51 | } 52 | 53 | public void setStopOnError(boolean stopOnError) { 54 | this.stopOnError = stopOnError; 55 | } 56 | 57 | public boolean isThrowWarning() { 58 | return throwWarning; 59 | } 60 | 61 | public void setThrowWarning(boolean throwWarning) { 62 | this.throwWarning = throwWarning; 63 | } 64 | 65 | public boolean isAutoCommit() { 66 | return autoCommit; 67 | } 68 | 69 | public void setAutoCommit(boolean autoCommit) { 70 | this.autoCommit = autoCommit; 71 | } 72 | 73 | public boolean isSendFullScript() { 74 | return sendFullScript; 75 | } 76 | 77 | public void setSendFullScript(boolean sendFullScript) { 78 | this.sendFullScript = sendFullScript; 79 | } 80 | 81 | public boolean isRemoveCRs() { 82 | return removeCRs; 83 | } 84 | 85 | public void setRemoveCRs(boolean removeCRs) { 86 | this.removeCRs = removeCRs; 87 | } 88 | 89 | public boolean isEscapeProcessing() { 90 | return escapeProcessing; 91 | } 92 | 93 | public void setEscapeProcessing(boolean escapeProcessing) { 94 | this.escapeProcessing = escapeProcessing; 95 | } 96 | 97 | public boolean isFullLineDelimiter() { 98 | return fullLineDelimiter; 99 | } 100 | 101 | public void setFullLineDelimiter(boolean fullLineDelimiter) { 102 | this.fullLineDelimiter = fullLineDelimiter; 103 | } 104 | 105 | public String getDelimiter() { 106 | return delimiter == null ? DEFAULT_DELIMITER : delimiter; 107 | } 108 | 109 | public void setDelimiter(String delimiter) { 110 | this.delimiter = delimiter; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/options/Options.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.options; 17 | 18 | public enum Options { 19 | PATH, 20 | 21 | ENVPATH, 22 | 23 | SCRIPTPATH, 24 | 25 | DRIVERPATH, 26 | 27 | HOOKPATH, 28 | 29 | ENV, 30 | 31 | FORCE, 32 | 33 | TRACE, 34 | 35 | HELP, 36 | 37 | TEMPLATE, 38 | 39 | IDPATTERN, 40 | 41 | QUIET, 42 | 43 | COLOR 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/options/OptionsParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2025 the original author or authors. 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 | * https://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.apache.ibatis.migration.options; 17 | 18 | import static org.apache.ibatis.migration.utils.Util.isOption; 19 | 20 | import java.nio.file.Path; 21 | 22 | public enum OptionsParser { 23 | ; 24 | 25 | public static SelectedOptions parse(String[] args) { 26 | final SelectedOptions selectedOptions = new SelectedOptions(); 27 | 28 | for (String arg : args) { 29 | final boolean isOption = isOption(arg); 30 | if (isOption) { 31 | parseOptions(arg, selectedOptions); 32 | } else { 33 | setCommandOrAppendParams(arg, selectedOptions); 34 | } 35 | } 36 | 37 | return selectedOptions; 38 | } 39 | 40 | private static void setCommandOrAppendParams(String arg, SelectedOptions options) { 41 | if (options.getCommand() == null) { 42 | options.setCommand(arg); 43 | } else { 44 | final String myParams = options.getParams() == null ? arg : options.getParams() + " " + arg; 45 | options.setParams(myParams); 46 | } 47 | } 48 | 49 | private static boolean parseOptions(String arg, SelectedOptions options) { 50 | final boolean isOption = isOption(arg); 51 | 52 | if (isOption) { 53 | final String[] argParts = arg.substring(2).split("="); 54 | final Options option = Options.valueOf(argParts[0].toUpperCase()); 55 | 56 | switch (option) { 57 | case PATH: 58 | options.getPaths().setBasePath(Path.of(argParts[1]).toFile()); 59 | break; 60 | case ENVPATH: 61 | options.getPaths().setEnvPath(Path.of(argParts[1]).toFile()); 62 | break; 63 | case SCRIPTPATH: 64 | options.getPaths().setScriptPath(Path.of(argParts[1]).toFile()); 65 | break; 66 | case DRIVERPATH: 67 | options.getPaths().setDriverPath(Path.of(argParts[1]).toFile()); 68 | break; 69 | case HOOKPATH: 70 | options.getPaths().setHookPath(Path.of(argParts[1]).toFile()); 71 | break; 72 | case ENV: 73 | options.setEnvironment(argParts[1]); 74 | break; 75 | case FORCE: 76 | options.setForce(true); 77 | break; 78 | case TRACE: 79 | options.setTrace(true); 80 | break; 81 | case HELP: 82 | options.setHelp(true); 83 | break; 84 | case TEMPLATE: 85 | options.setTemplate(argParts[1]); 86 | break; 87 | case IDPATTERN: 88 | options.setIdPattern(argParts[1]); 89 | break; 90 | case QUIET: 91 | options.setQuiet(true); 92 | break; 93 | case COLOR: 94 | options.setColor(true); 95 | break; 96 | } 97 | } 98 | 99 | return isOption; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/options/SelectedOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.options; 17 | 18 | public class SelectedOptions { 19 | private SelectedPaths paths = new SelectedPaths(); 20 | private String environment = "development"; 21 | private String template; 22 | private String idPattern; 23 | private boolean force; 24 | private boolean trace; 25 | private String command; 26 | private String params; 27 | private boolean help; 28 | private boolean quiet; 29 | private boolean color; 30 | 31 | public boolean isQuiet() { 32 | return quiet; 33 | } 34 | 35 | public boolean hasColor() { 36 | return color; 37 | } 38 | 39 | public void setColor(boolean color) { 40 | this.color = color; 41 | } 42 | 43 | public void setQuiet(boolean quiet) { 44 | this.quiet = quiet; 45 | } 46 | 47 | public SelectedPaths getPaths() { 48 | return paths; 49 | } 50 | 51 | public String getEnvironment() { 52 | return environment; 53 | } 54 | 55 | public void setEnvironment(String aEnvironment) { 56 | environment = aEnvironment; 57 | } 58 | 59 | public String getTemplate() { 60 | return template; 61 | } 62 | 63 | public void setTemplate(String aTemplate) { 64 | template = aTemplate; 65 | } 66 | 67 | public String getIdPattern() { 68 | return idPattern; 69 | } 70 | 71 | public void setIdPattern(String idPattern) { 72 | this.idPattern = idPattern; 73 | } 74 | 75 | public boolean isForce() { 76 | return force; 77 | } 78 | 79 | public void setForce(boolean aForce) { 80 | force = aForce; 81 | } 82 | 83 | public boolean isTrace() { 84 | return trace; 85 | } 86 | 87 | public void setTrace(boolean aTrace) { 88 | trace = aTrace; 89 | } 90 | 91 | public String getCommand() { 92 | return command; 93 | } 94 | 95 | public void setCommand(String aCommand) { 96 | command = aCommand; 97 | } 98 | 99 | public String getParams() { 100 | return params; 101 | } 102 | 103 | public void setParams(String aParams) { 104 | params = aParams; 105 | } 106 | 107 | public boolean needsHelp() { 108 | return help; 109 | } 110 | 111 | public void setHelp(boolean aHelp) { 112 | help = aHelp; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/options/SelectedPaths.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2025 the original author or authors. 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 | * https://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.apache.ibatis.migration.options; 17 | 18 | import static org.apache.ibatis.migration.utils.Util.file; 19 | 20 | import java.io.File; 21 | import java.nio.file.Path; 22 | 23 | public class SelectedPaths { 24 | private File basePath = Path.of("./").toFile(); 25 | private File envPath; 26 | private File scriptPath; 27 | private File driverPath; 28 | private File hookPath; 29 | 30 | public File getBasePath() { 31 | return basePath; 32 | } 33 | 34 | public File getEnvPath() { 35 | return envPath == null ? file(basePath, "./environments") : envPath; 36 | } 37 | 38 | public File getScriptPath() { 39 | return scriptPath == null ? file(basePath, "./scripts") : scriptPath; 40 | } 41 | 42 | public File getDriverPath() { 43 | return driverPath == null ? file(basePath, "./drivers") : driverPath; 44 | } 45 | 46 | public File getHookPath() { 47 | return hookPath == null ? file(basePath, "./hooks") : hookPath; 48 | } 49 | 50 | public void setBasePath(File aBasePath) { 51 | basePath = aBasePath; 52 | } 53 | 54 | public void setEnvPath(File aEnvPath) { 55 | envPath = aEnvPath; 56 | } 57 | 58 | public void setScriptPath(File aScriptPath) { 59 | scriptPath = aScriptPath; 60 | } 61 | 62 | public void setDriverPath(File aDriverPath) { 63 | driverPath = aDriverPath; 64 | } 65 | 66 | public void setHookPath(File aHookPath) { 67 | hookPath = aHookPath; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/options/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.options; 17 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration; 17 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/utils/Util.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2025 the original author or authors. 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 | * https://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.apache.ibatis.migration.utils; 17 | 18 | import java.io.File; 19 | import java.io.InputStream; 20 | import java.nio.file.Files; 21 | import java.nio.file.Path; 22 | import java.util.Properties; 23 | 24 | public enum Util { 25 | ; 26 | 27 | private static final String MIGRATIONS_HOME = "MIGRATIONS_HOME"; 28 | 29 | /* TODO: remove in the next major release */ 30 | private static final String MIGRATIONS_HOME_PROPERTY_DEPRECATED = "migrationHome"; 31 | 32 | private static final String MIGRATIONS_HOME_PROPERTY = "migrationsHome"; 33 | 34 | private static final String MIGRATIONS_PROPERTIES = "migration.properties"; 35 | 36 | public static String migrationsHome() { 37 | String migrationsHome = System.getenv(MIGRATIONS_HOME); 38 | // Check if there is a system property 39 | if (migrationsHome == null) { 40 | migrationsHome = System.getProperty(MIGRATIONS_HOME_PROPERTY); 41 | if (migrationsHome == null) { 42 | migrationsHome = System.getProperty(MIGRATIONS_HOME_PROPERTY_DEPRECATED); 43 | } 44 | } 45 | return migrationsHome; 46 | } 47 | 48 | public static boolean getPropertyOptionAsBoolean(String key) { 49 | return Boolean.parseBoolean(getPropertyOption(key)); 50 | } 51 | 52 | /** 53 | * @param key 54 | * of the property. 55 | * 56 | * @return The value null if the property file does not exist or the key does not exist. 57 | */ 58 | public static String getPropertyOption(String key) { 59 | String migrationsHome = migrationsHome(); 60 | if (migrationsHome == null || migrationsHome.isEmpty()) { 61 | return null; 62 | } 63 | Properties properties = new Properties(); 64 | String path = migrationsHome + File.separator + MIGRATIONS_PROPERTIES; 65 | try (InputStream stream = Files.newInputStream(Path.of(path))) { 66 | properties.load(stream); 67 | return properties.getProperty(key); 68 | } catch (Exception e) { 69 | return null; 70 | } 71 | } 72 | 73 | public static boolean isOption(String arg) { 74 | return arg.startsWith("--") && !arg.trim().endsWith("="); 75 | } 76 | 77 | public static File file(File path, String fileName) { 78 | return Path.of(path.getAbsolutePath() + File.separator + fileName).toFile(); 79 | } 80 | 81 | public static String horizontalLine(String caption, int length) { 82 | StringBuilder builder = new StringBuilder(); 83 | builder.append("=========="); 84 | if (caption.length() > 0) { 85 | caption = " " + caption + " "; 86 | builder.append(caption); 87 | } 88 | for (int i = 0; i < length - caption.length() - 10; i++) { 89 | builder.append("="); 90 | } 91 | return builder.toString(); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/org/apache/ibatis/migration/utils/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.utils; 17 | -------------------------------------------------------------------------------- /src/main/resources/mybatis-migrations.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2022 the original author or authors. 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 | # https://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 | 17 | groupId=@project.groupId@ 18 | name=@project.name@ 19 | version=@project.version@ 20 | build=@java.version@ 21 | -------------------------------------------------------------------------------- /src/main/resources/org/apache/ibatis/migration/template_README: -------------------------------------------------------------------------------- 1 | Welcome! 2 | 3 | This is an MyBatis Migration repository. You can specify the repository 4 | directory when running migrations using the --path= 5 | option. The default path is the current working directory ("./"). 6 | 7 | The repository base directory contains three subdirectories as follows: 8 | 9 | ./drivers 10 | 11 | Place your JDBC driver .jar or .zip files in this directory. Upon running a 12 | migration, the drivers will be dynamically loaded. 13 | 14 | ./environments 15 | 16 | In the environments folder you will find .properties files that represent 17 | your database instances. By default a development.properties file is 18 | created for you to configure your development time database properties. 19 | You can also create test.properties and production.properties files. 20 | The environment can be specified when running a migration by using 21 | the --env= option (without the path or ".properties" part). 22 | 23 | The default environment is "development". 24 | 25 | ./scripts 26 | 27 | This directory contains your migration SQL files. These are the files 28 | that contain your DDL to both upgrade and downgrade your database 29 | structure. By default, the directory will contain the script to 30 | create the changelog table, plus one empty "first" migration script. 31 | To create a new migration script, use the "new" command. To run 32 | all pending migrations, use the "up" command. To undo the last 33 | migration applied, use the "down" command etc. 34 | 35 | For more information about commands and options, run the MyBatis 36 | Migration script with the --help option. 37 | 38 | Enjoy. 39 | -------------------------------------------------------------------------------- /src/main/resources/org/apache/ibatis/migration/template_bootstrap.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2023 the original author or authors. 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 | -- https://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 | 17 | -- // Bootstrap.sql 18 | 19 | -- This is the only SQL script file that is NOT 20 | -- a valid migration and will not be run or tracked 21 | -- in the changelog. There is no @UNDO section. 22 | 23 | -- // Do I need this file? 24 | 25 | -- New projects likely won't need this file. 26 | -- Existing projects will likely need this file. 27 | -- It's unlikely that this bootstrap should be run 28 | -- in the production environment. 29 | 30 | -- // Purpose 31 | 32 | -- The purpose of this file is to provide a facility 33 | -- to initialize the database to a state before MyBatis 34 | -- SQL migrations were applied. If you already have 35 | -- a database in production, then you probably have 36 | -- a script that you run on your developer machine 37 | -- to initialize the database. That script can now 38 | -- be put in this bootstrap file (but does not have 39 | -- to be if you are comfortable with your current process. 40 | 41 | -- // Running 42 | 43 | -- The bootstrap SQL is run with the "migrate bootstrap" 44 | -- command. It must be run manually, it's never run as 45 | -- part of the regular migration process and will never 46 | -- be undone. Variables (e.g. ${variable}) are still 47 | -- parsed in the bootstrap SQL. 48 | 49 | -- After the boostrap SQL has been run, you can then 50 | -- use the migrations and the changelog for all future 51 | -- database change management. 52 | 53 | -------------------------------------------------------------------------------- /src/main/resources/org/apache/ibatis/migration/template_changelog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2023 the original author or authors. 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 | -- https://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 | 17 | -- // Create Changelog 18 | 19 | -- Default DDL for changelog table that will keep 20 | -- a record of the migrations that have been run. 21 | 22 | -- You can modify this to suit your database before 23 | -- running your first migration. 24 | 25 | -- Be sure that ID and DESCRIPTION fields exist in 26 | -- BigInteger and String compatible fields respectively. 27 | 28 | CREATE TABLE ${changelog} ( 29 | ID NUMERIC(20,0) NOT NULL, 30 | APPLIED_AT VARCHAR(25) NOT NULL, 31 | DESCRIPTION VARCHAR(255) NOT NULL 32 | ); 33 | 34 | ALTER TABLE ${changelog} 35 | ADD CONSTRAINT PK_${changelog} 36 | PRIMARY KEY (id); 37 | 38 | -- //@UNDO 39 | 40 | DROP TABLE ${changelog}; 41 | -------------------------------------------------------------------------------- /src/main/resources/org/apache/ibatis/migration/template_environment.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2023 the original author or authors. 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 | # https://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 | 17 | ## Base time zone to ensure times are consistent across machines 18 | time_zone=GMT+0:00 19 | 20 | ## The character set that scripts are encoded with 21 | # script_char_set=UTF-8 22 | 23 | ## JDBC connection properties. 24 | driver= 25 | url= 26 | username= 27 | password= 28 | 29 | # 30 | # A NOTE ON STORED PROCEDURES AND DELIMITERS 31 | # 32 | # Stored procedures and functions commonly have nested delimiters 33 | # that conflict with the schema migration parsing. If you tend 34 | # to use procs, functions, triggers or anything that could create 35 | # this situation, then you may want to experiment with 36 | # send_full_script=true (preferred), or if you can't use 37 | # send_full_script, then you may have to resort to a full 38 | # line delimiter such as "GO" or "/" or "!RUN!". 39 | # 40 | # Also play with the autocommit settings, as some drivers 41 | # or databases don't support creating procs, functions or 42 | # even tables in a transaction, and others require it. 43 | # 44 | 45 | # This ignores the line delimiters and 46 | # simply sends the entire script at once. 47 | # Use with JDBC drivers that can accept large 48 | # blocks of delimited text at once. 49 | send_full_script=true 50 | 51 | # This controls how statements are delimited. 52 | # By default statements are delimited by an 53 | # end of line semicolon. Some databases may 54 | # (e.g. MS SQL Server) may require a full line 55 | # delimiter such as GO. 56 | # These are ignored if send_full_script is true. 57 | delimiter=; 58 | full_line_delimiter=false 59 | 60 | # If set to true, each statement is isolated 61 | # in its own transaction. Otherwise the entire 62 | # script is executed in one transaction. 63 | # Few databases should need this set to true, 64 | # but some do. 65 | auto_commit=false 66 | 67 | # If set to false, warnings from the database will interrupt migrations. 68 | ignore_warnings=true 69 | 70 | # Custom driver path to allow you to centralize your driver files 71 | # Default requires the drivers to be in the drivers directory of your 72 | # initialized migration directory (created with "migrate init") 73 | # driver_path= 74 | 75 | # Name of the table that tracks changes to the database 76 | changelog=CHANGELOG 77 | 78 | # Migrations support variable substitutions in the form of ${variable} 79 | # in the migration scripts. All of the above properties will be ignored though, 80 | # with the exception of changelog. 81 | # Example: The following would be referenced in a migration file as ${ip_address} 82 | # ip_address=192.168.0.1 83 | 84 | -------------------------------------------------------------------------------- /src/main/resources/org/apache/ibatis/migration/template_migration.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2023 the original author or authors. 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 | -- https://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 | 17 | -- // ${description} 18 | -- Migration SQL that makes the change goes here. 19 | 20 | 21 | 22 | -- //@UNDO 23 | -- SQL to undo the change goes here. 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/site/xdoc/index.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis Migrations | Introduction 24 | The MyBatis Team 25 | 26 | 27 | 28 |
29 |

Evolving databases has been one of the major challenges for software development. Often times, regardless of 30 | our software development methodology, the database follows a different change management process. Despite our best 31 | efforts, few tools and practices have been able to change that. The tools of the past have been GUI centric, 32 | proprietary for a particular database and/or carried a steep license cost. Yet, at the end of the day they suffered 33 | from the same challenges.

34 |

Recently, a few tools arrived and changed all of that. They did so by embracing simplicity and a few simple 35 | rules for database evolution to follow. A couple of good examples are Rails Migrations and dbdeploy. Both tools are 36 | similar in purpose, but quite different in implementation. The MyBatis Schema Migration System draws from both and 37 | seeks to be the best migration tool of its kind.

38 | 39 | 40 |

To achieve a good database change management practice, we need to identify a few key goals.

41 |

Thus, the MyBatis Schema Migration System (or MyBatis Migrations for short) seeks to:

42 |
    43 |
  • Work with any database, new or existing
  • 44 |
  • Leverage the source control system (e.g. Subversion)
  • 45 |
  • Enable concurrent developers or teams to work independently
  • 46 |
  • Allow conflicts very visible and easily manageable
  • 47 |
  • Allow for forward and backward migration (evolve, devolve respectively)
  • 48 |
  • Make the current status of the database easily accessible and comprehensible
  • 49 |
  • Enable migrations despite access privileges or bureaucracy
  • 50 |
  • Work with any methodology
  • 51 |
  • Encourages good, consistent practices
  • 52 |
53 |
54 | 55 | 56 |

To put it in short. Have a look at the video:

57 | 58 |
59 |
60 | 61 | 62 |
63 | -------------------------------------------------------------------------------- /src/site/xdoc/redo.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis Migrations | Migrate > redo 24 | The MyBatis Team 25 | 26 | 27 | 28 |
29 |

The redo command is a shortcut which internally executes a down command and up command. As it executes the same number of steps for both down and up operation, the final migration status does not change.
30 | The redo command is useful for testing undo part of scripts or re-applying recent migration scripts after edit.

31 | 32 |

The redo command takes an integer parameter which indicates the number of migrations to redo. No parameter means 1.

33 | 34 |

The redo command was added in version 3.3.10.

35 |
36 | 37 | 38 |
39 | -------------------------------------------------------------------------------- /src/site/xdoc/shortcuts.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis Migrations | Migrate > shortcuts 24 | The MyBatis Team 25 | 26 | 27 | 28 |
29 |

Any of the commands can be executed with only the first few (unambiguous) characters of their name.

30 | 31 |

For example, the status command can be shortened to:

32 | 33 | /home/cbegin/testdb$ migrate sta 34 | 35 |

Or even just:

36 | 37 | /home/cbegin/testdb$ migrate st 38 | 39 |

If you script migrations using unix shell scripts or Windows batch files, be sure to always use the full name 40 | for safety. That way, if the shortcut becomes ambiguous in the future, your script will still function.

41 |
42 | 43 | 44 |
45 | -------------------------------------------------------------------------------- /src/site/xdoc/status.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis Migrations | Migrate > status 24 | The MyBatis Team 25 | 26 | 27 | 28 |
29 |

The status command will report the current state of the database. 30 | The status command takes no parameters and operates on the current working directory or that specified by the 31 | --path option (as with all other commands).

32 | 33 | /home/cbegin/testdb$ migrate status 34 | ID Applied At Description 35 | ================================================================== 36 | 20090802210445 ...pending... create changelog 37 | 20090804225328 ...pending... create blog table 38 | 39 |

Since we’ve never run a migration, the status of all of the existing migration scripts is pending, 40 | including the changelog table itself, which is where more detailed status logs are kept. Once we run the up 41 | command (discussed next), the status will report something like the following:

42 | 43 | /home/cbegin/testdb$ migrate status 44 | ID Applied At Description 45 | ================================================================== 46 | 20090802210445 2009-08-04 22:51:16 create changelog 47 | 20090804225328 2009-08-04 22:51:16 create blog table 48 | 49 |

Thanks to our identifier format, things are in order, and we can see when a migration script was created, as 50 | well as when it was applied. The comment helps us read a high level overview of the evolution of this database. 51 | As we add migrations this status log will grow. For example:

52 | 53 | /home/cbegin/testdb$ migrate status 54 | ID Applied At Description 55 | ================================================================== 56 | 20090802210445 2009-08-04 22:51:16 create changelog 57 | 20090804225207 2009-08-04 22:52:51 create author table 58 | 20090804225328 2009-08-04 22:54:33 create blog table 59 | 20090804225333 2009-08-04 22:54:33 create post table 60 | 61 |

You can also get this information from the changelog table by querying it directly in the database. 62 | Of course, you won’t see any "pending" items, as those are only known to the migration repository until they're 63 | applied to the database.

64 |
65 | 66 | 67 |
68 | -------------------------------------------------------------------------------- /src/site/xdoc/version.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 23 | MyBatis Migrations | Migrate > version 24 | The MyBatis Team 25 | 26 | 27 | 28 |
29 |

The up and down commands are pretty prescriptive in how they work. 30 | The up command evolves all the way up, and down only devolves one step down. Sometimes that might be 31 | limiting, so the version command exists to allow you to migrate the schema to any specific version of the database 32 | you like. You simply call it, specifying the version you’d like to end up at, and the migrations system figures out whether 33 | it has to go up or down, and which migrations it needs to run. Here’s an example.

34 | 35 | /home/cbegin/testdb$ migrate status 36 | ID Applied At Description 37 | ================================================================== 38 | 20090802210445 ...pending... create changelog 39 | 20090804225207 ...pending... create author table 40 | 20090804225328 ...pending... create blog table 41 | 20090804225333 ...pending... create post table 42 | 43 | /home/cbegin/testdb$ migrate version 20090804225207 44 | 45 | /home/cbegin/testdb$ migrate status 46 | ID Applied At Description 47 | ================================================================== 48 | 20090802210445 2009-08-04 22:51:17 create changelog 49 | 20090804225207 2009-08-04 22:51:17 create author table 50 | 20090804225328 ...pending... create blog table 51 | 20090804225333 ...pending... create post table 52 | 53 | /home/cbegin/testdb$ migrate up 54 | 55 | /home/cbegin/testdb$ migrate status 56 | ID Applied At Description 57 | ================================================================== 58 | 20090802210445 2009-08-04 22:51:17 create changelog 59 | 20090804225207 2009-08-04 22:51:17 create author table 60 | 20090804225328 2009-08-04 22:54:32 create blog table 61 | 20090804225333 2009-08-04 22:54:32 create post table 62 | 63 | /home/cbegin/testdb$ migrate version 20090804225207 64 | 65 | /home/cbegin/testdb$ migrate status 66 | ID Applied At Description 67 | ================================================================== 68 | 20090802210445 2009-08-04 22:51:17 create changelog 69 | 20090804225207 2009-08-04 22:51:17 create author table 70 | 20090804225328 ...pending... create blog table 71 | 20090804225333 ...pending... create post table 72 | 73 |

The version command is a powerful utility for moving to a specific revision of the database.

74 |
75 | 76 | 77 |
78 | -------------------------------------------------------------------------------- /src/test/java/databases/blog/StoredProcedures.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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 databases.blog; 17 | 18 | import java.sql.Connection; 19 | import java.sql.DriverManager; 20 | import java.sql.PreparedStatement; 21 | import java.sql.ResultSet; 22 | import java.sql.SQLException; 23 | 24 | public class StoredProcedures { 25 | public static void selectTwoSetsOfTwoAuthors(int p1, int p2, ResultSet[] rs1, ResultSet[] rs2) throws SQLException { 26 | Connection conn = DriverManager.getConnection("jdbc:default:connection"); 27 | PreparedStatement ps1 = conn.prepareStatement("select * from author where id in (?,?)"); 28 | ps1.setInt(1, p1); 29 | ps1.setInt(2, p2); 30 | rs1[0] = ps1.executeQuery(); 31 | PreparedStatement ps2 = conn.prepareStatement("select * from author where id in (?,?)"); 32 | ps2.setInt(1, p2); 33 | ps2.setInt(2, p1); 34 | rs2[0] = ps2.executeQuery(); 35 | conn.close(); 36 | } 37 | 38 | public static void insertAuthor(int id, String username, String password, String email) throws SQLException { 39 | Connection conn = DriverManager.getConnection("jdbc:default:connection"); 40 | try { 41 | PreparedStatement ps = conn 42 | .prepareStatement("INSERT INTO author (id, username, password, email) VALUES (?,?,?,?)"); 43 | ps.setInt(1, id); 44 | ps.setString(2, username); 45 | ps.setString(3, password); 46 | ps.setString(4, email); 47 | ps.executeUpdate(); 48 | } finally { 49 | conn.close(); 50 | } 51 | } 52 | 53 | public static void selectAuthorViaOutParams(int id, String[] username, String[] password, String[] email, 54 | String[] bio) throws SQLException { 55 | Connection conn = DriverManager.getConnection("jdbc:default:connection"); 56 | PreparedStatement ps = conn.prepareStatement("select * from author where id = ?"); 57 | ps.setInt(1, id); 58 | ResultSet rs = ps.executeQuery(); 59 | rs.next(); 60 | username[0] = rs.getString("username"); 61 | password[0] = rs.getString("password"); 62 | email[0] = rs.getString("email"); 63 | bio[0] = rs.getString("bio"); 64 | conn.close(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/it/version_without_changelog/VersionWithoutChangeLogTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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 it.version_without_changelog; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertFalse; 19 | import static org.junit.jupiter.api.Assertions.assertTrue; 20 | 21 | import java.io.File; 22 | 23 | import org.apache.ibatis.migration.Migrator; 24 | import org.apache.ibatis.migration.io.Resources; 25 | import org.apache.ibatis.migration.utils.TestUtil; 26 | import org.junit.jupiter.api.Test; 27 | 28 | import uk.org.webcompere.systemstubs.SystemStubs; 29 | 30 | class VersionWithoutChangeLogTest { 31 | 32 | @Test 33 | void shouldUpToVersionEvenWithoutChangelog() throws Exception { 34 | // gh-160 35 | File dir = Resources.getResourceAsFile("it/version_without_changelog"); 36 | String output = SystemStubs.tapSystemOut(() -> { 37 | Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "version", "20000101000001")); 38 | }); 39 | assertFalse(output.contains("FAILURE")); 40 | assertTrue(output.contains("20000101000000")); 41 | assertTrue(output.contains("20000101000001")); 42 | assertFalse(output.contains("20000101000002")); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/runtime_migration/scripts_java/Bootstrap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.runtime_migration.scripts_java; 17 | 18 | import org.apache.ibatis.migration.BootstrapScript; 19 | 20 | public class Bootstrap implements BootstrapScript { 21 | 22 | @Override 23 | public String getScript() { 24 | return "CREATE TABLE bootstrap_table (ID INTEGER NOT NULL, NAME VARCHAR(16));"; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/runtime_migration/scripts_java/JavaMigrationLoaderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.runtime_migration.scripts_java; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals; 19 | import static org.junit.jupiter.api.Assertions.assertTrue; 20 | 21 | import java.io.Reader; 22 | import java.io.StringWriter; 23 | import java.io.Writer; 24 | import java.util.List; 25 | 26 | import org.apache.ibatis.migration.Change; 27 | import org.apache.ibatis.migration.JavaMigrationLoader; 28 | import org.junit.jupiter.api.Test; 29 | 30 | class JavaMigrationLoaderTest { 31 | 32 | @Test 33 | void testGetMigrations() throws Exception { 34 | JavaMigrationLoader loader = createMigrationLoader(); 35 | List migrations = loader.getMigrations(); 36 | assertEquals(3, migrations.size()); 37 | } 38 | 39 | @Test 40 | void testGetScriptReader() throws Exception { 41 | JavaMigrationLoader loader = createMigrationLoader(); 42 | Change change = new Change(); 43 | change.setFilename("org.apache.ibatis.migration.runtime_migration.scripts_java.V002_CreateFirstTable"); 44 | try (Reader reader = loader.getScriptReader(change, false); Writer writer = new StringWriter()) { 45 | int c; 46 | while ((c = reader.read()) != -1) { 47 | writer.write(c); 48 | } 49 | assertTrue(writer.toString().indexOf("CREATE TABLE first_table (ID INTEGER NOT NULL, NAME VARCHAR(16));") > -1); 50 | } 51 | } 52 | 53 | @Test 54 | void testGetBootstrapReader() throws Exception { 55 | JavaMigrationLoader loader = createMigrationLoader(); 56 | try (Reader reader = loader.getBootstrapReader(); Writer writer = new StringWriter()) { 57 | int c; 58 | while ((c = reader.read()) != -1) { 59 | writer.write(c); 60 | } 61 | assertTrue( 62 | writer.toString().indexOf("CREATE TABLE bootstrap_table (ID INTEGER NOT NULL, NAME VARCHAR(16));") > -1); 63 | } 64 | } 65 | 66 | protected JavaMigrationLoader createMigrationLoader() { 67 | return new JavaMigrationLoader("org.apache.ibatis.migration.runtime_migration.scripts_java"); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/runtime_migration/scripts_java/V001_CreateChangelog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.runtime_migration.scripts_java; 17 | 18 | import java.math.BigDecimal; 19 | 20 | import org.apache.ibatis.migration.MigrationScript; 21 | 22 | public class V001_CreateChangelog implements MigrationScript { 23 | 24 | @Override 25 | public BigDecimal getId() { 26 | return new BigDecimal(this.getClass().getSimpleName().substring(1, 4)); 27 | } 28 | 29 | @Override 30 | public String getDescription() { 31 | return "Create changelog"; 32 | } 33 | 34 | @Override 35 | public String getUpScript() { 36 | return "CREATE TABLE changelog (" + "ID NUMERIC(20,0) NOT NULL," + "APPLIED_AT VARCHAR(25) NOT NULL," 37 | + "DESCRIPTION VARCHAR(255) NOT NULL); " 38 | 39 | + "ALTER TABLE changelog " + "ADD CONSTRAINT PK_changelog " + "PRIMARY KEY (id);"; 40 | } 41 | 42 | @Override 43 | public String getDownScript() { 44 | return "DROP TABLE changelog;"; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/runtime_migration/scripts_java/V002_CreateFirstTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.runtime_migration.scripts_java; 17 | 18 | import java.math.BigDecimal; 19 | 20 | import org.apache.ibatis.migration.MigrationScript; 21 | 22 | public class V002_CreateFirstTable implements MigrationScript { 23 | 24 | @Override 25 | public BigDecimal getId() { 26 | return new BigDecimal(this.getClass().getSimpleName().substring(1, 4)); 27 | } 28 | 29 | @Override 30 | public String getDescription() { 31 | return "Create first table"; 32 | } 33 | 34 | @Override 35 | public String getUpScript() { 36 | return "CREATE TABLE first_table (ID INTEGER NOT NULL, NAME VARCHAR(16));"; 37 | } 38 | 39 | @Override 40 | public String getDownScript() { 41 | return "DROP TABLE first_table;"; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/runtime_migration/scripts_java/V003_CreateSecondTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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.apache.ibatis.migration.runtime_migration.scripts_java; 17 | 18 | import java.math.BigDecimal; 19 | 20 | import org.apache.ibatis.migration.MigrationScript; 21 | 22 | public class V003_CreateSecondTable implements MigrationScript { 23 | 24 | @Override 25 | public BigDecimal getId() { 26 | return new BigDecimal(this.getClass().getSimpleName().substring(1, 4)); 27 | } 28 | 29 | @Override 30 | public String getDescription() { 31 | return "Create second table"; 32 | } 33 | 34 | @Override 35 | public String getUpScript() { 36 | return "CREATE TABLE second_table (ID INTEGER NOT NULL,NAME VARCHAR(16));"; 37 | } 38 | 39 | @Override 40 | public String getDownScript() { 41 | return "DROP TABLE second_table;"; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/system_property/SystemPropertyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.system_property; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertTrue; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | 23 | import org.apache.ibatis.migration.Migrator; 24 | import org.apache.ibatis.migration.io.Resources; 25 | import org.apache.ibatis.migration.utils.TestUtil; 26 | import org.junit.jupiter.api.BeforeAll; 27 | import org.junit.jupiter.api.Test; 28 | import org.junit.jupiter.api.extension.ExtendWith; 29 | 30 | import uk.org.webcompere.systemstubs.SystemStubs; 31 | import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; 32 | import uk.org.webcompere.systemstubs.jupiter.SystemStub; 33 | import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; 34 | 35 | @ExtendWith(SystemStubsExtension.class) 36 | class SystemPropertyTest { 37 | 38 | private static File dir; 39 | 40 | @SystemStub 41 | private EnvironmentVariables variables = new EnvironmentVariables("MIGRATIONS_VAR3", "bogus_var3", 42 | "MIGRATIONS_ENVVAR1", "Environment variable 1"); 43 | 44 | @BeforeAll 45 | static void init() throws IOException { 46 | dir = Resources.getResourceAsFile("org/apache/ibatis/migration/system_property/testdir"); 47 | } 48 | 49 | @Test 50 | void testSystemProperties() throws Exception { 51 | variables.setup(); 52 | SystemStubs.restoreSystemProperties(() -> { 53 | System.setProperty("MIGRATIONS_DRIVER", "org.hsqldb.jdbcDriver"); 54 | System.setProperty("username", "Pocahontas"); 55 | System.setProperty("var1", "Variable 1"); 56 | System.setProperty("MIGRATIONS_VAR3", "Variable 3"); 57 | System.setProperty("migrations_var4", "Variable 4"); 58 | System.setProperty("MIGRATIONS_VAR5", "Variable 5"); 59 | 60 | String output = SystemStubs.tapSystemOut(() -> { 61 | Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "up", "1", "--trace")); 62 | }); 63 | assertTrue(output.contains("SUCCESS"), "Output is:" + output); 64 | assertTrue(output.contains("username: Pocahontas"), "Output is:" + output); 65 | assertTrue(output.contains("var1: Variable 1"), "Output is:" + output); 66 | assertTrue(output.contains("var2: ${var2}"), "Output is:" + output); 67 | assertTrue(output.contains("var3: Variable 3"), 68 | "System property should overwrite env var," + "Output is:" + output); 69 | assertTrue(output.contains("var4: Variable 4"), "Output is:" + output); 70 | assertTrue(output.contains("var5: Variable 5"), "Output is:" + output); 71 | assertTrue(output.contains("Var5: Var5 in properties file"), "Output is:" + output); 72 | assertTrue(output.contains("envvar1: Environment variable 1"), "Output is:" + output); 73 | 74 | Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "down", "1")); 75 | }); 76 | variables.teardown(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/utils/TestUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2023 the original author or authors. 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 | * https://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.apache.ibatis.migration.utils; 17 | 18 | import java.io.File; 19 | import java.io.IOException; 20 | import java.nio.file.Files; 21 | import java.sql.Connection; 22 | import java.sql.DriverManager; 23 | import java.sql.SQLException; 24 | import java.util.Properties; 25 | 26 | public class TestUtil { 27 | public static Connection getConnection(Properties envProperties) throws SQLException, ClassNotFoundException { 28 | Class.forName(envProperties.getProperty("driver")); 29 | return DriverManager.getConnection(envProperties.getProperty("url"), envProperties.getProperty("username"), 30 | envProperties.getProperty("password")); 31 | } 32 | 33 | public static String[] args(String... args) { 34 | return args; 35 | } 36 | 37 | public static int countStr(String output, String str) { 38 | int count = 0; 39 | for (int i = 0; i < output.length();) { 40 | int idx = output.indexOf(str, i); 41 | if (idx == -1) { 42 | break; 43 | } 44 | i = idx + 1; 45 | count++; 46 | } 47 | return count; 48 | } 49 | 50 | public static File getTempDir() throws IOException { 51 | File f = Files.createTempDirectory("migrations_test").toFile(); 52 | f.deleteOnExit(); 53 | return f; 54 | } 55 | 56 | public static boolean deleteDirectory(File dir) throws IOException { 57 | boolean result = dir.exists(); 58 | if (result) { 59 | for (File f : dir.listFiles()) { 60 | result = result && (f.isDirectory() ? deleteDirectory(f) : f.delete()); 61 | } 62 | } 63 | return result && dir.delete(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/org/apache/ibatis/migration/utils/UtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2025 the original author or authors. 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 | * https://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.apache.ibatis.migration.utils; 17 | 18 | import static org.apache.ibatis.migration.utils.Util.file; 19 | import static org.apache.ibatis.migration.utils.Util.isOption; 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | import static org.junit.jupiter.api.Assertions.assertFalse; 22 | import static org.junit.jupiter.api.Assertions.assertTrue; 23 | 24 | import java.io.File; 25 | import java.nio.file.Path; 26 | 27 | import org.junit.jupiter.api.Test; 28 | 29 | class UtilTest { 30 | @Test 31 | void testIsOption() { 32 | assertTrue(isOption("--properOption"), "Util doesn't recognize proper option"); 33 | assertFalse(isOption("-improperOption"), "Util doesn't recognize improper option"); 34 | assertTrue(isOption("--properOptionValue=value"), "Util doesn't recognize proper option with value"); 35 | assertFalse(isOption("--missingOptionValue="), "Util doesn't recognize proper option with value"); 36 | } 37 | 38 | @Test 39 | void testFile() { 40 | final File parentDirectory = Path.of(".").toFile(); 41 | final String childFile = "child.file"; 42 | final File absoluteFile = file(parentDirectory, childFile); 43 | 44 | assertThat(absoluteFile.getAbsolutePath()).startsWith(parentDirectory.getAbsolutePath()); 45 | assertThat(absoluteFile.getAbsolutePath()).endsWith(File.separator + childFile); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/resources/it/version_without_changelog/environments/development.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2022 the original author or authors. 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 | # https://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 | 17 | driver=org.hsqldb.jdbcDriver 18 | url=jdbc:hsqldb:mem:version_without_changelog 19 | username=sa 20 | password= 21 | changelog=CHANGELOG 22 | -------------------------------------------------------------------------------- /src/test/resources/it/version_without_changelog/scripts/20000101000000_create_changelog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | CREATE TABLE ${changelog} ( 18 | ID NUMERIC(20,0) NOT NULL, 19 | APPLIED_AT VARCHAR(25) NOT NULL, 20 | DESCRIPTION VARCHAR(255) NOT NULL 21 | ); 22 | 23 | ALTER TABLE ${changelog} 24 | ADD CONSTRAINT PK_${changelog} 25 | PRIMARY KEY (id); 26 | 27 | -- //@UNDO 28 | 29 | DROP TABLE ${changelog}; 30 | -------------------------------------------------------------------------------- /src/test/resources/it/version_without_changelog/scripts/20000101000001_first_migration.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | create table test1 ( 18 | id int primary key, 19 | name varchar(10) 20 | ); 21 | 22 | -- //@UNDO 23 | 24 | drop table test1; 25 | 26 | -------------------------------------------------------------------------------- /src/test/resources/it/version_without_changelog/scripts/20000101000002_second_migration.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | create table test2 ( 18 | id int primary key, 19 | name varchar(10) 20 | ); 21 | 22 | -- //@UNDO 23 | 24 | drop table test2; 25 | 26 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/commands/TestTemplate.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | // ${var} 18 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/README: -------------------------------------------------------------------------------- 1 | Welcome! 2 | 3 | This is an iBATIS Migration repository. You can specify the repository 4 | directory when running migrations using the --path= 5 | option. The default path is the current working directory ("./"). 6 | 7 | The repository base directory contains three subdirectories as follows: 8 | 9 | ./drivers 10 | 11 | Place your JDBC driver .jar or .zip files in this directory. Upon running a 12 | migration, the drivers will be dynamically loaded. 13 | 14 | ./environments 15 | 16 | In the environments folder you will find .properties files that represent 17 | your database instances. By default a development.properties file is 18 | created for you to configure your development time database properties. 19 | You can also create test.properties and production.properties files. 20 | The environment can be specified when running a migration by using 21 | the --env= option (without the path or ".properties" part). 22 | 23 | The default environment is "development". 24 | 25 | ./scripts 26 | 27 | This directory contains your migration SQL files. These are the files 28 | that contain your DDL to both upgrade and downgrade your database 29 | structure. By default, the directory will contain the script to 30 | create the changelog table, plus one empty "first" migration script. 31 | To create a new migration script, use the "new" command. To run 32 | all pending migrations, use the "up" command. To undo the last 33 | migration applied, use the "down" command etc. 34 | 35 | For more information about commands and options, run the ibatis 36 | migration script with the --help option. 37 | 38 | Enjoy. 39 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/environments/development.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2022 the original author or authors. 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 | # https://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 | 17 | ## Base time zone to ensure times are consistent across machines 18 | time_zone=GMT+0:00 19 | 20 | ## The character set that scripts are encoded with 21 | # script_char_set=UTF-8 22 | 23 | ## JDBC connection properties. 24 | driver=org.hsqldb.jdbcDriver 25 | url=jdbc:hsqldb:mem:blog 26 | username=sa 27 | password= 28 | 29 | # 30 | # A NOTE ON STORED PROCEDURES AND DELIMITERS 31 | # 32 | # Stored procedures and 33 | # functions commonly have nested delimiters that conflict 34 | # with the schema migration parsing. If you tend to use 35 | # procs, functions, triggers or anything that could create 36 | # this situation, then you may want to experiment with 37 | # send_full_script=true (preferred), or if you can't use 38 | # send_full_script, then you may have to resort to a full 39 | # line delimiter such as "GO" or "/" or "!RUN!". 40 | # 41 | # Also play with the autocommit settings, as some drivers 42 | # or databases don't support creating procs, functions or 43 | # even tables in a transaction, and others require it. 44 | # 45 | 46 | # This ignores the line delimiters and 47 | # simply sends the entire script at once. 48 | # Use with JDBC drivers that can accept large 49 | # blocks of delimited text at once. 50 | send_full_script=false 51 | 52 | # This controls how statements are delimited. 53 | # By default statements are delimited by an 54 | # end of line semicolon. Some databases may 55 | # (e.g. MS SQL Server) may require a full line 56 | # delimiter such as GO. 57 | # These are ignored if send_full_script is true. 58 | delimiter=; 59 | full_line_delimiter=false 60 | 61 | # If set to true, each statement is isolated 62 | # in its own transaction. Otherwise the entire 63 | # script is executed in one transaction. 64 | # Few databases should need this set to true, 65 | # but some do. 66 | auto_commit=false 67 | 68 | # Custom driver path to allow you to centralize your driver files 69 | # Default requires the drivers to be in the drivers directory of your 70 | # initialized migration directory (created with "migrate init") 71 | # driver_path= 72 | 73 | # Name of the table that tracks changes to the database 74 | changelog=CHANGELOG 75 | 76 | # Migrations support variable substitutions in the form of ${variable} 77 | # in the migration scripts. All of the above properties will be ignored though, 78 | # with the exception of changelog. 79 | # Example: The following would be referenced in a migration file as ${ip_address} 80 | # ip_address=192.168.0.1 81 | 82 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/20080827200210_create_changelog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Create Changelog 18 | 19 | -- Default DDL for changelog table that will keep 20 | -- a record of the migrations that have been run. 21 | 22 | -- You can modify this to suit your database before 23 | -- running your first migration. 24 | 25 | -- Be sure that ID and DESCRIPTION fields exist in 26 | -- BigInteger and String compatible fields respectively. 27 | 28 | CREATE TABLE ${changelog} ( 29 | ID NUMERIC(20,0) NOT NULL, 30 | APPLIED_AT VARCHAR(25) NOT NULL, 31 | DESCRIPTION VARCHAR(255) NOT NULL 32 | ); 33 | 34 | ALTER TABLE ${changelog} 35 | ADD CONSTRAINT PK_${changelog} 36 | PRIMARY KEY (id); 37 | 38 | -- //@UNDO 39 | 40 | DROP TABLE ${changelog}; 41 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/20080827200211_create_author.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // First migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE TABLE author ( 21 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 10000), 22 | username VARCHAR(255) NOT NULL, 23 | password VARCHAR(255) NOT NULL, 24 | email VARCHAR(255) NOT NULL, 25 | bio LONGVARCHAR, 26 | favourite_section VARCHAR(25), 27 | PRIMARY KEY (id) 28 | ); 29 | 30 | insert into author (id,username,password,email,bio,favourite_section) 31 | values (1,'jsmith','*****','jim@${url}','No bio','NONE'); 32 | 33 | -- //@UNDO 34 | -- SQL to undo the change goes here. 35 | 36 | DROP TABLE author; 37 | 38 | DROP TABLE comment; 39 | DROP TABLE post_tag; 40 | DROP TABLE tag; 41 | DROP TABLE post; 42 | DROP TABLE blog; 43 | DROP PROCEDURE selectTwoSetsOfAuthors; 44 | DROP PROCEDURE insertAuthor; 45 | DROP PROCEDURE selectAuthorViaOutParams; 46 | 47 | CREATE TABLE blog ( 48 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 49 | author_id INT NOT NULL, 50 | title VARCHAR(255), 51 | PRIMARY KEY (id) 52 | ); 53 | 54 | CREATE TABLE post ( 55 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 56 | blog_id INT, 57 | author_id INT NOT NULL, 58 | created_on TIMESTAMP NOT NULL, 59 | section VARCHAR(25) NOT NULL, 60 | subject VARCHAR(255) NOT NULL, 61 | body CLOB NOT NULL, 62 | PRIMARY KEY (id), 63 | FOREIGN KEY (blog_id) REFERENCES blog(id) 64 | ); 65 | 66 | CREATE TABLE tag ( 67 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 68 | name VARCHAR(255) NOT NULL, 69 | PRIMARY KEY (id) 70 | ); 71 | 72 | CREATE TABLE post_tag ( 73 | post_id INT NOT NULL, 74 | tag_id INT NOT NULL, 75 | PRIMARY KEY (post_id, tag_id) 76 | ); 77 | 78 | CREATE TABLE comment ( 79 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 80 | post_id INT NOT NULL, 81 | name LONGVARCHAR NOT NULL, 82 | comment LONGVARCHAR NOT NULL, 83 | PRIMARY KEY (id) 84 | ); 85 | 86 | CREATE PROCEDURE selectTwoSetsOfAuthors(DP1 INTEGER, DP2 INTEGER) 87 | PARAMETER STYLE JAVA 88 | LANGUAGE JAVA 89 | READS SQL DATA 90 | DYNAMIC RESULT SETS 2 91 | EXTERNAL NAME 'databases.blog.StoredProcedures.selectTwoSetsOfTwoAuthors'; 92 | 93 | CREATE PROCEDURE insertAuthor(DP1 INTEGER, DP2 VARCHAR(255), DP3 VARCHAR(255), DP4 VARCHAR(255)) 94 | PARAMETER STYLE JAVA 95 | LANGUAGE JAVA 96 | EXTERNAL NAME 'databases.blog.StoredProcedures.insertAuthor'; 97 | 98 | CREATE PROCEDURE selectAuthorViaOutParams(ID INTEGER, OUT USERNAME VARCHAR(255), OUT PASSWORD VARCHAR(255), OUT EMAIL VARCHAR(255), OUT BIO VARCHAR(255)) 99 | PARAMETER STYLE JAVA 100 | LANGUAGE JAVA 101 | EXTERNAL NAME 'databases.blog.StoredProcedures.selectAuthorViaOutParams'; 102 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/20080827200212_create_blog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // First migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE TABLE blog ( 21 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 22 | author_id INT NOT NULL, 23 | title VARCHAR(255), 24 | PRIMARY KEY (id) 25 | ); 26 | 27 | -- //@UNDO 28 | -- SQL to undo the change goes here. 29 | 30 | DROP TABLE blog; 31 | 32 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/20080827200213_create_tags_posts.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // First migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE TABLE post ( 21 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 22 | blog_id INT, 23 | author_id INT NOT NULL, 24 | created_on TIMESTAMP NOT NULL, 25 | section VARCHAR(25) NOT NULL, 26 | subject VARCHAR(255) NOT NULL, 27 | body CLOB NOT NULL, 28 | PRIMARY KEY (id), 29 | FOREIGN KEY (blog_id) REFERENCES blog(id) 30 | ); 31 | 32 | CREATE TABLE tag ( 33 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 34 | name VARCHAR(255) NOT NULL, 35 | PRIMARY KEY (id) 36 | ); 37 | 38 | CREATE TABLE post_tag ( 39 | post_id INT NOT NULL, 40 | tag_id INT NOT NULL, 41 | PRIMARY KEY (post_id, tag_id) 42 | ); 43 | 44 | -- //@UNDO 45 | -- SQL to undo the change goes here. 46 | 47 | DROP TABLE post_tag; 48 | DROP TABLE tag; 49 | DROP TABLE post; 50 | 51 | 52 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/20080827200214_create_comments.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // First migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE TABLE comment ( 21 | id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY, 22 | post_id INT NOT NULL, 23 | name LONGVARCHAR NOT NULL, 24 | comment LONGVARCHAR NOT NULL, 25 | PRIMARY KEY (id) 26 | ); 27 | 28 | -- //@UNDO 29 | -- SQL to undo the change goes here. 30 | 31 | DROP TABLE comment; 32 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/20080827200216_create_procs.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // First migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE PROCEDURE selectTwoSetsOfAuthors(DP1 INTEGER, DP2 INTEGER) 21 | PARAMETER STYLE JAVA 22 | LANGUAGE JAVA 23 | READS SQL DATA 24 | DYNAMIC RESULT SETS 2 25 | EXTERNAL NAME 'databases.blog.StoredProcedures.selectTwoSetsOfTwoAuthors'; 26 | 27 | CREATE PROCEDURE insertAuthor(DP1 INTEGER, DP2 VARCHAR(255), DP3 VARCHAR(255), DP4 VARCHAR(255)) 28 | PARAMETER STYLE JAVA 29 | LANGUAGE JAVA 30 | EXTERNAL NAME 'databases.blog.StoredProcedures.insertAuthor'; 31 | 32 | CREATE PROCEDURE selectAuthorViaOutParams(ID INTEGER, OUT USERNAME VARCHAR(255), OUT PASSWORD VARCHAR(255), OUT EMAIL VARCHAR(255), OUT BIO VARCHAR(255)) 33 | PARAMETER STYLE JAVA 34 | LANGUAGE JAVA 35 | EXTERNAL NAME 'databases.blog.StoredProcedures.selectAuthorViaOutParams'; 36 | 37 | -- //@UNDO 38 | -- SQL to undo the change goes here. 39 | 40 | DROP PROCEDURE selectTwoSetsOfAuthors; 41 | DROP PROCEDURE insertAuthor; 42 | DROP PROCEDURE selectAuthorViaOutParams; 43 | 44 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/20080827200217_parse_file_with_dot_._in_file_name.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // First migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | -- //@UNDO 21 | -- SQL to undo the change goes here. 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/example/scripts/bootstrap.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Bootstrap.sql 18 | 19 | -- This is the only SQL script file that is NOT 20 | -- a valid migration and will not be run or tracked 21 | -- in the changelog. There is no @UNDO section. 22 | 23 | -- // Do I need this file? 24 | 25 | -- New projects likely won't need this file. 26 | -- Existing projects will likely need this file. 27 | -- It's unlikely that this bootstrap should be run 28 | -- in the production environment. 29 | 30 | -- // Purpose 31 | 32 | -- The purpose of this file is to provide a facility 33 | -- to initialize the database to a state before MyBatis 34 | -- SQL migrations were applied. If you already have 35 | -- a database in production, then you probably have 36 | -- a script that you run on your developer machine 37 | -- to initialize the database. That script can now 38 | -- be put in this bootstrap file (but does not have 39 | -- to be if you are comfortable with your current process. 40 | 41 | -- // Running 42 | 43 | -- The bootstrap SQL is run with the "migrate bootstrap" 44 | -- command. It must be run manually, it's never run as 45 | -- part of the regular migration process and will never 46 | -- be undone. Variables (e.g. ${variable}) are still 47 | -- parsed in the bootstrap SQL. 48 | 49 | -- After the boostrap SQL has been run, you can then 50 | -- use the migrations and the changelog for all future 51 | -- database change management. 52 | 53 | create table BOOTSTRAP ( 54 | ID NUMERIC(8,0), 55 | PRIMARY KEY(ID) 56 | ); 57 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/environments/development.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2022 the original author or authors. 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 | # https://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 | 17 | ## Base time zone to ensure times are consistent across machines 18 | time_zone=GMT+0:00 19 | 20 | ## The character set that scripts are encoded with 21 | # script_char_set=UTF-8 22 | 23 | ## JDBC connection properties. 24 | driver=org.hsqldb.jdbcDriver 25 | url=jdbc:hsqldb:mem:hook 26 | username=sa 27 | password= 28 | 29 | # 30 | # A NOTE ON STORED PROCEDURES AND DELIMITERS 31 | # 32 | # Stored procedures and 33 | # functions commonly have nested delimiters that conflict 34 | # with the schema migration parsing. If you tend to use 35 | # procs, functions, triggers or anything that could create 36 | # this situation, then you may want to experiment with 37 | # send_full_script=true (preferred), or if you can't use 38 | # send_full_script, then you may have to resort to a full 39 | # line delimiter such as "GO" or "/" or "!RUN!". 40 | # 41 | # Also play with the autocommit settings, as some drivers 42 | # or databases don't support creating procs, functions or 43 | # even tables in a transaction, and others require it. 44 | # 45 | 46 | # This ignores the line delimiters and 47 | # simply sends the entire script at once. 48 | # Use with JDBC drivers that can accept large 49 | # blocks of delimited text at once. 50 | send_full_script=false 51 | 52 | # This controls how statements are delimited. 53 | # By default statements are delimited by an 54 | # end of line semicolon. Some databases may 55 | # (e.g. MS SQL Server) may require a full line 56 | # delimiter such as GO. 57 | # These are ignored if send_full_script is true. 58 | delimiter=; 59 | full_line_delimiter=false 60 | 61 | # If set to true, each statement is isolated 62 | # in its own transaction. Otherwise the entire 63 | # script is executed in one transaction. 64 | # Few databases should need this set to true, 65 | # but some do. 66 | auto_commit=false 67 | 68 | # Custom driver path to allow you to centralize your driver files 69 | # Default requires the drivers to be in the drivers directory of your 70 | # initialized migration directory (created with "migrate init") 71 | # driver_path= 72 | 73 | # Name of the table that tracks changes to the database 74 | changelog=CHANGES 75 | 76 | # Migrations support variable substitutions in the form of ${variable} 77 | # in the migration scripts. All of the above properties will be ignored though, 78 | # with the exception of changelog. 79 | # Example: The following would be referenced in a migration file as ${ip_address} 80 | # ip_address=192.168.0.1 81 | 82 | # demo of simple JSR-223 hook script 83 | hook_before_up=JavaScript:PrintHello1.js 84 | # demo of function invocation 85 | hook_before_each_up=javascript:PrintVar.js:_function=printVars:_arg=ARG1:_arg=ARG2:local_var1=LOCALVAR1:local_var2=LOCALVAR2 86 | # demo of SQL hook script 87 | hook_after_each_up=sql:InsertWorklog.sql:local_var1=LOCALVAR1:local_var2=LOCALVAR2 88 | # demo of method invocation 89 | hook_after_up=js:PrintVar.js:_object=dog:_method=bark:_arg=ARG1:_arg=ARG2:local_var1=LOCALVAR1:local_var2=LOCALVAR2 90 | 91 | hook_before_each_down=JavaScript:InsertWorklog.js 92 | 93 | global_var=GLOBALVAR 94 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/hooks/InsertWorklog.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | // Rhino has println(), but Nashorn does not. 17 | // Both has print(), but Nashorn's outputs newline. 18 | if (typeof println == 'undefined') 19 | this.println = print; 20 | 21 | hookContext.executeSql("insert into worklog (str1) values ('" + hookContext.getChange().getId() + "');"); 22 | println(''); 23 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/hooks/InsertWorklog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | insert into worklog (str1, str2, str3) values ('${global_var}', '${local_var1}', '${local_var2}'); 18 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/hooks/NewHook.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | // Rhino has println(), but Nashorn does not. 17 | // Both has print(), but Nashorn's outputs newline. 18 | if (typeof println == 'undefined') 19 | this.println = print; 20 | 21 | function validateDesc() { 22 | if (hookContext.getDescription().matches('.*JIRA-[0-9]+.*')) { 23 | println('Description is valid.'); 24 | println(''); 25 | } else { 26 | throw 'Description must contain JIRA ticket number.'; 27 | } 28 | } 29 | 30 | function renameFile() { 31 | var oldName = hookContext.getFilename(); 32 | var newName = oldName.replace("JIRA-", "JIRA"); 33 | var scriptsDir = migrationPaths.getScriptPath(); 34 | var src = new java.io.File(scriptsDir, oldName); 35 | var dest = new java.io.File(scriptsDir, newName); 36 | if (src.renameTo(dest)) { 37 | println('Renamed ' + oldName + ' to ' + newName); 38 | println(''); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/hooks/PrintHello1.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | // Rhino has println(), but Nashorn does not. 17 | // Both has print(), but Nashorn's outputs newline. 18 | if (typeof println == 'undefined') 19 | this.println = print; 20 | 21 | println('HELLO_1'); 22 | 23 | var SCRIPT_VAR = 1; 24 | println('SCRIPT_VAR=' + SCRIPT_VAR); 25 | 26 | println(''); 27 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/hooks/PrintVar.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | // Rhino has println(), but Nashorn does not. 17 | // Both has print(), but Nashorn's outputs newline. 18 | if (typeof println == 'undefined') 19 | this.println = print; 20 | 21 | function printVars(arg1, arg2) { 22 | println('FUNCTION_' + global_var + '_' + local_var1 + '_' + local_var2 + '_' + arg1 + '_' + arg2); 23 | println('SCRIPT_VAR=' + ++SCRIPT_VAR); 24 | println(''); 25 | 26 | // to verify 'change' object is just a clone 27 | hookContext.getChange().setDescription('bogus description'); 28 | 29 | local_var1 = 'This overwrites the variable defined in env file, ' + 'but should be reset on next invocation.' 30 | } 31 | 32 | var dog = new Object(); 33 | dog.bark = function(arg1, arg2) { 34 | println('METHOD_' + global_var + '_' + local_var1 + '_' + local_var2 + '_' + arg1 + '_' + arg2); 35 | println('SCRIPT_VAR=' + ++SCRIPT_VAR); 36 | println(''); 37 | } 38 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/scripts/001_create_changelog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Create Changelog 18 | 19 | -- Default DDL for changelog table that will keep 20 | -- a record of the migrations that have been run. 21 | 22 | -- You can modify this to suit your database before 23 | -- running your first migration. 24 | 25 | -- Be sure that ID and DESCRIPTION fields exist in 26 | -- BigInteger and String compatible fields respectively. 27 | 28 | CREATE TABLE ${changelog} ( 29 | ID NUMERIC(20,0) NOT NULL, 30 | APPLIED_AT VARCHAR(25) NOT NULL, 31 | DESCRIPTION VARCHAR(255) NOT NULL 32 | ); 33 | 34 | ALTER TABLE ${changelog} 35 | ADD CONSTRAINT PK_${changelog} 36 | PRIMARY KEY (id); 37 | 38 | -- //@UNDO 39 | 40 | DROP TABLE ${changelog}; 41 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/scripts/002_create_person.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | CREATE TABLE person ( 18 | ID INT, 19 | NAME VARCHAR(64) 20 | ); 21 | 22 | 23 | -- //@UNDO 24 | 25 | DROP TABLE person; 26 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/scripts/003_create_pet.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | CREATE TABLE pet ( 18 | ID INT, 19 | OWNER_ID INT, 20 | NAME VARCHAR(64) 21 | ); 22 | 23 | 24 | -- //@UNDO 25 | 26 | DROP TABLE pet; 27 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/hook/testdir/scripts/bootstrap.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | CREATE TABLE worklog ( 18 | id INT, 19 | str1 VARCHAR(64), 20 | str2 VARCHAR(64), 21 | str3 VARCHAR(64) 22 | ); 23 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/runtime_migration/scripts/20130707120737_create_changelog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Create Changelog 18 | 19 | -- Default DDL for changelog table that will keep 20 | -- a record of the migrations that have been run. 21 | 22 | -- You can modify this to suit your database before 23 | -- running your first migration. 24 | 25 | -- Be sure that ID and DESCRIPTION fields exist in 26 | -- BigInteger and String compatible fields respectively. 27 | 28 | CREATE TABLE ${changelog} ( 29 | ID NUMERIC(20,0) NOT NULL, 30 | APPLIED_AT VARCHAR(25) NOT NULL, 31 | DESCRIPTION VARCHAR(255) NOT NULL 32 | ); 33 | 34 | ALTER TABLE ${changelog} 35 | ADD CONSTRAINT PK_${changelog} 36 | PRIMARY KEY (id); 37 | 38 | -- //@UNDO 39 | 40 | DROP TABLE ${changelog}; 41 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/runtime_migration/scripts/20130707120738_create_first_table.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // First migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE TABLE first_table ( 21 | ID INTEGER NOT NULL, 22 | NAME VARCHAR(16) 23 | ); 24 | 25 | 26 | -- //@UNDO 27 | -- SQL to undo the change goes here. 28 | 29 | DROP TABLE first_table; 30 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/runtime_migration/scripts/20130707120739_create_second_table.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Second migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE TABLE second_table ( 21 | ID INTEGER NOT NULL, 22 | NAME VARCHAR(16) 23 | ); 24 | 25 | 26 | -- //@UNDO 27 | -- SQL to undo the change goes here. 28 | 29 | DROP TABLE second_table; 30 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/runtime_migration/scripts/bootstrap.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Bootstrap.sql 18 | 19 | -- This is the only SQL script file that is NOT 20 | -- a valid migration and will not be run or tracked 21 | -- in the changelog. There is no @UNDO section. 22 | 23 | -- // Do I need this file? 24 | 25 | -- New projects likely won't need this file. 26 | -- Existing projects will likely need this file. 27 | -- It's unlikely that this bootstrap should be run 28 | -- in the production environment. 29 | 30 | -- // Purpose 31 | 32 | -- The purpose of this file is to provide a facility 33 | -- to initialize the database to a state before MyBatis 34 | -- SQL migrations were applied. If you already have 35 | -- a database in production, then you probably have 36 | -- a script that you run on your developer machine 37 | -- to initialize the database. That script can now 38 | -- be put in this bootstrap file (but does not have 39 | -- to be if you are comfortable with your current process. 40 | 41 | -- // Running 42 | 43 | -- The bootstrap SQL is run with the "migrate bootstrap" 44 | -- command. It must be run manually, it's never run as 45 | -- part of the regular migration process and will never 46 | -- be undone. Variables (e.g. ${variable}) are still 47 | -- parsed in the bootstrap SQL. 48 | 49 | -- After the boostrap SQL has been run, you can then 50 | -- use the migrations and the changelog for all future 51 | -- database change management. 52 | 53 | CREATE TABLE bootstrap_table ( 54 | ID INTEGER NOT NULL, 55 | NAME VARCHAR(16) 56 | ); 57 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/runtime_migration/scripts_from_other_branch/20130707120740_create_third_table.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Second migration. 18 | -- Migration SQL that makes the change goes here. 19 | 20 | CREATE TABLE third_table ( 21 | ID INTEGER NOT NULL, 22 | NAME VARCHAR(16) 23 | ); 24 | 25 | 26 | -- //@UNDO 27 | -- SQL to undo the change goes here. 28 | 29 | DROP TABLE third_table; 30 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/environments/development.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2022 the original author or authors. 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 | # https://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 | 17 | ## Base time zone to ensure times are consistent across machines 18 | time_zone=GMT+0:00 19 | 20 | ## The character set that scripts are encoded with 21 | # script_char_set=UTF-8 22 | 23 | ## JDBC connection properties. 24 | driver=org.hsqldb.jdbcDriver 25 | url=jdbc:hsqldb:mem:${DB_NAME} 26 | username=sa 27 | password= 28 | 29 | # 30 | # A NOTE ON STORED PROCEDURES AND DELIMITERS 31 | # 32 | # Stored procedures and 33 | # functions commonly have nested delimiters that conflict 34 | # with the schema migration parsing. If you tend to use 35 | # procs, functions, triggers or anything that could create 36 | # this situation, then you may want to experiment with 37 | # send_full_script=true (preferred), or if you can't use 38 | # send_full_script, then you may have to resort to a full 39 | # line delimiter such as "GO" or "/" or "!RUN!". 40 | # 41 | # Also play with the autocommit settings, as some drivers 42 | # or databases don't support creating procs, functions or 43 | # even tables in a transaction, and others require it. 44 | # 45 | 46 | # This ignores the line delimiters and 47 | # simply sends the entire script at once. 48 | # Use with JDBC drivers that can accept large 49 | # blocks of delimited text at once. 50 | send_full_script=false 51 | 52 | # This controls how statements are delimited. 53 | # By default statements are delimited by an 54 | # end of line semicolon. Some databases may 55 | # (e.g. MS SQL Server) may require a full line 56 | # delimiter such as GO. 57 | # These are ignored if send_full_script is true. 58 | delimiter=; 59 | full_line_delimiter=false 60 | 61 | # If set to true, each statement is isolated 62 | # in its own transaction. Otherwise the entire 63 | # script is executed in one transaction. 64 | # Few databases should need this set to true, 65 | # but some do. 66 | auto_commit=false 67 | 68 | # Custom driver path to allow you to centralize your driver files 69 | # Default requires the drivers to be in the drivers directory of your 70 | # initialized migration directory (created with "migrate init") 71 | # driver_path= 72 | 73 | # Name of the table that tracks changes to the database 74 | changelog=CHANGES 75 | 76 | # Migrations support variable substitutions in the form of ${variable} 77 | # in the migration scripts. All of the above properties will be ignored though, 78 | # with the exception of changelog. 79 | # Example: The following would be referenced in a migration file as ${ip_address} 80 | # ip_address=192.168.0.1 81 | 82 | # demo of simple JSR-223 hook script 83 | hook_before_script=JavaScript:Before.js 84 | hook_before_each_script=JavaScript:BeforeEach.js 85 | hook_after_each_script=JavaScript:AfterEach.js 86 | hook_after_script=JavaScript:After.js 87 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/hooks/After.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | print('After hook: ' + hookContext.isUndo()); 17 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/hooks/AfterEach.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | print('After each hook: ' + hookContext.getChange().getId() + '/' + hookContext.isUndo()); 17 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/hooks/Before.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | print('Before hook: ' + hookContext.isUndo()); 17 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/hooks/BeforeEach.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2022 the original author or authors. 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 | * https://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 | print('Before each hook: ' + hookContext.getChange().getId() + '/' + hookContext.isUndo()); 17 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/scripts/001_create_changelog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | CREATE TABLE ${changelog} (ID NUMERIC(20,0) PRIMARY KEY NOT NULL, APPLIED_AT VARCHAR(25) NOT NULL, DESCRIPTION VARCHAR(255) NOT NULL); 18 | 19 | -- //@UNDO 20 | 21 | DROP TABLE ${changelog}; 22 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/scripts/002_second_migration.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | select 'do second migration' from (values(0)); 18 | 19 | -- //@UNDO 20 | 21 | select 'undo second migration' from (values(0)); 22 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/script_hook/testdir/scripts/003_third_migration.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | select 'do third migration' from (values(0)); 18 | 19 | -- //@UNDO 20 | 21 | select 'undo third migration' from (values(0)); 22 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/system_property/testdir/environments/development.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2010-2022 the original author or authors. 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 | # https://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 | 17 | driver=should_be_overwritten_by_system_property 18 | url=jdbc:hsqldb:mem:sysprop 19 | username=${username} 20 | password= 21 | 22 | changelog=CHANGELOG 23 | 24 | var1=${var1} 25 | var2=${var2} 26 | var4=should be overwritten by system property 27 | Var5=Var5 in properties file 28 | -------------------------------------------------------------------------------- /src/test/resources/org/apache/ibatis/migration/system_property/testdir/scripts/001_create_changelog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2022 the original author or authors. 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 | -- https://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 | 17 | -- // Create Changelog 18 | 19 | -- Default DDL for changelog table that will keep 20 | -- a record of the migrations that have been run. 21 | 22 | -- You can modify this to suit your database before 23 | -- running your first migration. 24 | 25 | -- Be sure that ID and DESCRIPTION fields exist in 26 | -- BigInteger and String compatible fields respectively. 27 | 28 | CREATE TABLE ${changelog} ( 29 | ID NUMERIC(20,0) NOT NULL, 30 | APPLIED_AT VARCHAR(25) NOT NULL, 31 | DESCRIPTION VARCHAR(255) NOT NULL 32 | ); 33 | 34 | ALTER TABLE ${changelog} 35 | ADD CONSTRAINT PK_${changelog} 36 | PRIMARY KEY (id); 37 | 38 | SELECT 'username: ' || USER_NAME FROM INFORMATION_SCHEMA.SYSTEM_USERS; 39 | SELECT 'var1: ' || '${var1}' FROM (VALUES(0)); 40 | SELECT 'var2: ' || '${var2}' FROM (VALUES(0)); 41 | SELECT 'var3: ' || '${var3}' FROM (VALUES(0)); 42 | SELECT 'var4: ' || '${var4}' FROM (VALUES(0)); 43 | SELECT 'var5: ' || '${var5}' FROM (VALUES(0)); 44 | SELECT 'Var5: ' || '${Var5}' FROM (VALUES(0)); 45 | SELECT 'envvar1: ' || '${envvar1}' FROM (VALUES(0)); 46 | 47 | 48 | -- //@UNDO 49 | 50 | DROP TABLE ${changelog}; 51 | --------------------------------------------------------------------------------