Skip to content

Commit

Permalink
Merge pull request #618 from ZakarFin/table-names
Browse files Browse the repository at this point in the history
Update view/appsetup database tables
  • Loading branch information
ZakarFin authored Aug 27, 2020
2 parents df1c8d3 + ad4fb7a commit 964b084
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
import java.util.Set;

/**
* Created with IntelliJ IDEA.
* User: SMAKINEN
* Date: 27.6.2014
* Time: 15:22
* To change this template use File | Settings | File Templates.
* Helpers for flyway scripts. Be very careful when making changes as previous versions of Oskari are using this
* to migrate database.
*/
public class AppsetupHelper {

Expand All @@ -47,7 +44,7 @@ public static long create(Connection conn, final String viewfile)
LayerHelper.refreshLayerCapabilities();
return viewId;
} catch (Exception ex) {
log.error(ex, "Unable to insert view! ");
log.error(ex, "Unable to insert appsetup! ");
throw new ServiceRuntimeException("Unable to insert appsetup", ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void registerBundle(final Bundle bundle, Connection conn) throws S
}

try (PreparedStatement statement = conn
.prepareStatement("INSERT INTO portti_bundle(name, config, state) VALUES(?,?,?)")) {
.prepareStatement("INSERT INTO oskari_bundle(name, config, state) VALUES(?,?,?)")) {
statement.setString(1, bundle.getName());
statement.setString(2, bundle.getConfig());
statement.setString(3, bundle.getState());
Expand All @@ -54,7 +54,7 @@ public static void registerBundle(final Bundle bundle, Connection conn) throws S

public static Bundle getRegisteredBundle(final String id, Connection conn) throws SQLException {
try (PreparedStatement statement = conn
.prepareStatement("SELECT id, name, config, state FROM portti_bundle WHERE name=?")) {
.prepareStatement("SELECT id, name, config, state FROM oskari_bundle WHERE name=?")) {
statement.setString(1, id);
try (ResultSet rs = statement.executeQuery()) {
if (!rs.next()) {
Expand All @@ -72,7 +72,7 @@ public static Bundle getRegisteredBundle(final String id, Connection conn) throw

public static void unregisterBundle(final String bundleName, Connection conn) throws SQLException {
if (isBundleRegistered(bundleName, conn)) {
try (PreparedStatement statement = conn.prepareStatement("DELETE FROM portti_bundle WHERE name=?")) {
try (PreparedStatement statement = conn.prepareStatement("DELETE FROM oskari_bundle WHERE name=?")) {
statement.setString(1, bundleName);
statement.execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
import java.util.List;
import java.util.Map;

/**
* Helpers for flyway scripts. Be very careful when making changes as previous versions of Oskari are using this
* to migrate database.
*/
public class FlywayHelper {
public static List<Long> getViewIdsForTypes(Connection connection, String... types)
throws SQLException {
ArrayList<Long> ids = new ArrayList<>();
StringBuilder sql = new StringBuilder("SELECT id FROM portti_view");
StringBuilder sql = new StringBuilder("SELECT id FROM oskari_appsetup");
if (types != null && types.length > 0) {
sql.append(" WHERE type IN (?");
for (int i = 1; i < types.length; ++i) {
Expand Down Expand Up @@ -60,7 +56,7 @@ public static List<Long> getUserAndDefaultViewIds (Connection conn, String appli
public static List<Long> getViewIdsForApplication(Connection conn, String applicationName, String... types)
throws SQLException {
ArrayList<Long> ids = new ArrayList<>();
StringBuilder sql = new StringBuilder("SELECT id FROM portti_view WHERE application=?");
StringBuilder sql = new StringBuilder("SELECT id FROM oskari_appsetup WHERE application=?");
if (types != null && types.length > 0) {
sql.append(" AND type IN (?");
for (int i = 1; i < types.length; ++i) {
Expand All @@ -87,7 +83,7 @@ public static List<Long> getViewIdsForApplication(Connection conn, String applic
}
public static String getDefaultViewUuid(Connection conn, String applicationName) throws SQLException {
Map<Long, String> uuids = new HashMap<>();
final String sql = "SELECT id, uuid FROM portti_view WHERE application=? and type=?";
final String sql = "SELECT id, uuid FROM oskari_appsetup WHERE application=? and type=?";
try (PreparedStatement statement = conn.prepareStatement(sql)) {
statement.setString(1, applicationName);
statement.setString(2, ViewTypes.DEFAULT);
Expand Down Expand Up @@ -116,9 +112,9 @@ public static String getDefaultViewUuid(Connection conn, String applicationName)

public static boolean viewContainsBundle(Connection connection, String bundle, Long viewId)
throws SQLException {
final String sql ="SELECT * FROM portti_view_bundle_seq " +
"WHERE bundle_id = (SELECT id FROM portti_bundle WHERE name=?) " +
"AND view_id=?";
final String sql ="SELECT * FROM oskari_appsetup_bundles " +
"WHERE bundle_id = (SELECT id FROM oskari_bundle WHERE name=?) " +
"AND appsetup_id=?";

try (final PreparedStatement statement =
connection.prepareStatement(sql)) {
Expand All @@ -132,9 +128,9 @@ public static boolean viewContainsBundle(Connection connection, String bundle, L

public static Bundle getBundleFromView(Connection connection, String bundle, Long viewId)
throws SQLException {
final String sql ="SELECT * FROM portti_view_bundle_seq " +
"WHERE bundle_id = (SELECT id FROM portti_bundle WHERE name=?) " +
"AND view_id=?";
final String sql ="SELECT * FROM oskari_appsetup_bundles " +
"WHERE bundle_id = (SELECT id FROM oskari_bundle WHERE name=?) " +
"AND appsetup_id=?";

try (final PreparedStatement statement =
connection.prepareStatement(sql)){
Expand All @@ -159,13 +155,13 @@ public static Bundle getBundleFromView(Connection connection, String bundle, Lon

public static Bundle updateBundleInView(Connection connection, Bundle bundle, Long viewId)
throws SQLException {
final String sql = "UPDATE portti_view_bundle_seq SET " +
final String sql = "UPDATE oskari_appsetup_bundles SET " +
"config=?, " +
"state=?, " +
"seqno=?, " +
"bundleinstance=? " +
" WHERE bundle_id=? " +
" AND view_id=?";
" AND appsetup_id=?";

try (final PreparedStatement statement =
connection.prepareStatement(sql)) {
Expand All @@ -182,14 +178,14 @@ public static Bundle updateBundleInView(Connection connection, Bundle bundle, Lo

public static void addBundleWithDefaults(Connection connection, Long viewId, String bundleid)
throws SQLException {
final String sql ="INSERT INTO portti_view_bundle_seq" +
"(view_id, bundle_id, seqno, config, state, bundleinstance) " +
final String sql ="INSERT INTO oskari_appsetup_bundles" +
"(appsetup_id, bundle_id, seqno, config, state, bundleinstance) " +
"VALUES (" +
"?, " +
"(SELECT id FROM portti_bundle WHERE name=?), " +
"(SELECT max(seqno)+1 FROM portti_view_bundle_seq WHERE view_id=?), " +
"(SELECT config FROM portti_bundle WHERE name=?), " +
"(SELECT state FROM portti_bundle WHERE name=?), " +
"(SELECT id FROM oskari_bundle WHERE name=?), " +
"(SELECT max(seqno)+1 FROM oskari_appsetup_bundles WHERE appsetup_id=?), " +
"(SELECT config FROM oskari_bundle WHERE name=?), " +
"(SELECT state FROM oskari_bundle WHERE name=?), " +
"?)";
try(final PreparedStatement statement =
connection.prepareStatement(sql)) {
Expand All @@ -205,8 +201,8 @@ public static void addBundleWithDefaults(Connection connection, Long viewId, Str

public static void removeBundleFromView(Connection connection, String bundleName, Long viewId)
throws SQLException {
final String sql ="DELETE FROM portti_view_bundle_seq " +
"WHERE bundle_id = (SELECT id FROM portti_bundle WHERE name=?) AND view_id=?";
final String sql ="DELETE FROM oskari_appsetup_bundles " +
"WHERE bundle_id = (SELECT id FROM oskari_bundle WHERE name=?) AND appsetup_id=?";
try(final PreparedStatement statement =
connection.prepareStatement(sql)) {
statement.setString(1, bundleName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ALTER TABLE portti_bundle RENAME TO oskari_bundle;

ALTER TABLE oskari_bundle
ALTER COLUMN name TYPE TEXT,
ALTER COLUMN config TYPE TEXT,
ALTER COLUMN state TYPE TEXT,
DROP COLUMN startup;

ALTER TABLE portti_view RENAME TO oskari_appsetup;

ALTER TABLE oskari_appsetup
ALTER COLUMN name TYPE TEXT,
ALTER COLUMN description TYPE TEXT,
ALTER COLUMN application TYPE TEXT,
ALTER COLUMN domain TYPE TEXT,
DROP COLUMN application_dev_prefix,
DROP COLUMN old_id;

ALTER TABLE portti_view_bundle_seq RENAME TO oskari_appsetup_bundles;

ALTER TABLE oskari_appsetup_bundles
ALTER COLUMN bundleinstance TYPE TEXT,
DROP COLUMN startup;

ALTER TABLE oskari_appsetup_bundles
RENAME COLUMN view_id TO appsetup_id;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;

public interface AppSetupMapper {
@Select("SELECT MAX(id) FROM portti_view WHERE is_default = TRUE AND type = #{type}")
@Select("SELECT MAX(id) FROM oskari_appsetup WHERE is_default = TRUE AND type = #{type}")
Long getDefaultViewId(String type);
List<View> getViews(final Map<String, Object> params);
View getViewWithConfByViewId(long viewId);
Expand All @@ -18,34 +18,34 @@ public interface AppSetupMapper {
List<View> getViewsWithConfByUserId(long userId);
List<Bundle> getBundlesByViewId(long viewId);

@Insert("INSERT INTO portti_view ( name, \"description\", type, application_dev_prefix, page, application, " +
@Insert("INSERT INTO oskari_appsetup ( name, \"description\", type, page, application, " +
" uuid, only_uuid, domain, lang, creator, is_public, is_default, metadata) " +
" VALUES ( #{name}, #{description}, #{type}, #{developmentPath}, #{page}, #{application}, '${uuid}', ${onlyForUuId}, " +
" VALUES ( #{name}, #{description}, #{type}, #{page}, #{application}, '${uuid}', ${onlyForUuId}, " +
" #{pubDomain}, #{lang}, #{creator}, #{isPublic}, #{isDefault}, #{metadataAsString} )")
@Options(useGeneratedKeys=true, keyColumn = "id", keyProperty = "id")
void addView(View view);

@Update("UPDATE portti_view SET is_public = #{isPublic} WHERE id = #{id}")
@Update("UPDATE oskari_appsetup SET is_public = #{isPublic} WHERE id = #{id}")
void updateAccessFlag(View view);

@Delete("DELETE FROM portti_view_bundle_seq WHERE view_id = #{id}")
@Delete("DELETE FROM oskari_appsetup_bundles WHERE appsetup_id = #{id}")
void deleteBundleByView(long id);

@Delete("DELETE FROM portti_view WHERE id = #{id}")
@Delete("DELETE FROM oskari_appsetup WHERE id = #{id}")
void deleteView(long id);
@Delete("DELETE FROM portti_view WHERE creator = #{userId}")
@Delete("DELETE FROM oskari_appsetup WHERE creator = #{userId}")
void deleteViewByUser(long userId);

@Update("UPDATE portti_view SET is_default = FALSE WHERE creator = #{userId} AND type = 'USER'")
@Update("UPDATE oskari_appsetup SET is_default = FALSE WHERE creator = #{userId} AND type = 'USER'")
void resetUsersDefaultViews(long userId);
void update(View view);
void updateUsage(View view);
void addBundle(Bundle bundle);
int updateBundleSettingsInView(final Map<String, Object> params);

@Select("SELECT id FROM portti_view WHERE is_default = TRUE AND type = 'DEFAULT' AND creator = -1")
@Select("SELECT id FROM oskari_appsetup WHERE is_default = TRUE AND type = 'DEFAULT' AND creator = -1")
List<Long> getDefaultViewIds();

@Select("SELECT MAX(id) FROM portti_view WHERE is_default = TRUE AND type = 'USER' AND creator = #{userId}")
@Select("SELECT MAX(id) FROM oskari_appsetup WHERE is_default = TRUE AND type = 'USER' AND creator = #{userId}")
Long geDefaultViewIdByUserId(long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

public interface BundleMapper {

@Select("select id, name, startup, config, state from portti_bundle where name = #{name}")
@Select("select id, name, config, state from oskari_bundle where name = #{name}")
@Results({
@Result(property = "bundleId", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "startup", column = "startup"),
@Result(property = "state", column = "state"),
@Result(property = "config", column = "config")
})
Bundle getBundleTemplateByName(final String name);

@Insert("insert into portti_bundle (name, startup, config, state ) values ( #{name}, #{startup}, #{config}, #{state} )")
@Insert("insert into oskari_bundle (name, config, state ) values ( #{name}, #{config}, #{state} )")
@Options(useGeneratedKeys=true, keyColumn = "id", keyProperty = "id")
long addBundleTemplate(final Bundle bundle);

Expand Down
Loading

0 comments on commit 964b084

Please sign in to comment.