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 |
--------------------------------------------------------------------------------
/.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@v6
29 |
30 | - name: Setup Java
31 | uses: actions/setup-java@v5
32 | with:
33 | cache: maven
34 | distribution: 'temurin'
35 | java-version: 21
36 |
37 | - name: Initialize CodeQL
38 | uses: github/codeql-action/init@v4
39 | with:
40 | languages: ${{ matrix.language }}
41 | queries: +security-and-quality
42 |
43 | - name: Autobuild
44 | uses: github/codeql-action/autobuild@v4
45 |
46 | - name: Perform CodeQL Analysis
47 | uses: github/codeql-action/analyze@v4
48 | with:
49 | category: "/language:${{matrix.language}}"
50 |
--------------------------------------------------------------------------------
/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/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/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/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/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/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/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/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/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/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.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/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 | ListThe 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.
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 |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:
Or even just:
36 | 37 |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 |null otherwise.
68 | */
69 | public Change getChange() {
70 | return change;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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/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 | ListEvolving 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 |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 |To put it in short. Have a look at the video:
57 | 58 |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/site/xdoc/status.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 | 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).
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 |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 |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 |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.
The version command is a powerful utility for moving to a specific revision of the database.
74 |