[Fix] Fixed migration scripts and added missing states in DB

This commit is contained in:
Robert von Burg 2017-04-11 11:05:07 +02:00
parent c835ef2735
commit 476bd0f728
6 changed files with 106 additions and 24 deletions

View File

@ -190,7 +190,7 @@ public class PostgreSqlActivityDao extends PostgresqlDao<Activity> implements Ac
}
String sql = "update " + getTableName()
+ " set created_by = ?, created_at = ?, deleted = ?, latest = ?, name = ?, type = ?, state = ?, asxml = ? where id = ? and version = ?";
+ " set created_by = ?, created_at = ?, deleted = ?, latest = ?, name = ?, type = ?, state = ?::order_state, asxml = ? where id = ? and version = ?";
try (PreparedStatement preparedStatement = tx().getConnection().prepareStatement(sql)) {

View File

@ -0,0 +1,26 @@
-- AUDITS
CREATE TYPE access_type AS ENUM ('READ', 'CREATE', 'UPDATE', 'DELETE');
CREATE TABLE IF NOT EXISTS audits (
id bigint PRIMARY KEY,
username VARCHAR(255) NOT NULL,
firstname VARCHAR(255) NOT NULL,
lastname VARCHAR(255) NOT NULL,
date timestamp with time zone NOT NULL,
element_type VARCHAR(255) NOT NULL,
element_accessed VARCHAR(255) NOT NULL,
new_version timestamp with time zone,
action VARCHAR(255) NOT NULL,
access_type access_type NOT NULL
);
-- set version
INSERT INTO db_version
(version, description, created)
values(
'0.2.0',
'Added new table for audits',
CURRENT_TIMESTAMP
);

View File

@ -8,4 +8,4 @@ DROP TABLE IF EXISTS audits;
DROP TABLE IF EXISTS db_version;
DROP TYPE IF EXISTS order_state;
DROP TYPE IF EXISTS access_type;
DROP TYPE IF EXISTS access_type;

View File

@ -24,7 +24,8 @@ CREATE TABLE IF NOT EXISTS resources (
);
-- ORDERS
CREATE TYPE order_state AS ENUM ('CREATED', 'OPEN', 'EXECUTION', 'CLOSED');
CREATE TYPE order_state AS ENUM ('CREATED', 'PLANNING', 'PLANNED', 'EXECUTION', 'STOPPED', 'WARNING', 'ERROR', 'EXECUTED', 'CLOSED');
CREATE TABLE IF NOT EXISTS orders (
id varchar(255) not null,
@ -136,6 +137,6 @@ INSERT INTO db_version
values(
'0.5.1',
'strolch',
'Added state column to activity',
'Added state column to activity, and added new states',
CURRENT_TIMESTAMP
);

View File

@ -1,5 +1,15 @@
-- add version columns
CREATE TYPE order_state1 AS ENUM ('CREATED', 'PLANNING', 'PLANNED', 'EXECUTION', 'STOPPED', 'WARNING', 'ERROR', 'EXECUTED', 'CLOSED');
-- Convert to new type, casting via text representation
ALTER TABLE orders
ALTER COLUMN state TYPE order_state1
USING (state::text::order_state1);
DROP TYPE order_state;
ALTER TYPE order_state1 RENAME TO order_state;
-- add state columns
ALTER TABLE activities ADD COLUMN state order_state;
INSERT INTO db_version
@ -7,6 +17,6 @@ INSERT INTO db_version
values(
'0.5.1',
'strolch',
'Added state column to activity',
'Added state column to activity, and added new states',
CURRENT_TIMESTAMP
);

View File

@ -20,10 +20,17 @@ import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_URL;
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_USERNAME;
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.dropSchema;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import li.strolch.db.DbException;
import li.strolch.db.DbSchemaVersionCheck;
@ -31,40 +38,78 @@ import li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler;
import li.strolch.runtime.StrolchConstants;
import li.strolch.utils.Version;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class DbMigrationTest {
@BeforeClass
public static void beforeClass() throws Exception {
private static final Logger logger = LoggerFactory.getLogger(DbMigrationTest.class);
@Before
public void before() throws Exception {
dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD);
}
@Test
public void shouldCreate() throws Exception {
DbSchemaVersionCheck dbCheck = new DbSchemaVersionCheck(PostgreSqlPersistenceHandler.SCRIPT_PREFIX,
PostgreSqlPersistenceHandler.class, true, true, true);
try (Connection con = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)) {
// CREATE 0.1.0
dbCheck.createSchema(con, StrolchConstants.DEFAULT_REALM, Version.valueOf("0.1.0"));
File scriptsD = new File("src/main/resources");
File[] scriptFiles = scriptsD.listFiles(f -> f.getName().endsWith("_initial.sql"));
Arrays.sort(scriptFiles, (f1, f2) -> f1.getName().compareTo(f2.getName()));
for (File scriptFile : scriptFiles) {
String name = scriptFile.getName();
String versionS = name.substring("strolch_db_schema_".length(),
name.length() - "_initial.sql".length());
Version version = Version.valueOf(versionS);
logger.info("Creating Version " + version);
dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD);
// CREATE
dbCheck.createSchema(con, StrolchConstants.DEFAULT_REALM, version);
}
} catch (SQLException e) {
String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, DB_URL, e.getMessage());
throw new DbException(msg, e);
}
}
@Test
public void shouldMigrate() throws Exception {
String scriptPrefix = PostgreSqlPersistenceHandler.SCRIPT_PREFIX;
Class<?> ctxClass = PostgreSqlPersistenceHandler.class;
boolean allowSchemaCreation = true;
boolean allowSchemaMigration = true;
boolean allowSchemaDrop = true;
DbSchemaVersionCheck dbCheck = new DbSchemaVersionCheck(scriptPrefix, ctxClass, allowSchemaCreation,
allowSchemaMigration, allowSchemaDrop);
DbSchemaVersionCheck dbCheck = new DbSchemaVersionCheck(PostgreSqlPersistenceHandler.SCRIPT_PREFIX,
PostgreSqlPersistenceHandler.class, true, true, true);
try (Connection con = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)) {
// DROP 0.2.1
dbCheck.dropSchema(con, StrolchConstants.DEFAULT_REALM, Version.valueOf("0.2.1"));
// CREATE 0.1.0
dbCheck.createSchema(con, StrolchConstants.DEFAULT_REALM, Version.valueOf("0.1.0"));
// CREATE 0.2.0
dbCheck.createSchema(con, StrolchConstants.DEFAULT_REALM, Version.valueOf("0.2.0"));
File scriptsD = new File("src/main/resources");
File[] scriptFiles = scriptsD.listFiles(f -> f.getName().endsWith("_migration.sql"));
Arrays.sort(scriptFiles, (f1, f2) -> f1.getName().compareTo(f2.getName()));
for (File scriptFile : scriptFiles) {
// MIGRATE 0.2.1
dbCheck.migrateSchema(con, StrolchConstants.DEFAULT_REALM, Version.valueOf("0.2.1"));
String name = scriptFile.getName();
String versionS = name.substring("strolch_db_schema_".length(),
name.length() - "_migration.sql".length());
Version version = Version.valueOf(versionS);
logger.info("Migrating Version " + version);
// MIGRATE
dbCheck.migrateSchema(con, StrolchConstants.DEFAULT_REALM, version);
}
} catch (SQLException e) {
String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$