Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove need to create 'main.sqlite' database and 'users' table by hand #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PackageDescription
let package = Package(
name: "FluentApp",
dependencies: [
.Package(url: "https://github.com/vapor/fluent.git", majorVersion: 1, minor: 3),
.Package(url: "https://github.com/vapor/fluent.git", majorVersion: 1, minor: 4),
.Package(url: "https://github.com/vapor/fluent-sqlite.git", majorVersion: 1, minor: 1),
]
)
21 changes: 3 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fluent Example

1. Read through the Example Table section below to setup the sample database
1. Make sure to have `sqlite3` installed (see **Install SQLite** bellow)
2. Run `swift package update` to download dependencies
3. Run `swift build` to build the example application
4. Execute the program `.build/debug/FluentApp`
Expand All @@ -9,9 +9,9 @@ View [Fluent](https://github.com/vapor/fluent) for documentation.

Swift 3.0 or later is required.

## Example Table
## Install SQLite

1. Install the `sqlite3` package. It's recommended to use a package manager:
Install the `sqlite3` package. It's recommended to use a package manager:

On macOS use [brew](http://brew.sh):

Expand All @@ -26,19 +26,4 @@ sudo apt-get update
sudo apt-get install sqlite3 libsqlite3-dev
```

Using the `sqlite3` command, create a database in the location the example
app will look for:

```bash
sqlite3 Database/main.sqlite
```

That will put you in a sql prompt. Copy and paste the following SQL query to set up the example table.

```sql
CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INT NOT NULL);
```

Then type `exit;` and you can open up the application with the steps at the top of this README.

Works on macOS and Ubuntu.
22 changes: 22 additions & 0 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ do {
print("Could not initialize driver: \(error)")
}

// Database preparation code from <https://github.com/vapor/fluent-provider/blob/master/Sources/Prepare.swift#L49-L62>

let preparations: [Preparation.Type] = [User.self]
if let database = Database.default {
do {
try preparations.forEach { preparation in
let name = "\(preparation.self)"

let hasPrepared = try database.hasPrepared(preparation)
// only prepare the unprepared
guard !hasPrepared else { return }
print("Preparing \(name)")
try database.prepare(preparation)
print("Prepared \(name)")
}
} catch {
print("Could not modify user: \(error)")
}

print("Database prepared")
}

do {
if var user = try User.find(1) {
print(user.name)
Expand Down