Skip to content

Commit

Permalink
元々ISO形式の文字列だったのでエポック秒へマイグレ
Browse files Browse the repository at this point in the history
  • Loading branch information
CASL0 committed Oct 2, 2023
1 parent befd646 commit 3419c24
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
buildToolsVersion = libs.versions.buildTools.get()

defaultConfig {
minSdk 23
minSdk 26
targetSdk libs.versions.targetSdk.get().toInteger()
javaCompileOptions {
annotationProcessorOptions {
Expand Down Expand Up @@ -73,6 +73,7 @@ dependencies {
kapt libs.androidx.room.compiler
implementation libs.com.google.dagger.hilt.android
kapt libs.com.google.dagger.hilt.compiler
implementation libs.org.jetbrains.kotlinx.datetime

androidTestImplementation libs.androidx.test.core
androidTestImplementation libs.androidx.test.ext.junit.ktx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package jp.co.casl0.android.simpleappblocker.core.database

import androidx.room.Room
import androidx.room.testing.MigrationTestHelper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
Expand All @@ -38,10 +39,14 @@ class MigrationTest {
INSERT INTO
blocked_packets (package_name, src_address, src_port, dst_address, dst_port, protocol, blocked_at)
VALUES
('package1', '10.10.10.10', 100, '20.20.20.20', 200, 'protocol1', '2000-01-01')
('package1', '10.10.10.10', 100, '20.20.20.20', 200, 'protocol1', '2000-01-01 00:00:00')
;
""".trimIndent()

private val ALL_MIGRATIONS = arrayOf(
MIGRATION_2_3
)

/** スキーマJSONファイルからDBを作成します */
@get:Rule
val helper = MigrationTestHelper(
Expand Down Expand Up @@ -79,4 +84,16 @@ class MigrationTest {
ret.close()
db.close()
}

@Test
@Throws(IOException::class)
fun migrateAll() {
Room.databaseBuilder(
InstrumentationRegistry.getInstrumentation().targetContext,
SimpleAppBlockerDatabase::class.java,
TEST_DB
).addMigrations(*ALL_MIGRATIONS).build().apply {
openHelper.writableDatabase.close()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2022 CASL0
*
* Licensed 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 jp.co.casl0.android.simpleappblocker.core.database

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toKotlinLocalDateTime
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

/**
* ブロックログテーブルのブロック時刻のカラム定義変更のマイグレ
* ISO文字列で保持していたので、エポック秒へ変換
*
* */
val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"""
CREATE TABLE new_blocked_packets (
package_name TEXT PRIMARY KEY NOT NULL,
app_name TEXT NOT NULL DEFAULT '',
src_address TEXT NOT NULL,
src_port INTEGER NOT NULL,
dst_address TEXT NOT NULL,
dst_port INTEGER NOT NULL,
protocol TEXT NOT NULL,
blocked_at INTEGER NOT NULL
)
;
""".trimIndent()
)

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

database.query(
"SELECT * FROM blocked_packets;"
).also { cursor ->
while (cursor.moveToNext()) {
val columnIndex = hashMapOf(
"package_name" to cursor.getColumnIndex("package_name"),
"app_name" to cursor.getColumnIndex("app_name"),
"src_address" to cursor.getColumnIndex("src_address"),
"src_port" to cursor.getColumnIndex("src_port"),
"dst_address" to cursor.getColumnIndex("dst_address"),
"dst_port" to cursor.getColumnIndex("dst_port"),
"protocol" to cursor.getColumnIndex("protocol"),
"blocked_at" to cursor.getColumnIndex("blocked_at")
)
val packageName =
columnIndex["package_name"]?.let { cursor.getString(it) } ?: continue
val appName =
columnIndex["app_name"]?.let { cursor.getString(it) } ?: continue
val srcAddress =
columnIndex["src_address"]?.let { cursor.getString(it) } ?: continue
val srcPort =
columnIndex["src_port"]?.let { cursor.getInt(it) } ?: continue
val dstAddress =
columnIndex["dst_address"]?.let { cursor.getString(it) } ?: continue
val dstPort =
columnIndex["dst_port"]?.let { cursor.getInt(it) } ?: continue
val protocol =
columnIndex["protocol"]?.let { cursor.getString(it) } ?: continue
val blockedAt =
columnIndex["blocked_at"]?.let { cursor.getString(it) } ?: continue

val dateTimeInstant =
LocalDateTime.parse(blockedAt, formatter).toKotlinLocalDateTime()
.toInstant(TimeZone.currentSystemDefault())
database.execSQL(
"""
INSERT INTO new_blocked_packets
(
package_name,
app_name,
src_address,
src_port,
dst_address,
dst_port,
protocol,
blocked_at
)
VALUES
(
'$packageName',
'$appName',
'$srcAddress',
$srcPort,
'$dstAddress',
$dstPort,
'$protocol',
${dateTimeInstant.toEpochMilliseconds()}
)
;
""".trimIndent()
)
}
}

database.execSQL("DROP TABLE blocked_packets;")
database.execSQL("ALTER TABLE new_blocked_packets RENAME TO blocked_packets;")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import jp.co.casl0.android.simpleappblocker.core.database.MIGRATION_2_3
import jp.co.casl0.android.simpleappblocker.core.database.SimpleAppBlockerDatabase
import javax.inject.Singleton

Expand All @@ -36,6 +37,8 @@ object DatabaseModule {
context.applicationContext,
SimpleAppBlockerDatabase::class.java,
"simple_app_blocker.db"
).build()
)
.addMigrations(MIGRATION_2_3)
.build()
}
}

0 comments on commit 3419c24

Please sign in to comment.