-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NSWStops table and necessary migrations
- Loading branch information
1 parent
338d101
commit facc3d6
Showing
8 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
## Sandook | ||
|
||
Sandook is the db for KRAIL app. | ||
Sandook is the db for KRAIL app. | ||
|
||
## Migrations | ||
|
||
Add migration in .sqm files in `migrations` folder. | ||
|
||
### Android | ||
### Android | ||
|
||
- Update in SandookCallback | ||
|
||
### iOS | ||
### iOS | ||
|
||
- Create a new class for after version migration for `SandookMigration` | ||
- Update in `IosSandookDriverFactory` and add `SandookMigrationAfterX` to `getMigrationCallbacks()` | ||
method. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Sandook | ||
|
||
### Generate Kotlin files for SQL tables | ||
|
||
Run `./gradlew generateCommonMainKrailSandookInterface` to generate the | ||
Kotlin files for the sql tables created in common module. The Kotlin files will be generated in the | ||
following dir: `build/generated/sqldelight/code/KrailSandook/commonMain` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Create NSW Stops Table -- | ||
CREATE TABLE IF NOT EXISTS NswStops ( | ||
stopId TEXT PRIMARY KEY, | ||
stopName TEXT NOT NULL, | ||
stopLat REAL NOT NULL, | ||
stopLon REAL NOT NULL | ||
); | ||
|
||
-- Create NSW Stop Product Class Table -- | ||
CREATE TABLE IF NOT EXISTS NswStopProductClass ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
stopId TEXT NOT NULL, | ||
productClass INTEGER NOT NULL, | ||
FOREIGN KEY (stopId) REFERENCES NswStops(stopId) | ||
); |
50 changes: 50 additions & 0 deletions
50
sandook/src/commonMain/sqldelight/xyz/ksharma/krail/sandook/NswStops.sq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
-- Create NSW Stops Table -- | ||
CREATE TABLE IF NOT EXISTS NswStops ( | ||
stopId TEXT PRIMARY KEY, | ||
stopName TEXT NOT NULL, | ||
stopLat REAL NOT NULL, | ||
stopLon REAL NOT NULL | ||
); | ||
|
||
-- Create NSW Stop Product Class Table -- | ||
-- Foregin key ensures that any stopId value inserted into NswStopProductClass must exist in | ||
-- NswStops, helping maintain data integrity between the two tables. | ||
CREATE TABLE IF NOT EXISTS NswStopProductClass ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
stopId TEXT NOT NULL, | ||
productClass INTEGER NOT NULL, | ||
FOREIGN KEY (stopId) REFERENCES NswStops(stopId) | ||
); | ||
|
||
-- Insert data into NswStops table (without productClass) -- | ||
insertStop: | ||
INSERT OR IGNORE INTO NswStops(stopId, stopName, stopLat, stopLon) | ||
VALUES (?, ?, ?, ?); | ||
|
||
-- Insert each productClass value for a given stop into NswStopProductClass -- | ||
insertStopProductClass: | ||
INSERT INTO NswStopProductClass(stopId, productClass) | ||
VALUES (?, ?); | ||
|
||
-- Select stops with partial match on stopName -- | ||
selectStopsByPartialName: | ||
SELECT * FROM NswStops | ||
WHERE stopName LIKE '%' || ? || '%'; | ||
|
||
-- Select stops with partial match on stopName and specific productClass values -- | ||
selectStopsByNameAndProductClass: | ||
SELECT DISTINCT s.* | ||
FROM NswStops AS s | ||
JOIN NswStopProductClass AS p ON s.stopId = p.stopId | ||
WHERE s.stopName LIKE '%' || ? || '%' | ||
AND p.productClass IN ?; | ||
|
||
selectStopsByNameExcludingProductClass: | ||
SELECT DISTINCT s.* | ||
FROM NswStops AS s | ||
WHERE s.stopName LIKE '%' || ? || '%' | ||
AND s.stopId NOT IN ( | ||
SELECT p.stopId | ||
FROM NswStopProductClass AS p | ||
WHERE p.productClass IN ? | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
sandook/src/iosMain/kotlin/xyz/ksharma/krail/sandook/migrations/SandookMigrationAfter2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package xyz.ksharma.krail.sandook.migrations | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import xyz.ksharma.krail.core.log.log | ||
|
||
internal object SandookMigrationAfter2 : SandookMigration { | ||
|
||
override fun migrate(sqlDriver: SqlDriver) { | ||
log("Upgrading database from version 2 to 3") | ||
sqlDriver.execute( | ||
identifier = null, | ||
sql = """ | ||
CREATE TABLE IF NOT EXISTS NswStops ( | ||
stopId TEXT PRIMARY KEY, | ||
stopName TEXT NOT NULL, | ||
stopLat REAL NOT NULL, | ||
stopLon REAL NOT NULL | ||
); | ||
CREATE TABLE IF NOT EXISTS NswStopProductClass ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
stopId TEXT NOT NULL, | ||
productClass INTEGER NOT NULL, | ||
FOREIGN KEY (stopId) REFERENCES NswStops(stopId) | ||
); | ||
""".trimIndent(), | ||
parameters = 0, | ||
) | ||
} | ||
} |