diff --git a/drizzle-orm/src/mysql-proxy/migrator.ts b/drizzle-orm/src/mysql-proxy/migrator.ts index b75184934..368efc18b 100644 --- a/drizzle-orm/src/mysql-proxy/migrator.ts +++ b/drizzle-orm/src/mysql-proxy/migrator.ts @@ -26,8 +26,8 @@ export async function migrate>( id: sql.raw('id'), hash: sql.raw('hash'), created_at: sql.raw('created_at'), - }).from(sql.raw(migrationsTable)).orderBy( - sql.raw('created_at desc'), + }).from(sql.identifier(migrationsTable).getSQL()).orderBy( + sql.raw('created_at desc') ).limit(1); const lastDbMigration = dbMigrations[0]; @@ -41,9 +41,7 @@ export async function migrate>( ) { queriesToRun.push( ...migration.sql, - `insert into ${ - sql.identifier(migrationsTable) - } (\`hash\`, \`created_at\`) values(${migration.hash}, ${migration.folderMillis})`, + `insert into ${sql.identifier(migrationsTable).value} (\`hash\`, \`created_at\`) values('${migration.hash}', '${migration.folderMillis}')`, ); } } diff --git a/drizzle-orm/src/pg-proxy/migrator.ts b/drizzle-orm/src/pg-proxy/migrator.ts index f1b3fb107..3224afa8c 100644 --- a/drizzle-orm/src/pg-proxy/migrator.ts +++ b/drizzle-orm/src/pg-proxy/migrator.ts @@ -23,9 +23,13 @@ export async function migrate>( await db.execute(sql`CREATE SCHEMA IF NOT EXISTS "drizzle"`); await db.execute(migrationTableCreate); - const dbMigrations = await db.execute( - sql`SELECT id, hash, created_at FROM "drizzle"."__drizzle_migrations" ORDER BY created_at DESC LIMIT 1`, - ) as unknown as [number, string, string][]; + const dbMigrations = await db.execute<{ + id: number; + hash: string; + created_at: string; + }>( + sql`SELECT id, hash, created_at FROM "drizzle"."__drizzle_migrations" ORDER BY created_at DESC LIMIT 1` + ); const lastDbMigration = dbMigrations[0] ?? undefined; @@ -34,7 +38,7 @@ export async function migrate>( for (const migration of migrations) { if ( !lastDbMigration - || Number(lastDbMigration[2])! < migration.folderMillis + || Number(lastDbMigration.created_at)! < migration.folderMillis ) { queriesToRun.push( ...migration.sql, diff --git a/integration-tests/drizzle2/mysql-proxy/first/0000_nostalgic_carnage.sql b/integration-tests/drizzle2/mysql-proxy/first/0000_nostalgic_carnage.sql new file mode 100644 index 000000000..4266589a6 --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/first/0000_nostalgic_carnage.sql @@ -0,0 +1,7 @@ +CREATE TABLE `userstest` ( + `id` serial PRIMARY KEY, + `name` text NOT NULL, + `verified` boolean NOT NULL DEFAULT false, + `jsonb` json, + `created_at` timestamp NOT NULL DEFAULT now() +); \ No newline at end of file diff --git a/integration-tests/drizzle2/mysql-proxy/first/meta/0000_snapshot.json b/integration-tests/drizzle2/mysql-proxy/first/meta/0000_snapshot.json new file mode 100644 index 000000000..1d01f3bcf --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/first/meta/0000_snapshot.json @@ -0,0 +1,60 @@ +{ + "version": "5", + "dialect": "mysql", + "id": "8e8c8378-0496-40f6-88e3-98aab8282b1f", + "prevId": "00000000-0000-0000-0000-000000000000", + "tables": { + "userstest": { + "name": "userstest", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": false, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "verified": { + "name": "verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false, + "autoincrement": false + }, + "jsonb": { + "name": "jsonb", + "type": "json", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()", + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} diff --git a/integration-tests/drizzle2/mysql-proxy/first/meta/_journal.json b/integration-tests/drizzle2/mysql-proxy/first/meta/_journal.json new file mode 100644 index 000000000..708471cf5 --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/first/meta/_journal.json @@ -0,0 +1,13 @@ +{ + "version": "5", + "dialect": "mysql", + "entries": [ + { + "idx": 0, + "version": "5", + "when": 1680270921944, + "tag": "0000_nostalgic_carnage", + "breakpoints": true + } + ] +} \ No newline at end of file diff --git a/integration-tests/drizzle2/mysql-proxy/second/0000_nostalgic_carnage.sql b/integration-tests/drizzle2/mysql-proxy/second/0000_nostalgic_carnage.sql new file mode 100644 index 000000000..4266589a6 --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/second/0000_nostalgic_carnage.sql @@ -0,0 +1,7 @@ +CREATE TABLE `userstest` ( + `id` serial PRIMARY KEY, + `name` text NOT NULL, + `verified` boolean NOT NULL DEFAULT false, + `jsonb` json, + `created_at` timestamp NOT NULL DEFAULT now() +); \ No newline at end of file diff --git a/integration-tests/drizzle2/mysql-proxy/second/0001_test.sql b/integration-tests/drizzle2/mysql-proxy/second/0001_test.sql new file mode 100644 index 000000000..1d2757a0b --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/second/0001_test.sql @@ -0,0 +1,5 @@ +CREATE TABLE `users12` ( + `id` serial AUTO_INCREMENT PRIMARY KEY NOT NULL, + `name` text NOT NULL, + `email` text NOT NULL +); \ No newline at end of file diff --git a/integration-tests/drizzle2/mysql-proxy/second/meta/0000_snapshot.json b/integration-tests/drizzle2/mysql-proxy/second/meta/0000_snapshot.json new file mode 100644 index 000000000..1d01f3bcf --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/second/meta/0000_snapshot.json @@ -0,0 +1,60 @@ +{ + "version": "5", + "dialect": "mysql", + "id": "8e8c8378-0496-40f6-88e3-98aab8282b1f", + "prevId": "00000000-0000-0000-0000-000000000000", + "tables": { + "userstest": { + "name": "userstest", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": false, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "verified": { + "name": "verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false, + "autoincrement": false + }, + "jsonb": { + "name": "jsonb", + "type": "json", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()", + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} diff --git a/integration-tests/drizzle2/mysql-proxy/second/meta/0001_snapshot.json b/integration-tests/drizzle2/mysql-proxy/second/meta/0001_snapshot.json new file mode 100644 index 000000000..bda94226a --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/second/meta/0001_snapshot.json @@ -0,0 +1,96 @@ +{ + "version": "5", + "dialect": "mysql", + "id": "47362df0-c353-4bd1-8107-fcc36f0e61bd", + "prevId": "8e8c8378-0496-40f6-88e3-98aab8282b1f", + "tables": { + "userstest": { + "name": "userstest", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": false, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "verified": { + "name": "verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false, + "autoincrement": false + }, + "jsonb": { + "name": "jsonb", + "type": "json", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()", + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + }, + "users12": { + "name": "users12", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "my_unique_index": { + "name": "my_unique_index", + "columns": ["name"], + "isUnique": true, + "using": "btree" + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} diff --git a/integration-tests/drizzle2/mysql-proxy/second/meta/_journal.json b/integration-tests/drizzle2/mysql-proxy/second/meta/_journal.json new file mode 100644 index 000000000..fdd1a2ee3 --- /dev/null +++ b/integration-tests/drizzle2/mysql-proxy/second/meta/_journal.json @@ -0,0 +1,20 @@ +{ + "version": "5", + "dialect": "mysql", + "entries": [ + { + "idx": 0, + "version": "5", + "when": 1680270921944, + "tag": "0000_nostalgic_carnage", + "breakpoints": true + }, + { + "idx": 1, + "version": "5", + "when": 1680270921945, + "tag": "0001_test", + "breakpoints": true + } + ] +} \ No newline at end of file diff --git a/integration-tests/drizzle2/pg-proxy/first/0000_puzzling_flatman.sql b/integration-tests/drizzle2/pg-proxy/first/0000_puzzling_flatman.sql new file mode 100644 index 000000000..06ed6ebf7 --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/first/0000_puzzling_flatman.sql @@ -0,0 +1,7 @@ +CREATE TABLE "users" ( + id serial PRIMARY KEY, + name text NOT NULL, + verified boolean NOT NULL DEFAULT false, + jsonb jsonb, + created_at timestamptz NOT NULL DEFAULT now() +); \ No newline at end of file diff --git a/integration-tests/drizzle2/pg-proxy/first/meta/0000_snapshot.json b/integration-tests/drizzle2/pg-proxy/first/meta/0000_snapshot.json new file mode 100644 index 000000000..4bb618d40 --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/first/meta/0000_snapshot.json @@ -0,0 +1,56 @@ +{ + "version": "5", + "dialect": "pg", + "id": "cb1644bb-c5da-465a-8d70-f63d81e34514", + "prevId": "00000000-0000-0000-0000-000000000000", + "tables": { + "users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "verified": { + "name": "verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "jsonb": { + "name": "jsonb", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} diff --git a/integration-tests/drizzle2/pg-proxy/first/meta/_journal.json b/integration-tests/drizzle2/pg-proxy/first/meta/_journal.json new file mode 100644 index 000000000..6b2a35b4a --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/first/meta/_journal.json @@ -0,0 +1,13 @@ +{ + "version": "5", + "dialect": "pg", + "entries": [ + { + "idx": 0, + "version": "5", + "when": 1680271923328, + "tag": "0000_puzzling_flatman", + "breakpoints": true + } + ] +} \ No newline at end of file diff --git a/integration-tests/drizzle2/pg-proxy/second/0000_puzzling_flatman.sql b/integration-tests/drizzle2/pg-proxy/second/0000_puzzling_flatman.sql new file mode 100644 index 000000000..06ed6ebf7 --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/second/0000_puzzling_flatman.sql @@ -0,0 +1,7 @@ +CREATE TABLE "users" ( + id serial PRIMARY KEY, + name text NOT NULL, + verified boolean NOT NULL DEFAULT false, + jsonb jsonb, + created_at timestamptz NOT NULL DEFAULT now() +); \ No newline at end of file diff --git a/integration-tests/drizzle2/pg-proxy/second/0001_test.sql b/integration-tests/drizzle2/pg-proxy/second/0001_test.sql new file mode 100644 index 000000000..bb8e5f34d --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/second/0001_test.sql @@ -0,0 +1,5 @@ +CREATE TABLE "users12" ( + "id" serial PRIMARY KEY NOT NULL, + "name" text NOT NULL, + "email" text NOT NULL +); \ No newline at end of file diff --git a/integration-tests/drizzle2/pg-proxy/second/meta/0000_snapshot.json b/integration-tests/drizzle2/pg-proxy/second/meta/0000_snapshot.json new file mode 100644 index 000000000..4bb618d40 --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/second/meta/0000_snapshot.json @@ -0,0 +1,56 @@ +{ + "version": "5", + "dialect": "pg", + "id": "cb1644bb-c5da-465a-8d70-f63d81e34514", + "prevId": "00000000-0000-0000-0000-000000000000", + "tables": { + "users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "verified": { + "name": "verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "jsonb": { + "name": "jsonb", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} diff --git a/integration-tests/drizzle2/pg-proxy/second/meta/0001_snapshot.json b/integration-tests/drizzle2/pg-proxy/second/meta/0001_snapshot.json new file mode 100644 index 000000000..c5d4b7cfe --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/second/meta/0001_snapshot.json @@ -0,0 +1,56 @@ +{ + "version": "5", + "dialect": "pg", + "id": "f2a88b25-f2da-4973-879e-60b57f24e7b9", + "prevId": "cb1644bb-c5da-465a-8d70-f63d81e34514", + "tables": { + "users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "verified": { + "name": "verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "jsonb": { + "name": "jsonb", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {} + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + } +} diff --git a/integration-tests/drizzle2/pg-proxy/second/meta/_journal.json b/integration-tests/drizzle2/pg-proxy/second/meta/_journal.json new file mode 100644 index 000000000..de7a2ac56 --- /dev/null +++ b/integration-tests/drizzle2/pg-proxy/second/meta/_journal.json @@ -0,0 +1,20 @@ +{ + "version": "5", + "dialect": "pg", + "entries": [ + { + "idx": 0, + "version": "5", + "when": 1680271923328, + "tag": "0000_puzzling_flatman", + "breakpoints": true + }, + { + "idx": 1, + "version": "5", + "when": 1680271923329, + "tag": "0001_test", + "breakpoints": true + } + ] +} \ No newline at end of file diff --git a/integration-tests/tests/mysql-proxy.test.ts b/integration-tests/tests/mysql-proxy.test.ts index 861d6811e..df2e5e346 100644 --- a/integration-tests/tests/mysql-proxy.test.ts +++ b/integration-tests/tests/mysql-proxy.test.ts @@ -97,7 +97,7 @@ const usersMigratorTable = mysqlTable('users12', { // eslint-disable-next-line drizzle/require-entity-kind class ServerSimulator { - constructor(private db: mysql.Connection) {} + constructor(private db: mysql.Connection) { } async query(sql: string, params: any[], method: 'all' | 'execute') { if (method === 'all') { @@ -106,7 +106,7 @@ class ServerSimulator { sql, values: params, rowsAsArray: true, - typeCast: function(field: any, next: any) { + typeCast: function (field: any, next: any) { if (field.type === 'TIMESTAMP' || field.type === 'DATETIME' || field.type === 'DATE') { return field.string(); } @@ -123,7 +123,7 @@ class ServerSimulator { const result = await this.db.query({ sql, values: params, - typeCast: function(field: any, next: any) { + typeCast: function (field: any, next: any) { if (field.type === 'TIMESTAMP' || field.type === 'DATETIME' || field.type === 'DATE') { return field.string(); } @@ -141,14 +141,15 @@ class ServerSimulator { } async migrations(queries: string[]) { - await this.db.query('BEGIN'); + await this.db.query('START TRANSACTION'); try { for (const query of queries) { await this.db.query(query); } await this.db.query('COMMIT'); - } catch { + } catch (e) { await this.db.query('ROLLBACK'); + throw e; } return {}; @@ -253,11 +254,11 @@ test.beforeEach(async (t) => { await ctx.db.execute( sql` create table \`userstest\` ( - \`id\` serial primary key, - \`name\` text not null, - \`verified\` boolean not null default false, - \`jsonb\` json, - \`created_at\` timestamp not null default now() + \`id\` serial primary key, + \`name\` text not null, + \`verified\` boolean not null default false, + \`jsonb\` json, + \`created_at\` timestamp not null default now() ) `, ); @@ -265,9 +266,9 @@ test.beforeEach(async (t) => { await ctx.db.execute( sql` create table \`users2\` ( - \`id\` serial primary key, - \`name\` text not null, - \`city_id\` int references \`cities\`(\`id\`) + \`id\` serial primary key, + \`name\` text not null, + \`city_id\` int references \`cities\`(\`id\`) ) `, ); @@ -275,8 +276,8 @@ test.beforeEach(async (t) => { await ctx.db.execute( sql` create table \`cities\` ( - \`id\` serial primary key, - \`name\` text not null + \`id\` serial primary key, + \`name\` text not null ) `, ); @@ -994,8 +995,7 @@ test.serial('prepared statement with placeholder in .where', async (t) => { test.serial('migrator', async (t) => { const { db, serverSimulator } = t.context; - await db.execute(sql`drop table if exists cities_migration`); - await db.execute(sql`drop table if exists users_migration`); + await db.execute(sql`drop table if exists userstest`); await db.execute(sql`drop table if exists users12`); await db.execute(sql`drop table if exists __drizzle_migrations`); @@ -1006,16 +1006,36 @@ test.serial('migrator', async (t) => { console.error(e); throw new Error('Proxy server cannot run migrations'); } - }, { migrationsFolder: './drizzle2/mysql' }); + }, { migrationsFolder: './drizzle2/mysql-proxy/first' }); - await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' }); + await t.notThrowsAsync(async () => { + await db.insert(usersTable).values({ name: 'John' }); + }); - const result = await db.select().from(usersMigratorTable); + await t.throwsAsync(async () => { + await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' }); + }, { + message: 'Table \'drizzle.users12\' doesn\'t exist' + }); - t.deepEqual(result, [{ id: 1, name: 'John', email: 'email' }]); + await migrate(db, async (queries) => { + try { + await serverSimulator.migrations(queries); + } catch (e) { + console.error(e); + throw new Error('Proxy server cannot run migrations'); + } + }, { migrationsFolder: './drizzle2/mysql-proxy/second' }); + + await t.notThrowsAsync(async () => { + await db.insert(usersTable).values({ name: 'John' }); + }); + + await t.notThrowsAsync(async () => { + await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' }); + }); - await db.execute(sql`drop table cities_migration`); - await db.execute(sql`drop table users_migration`); + await db.execute(sql`drop table userstest`); await db.execute(sql`drop table users12`); await db.execute(sql`drop table __drizzle_migrations`); }); diff --git a/integration-tests/tests/pg-proxy.test.ts b/integration-tests/tests/pg-proxy.test.ts index 02c48cffc..bc494c662 100644 --- a/integration-tests/tests/pg-proxy.test.ts +++ b/integration-tests/tests/pg-proxy.test.ts @@ -58,7 +58,7 @@ import { Expect } from './utils.ts'; // eslint-disable-next-line drizzle/require-entity-kind class ServerSimulator { - constructor(private db: pg.Client) {} + constructor(private db: pg.Client) { } async query(sql: string, params: any[], method: 'all' | 'execute') { if (method === 'all') { @@ -96,8 +96,9 @@ class ServerSimulator { await this.db.query(query); } await this.db.query('COMMIT'); - } catch { + } catch (e) { await this.db.query('ROLLBACK'); + throw e; } return {}; @@ -1045,7 +1046,7 @@ test.serial('prepared statement with placeholder in .offset', async (t) => { test.serial('migrator', async (t) => { const { db, serverSimulator } = t.context; - await db.execute(sql`drop table if exists all_columns`); + await db.execute(sql`drop table if exists users`); await db.execute(sql`drop table if exists users12`); await db.execute(sql`drop table if exists "drizzle"."__drizzle_migrations"`); @@ -1056,15 +1057,36 @@ test.serial('migrator', async (t) => { console.error(e); throw new Error('Proxy server cannot run migrations'); } - }, { migrationsFolder: './drizzle2/pg' }); + }, { migrationsFolder: './drizzle2/pg-proxy/first' }); + + await t.notThrowsAsync(async () => { + await db.insert(usersTable).values({ name: 'John' }); + }); - await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' }); + await t.throwsAsync(async () => { + await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' }); + }, { + message: 'relation "users12" does not exist' + }); + + await migrate(db, async (queries) => { + try { + await serverSimulator.migrations(queries); + } catch (e) { + console.error(e); + throw new Error('Proxy server cannot run migrations'); + } + }, { migrationsFolder: './drizzle2/pg-proxy/second' }); - const result = await db.select().from(usersMigratorTable); + await t.notThrowsAsync(async () => { + await db.insert(usersTable).values({ name: 'John' }); + }); - t.deepEqual(result, [{ id: 1, name: 'John', email: 'email' }]); + await t.notThrowsAsync(async () => { + await db.insert(usersMigratorTable).values({ name: 'John', email: 'email' }); + }); - await db.execute(sql`drop table all_columns`); + await db.execute(sql`drop table users`); await db.execute(sql`drop table users12`); await db.execute(sql`drop table "drizzle"."__drizzle_migrations"`); }); @@ -1087,11 +1109,10 @@ test.serial('insert via db.execute + returning', async (t) => { const { db } = t.context; const inserted = await db.execute<{ id: number; name: string }>( - sql`insert into ${usersTable} (${ - name( - usersTable.name.name, - ) - }) values (${'John'}) returning ${usersTable.id}, ${usersTable.name}`, + sql`insert into ${usersTable} (${name( + usersTable.name.name, + ) + }) values (${'John'}) returning ${usersTable.id}, ${usersTable.name}`, ); t.deepEqual(inserted, [{ id: 1, name: 'John' }]); }); @@ -2064,19 +2085,11 @@ test.serial('select from enum', async (t) => { await db.execute(sql`drop type if exists ${name(equipmentEnum.enumName)}`); await db.execute(sql`drop type if exists ${name(categoryEnum.enumName)}`); - await db.execute( - sql`create type ${ - name(muscleEnum.enumName) - } as enum ('abdominals', 'hamstrings', 'adductors', 'quadriceps', 'biceps', 'shoulders', 'chest', 'middle_back', 'calves', 'glutes', 'lower_back', 'lats', 'triceps', 'traps', 'forearms', 'neck', 'abductors')`, - ); + await db.execute(sql`create type ${name(muscleEnum.enumName)} as enum ('abdominals', 'hamstrings', 'adductors', 'quadriceps', 'biceps', 'shoulders', 'chest', 'middle_back', 'calves', 'glutes', 'lower_back', 'lats', 'triceps', 'traps', 'forearms', 'neck', 'abductors')`,); await db.execute(sql`create type ${name(forceEnum.enumName)} as enum ('isometric', 'isotonic', 'isokinetic')`); await db.execute(sql`create type ${name(levelEnum.enumName)} as enum ('beginner', 'intermediate', 'advanced')`); await db.execute(sql`create type ${name(mechanicEnum.enumName)} as enum ('compound', 'isolation')`); - await db.execute( - sql`create type ${ - name(equipmentEnum.enumName) - } as enum ('barbell', 'dumbbell', 'bodyweight', 'machine', 'cable', 'kettlebell')`, - ); + await db.execute(sql`create type ${name(equipmentEnum.enumName)} as enum ('barbell', 'dumbbell', 'bodyweight', 'machine', 'cable', 'kettlebell')`,); await db.execute(sql`create type ${name(categoryEnum.enumName)} as enum ('upper_body', 'lower_body', 'full_body')`); await db.execute(sql` create table ${exercises} (