From e65a4e10d6b5ec9937ce67d9a898a0ee50debd46 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Thu, 13 Aug 2020 20:39:29 +0200 Subject: [PATCH] Fix mariadb utf8 encoding.. --- .../src/main/kotlin/DbMigrationsImpl.kt | 13 ++++++- .../src/main/kotlin/PersistanceModule.kt | 2 +- .../migration/mariadb/V1__Create_tables.sql | 36 +++++++++++++++++++ .../{ => other}/V1__Create_tables.sql | 0 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 persistance/src/main/resources/db/migration/mariadb/V1__Create_tables.sql rename persistance/src/main/resources/db/migration/{ => other}/V1__Create_tables.sql (100%) diff --git a/persistance/src/main/kotlin/DbMigrationsImpl.kt b/persistance/src/main/kotlin/DbMigrationsImpl.kt index 0a5bc71..8ddd48f 100644 --- a/persistance/src/main/kotlin/DbMigrationsImpl.kt +++ b/persistance/src/main/kotlin/DbMigrationsImpl.kt @@ -1,12 +1,23 @@ package be.simplenotes.persistance +import be.simplenotes.shared.config.DataSourceConfig import org.flywaydb.core.Flyway import javax.sql.DataSource -internal class DbMigrationsImpl(private val dataSource: DataSource) : DbMigrations { +internal class DbMigrationsImpl( + private val dataSource: DataSource, + private val dataSourceConfig: DataSourceConfig +) : DbMigrations { override fun migrate() { + + val migrationDir = when { + dataSourceConfig.jdbcUrl.contains("mariadb") -> "db/migration/mariadb" + else -> "db/migration/other" + } + Flyway.configure() .dataSource(dataSource) + .locations(migrationDir) .load() .migrate() } diff --git a/persistance/src/main/kotlin/PersistanceModule.kt b/persistance/src/main/kotlin/PersistanceModule.kt index 9e00de3..a0edf85 100644 --- a/persistance/src/main/kotlin/PersistanceModule.kt +++ b/persistance/src/main/kotlin/PersistanceModule.kt @@ -30,7 +30,7 @@ private fun hikariDataSource(conf: DataSourceConfig): HikariDataSource { val persistanceModule = module { single { UserRepositoryImpl(get()) } single { NoteRepositoryImpl(get()) } - single { DbMigrationsImpl(get()) } + single { DbMigrationsImpl(get(), get()) } single { hikariDataSource(get()) } single { Database.connect(get()) } } diff --git a/persistance/src/main/resources/db/migration/mariadb/V1__Create_tables.sql b/persistance/src/main/resources/db/migration/mariadb/V1__Create_tables.sql new file mode 100644 index 0000000..a395f25 --- /dev/null +++ b/persistance/src/main/resources/db/migration/mariadb/V1__Create_tables.sql @@ -0,0 +1,36 @@ +create table Users +( + id int auto_increment primary key, + username varchar(50) not null, + password varchar(255) not null, + + constraint username unique (username) +) character set 'utf8mb4' + collate 'utf8mb4_general_ci'; + +create table Notes +( + uuid binary(16) not null primary key, + title varchar(50) not null, + markdown mediumtext not null, + html mediumtext not null, + user_id int not null, + updated_at datetime null, + + constraint Notes_fk_user foreign key (user_id) references Users (id) on delete cascade + +) character set 'utf8mb4' + collate 'utf8mb4_general_ci'; + +create index user_id on Notes (user_id); + +create table Tags +( + id int auto_increment primary key, + name varchar(50) not null, + note_uuid binary(16) not null, + constraint Tags_fk_note foreign key (note_uuid) references Notes (uuid) on delete cascade +) character set 'utf8mb4' + collate 'utf8mb4_general_ci'; + +create index note_uuid on Tags (note_uuid); diff --git a/persistance/src/main/resources/db/migration/V1__Create_tables.sql b/persistance/src/main/resources/db/migration/other/V1__Create_tables.sql similarity index 100% rename from persistance/src/main/resources/db/migration/V1__Create_tables.sql rename to persistance/src/main/resources/db/migration/other/V1__Create_tables.sql