Skip to content

Commit 01340d7

Browse files
authored
feat: add helpers to create/drop a schema (#855)
1 parent 0937226 commit 01340d7

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#### Added
1212

13-
- ...
13+
- [#855](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/855) Add helpers to create/change/drop a schema.
1414

1515
## v6.0.1
1616

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ Depending on your user and schema setup, it may be needed to use a table name pr
5252
ActiveRecord::Base.table_name_prefix = 'dbo.'
5353
```
5454

55+
It's also possible to create/change/drop a schema in the migration file as in the example below:
56+
57+
```ruby
58+
class CreateFooSchema < ActiveRecord::Migration[6.0]
59+
def up
60+
create_schema('foo')
61+
62+
# Or you could move a table to a different schema
63+
64+
change_table_schema('foo', 'dbo.admin')
65+
end
66+
67+
def down
68+
drop_schema('foo')
69+
end
70+
end
71+
```
72+
5573

5674
#### Configure Connection & App Name
5775

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ def create_schema_dumper(options)
281281
SQLServer::SchemaDumper.create(self, options)
282282
end
283283

284+
def create_schema(schema_name, authorization = nil)
285+
sql = "CREATE SCHEMA [#{schema_name}]"
286+
sql += " AUTHORIZATION [#{authorization}]" if authorization
287+
288+
execute sql
289+
end
290+
291+
def change_table_schema(schema_name, table_name)
292+
execute "ALTER SCHEMA [#{schema_name}] TRANSFER [#{table_name}]"
293+
end
294+
295+
def drop_schema(schema_name)
296+
execute "DROP SCHEMA [#{schema_name}]"
297+
end
298+
284299
private
285300

286301
def data_source_sql(name = nil, type: nil)

test/cases/migration_test_sqlserver.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,48 @@ class MigrationTestSQLServer < ActiveRecord::TestCase
6464
assert_nothing_raised { connection.change_column :people, :first_name, :text, null: true, default: nil }
6565
end
6666
end
67+
68+
describe "#create_schema" do
69+
it "creates a new schema" do
70+
connection.create_schema("some schema")
71+
72+
schemas = connection.exec_query("select name from sys.schemas").to_a
73+
74+
assert_includes schemas, { "name" => "some schema" }
75+
end
76+
77+
it "creates a new schema with an owner" do
78+
connection.create_schema("some schema", :guest)
79+
80+
schemas = connection.exec_query("select name, principal_id from sys.schemas").to_a
81+
82+
assert_includes schemas, { "name" => "some schema", "principal_id" => 2 }
83+
end
84+
end
85+
86+
describe "#change_table_schema" do
87+
before { connection.create_schema("foo") }
88+
89+
it "transfer the given table to the given schema" do
90+
connection.change_table_schema("foo", "orders")
91+
92+
assert connection.data_source_exists?("foo.orders")
93+
end
94+
end
95+
96+
describe "#drop_schema" do
97+
before { connection.create_schema("some schema") }
98+
99+
it "drops a schema" do
100+
schemas = connection.exec_query("select name from sys.schemas").to_a
101+
102+
assert_includes schemas, { "name" => "some schema" }
103+
104+
connection.drop_schema("some schema")
105+
106+
schemas = connection.exec_query("select name from sys.schemas").to_a
107+
108+
refute_includes schemas, { "name" => "some schema" }
109+
end
110+
end
67111
end

0 commit comments

Comments
 (0)