Skip to content

Commit c5b43a8

Browse files
committed
CREATE TABLE advice that works for mssql
1 parent 4f8d21c commit c5b43a8

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/filesystem.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ impl FileSystem {
2323
Err(e) => {
2424
log::debug!(
2525
"Using local filesystem only, could not initialize on-database filesystem. \
26-
You can host sql files directly in your database by creating the following table: \
27-
CREATE TABLE sqlpage_files(path VARCHAR(255) NOT NULL PRIMARY KEY, contents TEXT, last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
28-
The error while trying to use the database file system is: {e:#}");
26+
You can host sql files directly in your database by creating the following table: \n\
27+
{} \n\
28+
The error while trying to use the database file system is: {e:#}",
29+
DbFsQueries::get_create_table_sql(db.connection.any_kind())
30+
);
2931
None
3032
}
3133
},
@@ -144,6 +146,13 @@ pub(crate) struct DbFsQueries {
144146
}
145147

146148
impl DbFsQueries {
149+
fn get_create_table_sql(db_kind: AnyKind) -> &'static str {
150+
match db_kind {
151+
AnyKind::Mssql => "CREATE TABLE sqlpage_files(path NVARCHAR(255) NOT NULL PRIMARY KEY, contents TEXT, last_modified DATETIME2(3) NOT NULL DEFAULT CURRENT_TIMESTAMP);",
152+
_ => "CREATE TABLE IF NOT EXISTS sqlpage_files(path VARCHAR(255) NOT NULL PRIMARY KEY, contents BLOB, last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP);",
153+
}
154+
}
155+
147156
async fn init(db: &Database) -> anyhow::Result<Self> {
148157
log::debug!("Initializing database filesystem queries");
149158
let db_kind = db.connection.any_kind();
@@ -158,7 +167,7 @@ impl DbFsQueries {
158167
db_kind: AnyKind,
159168
) -> anyhow::Result<AnyStatement<'static>> {
160169
let was_modified_query = format!(
161-
"SELECT last_modified >= {} from sqlpage_files WHERE path = {} LIMIT 1",
170+
"SELECT 1 from sqlpage_files WHERE last_modified >= {} AND path = {}",
162171
make_placeholder(db_kind, 1),
163172
make_placeholder(db_kind, 2)
164173
);
@@ -191,9 +200,9 @@ impl DbFsQueries {
191200
.query_as::<(bool,)>()
192201
.bind(since)
193202
.bind(path.display().to_string())
194-
.fetch_one(&app_state.db.connection)
203+
.fetch_optional(&app_state.db.connection)
195204
.await
196-
.map(|(modified,)| modified)
205+
.map(|modified| modified.is_some())
197206
.with_context(|| {
198207
format!("Unable to check when {path:?} was last modified in the database")
199208
})
@@ -227,19 +236,14 @@ async fn test_sql_file_read_utf8() -> anyhow::Result<()> {
227236
use sqlx::Executor;
228237
let config = app_config::tests::test_config();
229238
let state = AppState::init(&config).await?;
239+
let create_table_sql = DbFsQueries::get_create_table_sql(state.db.connection.any_kind());
230240
state
231241
.db
232242
.connection
233-
.execute(
234-
r"
235-
CREATE TABLE sqlpage_files(
236-
path VARCHAR(255) NOT NULL PRIMARY KEY,
237-
contents BLOB,
238-
last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP
239-
);
240-
INSERT INTO sqlpage_files(path, contents) VALUES ('unit test file.txt', 'Héllö world! 😀');
241-
",
242-
)
243+
.execute(format!(
244+
"{create_table_sql}
245+
INSERT INTO sqlpage_files(path, contents) VALUES ('unit test file.txt', 'Héllö world! 😀');
246+
").as_str())
243247
.await?;
244248
let fs = FileSystem::init("/", &state.db).await;
245249
let actual = fs

0 commit comments

Comments
 (0)