Skip to content

Commit 812badd

Browse files
committed
fix: check if tables already exists or not before attempting to create them
1 parent 5d11a0a commit 812badd

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

scripts/e2e/database/migrations/20210820161410_create_personal_access_tokens_table.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ const { Knex } = require('knex');
22

33
/** @param {Knex} knex */
44
exports.up = (knex) => {
5-
return knex.schema.createTable('personal_access_tokens', (table) => {
6-
table.increments('id').primary();
7-
table.string('tokenable_type');
8-
table.bigInteger('tokenable_id').index().unsigned();
9-
table.string('name');
10-
table.string('abilities').nullable();
11-
table.string('payload').nullable();
12-
table.integer('ttl').index().nullable();
13-
table.timestamp('last_used_at').nullable();
14-
table.timestamps(true, true);
15-
});
5+
knex.schema.hasTable('personal_access_tokens')
6+
.then((exists) => {
7+
if (!exists) {
8+
return knex.schema.createTable('personal_access_tokens', (table) => {
9+
table.increments('id').primary();
10+
table.string('tokenable_type');
11+
table.bigInteger('tokenable_id').index().unsigned();
12+
table.string('name');
13+
table.string('abilities').nullable();
14+
table.string('payload').nullable();
15+
table.integer('ttl').index().nullable();
16+
table.timestamp('last_used_at').nullable();
17+
table.timestamps(true, true);
18+
});
19+
}
20+
})
21+
.catch((error) => {
22+
console.error('Error checking for personal_access_tokens table:', error);
23+
});
1624
}
1725

1826
/** @param {Knex} knex */
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
const { Knex } = require('knex');
2+
3+
/** @param {Knex} knex */
14
exports.up = (knex) => {
2-
return knex.schema.createTable('posts', (table) => {
3-
table.increments('id').primary();
4-
table.string('body');
5-
table.timestamps(true, true);
6-
});
5+
knex.schema.hasTable('posts')
6+
.then((exists) => {
7+
if (!exists) {
8+
return knex.schema.createTable('posts', (table) => {
9+
table.increments('id').primary();
10+
table.string('body');
11+
table.timestamps(true, true);
12+
});
13+
}
14+
})
15+
.catch((error) => {
16+
console.error('Error checking for posts table:', error);
17+
});
718
};
819

920
exports.down = (knex) => knex.schema.dropTable('posts');

0 commit comments

Comments
 (0)