Skip to content

Commit

Permalink
fix: migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Léo Guillaume committed Jul 30, 2023
1 parent 1c030a7 commit 83e49d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@ import Database from '@ioc:Adonis/Lucid/Database'

export default class MoveSearchesLocationsToUserLocations extends BaseSchema {
public async up() {
const searchUsers = await Database.from('search_users')

searchUsers.map(async (searchUser) => {
const search = await Database.from('searches')
.where('id', searchUser.search_id)
.whereNotNull('location_id')
.first()

await Database.table('user_locations').insert({
user_id: searchUser.user_id,
location_id: search.location_id,
name: `Location ${search.location_id}`,
})
})

const searches = await Database.from('searches').whereNotNull('location_id')

for (let search of searches) {
searches.map(async (search) => {
await Database.table('user_locations').insert({
user_id: search.user_id,
user_id: search.creator_id,
location_id: search.location_id,
name: `Location ${search.location_id}`,
})
}
})
}

public async down() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,10 @@ export default class RemoveLocationIdFromSearches extends BaseSchema {
protected tableName = 'searches'

public async up() {
const result = await Database.rawQuery(`
SELECT COUNT(*) as count
FROM information_schema.table_constraints
WHERE constraint_schema = 'your_database_name'
AND table_name = 'searches'
AND constraint_name = 'searches_location_id_foreign'
`)

const count = result[0].count

if (count > 0) {
const hasColumn = await this.schema.hasColumn(this.tableName, 'location_id')
if (hasColumn) {
await Database.rawQuery('ALTER TABLE searches DROP FOREIGN KEY searches_location_id_foreign')
await this.schema.table(this.tableName, (table) => {
table.dropForeign(['location_id'])
table.dropColumn('location_id')
})
}
Expand All @@ -34,8 +25,8 @@ export default class RemoveLocationIdFromSearches extends BaseSchema {
.references('id')
.inTable('locations')
.onDelete('CASCADE')
table.foreign('location_id')
})
await Database.rawQuery('ALTER TABLE searches ADD CONSTRAINT searches_location_id_foreign FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE CASCADE')
}
}
}

0 comments on commit 83e49d0

Please sign in to comment.