Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration not running in Release APK, resetting the database #1773

Open
pankajnegi1893 opened this issue Apr 16, 2024 · 0 comments
Open

Migration not running in Release APK, resetting the database #1773

pankajnegi1893 opened this issue Apr 16, 2024 · 0 comments

Comments

@pankajnegi1893
Copy link

I am using migration docs to create migration but getting issue in release APK.

Below function is to make column unique

export const makeColumnUniqueInSQL = (
  sql: string,
  tableName: string,
  colName: string,
) => {
  const modifiedSQL = sql.replace(`"${colName}"`, `"${colName}" unique`);
  return modifiedSQL;
};

schema.ts

import {appSchema, tableSchema} from '@nozbe/watermelondb';
import {makeColumnUniqueInSQL, TableName} from './TableName';

export default appSchema({
  version: 2,
  tables: [
    tableSchema({
      name: TableName.MESSAGES,
      columns: [
        {name: 'server_id', type: 'string', isOptional: true},
        // some other fields is here
      ],
      unsafeSql: (sql: string) =>
        makeColumnUniqueInSQL(sql, TableName.MESSAGES, 'server_id'),
    }),
  ],
});

migrations.ts

import {
  addColumns,
  schemaMigrations,
} from '@nozbe/watermelondb/Schema/migrations';
import {TableName} from './TableName';

export default schemaMigrations({
  migrations: [
    {
      toVersion: 2, // minimum migration version is 2
      steps: [
        addColumns({
          table: TableName.MESSAGES,
          columns: [{name: 'errors', type: 'string', isOptional: true}],
          unsafeSql: (sql: string) => {
            console.log('migration sql ', sql);
            return sql;
          },
        }),
      ],
    },
  ],
});

index.ts

import {Database} from '@nozbe/watermelondb';
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
import Config from 'react-native-config';
import MessageModal from './modals/MessageModal';
import schema from './schema';
import migrations from './migrations';

const adapter = new SQLiteAdapter({
  schema,
  migrations,
  dbName: Config.DB_NAME,
});

export const database = new Database({
  adapter,
  modelClasses: [MessageModal],
});

But testing above changes in release APK migration is not running "Setting up database with schema version 2" is coming but it resetting the database.

NOTE : if I have remove unsafeSql makeColumnUniqueInSQL from schema.ts. then it's working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant