Skip to content

Commit 9a1ccf0

Browse files
sj-robertrobert-sjoblom
authored andcommitted
refactor: remove dead code and simplify trivial abstractions
1 parent e2bfb36 commit 9a1ccf0

6 files changed

Lines changed: 14 additions & 66 deletions

File tree

src/config.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum ConfigError {
1919
}
2020

2121
/// Main configuration structure
22-
#[derive(Debug, Clone, Deserialize, Serialize)]
22+
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
2323
pub struct Config {
2424
#[serde(default)]
2525
pub migrations: MigrationsConfig,
@@ -157,20 +157,4 @@ impl Config {
157157
let config: Config = toml::from_str(&contents)?;
158158
Ok(config)
159159
}
160-
161-
/// Create a default configuration
162-
pub fn default_config() -> Self {
163-
Self {
164-
migrations: MigrationsConfig::default(),
165-
liquibase: LiquibaseConfig::default(),
166-
output: OutputConfig::default(),
167-
cli: CliConfig::default(),
168-
}
169-
}
170-
}
171-
172-
impl Default for Config {
173-
fn default() -> Self {
174-
Self::default_config()
175-
}
176160
}

src/input/liquibase_bridge.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ pub struct BridgeLoader {
2626
#[derive(Debug, Deserialize)]
2727
struct BridgeChangeset {
2828
changeset_id: String,
29-
#[serde(default)]
30-
#[allow(dead_code)]
31-
author: String,
3229
sql: String,
3330
xml_file: String,
3431
#[serde(default = "default_xml_line")]

src/input/liquibase_xml.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ enum ParseState {
7878
#[derive(Debug, Clone)]
7979
struct ChangeSetInfo {
8080
id: String,
81-
#[allow(dead_code)]
82-
author: String,
8381
run_in_transaction: bool,
8482
line: usize,
8583
sql_parts: Vec<String>,
@@ -89,14 +87,12 @@ impl ChangeSetInfo {
8987
/// Create a new ChangeSetInfo from attributes of a <changeSet> element.
9088
fn from_attributes(attrs: &[(String, String)], line: usize) -> Self {
9189
let id = get_attr(attrs, "id").unwrap_or_default();
92-
let author = get_attr(attrs, "author").unwrap_or_default();
9390
let run_in_transaction = get_attr(attrs, "runInTransaction")
9491
.map(|v| v != "false")
9592
.unwrap_or(true);
9693

9794
Self {
9895
id,
99-
author,
10096
run_in_transaction,
10197
line,
10298
sql_parts: Vec::new(),

src/input/sql.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ use std::path::{Path, PathBuf};
1414
/// lexicographically by filename. Each file becomes one `MigrationUnit`.
1515
///
1616
/// Down migrations are detected by filename patterns: `.down.sql` or `_down.sql`.
17+
#[derive(Default)]
1718
pub struct SqlLoader;
1819

1920
impl SqlLoader {
20-
/// Create a new `SqlLoader`.
21-
pub fn new() -> Self {
22-
Self
23-
}
24-
2521
/// Load a single SQL file and parse it into a `MigrationUnit`.
2622
///
2723
/// The file is read entirely into memory, parsed into IR nodes, and
@@ -52,12 +48,6 @@ impl SqlLoader {
5248
}
5349
}
5450

55-
impl Default for SqlLoader {
56-
fn default() -> Self {
57-
Self::new()
58-
}
59-
}
60-
6151
impl MigrationLoader for SqlLoader {
6252
/// Load migrations from the given paths.
6353
///
@@ -108,8 +98,6 @@ impl MigrationLoader for SqlLoader {
10898
}
10999

110100
/// Collect all `.sql` files from a directory (non-recursive).
111-
///
112-
/// Returns the files sorted lexicographically by filename.
113101
fn collect_sql_files(dir: &Path) -> Result<Vec<PathBuf>, LoadError> {
114102
let entries = std::fs::read_dir(dir).map_err(|e| LoadError::Io {
115103
path: dir.to_path_buf(),
@@ -130,7 +118,6 @@ fn collect_sql_files(dir: &Path) -> Result<Vec<PathBuf>, LoadError> {
130118
}
131119
}
132120

133-
files.sort();
134121
Ok(files)
135122
}
136123

@@ -255,7 +242,7 @@ mod tests {
255242
// Also create a non-SQL file that should be ignored
256243
fs::write(dir.path().join("README.md"), "# Migrations").expect("write");
257244

258-
let loader = SqlLoader::new();
245+
let loader = SqlLoader;
259246
let history = loader
260247
.load(&[dir.path().to_path_buf()])
261248
.expect("Failed to load migrations");
@@ -274,21 +261,21 @@ mod tests {
274261
let file_path = dir.path().join("migration.sql");
275262
fs::write(&file_path, "CREATE TABLE t (id int);").expect("write");
276263

277-
let loader = SqlLoader::new();
264+
let loader = SqlLoader;
278265
let history = loader.load(&[file_path]).expect("Failed to load migration");
279266
assert_eq!(history.units.len(), 1);
280267
}
281268

282269
#[test]
283270
fn test_loader_nonexistent_path() {
284-
let loader = SqlLoader::new();
271+
let loader = SqlLoader;
285272
let result = loader.load(&[PathBuf::from("/nonexistent/path")]);
286273
assert!(result.is_err());
287274
}
288275

289276
#[test]
290277
fn test_loader_empty_paths() {
291-
let loader = SqlLoader::new();
278+
let loader = SqlLoader;
292279
let history = loader.load(&[]).expect("Empty paths should succeed");
293280
assert!(history.units.is_empty());
294281
}
@@ -309,7 +296,7 @@ mod tests {
309296
)
310297
.expect("write");
311298

312-
let loader = SqlLoader::new();
299+
let loader = SqlLoader;
313300
let history = loader
314301
.load(&[dir1.path().to_path_buf(), dir2.path().to_path_buf()])
315302
.expect("Failed to load migrations");

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn load_migrations(config: &Config) -> Result<MigrationHistory> {
357357
}
358358
"filename_lexicographic" => {
359359
eprintln!("pg-migration-lint: using filename_lexicographic strategy");
360-
let loader = SqlLoader::new();
360+
let loader = SqlLoader;
361361
let history = loader
362362
.load(&config.migrations.paths)
363363
.context("Failed to load migrations")?;
@@ -368,7 +368,7 @@ fn load_migrations(config: &Config) -> Result<MigrationHistory> {
368368
"pg-migration-lint: unknown strategy '{}', falling back to filename_lexicographic",
369369
other
370370
);
371-
let loader = SqlLoader::new();
371+
let loader = SqlLoader;
372372
let history = loader
373373
.load(&config.migrations.paths)
374374
.context("Failed to load migrations")?;

src/parser/pg_query.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn convert_node(node: &NodeEnum, raw_sql: &str) -> IrNode {
116116
// ---------------------------------------------------------------------------
117117

118118
/// Convert a pg_query `CreateStmt` to `IrNode::CreateTable`.
119-
fn convert_create_table(create: &pg_query::protobuf::CreateStmt, raw_sql: &str) -> IrNode {
119+
fn convert_create_table(create: &pg_query::protobuf::CreateStmt, _raw_sql: &str) -> IrNode {
120120
let name = relation_to_qualified_name(create.relation.as_ref());
121121

122122
let temporary = matches!(
@@ -155,7 +155,6 @@ fn convert_create_table(create: &pg_query::protobuf::CreateStmt, raw_sql: &str)
155155
constraints,
156156
temporary,
157157
})
158-
.or_unparseable(raw_sql)
159158
}
160159

161160
/// Convert a pg_query `ColumnDef` into an IR `ColumnDef` plus any inline
@@ -661,14 +660,11 @@ fn extract_qualified_name_from_drop_objects(
661660
1 => Some(QualifiedName::unqualified(&strings[0])),
662661
2 => Some(QualifiedName::qualified(&strings[0], &strings[1])),
663662
_ if !strings.is_empty() => {
664-
// Take last two as schema.name
663+
// Take last two as schema.name (len >= 3 guaranteed here
664+
// since len == 1 and len == 2 are handled above)
665665
let name = strings.last().cloned().unwrap_or_default();
666-
if strings.len() >= 2 {
667-
let schema = strings[strings.len() - 2].clone();
668-
Some(QualifiedName::qualified(schema, name))
669-
} else {
670-
Some(QualifiedName::unqualified(name))
671-
}
666+
let schema = strings[strings.len() - 2].clone();
667+
Some(QualifiedName::qualified(schema, name))
672668
}
673669
_ => None,
674670
};
@@ -805,18 +801,6 @@ fn extract_first_identifier(s: &str) -> Option<String> {
805801
}
806802
}
807803

808-
/// Extension trait to allow fallback to `Unparseable` if conversion panics.
809-
/// This is used internally as a safe guard.
810-
trait OrUnparseable {
811-
fn or_unparseable(self, raw_sql: &str) -> IrNode;
812-
}
813-
814-
impl OrUnparseable for IrNode {
815-
fn or_unparseable(self, _raw_sql: &str) -> IrNode {
816-
self
817-
}
818-
}
819-
820804
#[cfg(test)]
821805
mod tests {
822806
use super::*;

0 commit comments

Comments
 (0)