diff --git a/smart-migration/build.gradle b/smart-migration/build.gradle index 8e9fb4c..74e8552 100644 --- a/smart-migration/build.gradle +++ b/smart-migration/build.gradle @@ -7,5 +7,6 @@ plugins { dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.springframework.boot:spring-boot") + implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation project(":common-spring") } diff --git a/smart-migration/src/main/kotlin/ru/touchin/smartmigration/BeforeLiquibase.kt b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/BeforeLiquibase.kt index 0adb5a2..2dee228 100644 --- a/smart-migration/src/main/kotlin/ru/touchin/smartmigration/BeforeLiquibase.kt +++ b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/BeforeLiquibase.kt @@ -1,16 +1,15 @@ package ru.touchin.smartmigration -import org.intellij.lang.annotations.Language import org.springframework.stereotype.Component import ru.touchin.common.spring.annotations.RequiredBy +import ru.touchin.smartmigration.logic.DataSourceSQL +import ru.touchin.smartmigration.logic.factory.DataSourceSqlFactoryImpl import java.sql.Date import java.sql.ResultSet import java.text.SimpleDateFormat import javax.annotation.PostConstruct import javax.sql.DataSource -private const val MIGRATION_TABLE_NAME = "SMART_MIGRATION" - val CURRENT_TIME_SQL: String get() = Date(System.currentTimeMillis()).let { date -> SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date) @@ -21,9 +20,12 @@ val CURRENT_TIME_SQL: String class BeforeLiquibase( private val dataSource: DataSource ) { + val dataSourceSql: DataSourceSQL = DataSourceSqlFactoryImpl() + .getDataSourceSql(dataSource.connection.metaData.databaseProductName) @PostConstruct fun doAction() { + val buildNumber = System.getenv("BUILD_NUMBER") if (buildNumber != null) { checkMigrationTable() @@ -36,9 +38,8 @@ class BeforeLiquibase( } private fun checkBuildMigrationExecuted(buildNumber: String): Boolean { - @Language("TSQL") - val checkBuildNumber = "SELECT * FROM $MIGRATION_TABLE_NAME WHERE BUILD_NUMBER = '$buildNumber'" + val checkBuildNumber = dataSourceSql.getMigrationCheckSQL(buildNumber) val result: ResultSet = dataSource.connection.createStatement().executeQuery(checkBuildNumber) var rowCount = 0 while (result.next()) { @@ -48,23 +49,13 @@ class BeforeLiquibase( } private fun checkMigrationTable() { - @Language("TSQL") - val createTable = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = '$MIGRATION_TABLE_NAME' and xtype='U')" + - "CREATE TABLE SMART_MIGRATION (\n" + - "\"ID\" BIGINT PRIMARY KEY IDENTITY ,\n" + - "\"BUILD_NUMBER\" VARCHAR(255) NOT NULL,\n" + - "\"DATE\" DATETIME NOT NULL" + - ");" - + val createTable = dataSourceSql.getTableCheckSQL() dataSource.connection.createStatement() .execute(createTable) } private fun insertMigration(buildNumber: String) { - @Language("TSQL") - val insertMigration = "INSERT INTO $MIGRATION_TABLE_NAME (BUILD_NUMBER, DATE)\n" + - "VALUES ('$buildNumber', '$CURRENT_TIME_SQL');" - + val insertMigration =dataSourceSql.getInsertMigrationSQL(buildNumber, CURRENT_TIME_SQL) dataSource.connection.createStatement() .execute(insertMigration) } diff --git a/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/DataSourceSQL.kt b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/DataSourceSQL.kt new file mode 100644 index 0000000..0b37da7 --- /dev/null +++ b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/DataSourceSQL.kt @@ -0,0 +1,7 @@ +package ru.touchin.smartmigration.logic + +interface DataSourceSQL { + fun getTableCheckSQL(): String + fun getMigrationCheckSQL(buildNumber: String): String + fun getInsertMigrationSQL(buildNumber: String, formattedTime: String): String +} diff --git a/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/PostgresDataSourceImpl.kt b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/PostgresDataSourceImpl.kt new file mode 100644 index 0000000..1f7218b --- /dev/null +++ b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/PostgresDataSourceImpl.kt @@ -0,0 +1,22 @@ +package ru.touchin.smartmigration.logic + +private const val MIGRATION_TABLE_NAME = "SMART_MIGRATION" + +class PostgresDataSourceImpl: DataSourceSQL { + override fun getTableCheckSQL(): String { + return "CREATE TABLE IF NOT EXISTS smart_migration (\n" + + "ID BIGSERIAL PRIMARY KEY ,\n" + + "BUILD_NUMBER VARCHAR(255) NOT NULL,\n" + + "DATE timestamp NOT NULL" + + ");" + } + + override fun getMigrationCheckSQL(buildNumber: String): String { + return "SELECT * FROM $MIGRATION_TABLE_NAME WHERE BUILD_NUMBER = '$buildNumber'" + } + + override fun getInsertMigrationSQL(buildNumber: String, formattedTime: String): String { + return "INSERT INTO $MIGRATION_TABLE_NAME (BUILD_NUMBER, DATE)\n" + + "VALUES ('$buildNumber', '$formattedTime');" + } +} diff --git a/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/SqlDataSourceImpl.kt b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/SqlDataSourceImpl.kt new file mode 100644 index 0000000..164b105 --- /dev/null +++ b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/SqlDataSourceImpl.kt @@ -0,0 +1,26 @@ +package ru.touchin.smartmigration.logic + +private const val MIGRATION_TABLE_NAME = "SMART_MIGRATION" + +class SqlDatasourceImpl:DataSourceSQL { + override fun getTableCheckSQL(): String { + return "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = '$MIGRATION_TABLE_NAME' and xtype='U')" + + "CREATE TABLE SMART_MIGRATION (\n" + + "\"ID\" BIGINT PRIMARY KEY IDENTITY ,\n" + + "\"BUILD_NUMBER\" VARCHAR(255) NOT NULL,\n" + + "\"DATE\" DATETIME NOT NULL" + + ");" + } + + override fun getMigrationCheckSQL(buildNumber: String): String { + return "SELECT * FROM $MIGRATION_TABLE_NAME WHERE BUILD_NUMBER = '$buildNumber'" + } + + override fun getInsertMigrationSQL(buildNumber: String, formattedTime: String): String { + return "INSERT INTO $MIGRATION_TABLE_NAME (BUILD_NUMBER, DATE)\n" + + "VALUES ('$buildNumber', '$formattedTime');" + } + +} + + diff --git a/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/factory/DataSourceSqlFactory.kt b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/factory/DataSourceSqlFactory.kt new file mode 100644 index 0000000..391a2c5 --- /dev/null +++ b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/factory/DataSourceSqlFactory.kt @@ -0,0 +1,7 @@ +package ru.touchin.smartmigration.logic.factory + +import ru.touchin.smartmigration.logic.DataSourceSQL + +interface DataSourceSqlFactory { + fun getDataSourceSql(driverName: String): DataSourceSQL +} diff --git a/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/factory/DataSourceSqlFactoryImpl.kt b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/factory/DataSourceSqlFactoryImpl.kt new file mode 100644 index 0000000..971d5ac --- /dev/null +++ b/smart-migration/src/main/kotlin/ru/touchin/smartmigration/logic/factory/DataSourceSqlFactoryImpl.kt @@ -0,0 +1,18 @@ +package ru.touchin.smartmigration.logic.factory + +import ru.touchin.smartmigration.logic.DataSourceSQL +import ru.touchin.smartmigration.logic.PostgresDataSourceImpl +import ru.touchin.smartmigration.logic.SqlDatasourceImpl + +class DataSourceSqlFactoryImpl: DataSourceSqlFactory { + + override fun getDataSourceSql(driverName: String): DataSourceSQL { + return when(driverName){ + "sql" -> SqlDatasourceImpl() + "PostgresSQL" -> PostgresDataSourceImpl() + else -> { + PostgresDataSourceImpl() + } + } + } +}