|
| 1 | +import { KnexDriver } from '../src'; |
| 2 | + |
| 3 | +describe('KnexDriver Schema Sync (SQLite)', () => { |
| 4 | + let driver: KnexDriver; |
| 5 | + let knexInstance: any; |
| 6 | + |
| 7 | + beforeEach(async () => { |
| 8 | + // Init ephemeral in-memory database |
| 9 | + driver = new KnexDriver({ |
| 10 | + client: 'sqlite3', |
| 11 | + connection: { |
| 12 | + filename: ':memory:' |
| 13 | + }, |
| 14 | + useNullAsDefault: true |
| 15 | + }); |
| 16 | + knexInstance = (driver as any).knex; |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(async () => { |
| 20 | + await knexInstance.destroy(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should create table if not exists', async () => { |
| 24 | + const objects = [{ |
| 25 | + name: 'test_obj', |
| 26 | + fields: { |
| 27 | + name: { type: 'string' }, |
| 28 | + age: { type: 'integer' } |
| 29 | + } |
| 30 | + }]; |
| 31 | + |
| 32 | + await driver.init(objects); |
| 33 | + |
| 34 | + const exists = await knexInstance.schema.hasTable('test_obj'); |
| 35 | + expect(exists).toBe(true); |
| 36 | + |
| 37 | + const columns = await knexInstance('test_obj').columnInfo(); |
| 38 | + expect(columns).toHaveProperty('_id'); |
| 39 | + expect(columns).toHaveProperty('created_at'); |
| 40 | + expect(columns).toHaveProperty('updated_at'); |
| 41 | + expect(columns).toHaveProperty('name'); |
| 42 | + expect(columns).toHaveProperty('age'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should add new columns if table exists', async () => { |
| 46 | + // 1. Setup existing table with subset of columns |
| 47 | + await knexInstance.schema.createTable('test_obj', (t: any) => { |
| 48 | + t.string('_id').primary(); |
| 49 | + t.string('name'); |
| 50 | + }); |
| 51 | + |
| 52 | + // 2. Insert some data |
| 53 | + await knexInstance('test_obj').insert({ _id: '1', name: 'Old Data' }); |
| 54 | + |
| 55 | + // 3. Init with new fields |
| 56 | + const objects = [{ |
| 57 | + name: 'test_obj', |
| 58 | + fields: { |
| 59 | + name: { type: 'string' }, |
| 60 | + age: { type: 'integer' }, // New field |
| 61 | + active: { type: 'boolean' } // New field |
| 62 | + } |
| 63 | + }]; |
| 64 | + |
| 65 | + await driver.init(objects); |
| 66 | + |
| 67 | + // 4. Verify columns |
| 68 | + const columns = await knexInstance('test_obj').columnInfo(); |
| 69 | + expect(columns).toHaveProperty('age'); |
| 70 | + expect(columns).toHaveProperty('active'); |
| 71 | + |
| 72 | + // 5. Verify data is intact |
| 73 | + const row = await knexInstance('test_obj').where('_id', '1').first(); |
| 74 | + expect(row.name).toBe('Old Data'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should not delete existing columns', async () => { |
| 78 | + // 1. Setup table with extra column |
| 79 | + await knexInstance.schema.createTable('test_obj', (t: any) => { |
| 80 | + t.string('_id').primary(); |
| 81 | + t.string('name'); |
| 82 | + t.string('extra_column'); // Should stay |
| 83 | + }); |
| 84 | + |
| 85 | + // 2. Init with only 'name' |
| 86 | + const objects = [{ |
| 87 | + name: 'test_obj', |
| 88 | + fields: { |
| 89 | + name: { type: 'string' } |
| 90 | + } |
| 91 | + }]; |
| 92 | + |
| 93 | + await driver.init(objects); |
| 94 | + |
| 95 | + const columns = await knexInstance('test_obj').columnInfo(); |
| 96 | + expect(columns).toHaveProperty('name'); |
| 97 | + expect(columns).toHaveProperty('extra_column'); // Preservation check |
| 98 | + }); |
| 99 | + |
| 100 | + it('should not fail if table creation is repeated', async () => { |
| 101 | + const objects = [{ |
| 102 | + name: 'test_obj', |
| 103 | + fields: { |
| 104 | + name: { type: 'string' } |
| 105 | + } |
| 106 | + }]; |
| 107 | + |
| 108 | + // First init |
| 109 | + await driver.init(objects); |
| 110 | + |
| 111 | + // Second init (should be idempotent-ish, or just skip creation) |
| 112 | + await driver.init(objects); |
| 113 | + |
| 114 | + const exists = await knexInstance.schema.hasTable('test_obj'); |
| 115 | + expect(exists).toBe(true); |
| 116 | + }); |
| 117 | +}); |
0 commit comments