refactor: auto run migrations on startup

This commit is contained in:
Juan Rodriguez
2024-07-14 08:46:11 +02:00
parent 115bbf7366
commit 7cc6c1197f
5 changed files with 9 additions and 19 deletions
+1 -4
View File
@@ -44,7 +44,7 @@ Average Response Time: 12.37 µs
```bash
docker-compose up
docker-compose exec -it app migrate
# Generate an api key
docker-compose exec -it app cli --create-user=Admin
```
@@ -59,7 +59,6 @@ docker run \
-e APP_URL="http://localhost:4000" \
sjdonado/bit
docker exec -it bit migrate
docker exec -it bit cli --create-user=Admin
```
@@ -83,7 +82,6 @@ dokku config:set bit DATABASE_URL="sqlite3://./sqlite/data.db?journal_mode=wal&s
dokku ports:add bit http:80:4000
dokku ports:add bit https:443:4000
dokku run bit migrate
dokku run bit cli --create-user=Admin
```
@@ -261,7 +259,6 @@ brew install micrate
```
```bash
shards run migrate
shards run bit
```
+8
View File
@@ -1,5 +1,6 @@
require "sqlite3"
require "crecto"
require"micrate"
module App::Lib
class Database
@@ -14,5 +15,12 @@ module App::Lib
if ENV["ENV"] == "development"
Crecto::DbLogger.set_handler(STDOUT)
end
def self.run_migrations
Micrate::DB.connection_url = ENV["DATABASE_URL"]
Micrate::Cli.run_up
end
run_migrations
end
end
-6
View File
@@ -134,12 +134,6 @@ if [ $? -ne 0 ]; then
exit 1
fi
docker-compose exec -T app migrate
if [ $? -ne 0 ]; then
echo "Failed to run database migrations."
exit 1
fi
# Create a new user and capture the API key
output=$(docker-compose exec -T app cli --create-user=Admin)
api_key=$(echo "$output" | awk -F' ' '/X-Api-Key:/{print $NF}')
-7
View File
@@ -1,7 +0,0 @@
require "sqlite3"
require"micrate"
require "../app/config/env"
Micrate::DB.connection_url = ENV["DATABASE_URL"]
Micrate::Cli.run_up
-2
View File
@@ -9,8 +9,6 @@ targets:
main: bit.cr
cli:
main: scripts/cli.cr
migrate:
main: scripts/migrate.cr
dependencies:
kemal: