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

[featurn] support flyway database migration #1875

Merged
merged 14 commits into from
Apr 28, 2024
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
12 changes: 11 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,8 @@
"avatar_url": "https://avatars.githubusercontent.com/u/71874373?v=4",
"profile": "https://github.com/gjjjj0101",
"contributions": [
"code"
"code",
"design"
]
},
{
Expand Down Expand Up @@ -1780,6 +1781,15 @@
"contributions": [
"code"
]
},
{
"login": "LLP2333",
"name": "llp2333",
"avatar_url": "https://avatars.githubusercontent.com/u/61670545?v=4",
"profile": "https://github.com/LLP2333",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ Thanks to these wonderful people, welcome to join us:
<td align="center" valign="top" width="14.28%"><a href="http://sharehoo.cn/"><img src="https://avatars.githubusercontent.com/u/45377370?v=4?s=100" width="100px;" alt="miki"/><br /><sub><b>miki</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=miki-hmt" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://codeflex.substack.com/"><img src="https://avatars.githubusercontent.com/u/85513042?v=4?s=100" width="100px;" alt="Keshav Carpenter"/><br /><sub><b>Keshav Carpenter</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=alpha951" title="Code">💻</a> <a href="https://github.com/apache/hertzbeat/commits?author=alpha951" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/makechoicenow"><img src="https://avatars.githubusercontent.com/u/9911918?v=4?s=100" width="100px;" alt="makechoicenow"/><br /><sub><b>makechoicenow</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=makechoicenow" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/gjjjj0101"><img src="https://avatars.githubusercontent.com/u/71874373?v=4?s=100" width="100px;" alt="Gao Jian"/><br /><sub><b>Gao Jian</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=gjjjj0101" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/gjjjj0101"><img src="https://avatars.githubusercontent.com/u/71874373?v=4?s=100" width="100px;" alt="Gao Jian"/><br /><sub><b>Gao Jian</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=gjjjj0101" title="Code">💻</a> <a href="#design-gjjjj0101" title="Design">🎨</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://jangto.tistory.com/"><img src="https://avatars.githubusercontent.com/u/37864182?v=4?s=100" width="100px;" alt="Hyeon Sung"/><br /><sub><b>Hyeon Sung</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=dukbong" title="Code">💻</a></td>
Expand All @@ -436,6 +436,9 @@ Thanks to these wonderful people, welcome to join us:
<td align="center" valign="top" width="14.28%"><a href="https://github.com/zuobiao-zhou"><img src="https://avatars.githubusercontent.com/u/61108539?v=4?s=100" width="100px;" alt="Zhang Yuxuan"/><br /><sub><b>Zhang Yuxuan</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=zuobiao-zhou" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Pzz-2021"><img src="https://avatars.githubusercontent.com/u/118056735?v=4?s=100" width="100px;" alt="P.P."/><br /><sub><b>P.P.</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=Pzz-2021" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/LLP2333"><img src="https://avatars.githubusercontent.com/u/61670545?v=4?s=100" width="100px;" alt="llp2333"/><br /><sub><b>llp2333</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=LLP2333" title="Code">💻</a></td>
</tr>
</tbody>
</table>

Expand Down
5 changes: 4 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ Thanks these wonderful people, welcome to join us:
<td align="center" valign="top" width="14.28%"><a href="http://sharehoo.cn/"><img src="https://avatars.githubusercontent.com/u/45377370?v=4?s=100" width="100px;" alt="miki"/><br /><sub><b>miki</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=miki-hmt" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://codeflex.substack.com/"><img src="https://avatars.githubusercontent.com/u/85513042?v=4?s=100" width="100px;" alt="Keshav Carpenter"/><br /><sub><b>Keshav Carpenter</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=alpha951" title="Code">💻</a> <a href="https://github.com/apache/hertzbeat/commits?author=alpha951" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/makechoicenow"><img src="https://avatars.githubusercontent.com/u/9911918?v=4?s=100" width="100px;" alt="makechoicenow"/><br /><sub><b>makechoicenow</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=makechoicenow" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/gjjjj0101"><img src="https://avatars.githubusercontent.com/u/71874373?v=4?s=100" width="100px;" alt="Gao Jian"/><br /><sub><b>Gao Jian</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=gjjjj0101" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/gjjjj0101"><img src="https://avatars.githubusercontent.com/u/71874373?v=4?s=100" width="100px;" alt="Gao Jian"/><br /><sub><b>Gao Jian</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=gjjjj0101" title="Code">💻</a> <a href="#design-gjjjj0101" title="Design">🎨</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://jangto.tistory.com/"><img src="https://avatars.githubusercontent.com/u/37864182?v=4?s=100" width="100px;" alt="Hyeon Sung"/><br /><sub><b>Hyeon Sung</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=dukbong" title="Code">💻</a></td>
Expand All @@ -435,6 +435,9 @@ Thanks these wonderful people, welcome to join us:
<td align="center" valign="top" width="14.28%"><a href="https://github.com/zuobiao-zhou"><img src="https://avatars.githubusercontent.com/u/61108539?v=4?s=100" width="100px;" alt="Zhang Yuxuan"/><br /><sub><b>Zhang Yuxuan</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=zuobiao-zhou" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Pzz-2021"><img src="https://avatars.githubusercontent.com/u/118056735?v=4?s=100" width="100px;" alt="P.P."/><br /><sub><b>P.P.</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=Pzz-2021" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/LLP2333"><img src="https://avatars.githubusercontent.com/u/61670545?v=4?s=100" width="100px;" alt="llp2333"/><br /><sub><b>llp2333</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=LLP2333" title="Code">💻</a></td>
</tr>
</tbody>
</table>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ public List<String> convertToEntityAttribute(String dbData) {
if (StringUtils.isBlank(dbData)) {
return List.of();
}
if (!JsonUtil.isJsonStr(dbData)) {
return List.of(dbData);
}
TypeReference<List<String>> typeReference = new TypeReference<>() {
};
List<String> stringList = JsonUtil.fromJson(dbData, typeReference);
if (stringList == null && !dbData.isEmpty()) {
return List.of(dbData);
} else return stringList;
} else {
return stringList;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ public static JsonNode fromJson(String jsonStr) {
return null;
}
}

public static boolean isJsonStr(String jsonStr) {
if (!StringUtils.hasText(jsonStr)) {
return false;
}
try {
OBJECT_MAPPER.readTree(jsonStr);
return true;
} catch (Exception ignored) {
return false;
}
}
}
8 changes: 7 additions & 1 deletion home/src/pages/team/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,8 @@ export default function () {
<td align="center" valign="top" width="14.28%"><a href="https://github.com/gjjjj0101"><img
src="https://avatars.githubusercontent.com/u/71874373?v=4?s=100" width="100px;"
alt="Gao Jian"/><br/><sub><b>Gao Jian</b></sub></a><br/><a
href="https://github.com/apache/hertzbeat/commits?author=gjjjj0101" title="Code">💻</a></td>
href="https://github.com/apache/hertzbeat/commits?author=gjjjj0101" title="Code">💻</a> <a
href="#design-gjjjj0101" title="Design">🎨</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://jangto.tistory.com/"><img
Expand Down Expand Up @@ -1013,6 +1014,11 @@ export default function () {
alt="P.P."/><br/><sub><b>P.P.</b></sub></a><br/><a
href="https://github.com/apache/hertzbeat/commits?author=Pzz-2021" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/LLP2333"><img
src="https://avatars.githubusercontent.com/u/61670545?v=4?s=100" width="100px;" alt="llp2333"/><br/><sub><b>llp2333</b></sub></a><br/><a
href="https://github.com/apache/hertzbeat/commits?author=LLP2333" title="Code">💻</a></td>
</tr>
</tbody>
</table>

Expand Down
14 changes: 14 additions & 0 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- database migration -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
Expand Down Expand Up @@ -194,6 +207,7 @@
<include>*.xml</include>
<include>banner.txt</include>
<include>define/**</include>
<include>db/**</include>
<include>templates/**</include>
<include>**/*.html</include>
</includes>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.manager.config;

import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer;
import org.springframework.boot.autoconfigure.flyway.FlywayProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
* flyway database migration config
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", havingValue = "true")
public class FlywayConfiguration {

@Bean
public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, (f) -> {
});
}

static class Dummy {
}

@Bean
@DependsOn("entityManagerFactory")
Dummy delayedFlywayInitializer(Flyway flyway, FlywayProperties flywayProperties) {
if (flywayProperties.isEnabled()) {
flyway.migrate();
}
return new Dummy();
}
}
2 changes: 2 additions & 0 deletions manager/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ spring:
eclipselink:
logging:
level: SEVERE
flyway:
enabled: false
mail:
host: smtp.exmail.qq.com
username: example@tancloud.cn
Expand Down
8 changes: 8 additions & 0 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ spring:
logging:
level: SEVERE

flyway:
enabled: true
clean-disabled: true
baseline-on-migrate: true
baseline-version: 1
locations:
- classpath:db/migration/{vendor}

mail:
# Attention: this is mail server address.
# 请注意此为邮件服务器地址:qq邮箱为 smtp.qq.com qq 企业邮箱为 smtp.exmail.qq.com
Expand Down
30 changes: 30 additions & 0 deletions manager/src/main/resources/db/migration/h2/V160__update_column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- ensure every sql can rerun without error
tomsun28 marked this conversation as resolved.
Show resolved Hide resolved

ALTER TABLE HZB_PARAM ADD COLUMN IF NOT EXISTS "value" VARCHAR(255);
UPDATE HZB_PARAM SET param_value = "value" WHERE "value" IS NOT NULL AND param_value IS NULL;
ALTER TABLE HZB_PARAM DROP COLUMN "value";

ALTER TABLE HZB_TAG ADD COLUMN IF NOT EXISTS "value" VARCHAR(255);
UPDATE HZB_TAG SET tag_value = "value" WHERE "value" IS NOT NULL AND tag_value IS NULL;
ALTER TABLE HZB_TAG DROP COLUMN "value";

ALTER TABLE HZB_STATUS_PAGE_HISTORY ADD COLUMN IF NOT EXISTS "unknown" integer;
UPDATE HZB_STATUS_PAGE_HISTORY SET unknowing = "unknown" WHERE "unknown" IS NOT NULL AND unknowing IS NULL;
ALTER TABLE HZB_STATUS_PAGE_HISTORY DROP COLUMN "unknown";
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- ensure every sql can rerun without error

-- for to hertzbeat v1.6.0 upgrade in mysql, user need run this by themself, ignore for non-upgrading -> 1.6.0 users

# UPDATE hzb_param SET param_value = `value` WHERE `value` IS NOT NULL AND param_value IS NULL;
# ALTER TABLE hzb_param DROP COLUMN `value`;
# commit;
#
# UPDATE hzb_tag SET tag_value = `value` WHERE `value` IS NOT NULL AND tag_value IS NULL;
# ALTER TABLE hzb_tag DROP COLUMN `value`;
# commit;
#
# UPDATE hzb_status_page_history SET unknowing = `unknown` WHERE `unknown` IS NOT NULL AND unknowing IS NULL;
# ALTER TABLE hzb_status_page_history DROP COLUMN `unknown`;
# commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- ensure every sql can rerun without error
tomsun28 marked this conversation as resolved.
Show resolved Hide resolved

ALTER TABLE HZB_PARAM ADD COLUMN IF NOT EXISTS value VARCHAR(255);
UPDATE HZB_PARAM SET param_value = value WHERE value IS NOT NULL AND param_value IS NULL;
ALTER TABLE HZB_PARAM DROP COLUMN value;

ALTER TABLE HZB_TAG ADD COLUMN IF NOT EXISTS value VARCHAR(255);
UPDATE HZB_TAG SET tag_value = value WHERE value IS NOT NULL AND tag_value IS NULL;
ALTER TABLE HZB_TAG DROP COLUMN value;

ALTER TABLE HZB_STATUS_PAGE_HISTORY ADD COLUMN IF NOT EXISTS unknown integer;
UPDATE HZB_STATUS_PAGE_HISTORY SET unknowing = unknown WHERE unknown IS NOT NULL AND unknowing IS NULL;
ALTER TABLE HZB_STATUS_PAGE_HISTORY DROP COLUMN unknown;

commit;
9 changes: 9 additions & 0 deletions manager/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,13 @@
</root>
</springProfile>

<!-- Development environment configuration -->
<springProfile name="pg">
<root level="INFO">
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="SystemOutFileAppender"/>
<appender-ref ref="ErrOutFileAppender"/>
</root>
</springProfile>

</configuration>
3 changes: 3 additions & 0 deletions material/licenses/backend/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ The text of each license is the standard Apache 2.0 license.
https://mvnrepository.com/artifact/org.webjars/swagger-ui/5.10.3
https://mvnrepository.com/artifact/com.google.flatbuffers/flatbuffers-java/1.12.0
https://mvnrepository.com/artifact/org.apache.commons/commons-jexl3/3.2.1
https://mvnrepository.com/artifact/org.flywaydb/flyway-core/10.11.1
https://mvnrepository.com/artifact/org.flywaydb/flyway-mysql/10.11.1
https://mvnrepository.com/artifact/org.flywaydb/flyway-database-postgresql/10.11.1


========================================================================
Expand Down
Loading
Loading