1+ package ua.pp.lumivoid.iwtcms.server
2+
3+ import kotlinx.coroutines.runBlocking
4+ import org.jetbrains.exposed.v1.jdbc.Database
5+ import org.jetbrains.exposed.v1.jdbc.SchemaUtils
6+ import org.jetbrains.exposed.v1.jdbc.insertIgnore
7+ import org.jetbrains.exposed.v1.jdbc.transactions.transaction
8+ import org.jetbrains.exposed.v1.jdbc.upsert
9+ import ua.pp.lumivoid.iwtcms.server.api.requests.api.user.CreateUser
10+ import ua.pp.lumivoid.iwtcms.server.tables.*
11+ import ua.pp.lumivoid.iwtcms.server.util.Config
12+ import java.io.File
13+ import kotlin.time.Instant
14+
15+ /* *
16+ * This object used just to generate a database file in generateDbFile task
17+ * Used in actions to upload db copy to artifacts for future migrations testing
18+ */
19+ object DbSchemaGenerator {
20+ @JvmStatic
21+ @Suppress(" DuplicatedCode" )
22+ fun main (args : Array <String >) {
23+ File (" ./build/db.mv.db" ).delete()
24+ File (" ./build/db.trace.db" ).delete()
25+
26+ Database .connect(
27+ url = " jdbc:h2:file:./build/db;MODE=MYSQL" ,
28+ driver = Config .DbDriver .H2 .driver
29+ )
30+
31+ transaction {
32+ SchemaUtils .create(
33+ UsersTable ,
34+ UserPermissionsTable ,
35+ MetaTable
36+ )
37+
38+ MetaTable .insertIgnore { it[key] = " iwtcms" ; it[value] = " iwtcms" } // just why no?
39+ MetaTable .insertIgnore { it[key] = " schema_version" ; it[value] = Constants .SCHEMA_VERSION }
40+ MetaTable .insertIgnore { it[key] = " last_migration_at" ; it[value] = " unknown" }
41+ MetaTable .insertIgnore { it[key] = " last_migration_from" ; it[value] = " unknown" }
42+ MetaTable .insertIgnore { it[key] = " last_migration_to" ; it[value] = " unknown" }
43+ MetaTable .insertIgnore { it[key] = " last_migration_success" ; it[value] = " unknown" }
44+ MetaTable .insertIgnore { it[key] = " total_migrations" ; it[value] = " 0" }
45+ MetaTable .upsert { it[key] = " last_used_at" ; it[value] = Instant .fromEpochMilliseconds(System .currentTimeMillis()).toString() }
46+ MetaTable .upsert { it[key] = " last_used_by" ; it[value] = " iwtcms-test" } // for easy debug (if used not by iwtcms must be different, for e.g. iwtcms forks)
47+ MetaTable .insertIgnore { it[key] = " last_shutdown_at" ; it[value] = " unknown" }
48+ MetaTable .insertIgnore { it[key] = " initial_iwtcms_version" ; it[value] = Constants .MOD_VERSION }
49+ MetaTable .upsert { it[key] = " last_iwtcms_version" ; it[value] = Constants .MOD_VERSION }
50+
51+ runBlocking { CreateUser .create(" admin" , " iwtcms" , true , emptyList()) }
52+ UserPermissionEntity .new {
53+ this .permissionName = " test"
54+ this .user = UserEntity .all().single()
55+ }
56+ }
57+ }
58+ }
0 commit comments