|
| 1 | +import Driver from '../driver'; |
| 2 | +import { destroyDriver, initDriver } from '../test-utils'; |
| 3 | +import { |
| 4 | + AlterTableDescription, |
| 5 | + AlterTableSettings, |
| 6 | + Column, |
| 7 | + OperationParams, |
| 8 | + TableDescription, |
| 9 | + TableIndex, |
| 10 | +} from '../table'; |
| 11 | +import { Types } from '../types'; |
| 12 | + |
| 13 | +const getTableName = () => `table_alter_${Math.trunc(1000 * Math.random())}`; |
| 14 | + |
| 15 | +describe('Alter table', () => { |
| 16 | + let driver: Driver; |
| 17 | + let TABLE_NAME: string; |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + driver = await initDriver(); |
| 21 | + TABLE_NAME = getTableName(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterAll(async () => await destroyDriver(driver)); |
| 25 | + |
| 26 | + it('Create table', async () => { |
| 27 | + await driver.tableClient.withSession(async (session) => { |
| 28 | + await session.createTable( |
| 29 | + TABLE_NAME, |
| 30 | + new TableDescription() |
| 31 | + .withColumn(new Column('id', Types.optional(Types.UINT64))) |
| 32 | + .withColumn(new Column('title', Types.optional(Types.UTF8))) |
| 33 | + .withPrimaryKey('id') |
| 34 | + ); |
| 35 | + |
| 36 | + const createdTableDescription = await session.describeTable(TABLE_NAME); |
| 37 | + |
| 38 | + expect(createdTableDescription.primaryKey).toStrictEqual(['id']); |
| 39 | + expect(JSON.stringify(createdTableDescription.columns)).toStrictEqual( |
| 40 | + JSON.stringify([ |
| 41 | + { name: 'id', type: { optionalType: { item: { typeId: 'UINT64' } } } }, |
| 42 | + { name: 'title', type: { optionalType: { item: { typeId: 'UTF8' } } } }, |
| 43 | + ]) |
| 44 | + ); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('Alter table - add columns', async () => { |
| 49 | + await driver.tableClient.withSession(async (session) => { |
| 50 | + const alterTableDescription = new AlterTableDescription(); |
| 51 | + alterTableDescription.addColumns = [ |
| 52 | + new Column('testBool', Types.optional(Types.BOOL)), |
| 53 | + new Column('testDate', Types.optional(Types.DATE)), |
| 54 | + ]; |
| 55 | + |
| 56 | + await session.alterTable(TABLE_NAME, alterTableDescription); |
| 57 | + const alteredTableDescription = await session.describeTable(TABLE_NAME); |
| 58 | + |
| 59 | + expect(JSON.stringify(alteredTableDescription.columns)).toBe( |
| 60 | + JSON.stringify([ |
| 61 | + { name: 'id', type: { optionalType: { item: { typeId: 'UINT64' } } } }, |
| 62 | + { name: 'title', type: { optionalType: { item: { typeId: 'UTF8' } } } }, |
| 63 | + { name: 'testBool', type: { optionalType: { item: { typeId: 'BOOL' } } } }, |
| 64 | + { name: 'testDate', type: { optionalType: { item: { typeId: 'DATE' } } } }, |
| 65 | + ]) |
| 66 | + ); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('Alter table - add indexes sync', async () => { |
| 71 | + await driver.tableClient.withSession(async (session) => { |
| 72 | + const alterTableDescription = new AlterTableDescription(); |
| 73 | + |
| 74 | + const idxOverTestBool = new TableIndex('data_index_over_testBool') |
| 75 | + .withIndexColumns('testDate', 'title') |
| 76 | + .withDataColumns('testBool') |
| 77 | + .withGlobalAsync(false); |
| 78 | + alterTableDescription.addIndexes = [idxOverTestBool]; |
| 79 | + |
| 80 | + await session.alterTable( |
| 81 | + TABLE_NAME, |
| 82 | + alterTableDescription, |
| 83 | + new AlterTableSettings().withOperationParams(new OperationParams().withSyncMode()) |
| 84 | + ); |
| 85 | + const alteredTableDescription = await session.describeTable(TABLE_NAME); |
| 86 | + |
| 87 | + expect(JSON.stringify(alteredTableDescription.indexes)).toBe( |
| 88 | + JSON.stringify([ |
| 89 | + { |
| 90 | + name: 'data_index_over_testBool', |
| 91 | + indexColumns: ['testDate', 'title'], |
| 92 | + globalIndex: {}, |
| 93 | + status: 'STATUS_READY', |
| 94 | + dataColumns: ['testBool'], |
| 95 | + }, |
| 96 | + ]) |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Alter table - drop indexes sync', async () => { |
| 102 | + await driver.tableClient.withSession(async (session) => { |
| 103 | + const alterTableDescription = new AlterTableDescription(); |
| 104 | + |
| 105 | + alterTableDescription.dropIndexes = ['data_index_over_testBool']; |
| 106 | + |
| 107 | + await session.alterTable( |
| 108 | + TABLE_NAME, |
| 109 | + alterTableDescription, |
| 110 | + new AlterTableSettings().withOperationParams(new OperationParams().withSyncMode()) |
| 111 | + ); |
| 112 | + const alteredTableDescription = await session.describeTable(TABLE_NAME); |
| 113 | + |
| 114 | + expect(alteredTableDescription.indexes).toStrictEqual([]); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('Alter table - add indexes async', async () => { |
| 119 | + await driver.tableClient.withSession(async (session) => { |
| 120 | + const alterTableDescription = new AlterTableDescription(); |
| 121 | + |
| 122 | + const idxOverTestBool = new TableIndex('data_index_over_testBool') |
| 123 | + .withIndexColumns('testDate', 'title') |
| 124 | + .withDataColumns('testBool') |
| 125 | + .withGlobalAsync(false); |
| 126 | + alterTableDescription.addIndexes = [idxOverTestBool]; |
| 127 | + |
| 128 | + await session.alterTable(TABLE_NAME, alterTableDescription); |
| 129 | + await new Promise((resolve) => setTimeout(resolve, 200)); // wait 200ms |
| 130 | + const alteredTableDescription = await session.describeTable(TABLE_NAME); |
| 131 | + |
| 132 | + expect(JSON.stringify(alteredTableDescription.indexes)).toBe( |
| 133 | + JSON.stringify([ |
| 134 | + { |
| 135 | + name: 'data_index_over_testBool', |
| 136 | + indexColumns: ['testDate', 'title'], |
| 137 | + globalIndex: {}, |
| 138 | + status: 'STATUS_READY', |
| 139 | + dataColumns: ['testBool'], |
| 140 | + }, |
| 141 | + ]) |
| 142 | + ); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('Alter table - drop indexes async', async () => { |
| 147 | + await driver.tableClient.withSession(async (session) => { |
| 148 | + const alterTableDescription = new AlterTableDescription(); |
| 149 | + |
| 150 | + alterTableDescription.dropIndexes = ['data_index_over_testBool']; |
| 151 | + |
| 152 | + await session.alterTable(TABLE_NAME, alterTableDescription); |
| 153 | + const alteredTableDescription = await session.describeTable(TABLE_NAME); |
| 154 | + |
| 155 | + expect(alteredTableDescription.indexes).toStrictEqual([]); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments