Skip to content

Commit

Permalink
Add migration for subwallet id as text
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueCarry committed Jul 5, 2024
1 parent 59ae5c4 commit fdad94c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/migrations/18_change_subwallet_id_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export function up(knex) {
return knex.schema.raw(`
PRAGMA foreign_keys = OFF;
BEGIN;
-- Add a new column with the desired type
ALTER TABLE wallets ADD COLUMN new_subwallet_id text;
-- Copy data from the old column to the new one
UPDATE wallets SET new_subwallet_id = CAST(subwallet_id AS text);
-- Drop the old column
ALTER TABLE wallets DROP COLUMN subwallet_id;
-- Rename the new column to the original name
ALTER TABLE wallets RENAME COLUMN new_subwallet_id TO subwallet_id;
COMMIT;
PRAGMA foreign_keys = ON;
`)
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export function down(knex) {
return knex.schema.raw(`
PRAGMA foreign_keys = OFF;
BEGIN;
-- Add a new column with the original type
ALTER TABLE wallets ADD COLUMN new_subwallet_id integer;
-- Copy data from the text column to the integer one
UPDATE wallets SET new_subwallet_id = CAST(subwallet_id AS integer);
-- Drop the text column
ALTER TABLE wallets DROP COLUMN subwallet_id;
-- Rename the new column to the original name
ALTER TABLE wallets RENAME COLUMN new_subwallet_id TO subwallet_id;
COMMIT;
PRAGMA foreign_keys = ON;
`)
}

export const config = { transaction: false }
1 change: 1 addition & 0 deletions src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * as m_14_add_networks_autoincrement from './14_add_networks_autoincremen
export * as m_15_add_wallets_autoincrement from './15_add_wallets_autoincrement'
export * as m_16_fix_connect_sessions_foreign_keys from './16_fix_connect_sessions_foreign_keys'
export * as m_17_fix_last_selected_wallets_foreign_keys from './17_fix_last_selected_wallets_foreign_keys'
export * as m_18_change_subwallet_id_type from './18_change_subwallet_id_type'

0 comments on commit fdad94c

Please sign in to comment.