alter-table-builder.vala 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using Invercargill.DataStructures;
  2. using InvercargillSql.Orm;
  3. namespace InvercargillSql.Migrations {
  4. /**
  5. * Builder for ALTER TABLE operations that provides a fluent API for modifying existing tables.
  6. *
  7. * This builder is used within MigrationBuilder.alter_table() to add, drop, or rename columns,
  8. * and to create or drop indexes. It follows the builder pattern, returning itself for chaining
  9. * alter operations.
  10. *
  11. * Example usage within a migration:
  12. * {{{
  13. * b.alter_table("users", t => {
  14. * t.add_column<string>("email", c => c.type_text().not_null())
  15. * t.rename_column("old_name", "new_name")
  16. * t.drop_column("deprecated_field")
  17. * t.create_index("idx_email").on_column("email")
  18. * t.drop_index("idx_old_email")
  19. * t.drop_foreign_key("fk_users_profile")
  20. * t.drop_foreign_key_on("profile_id")
  21. * t.drop_index_on("email")
  22. * });
  23. * }}}
  24. */
  25. public class AlterTableBuilder : Object {
  26. private MigrationBuilder _parent;
  27. private string _table_name;
  28. private Vector<SchemaOperation> _operations;
  29. private Vector<MigrationColumnBuilder> _column_builders;
  30. /**
  31. * Creates a new AlterTableBuilder for the specified table.
  32. *
  33. * @param parent the parent MigrationBuilder to return to after alterations
  34. * @param table_name the name of the table to alter
  35. */
  36. internal AlterTableBuilder(MigrationBuilder parent, string table_name) {
  37. _parent = parent;
  38. _table_name = table_name;
  39. _operations = new Vector<SchemaOperation>();
  40. _column_builders = new Vector<MigrationColumnBuilder>();
  41. }
  42. /**
  43. * Adds a new column to the table.
  44. *
  45. * The generic type T is used to infer the column type. The returned
  46. * MigrationColumnBuilder can be used to configure additional column properties.
  47. *
  48. * Note: The builder is tracked internally to collect FK constraints and index
  49. * operations when get_operations() is called.
  50. *
  51. * @param name the name of the column to add
  52. * @return a MigrationColumnBuilder for configuring the column
  53. */
  54. public MigrationColumnBuilder add_column<T>(string name) {
  55. var col = new ColumnDefinition() {
  56. name = name,
  57. column_type = ColumnType.from_gtype(typeof(T)) ?? ColumnType.TEXT
  58. };
  59. // Add the operation immediately - the column builder will modify it in place
  60. _operations.add(new AddColumnOperation() {
  61. table_name = _table_name,
  62. column = col
  63. });
  64. var builder = new MigrationColumnBuilder.for_alter(this, _table_name, col);
  65. _column_builders.add(builder);
  66. return builder;
  67. }
  68. /**
  69. * Creates an index on this table.
  70. *
  71. * Use the returned IndexBuilder to specify columns and uniqueness.
  72. *
  73. * @param name the name of the index
  74. * @return an IndexBuilder for configuring the index
  75. *
  76. * Example:
  77. * {{{
  78. * t.create_index("idx_email").on_column("email")
  79. * t.create_index("idx_composite").on_columns("email", "name").unique()
  80. * }}}
  81. */
  82. public IndexBuilder create_index(string name) {
  83. return new IndexBuilder.for_alter(this, _table_name, name);
  84. }
  85. /**
  86. * Drops an index from this table.
  87. *
  88. * @param name the name of the index to drop
  89. * @return this builder for method chaining
  90. */
  91. public AlterTableBuilder drop_index(string name) {
  92. _operations.add(new DropIndexOperation() {
  93. index_name = name,
  94. table_name = _table_name
  95. });
  96. return this;
  97. }
  98. /**
  99. * Drops a foreign key constraint by its explicit name.
  100. *
  101. * Use this when you know the exact constraint name.
  102. *
  103. * @param constraint_name the name of the FK constraint to drop
  104. * @return this builder for method chaining
  105. */
  106. public AlterTableBuilder drop_foreign_key(string constraint_name) {
  107. var op = new DropForeignKeyOperation(_table_name);
  108. op.constraint_name = constraint_name;
  109. _operations.add(op);
  110. return this;
  111. }
  112. /**
  113. * Drops a foreign key constraint by column name.
  114. *
  115. * This is useful for auto-generated constraint names (fk_{table}_{column}).
  116. *
  117. * @param column_name the column name that has the FK constraint
  118. * @return this builder for method chaining
  119. */
  120. public AlterTableBuilder drop_foreign_key_on(string column_name) {
  121. var op = new DropForeignKeyOperation(_table_name);
  122. op.column_name = column_name;
  123. _operations.add(op);
  124. return this;
  125. }
  126. /**
  127. * Drops an index by column name.
  128. *
  129. * This is useful for auto-generated index names (idx_{table}_{column}).
  130. *
  131. * @param column_name the column name that has the index
  132. * @return this builder for method chaining
  133. */
  134. public AlterTableBuilder drop_index_on(string column_name) {
  135. string index_name = @"idx_$(_table_name)_$(column_name)";
  136. _operations.add(new DropIndexOperation() {
  137. index_name = index_name,
  138. table_name = _table_name
  139. });
  140. return this;
  141. }
  142. /**
  143. * Adds an AddColumnOperation to the operations list.
  144. *
  145. * This is used internally by MigrationColumnBuilder to add the configured
  146. * column definition to the alter operations.
  147. *
  148. * @param col the column definition to add
  149. */
  150. internal void add_column_operation(ColumnDefinition col) {
  151. _operations.add(new AddColumnOperation() {
  152. table_name = _table_name,
  153. column = col
  154. });
  155. }
  156. /**
  157. * Adds an index operation to the operations list.
  158. *
  159. * This is used internally by IndexBuilder.
  160. *
  161. * @param op the index operation to add
  162. */
  163. internal void add_index_operation(CreateIndexOperation op) {
  164. _operations.add(op);
  165. }
  166. /**
  167. * Drops a column from the table.
  168. *
  169. * @param name the name of the column to drop
  170. * @return this builder for method chaining
  171. */
  172. public AlterTableBuilder drop_column(string name) {
  173. _operations.add(new DropColumnOperation() {
  174. table_name = _table_name,
  175. column_name = name
  176. });
  177. return this;
  178. }
  179. /**
  180. * Renames a column in the table.
  181. *
  182. * @param old_name the current name of the column
  183. * @param new_name the new name for the column
  184. * @return this builder for method chaining
  185. */
  186. public AlterTableBuilder rename_column(string old_name, string new_name) {
  187. _operations.add(new RenameColumnOperation() {
  188. table_name = _table_name,
  189. old_name = old_name,
  190. new_name = new_name
  191. });
  192. return this;
  193. }
  194. /**
  195. * Returns to the parent MigrationBuilder.
  196. *
  197. * This method is used by MigrationColumnBuilder to navigate back to the
  198. * MigrationBuilder after configuring a column.
  199. *
  200. * @return the parent MigrationBuilder
  201. */
  202. internal MigrationBuilder return_to_migration() {
  203. return _parent;
  204. }
  205. /**
  206. * Gets all schema operations collected by this builder.
  207. *
  208. * This method also collects FK constraints and index operations from column builders.
  209. * For ALTER TABLE ADD COLUMN, FK constraints are added as separate operations since
  210. * they need to be applied after the column is added.
  211. *
  212. * @return a Vector of SchemaOperation objects representing the ALTER TABLE operations
  213. */
  214. internal Vector<SchemaOperation> get_operations() {
  215. // Process column builders to collect FK and index operations
  216. // We need to find the corresponding AddColumnOperation for each builder
  217. // and add FK/index operations as needed
  218. // First, collect all add column operations to match with builders
  219. Vector<AddColumnOperation> add_col_ops = new Vector<AddColumnOperation>();
  220. foreach (var op in _operations) {
  221. if (op is AddColumnOperation) {
  222. add_col_ops.add((AddColumnOperation) op);
  223. }
  224. }
  225. // Process each column builder
  226. for (int i = 0; i < (int) _column_builders.length && i < (int) add_col_ops.length; i++) {
  227. var builder = _column_builders.get(i);
  228. var add_col_op = add_col_ops.get(i);
  229. var col = add_col_op.column;
  230. // Collect FK constraint if configured
  231. // For ALTER TABLE, we need to add the FK as a table constraint
  232. // Note: SQLite doesn't support ADD CONSTRAINT, so the dialect needs
  233. // to handle this appropriately (e.g., table recreation)
  234. var fk_builder = builder.fk_builder;
  235. if (fk_builder != null) {
  236. var constraint = fk_builder.build_constraint(col.name, _table_name);
  237. // Store the constraint info for the dialect to use
  238. // We add a marker to the AddColumnOperation by storing FK info
  239. // The dialect will generate inline REFERENCES clause
  240. add_col_op.foreign_key_constraint = constraint;
  241. }
  242. // Collect index operation if configured
  243. if (builder.should_create_index) {
  244. var index_op = new CreateIndexOperation() {
  245. table_name = _table_name,
  246. is_unique = col.is_unique
  247. };
  248. index_op.columns.add(col.name);
  249. // Use custom index name or auto-generate
  250. index_op.index_name = builder.index_name ?? @"idx_$(_table_name)_$(col.name)";
  251. _operations.add(index_op);
  252. }
  253. }
  254. return _operations;
  255. }
  256. }
  257. }