From a505bd5e11124e8eb8f258ce8dbb8168a535f7ae Mon Sep 17 00:00:00 2001 From: Avinash Lingaloo Date: Tue, 1 Dec 2020 16:43:23 +0400 Subject: [PATCH] MM_30476 : ADDED Schema Managers * The migration array will hold all the migration steps. * The initial app release (e.g. v2 )will have an empty array and subsequent releases (e.g. v2.1 ) will have the steps listed in that array. * On initialization, the database will perform the migration to accomodate for new columns/tables creation and while it will conserve the mobile phone's data, it will also make it conform to this new schema. * If a migration fails, the migration process will rollback any changes. This migration will be thoroughly tested in development before pushing it live. --- app/constants/database.ts | 8 ++++++++ app/database/default/migration/index.ts | 5 +++-- app/database/managers/schema_manager.ts | 22 ++++++++++++++++++++++ app/database/server/migration/index.ts | 4 ++-- app/database/server/schema/index.ts | 2 +- 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 app/database/managers/schema_manager.ts diff --git a/app/constants/database.ts b/app/constants/database.ts index 8467b63923..227a227ef3 100644 --- a/app/constants/database.ts +++ b/app/constants/database.ts @@ -1,6 +1,8 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import keyMirror from '@utils/key_mirror'; + export const MM_TABLES = { DEFAULT: { APP: 'app', @@ -39,6 +41,12 @@ export const MM_TABLES = { }, }; +export const DB_NAME = keyMirror({ + DEFAULT_DATABASE: null, + SERVER_DATABASE: null, +}); + export default { + DB_NAME, MM_TABLES, }; diff --git a/app/database/default/migration/index.ts b/app/database/default/migration/index.ts index a63214a573..cb11e66958 100644 --- a/app/database/default/migration/index.ts +++ b/app/database/default/migration/index.ts @@ -1,8 +1,9 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import {schemaMigrations} from '@nozbe/watermelondb/Schema/migrations'; +import {Migration} from '@nozbe/watermelondb/Schema/migrations'; // NOTE : To implement migration, please follow this document // https://nozbe.github.io/WatermelonDB/Advanced/Migrations.html -export default schemaMigrations({migrations: []}); +export const migrations: Migration [] = []; + diff --git a/app/database/managers/schema_manager.ts b/app/database/managers/schema_manager.ts new file mode 100644 index 0000000000..c86d7e8d3a --- /dev/null +++ b/app/database/managers/schema_manager.ts @@ -0,0 +1,22 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import {SQLiteAdapterOptions} from '@nozbe/watermelondb/adapters/sqlite'; +import {defaultSchema} from '../default/schema'; +import {DB_NAME} from '@constants/database'; +import {migrations as defaultMigration} from '../default/migration'; +import {migrations as serverMigration} from '../server/migration'; +import {schemaMigrations} from '@nozbe/watermelondb/Schema/migrations'; +import {serverSchema} from '../server/schema'; + +export const default_schema_manager:SQLiteAdapterOptions = { + dbName: DB_NAME.DEFAULT_DATABASE, + schema: defaultSchema, + ...(defaultMigration.length > 0 && {migrations: schemaMigrations({migrations: defaultMigration})}), +}; + +export const server_schema_manager:SQLiteAdapterOptions = { + dbName: DB_NAME.SERVER_DATABASE, + schema: serverSchema, + ...(serverMigration.length > 0 && {migrations: schemaMigrations({migrations: serverMigration})}), +}; diff --git a/app/database/server/migration/index.ts b/app/database/server/migration/index.ts index a63214a573..dea1a0fade 100644 --- a/app/database/server/migration/index.ts +++ b/app/database/server/migration/index.ts @@ -1,8 +1,8 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import {schemaMigrations} from '@nozbe/watermelondb/Schema/migrations'; +import {Migration} from '@nozbe/watermelondb/Schema/migrations'; // NOTE : To implement migration, please follow this document // https://nozbe.github.io/WatermelonDB/Advanced/Migrations.html -export default schemaMigrations({migrations: []}); +export const migrations: Migration [] = []; diff --git a/app/database/server/schema/index.ts b/app/database/server/schema/index.ts index 1644f96d46..07baa8f426 100644 --- a/app/database/server/schema/index.ts +++ b/app/database/server/schema/index.ts @@ -3,7 +3,7 @@ import {AppSchema, appSchema, tableSchema} from '@nozbe/watermelondb'; import {MM_TABLES} from '@constants/database'; -export const defaultSchema: AppSchema = appSchema({ +export const serverSchema: AppSchema = appSchema({ version: 1, tables: [ tableSchema({