|
| 1 | +import { Client } from "pg"; |
| 2 | +import { PostgreSqlContainer } from "./postgresql-container"; |
| 3 | + |
| 4 | +describe("PostgreSqlContainer snapshot and restore", { timeout: 180_000 }, () => { |
| 5 | + // createAndRestoreFromSnapshot { |
| 6 | + it("should create and restore from snapshot", async () => { |
| 7 | + const container = await new PostgreSqlContainer().start(); |
| 8 | + |
| 9 | + // Connect to the database |
| 10 | + let client = new Client({ |
| 11 | + connectionString: container.getConnectionUri(), |
| 12 | + }); |
| 13 | + await client.connect(); |
| 14 | + |
| 15 | + // Create some test data |
| 16 | + await client.query("CREATE TABLE test_table (id SERIAL PRIMARY KEY, name TEXT)"); |
| 17 | + await client.query("INSERT INTO test_table (name) VALUES ('initial data')"); |
| 18 | + |
| 19 | + // Close connection before snapshot (otherwise we'll get an error because user is already connected) |
| 20 | + await client.end(); |
| 21 | + |
| 22 | + // Take a snapshot |
| 23 | + await container.snapshot(); |
| 24 | + |
| 25 | + // Reconnect to database |
| 26 | + client = new Client({ |
| 27 | + connectionString: container.getConnectionUri(), |
| 28 | + }); |
| 29 | + await client.connect(); |
| 30 | + |
| 31 | + // Modify the database |
| 32 | + await client.query("INSERT INTO test_table (name) VALUES ('data after snapshot')"); |
| 33 | + |
| 34 | + // Verify both records exist |
| 35 | + let result = await client.query("SELECT * FROM test_table ORDER BY id"); |
| 36 | + expect(result.rows).toHaveLength(2); |
| 37 | + expect(result.rows[0].name).toEqual("initial data"); |
| 38 | + expect(result.rows[1].name).toEqual("data after snapshot"); |
| 39 | + |
| 40 | + // Close connection before restore (same reason as above) |
| 41 | + await client.end(); |
| 42 | + |
| 43 | + // Restore to the snapshot |
| 44 | + await container.restoreSnapshot(); |
| 45 | + |
| 46 | + // Reconnect to database |
| 47 | + client = new Client({ |
| 48 | + connectionString: container.getConnectionUri(), |
| 49 | + }); |
| 50 | + await client.connect(); |
| 51 | + |
| 52 | + // Verify only the initial data exists after restore |
| 53 | + result = await client.query("SELECT * FROM test_table ORDER BY id"); |
| 54 | + expect(result.rows).toHaveLength(1); |
| 55 | + expect(result.rows[0].name).toEqual("initial data"); |
| 56 | + |
| 57 | + await client.end(); |
| 58 | + await container.stop(); |
| 59 | + }); |
| 60 | + // } |
| 61 | + |
| 62 | + it("should use custom snapshot name", async () => { |
| 63 | + const container = await new PostgreSqlContainer().start(); |
| 64 | + const customSnapshotName = "my_custom_snapshot"; |
| 65 | + |
| 66 | + // Connect to the database |
| 67 | + let client = new Client({ |
| 68 | + connectionString: container.getConnectionUri(), |
| 69 | + }); |
| 70 | + await client.connect(); |
| 71 | + |
| 72 | + // Create a test table and insert data |
| 73 | + await client.query("CREATE TABLE test_table (id SERIAL PRIMARY KEY, name TEXT)"); |
| 74 | + await client.query("INSERT INTO test_table (name) VALUES ('initial data')"); |
| 75 | + |
| 76 | + // Close connection before snapshot |
| 77 | + await client.end(); |
| 78 | + |
| 79 | + // Take a snapshot with custom name |
| 80 | + await container.snapshot(customSnapshotName); |
| 81 | + |
| 82 | + // Reconnect to database |
| 83 | + client = new Client({ |
| 84 | + connectionString: container.getConnectionUri(), |
| 85 | + }); |
| 86 | + await client.connect(); |
| 87 | + |
| 88 | + // Modify the database |
| 89 | + await client.query("INSERT INTO test_table (name) VALUES ('data after snapshot')"); |
| 90 | + |
| 91 | + // Close connection before restore |
| 92 | + await client.end(); |
| 93 | + |
| 94 | + // Restore using the custom snapshot name |
| 95 | + await container.restoreSnapshot(customSnapshotName); |
| 96 | + |
| 97 | + // Reconnect to database |
| 98 | + client = new Client({ |
| 99 | + connectionString: container.getConnectionUri(), |
| 100 | + }); |
| 101 | + await client.connect(); |
| 102 | + |
| 103 | + // Verify only the initial data exists after restore |
| 104 | + const result = await client.query("SELECT * FROM test_table ORDER BY id"); |
| 105 | + expect(result.rows).toHaveLength(1); |
| 106 | + expect(result.rows[0].name).toEqual("initial data"); |
| 107 | + |
| 108 | + await client.end(); |
| 109 | + await container.stop(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should handle multiple snapshots", async () => { |
| 113 | + const container = await new PostgreSqlContainer().start(); |
| 114 | + |
| 115 | + // Connect to the database |
| 116 | + let client = new Client({ |
| 117 | + connectionString: container.getConnectionUri(), |
| 118 | + }); |
| 119 | + await client.connect(); |
| 120 | + |
| 121 | + // Create a test table |
| 122 | + await client.query("CREATE TABLE test_table (id SERIAL PRIMARY KEY, name TEXT)"); |
| 123 | + |
| 124 | + // Close connection before snapshot |
| 125 | + await client.end(); |
| 126 | + |
| 127 | + // Take first snapshot with empty table |
| 128 | + await container.snapshot("snapshot1"); |
| 129 | + |
| 130 | + // Reconnect to database |
| 131 | + client = new Client({ |
| 132 | + connectionString: container.getConnectionUri(), |
| 133 | + }); |
| 134 | + await client.connect(); |
| 135 | + |
| 136 | + // Add first record |
| 137 | + await client.query("INSERT INTO test_table (name) VALUES ('data for snapshot 2')"); |
| 138 | + |
| 139 | + // Close connection before snapshot |
| 140 | + await client.end(); |
| 141 | + |
| 142 | + // Take second snapshot with one record |
| 143 | + await container.snapshot("snapshot2"); |
| 144 | + |
| 145 | + // Reconnect to database |
| 146 | + client = new Client({ |
| 147 | + connectionString: container.getConnectionUri(), |
| 148 | + }); |
| 149 | + await client.connect(); |
| 150 | + |
| 151 | + // Add second record |
| 152 | + await client.query("INSERT INTO test_table (name) VALUES ('data after snapshots')"); |
| 153 | + |
| 154 | + // Verify we have two records |
| 155 | + let result = await client.query("SELECT COUNT(*) as count FROM test_table"); |
| 156 | + expect(result.rows[0].count).toEqual("2"); |
| 157 | + |
| 158 | + // Close connection before restore |
| 159 | + await client.end(); |
| 160 | + |
| 161 | + // Restore to first snapshot (empty table) |
| 162 | + await container.restoreSnapshot("snapshot1"); |
| 163 | + |
| 164 | + // Reconnect to database |
| 165 | + client = new Client({ |
| 166 | + connectionString: container.getConnectionUri(), |
| 167 | + }); |
| 168 | + await client.connect(); |
| 169 | + |
| 170 | + // Verify table is empty |
| 171 | + result = await client.query("SELECT COUNT(*) as count FROM test_table"); |
| 172 | + expect(result.rows[0].count).toEqual("0"); |
| 173 | + |
| 174 | + // Close connection before restore |
| 175 | + await client.end(); |
| 176 | + |
| 177 | + // Restore to second snapshot (one record) |
| 178 | + await container.restoreSnapshot("snapshot2"); |
| 179 | + |
| 180 | + // Reconnect to database |
| 181 | + client = new Client({ |
| 182 | + connectionString: container.getConnectionUri(), |
| 183 | + }); |
| 184 | + await client.connect(); |
| 185 | + |
| 186 | + // Verify we have one record |
| 187 | + result = await client.query("SELECT * FROM test_table"); |
| 188 | + expect(result.rows).toHaveLength(1); |
| 189 | + expect(result.rows[0].name).toEqual("data for snapshot 2"); |
| 190 | + |
| 191 | + await client.end(); |
| 192 | + await container.stop(); |
| 193 | + }); |
| 194 | + |
| 195 | + it("should throw an error when trying to snapshot postgres system database", async () => { |
| 196 | + const container = await new PostgreSqlContainer().withDatabase("postgres").start(); |
| 197 | + |
| 198 | + await expect(container.snapshot()).rejects.toThrow( |
| 199 | + "Snapshot feature is not supported when using the postgres system database" |
| 200 | + ); |
| 201 | + |
| 202 | + await expect(container.restoreSnapshot()).rejects.toThrow( |
| 203 | + "Snapshot feature is not supported when using the postgres system database" |
| 204 | + ); |
| 205 | + |
| 206 | + await container.stop(); |
| 207 | + }); |
| 208 | +}); |
0 commit comments