Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java database migration example #82

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions java/databaseMigration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties

# IDE files
.idea/
*.iml
.vscode/
.settings/
.project
.classpath
149 changes: 149 additions & 0 deletions java/databaseMigration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Database Migration

This example provisions an AWS Aurora SQL database and executes a database "migration" using the resulting connection info. This migration creates a table, inserts a few rows of data, and reads the data back to verify the setup. This is all done in a single `inline` Pulumi program. With Automation API you can orchestrate complex workflows that go beyond infrastructure provisioning and into application management, database setup, etc.

To run this example you'll need a few pre-reqs:
1. A Pulumi CLI installation ([v3.149.0](https://www.pulumi.com/docs/iac/download-install/) or later)
2. The AWS CLI, with appropriate credentials.
3. Install [Java 11 or later](https://www.oracle.com/java/technologies/downloads) and [Apache Maven 3.6.1 or later](https://maven.apache.org/install.html).

Running this program is just like any other Java program with Maven. You can run `mvn -q compile exec:java` from the project directory.


```shell
$ mvn -q compile exec:java
successfully initialized stack
installing plugins...
plugins installed
setting up config...
config set
refreshing stack...
Refreshing (dev)

View Live: https://app.pulumi.com/user/database_migration_project/dev/updates/1


Resources:

Duration: 1s

refresh complete
updating stack...
Updating (dev)

View Live: https://app.pulumi.com/user/database_migration_project/dev/updates/2


@ Updating.....
+ pulumi:pulumi:Stack database_migration_project-dev creating (0s)
+ aws:ec2:SecurityGroup public-security-group creating (0s)
@ Updating....
+ aws:rds:SubnetGroup db-subnet creating (0s)
@ Updating....
+ aws:rds:SubnetGroup db-subnet created (1s)
@ Updating....
+ aws:ec2:SecurityGroup public-security-group created (2s)
+ aws:rds:Cluster db creating (0s)
@ Updating.......................................................
+ aws:rds:Cluster db created (51s)
+ aws:rds:ClusterInstance db-instance creating (0s)
@ Updating..........................................................................................................................................................................................................................................................................................................................................................................................................................
+ aws:rds:ClusterInstance db-instance created (406s)
@ Updating....
+ pulumi:pulumi:Stack database_migration_project-dev created (462s)
Outputs:
db_name: "hellosql"
db_pass: "hellosql"
db_user: "hellosql"
host : "tf-20250210030519737400000001.cluster-chuqccm8uxqx.us-west-2.rds.amazonaws.com"

Resources:
+ 5 created

Duration: 7m45s

update summary:
CREATE: $d
db host url: tf-20250210030519737400000001.cluster-chuqccm8uxqx.us-west-2.rds.amazonaws.com
configuring db...
db configured!
rows inserted!
querying to verify data...
Result: 3 rows
database, table, and rows successfully configured
```

To destroy the stack when you're done, invoke the program with an additional `destroy` argument:

```shell
$ mvn -q compile exec:java -Dexec.args="destroy"
successfully initialized stack
installing plugins...
plugins installed
setting up config...
config set
refreshing stack...
Refreshing (dev)

View Live: https://app.pulumi.com/user/database_migration_project/dev/updates/3


@ Refreshing....
~ pulumi:pulumi:Stack database_migration_project-dev refreshing (0s)
~ aws:rds:SubnetGroup db-subnet refreshing (0s)
~ aws:ec2:SecurityGroup public-security-group refreshing (0s)
~ aws:rds:ClusterInstance db-instance refreshing (0s)
pulumi:pulumi:Stack database_migration_project-dev running
~ aws:rds:Cluster db refreshing (0s)
aws:ec2:SecurityGroup public-security-group
@ Refreshing....
aws:rds:Cluster db
aws:rds:SubnetGroup db-subnet
aws:rds:ClusterInstance db-instance
pulumi:pulumi:Stack database_migration_project-dev
Outputs:
db_name: "hellosql"
db_pass: "hellosql"
db_user: "hellosql"
host : "tf-20250210030519737400000001.cluster-chuqccm8uxqx.us-west-2.rds.amazonaws.com"

Resources:
5 unchanged

Duration: 3s

refresh complete
destroying stack...
Destroying (dev)

View Live: https://app.pulumi.com/user/database_migration_project/dev/updates/4


- aws:rds:ClusterInstance db-instance deleting (0s)
@ Destroying...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
- aws:rds:ClusterInstance db-instance deleted (679s)
- aws:rds:Cluster db deleting (0s)
@ Destroying......................................................
- aws:rds:Cluster db deleted (50s)
- aws:rds:SubnetGroup db-subnet deleting (0s)
- aws:ec2:SecurityGroup public-security-group deleting (0s)
- aws:rds:SubnetGroup db-subnet deleted (0.21s)
@ Destroying....
- aws:ec2:SecurityGroup public-security-group deleted (0.92s)
- pulumi:pulumi:Stack database_migration_project-dev deleting (0s)
- pulumi:pulumi:Stack database_migration_project-dev deleted (0.09s)
Outputs:
- db_name: "hellosql"
- db_pass: "hellosql"
- db_user: "hellosql"
- host : "tf-20250210030519737400000001.cluster-chuqccm8uxqx.us-west-2.rds.amazonaws.com"

Resources:
- 5 deleted

Duration: 12m13s

The resources in the stack have been deleted, but the history and configuration associated with the stack are still maintained.
If you want to remove the stack completely, run `pulumi stack rm dev`.
stack destroy complete
```
46 changes: 46 additions & 0 deletions java/databaseMigration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>pulumi</artifactId>
<version>[1.3,2.0)</version>
</dependency>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>aws</artifactId>
<version>6.68.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>com.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading
Loading