How to reconnect to your Postico instance

Scott
3 min readMay 10, 2024

--

So you are working on a Vapor server side project, and you are opening it up fresh after a period away. This is the step by step guide on what to do to connect to your database, make sure all the tables, fields and rows are showing.

preface:

Make sure your server credentials in the app:

    app.databases.use(
.postgres(
configuration: .init(
hostname: "localhost",
username: "your username",
password: "your password",
database: app.environment == .testing ? "your test db" : "yourdb",
tls: PostgresConnection.Configuration.TLS.disable
),
encodingContext: .iso8601,
decodingContext: .iso8601
),
as: .psql
)

As mentioned a short scroll down.

  1. Run your vapor app and then stop it.
 sudo lsof -i -P -n | grep VaporApp

That should show you if there is any vapor instance running that needs to be closed as you will want to use the same port.

The output may look something like this.

            COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
VaporApp 1234 user 10u IPv4 12345 0t0 TCP *:8080 (LISTEN)

In which case you will need to kill the instance vis the PID code.

kill 1234

2.

Vapor separates the concern of database creation from running the project, to avoid accidental database changes when running the project normally. So you have to run terminal commands to change the database table setup.

Also, if database changes were included in the startup of the project, it would slow down the start time of the vapor project instance.

In order to update the tables and the databases to the latest changes (migrations) in the code, we need to run the following command:

swift run Run migrate --revert

This will undo some tables, and is a destructive command, so if you have data stored in the database that you care about, then you should do another way.

Here are the full details of what it does:
a) Initialization: The Vapor application initializes, setting up its environment and configurations.

b) Database Connection: It connects to the configured database.

c) Migration Reversion: This command reverts the last batch of migrations applied to the database. If multiple migrations were applied at the same batch level, all of them are reverted. It effectively undoes the changes made by these migrations, altering the database schema back to its previous state.

d) Logging: Vapor logs the actions taken, providing feedback in the terminal about which migrations were reverted and any errors encountered during the process.

After saying yes to the prompts:

It should finish, hopefully without any errors. And then you can run:

swift run Run migrate
  1. Initialization: Like with the revert command, the application initializes.
  2. Database Connection: It establishes a connection to the configured database.
  3. Migration Execution: This command applies migrations that have not yet been executed on the database. Migrations are typically defined in your project as Swift classes or structures, where each migration contains methods to update or modify the database schema. This might involve creating new tables, altering existing ones, or updating data formats.
  4. Schema Version Management: Vapor keeps track of which migrations have been applied in a special table in the database, often called something like fluent_migrations. This tracking ensures that each migration is only applied once.
  5. Logging: The system logs the results of the migration process, including any errors or issues encountered.

--

--