Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-szalwinski committed Feb 3, 2020
0 parents commit 77500d1
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vscode
.settings
.history
.project
.classpath
target/
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# liquibase-snowflake-sample <!-- omit in toc -->

- [Dependencies](#dependencies)
- [Setup](#setup)
- [Creating changes](#creating-changes)
- [Applying changes](#applying-changes)
- [Resolving common issues](#resolving-common-issues)
- [Session does not have a current database](#session-does-not-have-a-current-database)
- [Issue](#issue)
- [Resolution](#resolution)
- [No active warehouse](#no-active-warehouse)
- [Issue](#issue-1)
- [Resolution](#resolution-1)


# Dependencies

This project has a parent pom, [liquibase-snowflake-db-build](https://github.com/bruce-szalwinski/liquibase-snowflake-db-build), that manages the common dependencies. Install that before continuing.

# Setup

Create a file called `db.properties` containing the contents below. Store this file on your file system.

```bash
# update with your account information
snowflake.account=12345
db.username=my_user
db.password=my_password
db.database=my_database
db.warehouse=my_warehouse
db.schema=my_schema

db.host=${snowflake.account}.snowflakecomputing.com
db.url=jdbc:snowflake://${db.host}/?db=${db.database}&warehouse=${db.warehouse}&schema=${db.schema}
db.driver=net.snowflake.client.jdbc.SnowflakeDriver

liquibase.verbose: true
liquibase.dropFirst: false
liquibase.logging: debug
liquibase.driver: ${db.driver}
liquibase.url: ${db.url}
liquibase.username: ${db.username}
liquibase.password: ${db.password}
liquibase.defaultCatalogName: ${db.database}
liquibase.defaultSchemaName: ${db.username}
liquibase.contexts:
```

Define an environment variable `DB_PROPERTIES` that points at the location of this file.

```bash
export DB_PROPERTIES=$HOME/db.properties
```

# Creating changes

The [master.xml](src/main/resources/master.xml) is the top level [databasechangelog](https://www.liquibase.org/documentation/databasechangelog.html) for the project. It contains a list of changelogs, organized by year. Each of the yearly changelogs contains a list of changes for that year. To add a change, create a file in the yearly directory and update the `changelog.xml` in that directory to include the newly created file. See [changelog](src/main/resources/changesets/2020/changelog.xml) as an example.

This project currently just uses `plain SQL` [changelog files](https://www.liquibase.org/documentation/sql_format.html). Each changelog file contains one or more `changeset` entries, with each entry having an `author:id` tag. See [BPS-1.sql](src/main/resources/changesets/2020/BPS-1.sql) for an example.


# Applying changes

With the changes in place, apply them with:

```bash
mvn install
```

# Resolving common issues
## Session does not have a current database

### Issue
```bash
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.8.5:update (default) on project sample-app: Error setting up or running Liquibase: liquibase.exception.DatabaseException: Cannot perform CREATE TABLE. This session does not have a current database. Call 'USE DATABASE', or use a qualified name. [Failed SQL: CREATE TABLE MY_SCHEMA.DATABASECHANGELOGLOCK (ID INT NOT NULL, LOCKED BOOLEAN NOT NULL, LOCKGRANTED TIMESTAMP_NTZ, LOCKEDBY VARCHAR(255), CONSTRAINT PK_DATABASECHANGELOGLOCK PRIMARY KEY (ID))] -> [Help 1]
[ERROR]
```

### Resolution
Ensure that the user has a default role.
```sql
alter user my_user set default_role = my_default_role
```

## No active warehouse

### Issue
```bash
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.8.5:update (default) on project sample-app: Error setting up or running Liquibase: liquibase.exception.DatabaseException: No active warehouse selected in the current session. Select an active warehouse with the 'use warehouse' command.
[ERROR] [Failed SQL: DELETE FROM MY_SCHEMA.DATABASECHANGELOGLOCK]
[ERROR] -> [Help 1]
```

### Resolution

In the db.properties file, set `defaultSchema` equal to `db.username`.
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<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>

<parent>
<groupId>com.lazydba.db</groupId>
<artifactId>db-build</artifactId>
<version>1.0.0</version>
</parent>

<groupId>com.lazydba.db</groupId>
<artifactId>sample-app</artifactId>
<version>trunk-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sample-app</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
10 changes: 10 additions & 0 deletions src/main/resources/changesets/2020/BPS-1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--liquibase formatted sql

--changeset lazydba:BPS-1-1
DROP TABLE IF EXISTS employees;

--changeset lazydba:BPS-1-2
CREATE TABLE employees (
employee_id character varying(100),
employee_name character varying(100)
)
11 changes: 11 additions & 0 deletions src/main/resources/changesets/2020/changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">

<include file="BPS-1.sql" relativeToChangelogFile="true" />

</databaseChangeLog>
11 changes: 11 additions & 0 deletions src/main/resources/db.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contexts=${liquibase.contexts}
verbose=${liquibase.verbose}
dropFirst=${liquibase.dropFirst}
logging=${liquibase.logging}

driver=${liquibase.driver}
url=${liquibase.url}
defaultCatalogName=${liquibase.defaultCatalogName}
defaultSchemaName=${liquibase.defaultSchemaName}
username=${liquibase.username}
password=${liquibase.password}
12 changes: 12 additions & 0 deletions src/main/resources/master.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<include file="changesets/2020/changelog.xml" relativeToChangelogFile="true" />

</databaseChangeLog>

0 comments on commit 77500d1

Please sign in to comment.