patch seeds without hardcoded ids to ensure auto-increment

This commit is contained in:
Sorrel Bri 2020-05-10 17:08:13 -07:00
parent 8e25106be6
commit 2e613daa1b
6 changed files with 96 additions and 53 deletions

View file

@ -5,8 +5,8 @@ exports.seed = function(knex) {
.then(function () { .then(function () {
// Inserts seed entries // Inserts seed entries
return knex('room').insert([ return knex('room').insert([
{id: 1, name: 'main', description: 'A general place to play Go'}, {name: 'main', description: 'A general place to play Go'},
{id: 2, name: 'private', description: 'A private place to play Go', private: true}, {name: 'private', description: 'A private place to play Go', private: true},
]); ]);
}); });
}; };

View file

@ -5,7 +5,7 @@ exports.seed = function(knex) {
.then(function () { .then(function () {
// Inserts seed entries // Inserts seed entries
return knex('time_setting').insert([ return knex('time_setting').insert([
{id: 1, main_time: 'untimed', time_period: 1, period_length: 0, overtime: 'untimed', overtime_period: 0, overtime_length: 0}, {main_time: 'untimed', time_period: 1, period_length: 0, overtime: 'untimed', overtime_period: 0, overtime_length: 0},
]); ]);
}); });
}; };

View file

@ -10,9 +10,9 @@ exports.seed = async function(knex) {
.then(async function () { .then(async function () {
const hashedPassword = await hashPassword(password); const hashedPassword = await hashPassword(password);
// Inserts seed entries // Inserts seed entries
return knex('user').insert([ return knex('user').returning('*').insert([
{id: 2, username: 'user-one', email: email, password: hashedPassword, admin: true}, {username: 'user-one', email: email, password: hashedPassword, admin: true},
{id: 3, username: 'user-two', email: `2${email}`, password: hashedPassword, admin: true}, {username: 'user-two', email: `2${email}`, password: hashedPassword, admin: true},
]); ]).then(entries => console.log({success: 'user', entries}));
}); });
}; };

View file

@ -1,33 +1,49 @@
exports.seed = function(knex) { exports.seed = async function(knex) {
// Deletes ALL existing entries // Deletes ALL existing entries
return knex('game').del() return await knex('game').del()
.then(function () { .then(async function () {
// Inserts seed entries // Inserts seed entries
return knex('game').insert([ await knex('user')
.select('id')
.orderBy('id')
.whereIn('username', ['user-one', 'user-two'])
.then(async ([userOne, userTwo]) => {
const res = await knex('room')
.select('id')
.where({name: 'main'})
.then(([room]) => {
console.log('inserting')
return knex('game').insert(
[
{ {
id: 1, date: new Date(), date: new Date(),
application: 'node-go', application_version: '0.1.0', application: 'node-go', application_version: '0.1.0',
player_black: 'user-one', player_white: 'user-two', player_black: 'user-one', player_white: 'user-two',
player_black_rank: 'UR', player_white_rank: 'UR', player_black_rank: 'UR', player_white_rank: 'UR',
user_black: 2, user_white: 3, user_black: userOne.id, user_white: userTwo.id,
room: 1, time_setting: 1, open: false room: room.id, open: false
}, },
{ {
id: 2, date: new Date(), date: new Date(),
application: 'node-go', application_version: '0.1.0', application: 'node-go', application_version: '0.1.0',
player_black: 'user-one', player_black_rank: 'UR', player_black: 'user-one', player_black_rank: 'UR',
user_black: 2, user_black: userTwo.id,
room: 1, time_setting: 1, open: true room: room.id, open: true
}, },
{ {
id: 3, date: new Date('1971-05-06'), date: new Date('1971-05-06'),
application: 'node-go', application_version: '0.1.0', application: 'node-go', application_version: '0.1.0',
player_black: 'Ishida Yoshio', player_black_rank: 'D7', player_black: 'Ishida Yoshio', player_black_rank: 'D7',
player_white: 'Rin Kaiho', player_white_rank: 'D9', player_white: 'Rin Kaiho', player_white_rank: 'D9',
room: 1, time_setting: 1, open: false, room: room.id, open: false,
event: '', round: 2, win_type: 'B+', score: 1.5 event: '', round: 2, win_type: 'B+', score: 1.5
} }
]); ], ['*']
)
.then(res => res)
.catch(e => {console.log('error'); console.log(e)})
}).then(entries => {console.log({success: 'game', entries})})
}); });
})
}; };

View file

@ -1,11 +1,27 @@
exports.seed = function(knex) { exports.seed = async function(knex) {
// Deletes ALL existing entries // Deletes ALL existing entries
return knex('message').del() return await knex('message').del()
.then(function () { .then(async function () {
// Inserts seed entries // Inserts seed entries
return knex('message').insert([ await knex('room')
{id: 1, content: 'Hey! Welcome to the general room!', room: 1, user: 2} .where({name: 'main'})
]); .then(async ([room]) => await knex('user')
}); .where({username: 'user-two'})
.then(async ([user]) => {
const res = await knex('message')
.returning('*')
.insert(
[
{content: 'Hey! Welcome to the general room!', room: room.id, user: user.id}
]
)
.then(entries => {console.log({success: 'message', entries}); return res;})
.catch(e => e)
return res;
}
)
.then(() => {})
).then(() => {})
}).then(() => {})
}; };

View file

@ -1,14 +1,25 @@
exports.seed = function(knex) { exports.seed = async function(knex) {
// Deletes ALL existing entries // Deletes ALL existing entries
return knex('move').del() return await knex('move').del()
.then(function () { .then(async function () {
// Inserts seed entries // Inserts seed entries
return knex('move').insert([ await knex('game')
{id: 1, player: 'black', point_x: 3, point_y: 3, number: 1, game_record: true, game: 1, prior_move: null}, .where({player_black: 'user-one', player_white: 'user-two'})
{id: 2, player: 'white', point_x: 15, point_y: 15, number: 2, game_record: true, game: 1, prior_move: 1}, .then(async ([game]) => await knex('move')
{id: 3, player: 'black', point_x: 4, point_y: 15, number: 3, game_record: true, game: 1, prior_move: 2}, .returning('*')
{id: 4, player: 'white', point_x: 15, point_y: 4, number: 4, game_record: true, game: 1, prior_move: 3}, .insert(
]); [
}); {player: 'black', point_x: 3, point_y: 3, number: 1, game_record: true, game: game.id, prior_move: null},
{player: 'white', point_x: 15, point_y: 15, number: 2, game_record: true, game: game.id, prior_move: 1},
{player: 'black', point_x: 4, point_y: 15, number: 3, game_record: true, game: game.id, prior_move: 2},
{player: 'white', point_x: 15, point_y: 4, number: 4, game_record: true, game: game.id, prior_move: 3},
]
)
.then(res => {console.log(res); return res;})
.catch(e => {console.log(e); return e;})
)
.then(() => {})
})
.then(() => {})
}; };