altered table update:
now retaining the template name values from the template column as new template values in the document table Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -4,7 +4,6 @@ package de.srsoftware.umbrella.documents;
|
|||||||
import de.srsoftware.tools.Pair;
|
import de.srsoftware.tools.Pair;
|
||||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||||
import de.srsoftware.umbrella.core.model.Document;
|
import de.srsoftware.umbrella.core.model.Document;
|
||||||
import de.srsoftware.umbrella.core.model.Template;
|
|
||||||
import de.srsoftware.umbrella.core.model.Type;
|
import de.srsoftware.umbrella.core.model.Type;
|
||||||
import de.srsoftware.umbrella.documents.model.*;
|
import de.srsoftware.umbrella.documents.model.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|||||||
@@ -42,6 +42,15 @@ public class SqliteDb extends BaseDb implements DocumentDb{
|
|||||||
super(connection);
|
super(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addTemplateColumn() {
|
||||||
|
try {
|
||||||
|
var sql = format("ALTER TABLE {0} ADD COLUMN {1} VARCHAR(255)",TABLE_DOCUMENTS,TEMPLATE);
|
||||||
|
db.prepareStatement(sql).execute();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw databaseException("Failed to update column {0} → {1} of {2}",TEMPLATE_ID,TEMPLATE,TABLE_DOCUMENTS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected int createTables() {
|
protected int createTables() {
|
||||||
int currentVersion = createSettingsTable();
|
int currentVersion = createSettingsTable();
|
||||||
switch (currentVersion) {
|
switch (currentVersion) {
|
||||||
@@ -53,23 +62,14 @@ public class SqliteDb extends BaseDb implements DocumentDb{
|
|||||||
createTableCustomerPrices();
|
createTableCustomerPrices();
|
||||||
createTableCustomerSettings();
|
createTableCustomerSettings();
|
||||||
case 1:
|
case 1:
|
||||||
|
addTemplateColumn();
|
||||||
|
moveTemplateNames();
|
||||||
dropTemplateTable();
|
dropTemplateTable();
|
||||||
alterTemplateColumn();
|
dropTemplateIdColumn();
|
||||||
}
|
}
|
||||||
return setCurrentVersion(2);
|
return setCurrentVersion(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void alterTemplateColumn() {
|
|
||||||
try {
|
|
||||||
var sql = format("ALTER TABLE {0} DROP COLUMN {1}",TABLE_DOCUMENTS,TEMPLATE_ID);
|
|
||||||
db.prepareStatement(sql).execute();
|
|
||||||
sql = format("ALTER TABLE {0} ADD COLUMN {1} VARCHAR(255)",TABLE_DOCUMENTS,TEMPLATE);
|
|
||||||
db.prepareStatement(sql).execute();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw databaseException("Failed to update column {0} → {1} of {2}",TEMPLATE_ID,TEMPLATE,TABLE_DOCUMENTS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createTableCustomerPrices() {
|
private void createTableCustomerPrices() {
|
||||||
var sql = "CREATE TABLE IF NOT EXISTS {0} ({1} INT NOT NULL, {2} VARCHAR(255), {3} VARCHAR(50), {4} INTEGER)";
|
var sql = "CREATE TABLE IF NOT EXISTS {0} ({1} INT NOT NULL, {2} VARCHAR(255), {3} VARCHAR(50), {4} INTEGER)";
|
||||||
try {
|
try {
|
||||||
@@ -174,36 +174,6 @@ CREATE TABLE IF NOT EXISTS {0} (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int createTableSettings() {
|
|
||||||
var createTable = """
|
|
||||||
CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255) NOT NULL);
|
|
||||||
""";
|
|
||||||
try {
|
|
||||||
var stmt = db.prepareStatement(format(createTable,TABLE_SETTINGS, KEY, VALUE));
|
|
||||||
stmt.execute();
|
|
||||||
stmt.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_SETTINGS,e);
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer version = null;
|
|
||||||
try {
|
|
||||||
var rs = select(VALUE).from(TABLE_SETTINGS).where(KEY, equal(DB_VERSION)).exec(db);
|
|
||||||
if (rs.next()) version = rs.getInt(VALUE);
|
|
||||||
rs.close();
|
|
||||||
if (version == null) {
|
|
||||||
version = INITIAL_DB_VERSION;
|
|
||||||
insertInto(TABLE_SETTINGS, KEY, VALUE).values(DB_VERSION,version).execute(db).close();
|
|
||||||
}
|
|
||||||
|
|
||||||
return version;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
LOG.log(ERROR,ERROR_READ_TABLE,DB_VERSION,TABLE_SETTINGS,e);
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createTableTemplates() {
|
private void createTableTemplates() {
|
||||||
var createTable = "CREATE TABLE IF NOT EXISTS {0} ({1} INTEGER PRIMARY KEY, {2} INT NOT NULL, {3} VARCHAR(255) NOT NULL, {4} BLOB)";
|
var createTable = "CREATE TABLE IF NOT EXISTS {0} ({1} INTEGER PRIMARY KEY, {2} INT NOT NULL, {3} VARCHAR(255) NOT NULL, {4} BLOB)";
|
||||||
try {
|
try {
|
||||||
@@ -254,6 +224,15 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void dropTemplateIdColumn() {
|
||||||
|
try {
|
||||||
|
var sql = format("ALTER TABLE {0} DROP COLUMN {1}",TABLE_DOCUMENTS,TEMPLATE_ID);
|
||||||
|
db.prepareStatement(sql).execute();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw databaseException("Failed to update column {0} → {1} of {2}",TEMPLATE_ID,TEMPLATE,TABLE_DOCUMENTS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void dropTemplateTable() {
|
private void dropTemplateTable() {
|
||||||
try {
|
try {
|
||||||
var sql = format("DROP TABLE IF EXISTS {0};",TABLE_TEMPLATES);
|
var sql = format("DROP TABLE IF EXISTS {0};",TABLE_TEMPLATES);
|
||||||
@@ -304,11 +283,7 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
|||||||
}
|
}
|
||||||
throw new UmbrellaException(500,"No type with id = {0}",typeId);
|
throw new UmbrellaException(500,"No type with id = {0}",typeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
|
||||||
var version = createTables();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<Long, Map<Long, String>> docReferencedByTimes(Set<Long> timeIds) throws UmbrellaException {
|
public Map<Long, Map<Long, String>> docReferencedByTimes(Set<Long> timeIds) throws UmbrellaException {
|
||||||
try {
|
try {
|
||||||
@@ -449,6 +424,15 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
|||||||
throw new UmbrellaException(500,"Failed to load document {0}.",docId);
|
throw new UmbrellaException(500,"Failed to load document {0}.",docId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void moveTemplateNames() {
|
||||||
|
try {
|
||||||
|
var sql = format("UPDATE {0} SET template = (SELECT name FROM templates WHERE templates.id = documents.template_id);",TABLE_DOCUMENTS);
|
||||||
|
db.prepareStatement(sql).execute();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw databaseException("Failed to move template.names to document.templates!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String nextDocId(String language, long companyId, Type type) {
|
public String nextDocId(String language, long companyId, Type type) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -214,9 +214,9 @@
|
|||||||
<th>{t('template')}:</th>
|
<th>{t('template')}:</th>
|
||||||
<td>
|
<td>
|
||||||
{#if editable}
|
{#if editable}
|
||||||
<TemplateSelector company={doc.company.id} bind:value={doc.template} onchange={() => update('template_id',doc.template)} />
|
<TemplateSelector company={doc.company.id} bind:value={doc.template} onchange={() => update('template',doc.template)} />
|
||||||
{:else}
|
{:else}
|
||||||
{doc.template.name}
|
{doc.template}
|
||||||
{/if}
|
{/if}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user