Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
59eb9ad2
Commit
59eb9ad2
authored
Jan 13, 2020
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use plain SQL schema
Relates to
https://gitlab.com/gitlab-org/gitlab/issues/29465
parent
27b57558
Changes
20
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
272 additions
and
39 deletions
+272
-39
changelogs/unreleased/ab-structuresql.yml
changelogs/unreleased/ab-structuresql.yml
+5
-0
config/application.rb
config/application.rb
+1
-1
danger/database/Dangerfile
danger/database/Dangerfile
+2
-2
doc/development/database_review.md
doc/development/database_review.md
+2
-2
doc/development/omnibus.md
doc/development/omnibus.md
+1
-1
doc/development/scalability.md
doc/development/scalability.md
+1
-1
doc/update/patch_versions.md
doc/update/patch_versions.md
+1
-1
doc/update/upgrading_from_source.md
doc/update/upgrading_from_source.md
+1
-1
lib/gitlab/database/schema_cleaner.rb
lib/gitlab/database/schema_cleaner.rb
+43
-0
lib/gitlab/fake_application_settings.rb
lib/gitlab/fake_application_settings.rb
+1
-1
lib/tasks/gitlab/db.rake
lib/tasks/gitlab/db.rake
+16
-1
scripts/schema_changed.sh
scripts/schema_changed.sh
+4
-4
scripts/utils.sh
scripts/utils.sh
+1
-1
spec/fixtures/gitlab/database/structure_example.sql
spec/fixtures/gitlab/database/structure_example.sql
+96
-0
spec/fixtures/gitlab/database/structure_example_cleaned.sql
spec/fixtures/gitlab/database/structure_example_cleaned.sql
+44
-0
spec/lib/gitlab/danger/helper_spec.rb
spec/lib/gitlab/danger/helper_spec.rb
+1
-0
spec/lib/gitlab/database/schema_cleaner_spec.rb
spec/lib/gitlab/database/schema_cleaner_spec.rb
+31
-0
spec/migrations/active_record/schema_spec.rb
spec/migrations/active_record/schema_spec.rb
+11
-9
spec/tasks/gitlab/backup_rake_spec.rb
spec/tasks/gitlab/backup_rake_spec.rb
+1
-5
spec/tasks/gitlab/db_rake_spec.rb
spec/tasks/gitlab/db_rake_spec.rb
+9
-9
No files found.
changelogs/unreleased/ab-structuresql.yml
0 → 100644
View file @
59eb9ad2
---
title
:
Convert schema to plain SQL using structure.sql
merge_request
:
22808
author
:
type
:
other
config/application.rb
View file @
59eb9ad2
...
@@ -144,7 +144,7 @@ module Gitlab
...
@@ -144,7 +144,7 @@ module Gitlab
# Use SQL instead of Active Record's schema dumper when creating the database.
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# like if you have constraints or database-specific column types
#
config.active_record.schema_format = :sql
config
.
active_record
.
schema_format
=
:sql
# Configure webpack
# Configure webpack
config
.
webpack
.
config_file
=
"config/webpack.config.js"
config
.
webpack
.
config_file
=
"config/webpack.config.js"
...
...
danger/database/Dangerfile
View file @
59eb9ad2
...
@@ -31,7 +31,7 @@ MSG
...
@@ -31,7 +31,7 @@ MSG
DATABASE_APPROVED_LABEL
=
'database::approved'
DATABASE_APPROVED_LABEL
=
'database::approved'
non_geo_db_schema_updated
=
!
git
.
modified_files
.
grep
(
%r{
\A
db/s
chema
\.
rb
}
).
empty?
non_geo_db_schema_updated
=
!
git
.
modified_files
.
grep
(
%r{
\A
db/s
tructure
\.
sql
}
).
empty?
geo_db_schema_updated
=
!
git
.
modified_files
.
grep
(
%r{
\A
ee/db/geo/schema
\.
rb}
).
empty?
geo_db_schema_updated
=
!
git
.
modified_files
.
grep
(
%r{
\A
ee/db/geo/schema
\.
rb}
).
empty?
non_geo_migration_created
=
!
git
.
added_files
.
grep
(
%r{
\A
(db/(post_)?migrate)/}
).
empty?
non_geo_migration_created
=
!
git
.
added_files
.
grep
(
%r{
\A
(db/(post_)?migrate)/}
).
empty?
...
@@ -40,7 +40,7 @@ geo_migration_created = !git.added_files.grep(%r{\Aee/db/geo/(post_)?migrate/}).
...
@@ -40,7 +40,7 @@ geo_migration_created = !git.added_files.grep(%r{\Aee/db/geo/(post_)?migrate/}).
format_str
=
gitlab_danger
.
ci?
?
SCHEMA_NOT_UPDATED_MESSAGE_FULL
:
SCHEMA_NOT_UPDATED_MESSAGE_SHORT
format_str
=
gitlab_danger
.
ci?
?
SCHEMA_NOT_UPDATED_MESSAGE_FULL
:
SCHEMA_NOT_UPDATED_MESSAGE_SHORT
if
non_geo_migration_created
&&
!
non_geo_db_schema_updated
if
non_geo_migration_created
&&
!
non_geo_db_schema_updated
warn
format
(
format_str
,
migrations:
'migrations'
,
schema:
gitlab_danger
.
html_link
(
"db/s
chema.rb
"
))
warn
format
(
format_str
,
migrations:
'migrations'
,
schema:
gitlab_danger
.
html_link
(
"db/s
tructure.sql
"
))
end
end
if
geo_migration_created
&&
!
geo_db_schema_updated
if
geo_migration_created
&&
!
geo_db_schema_updated
...
...
doc/development/database_review.md
View file @
59eb9ad2
...
@@ -81,7 +81,7 @@ the following preparations into account.
...
@@ -81,7 +81,7 @@ the following preparations into account.
#### Preparation when adding migrations
#### Preparation when adding migrations
-
Ensure
`db/s
chema.rb
`
is updated.
-
Ensure
`db/s
tructure.sql
`
is updated.
-
Make migrations reversible by using the
`change`
method or include a
`down`
method when using
`up`
.
-
Make migrations reversible by using the
`change`
method or include a
`down`
method when using
`up`
.
-
Include either a rollback procedure or describe how to rollback changes.
-
Include either a rollback procedure or describe how to rollback changes.
-
Add the output of the migration(s) to the MR description.
-
Add the output of the migration(s) to the MR description.
...
@@ -137,7 +137,7 @@ the following preparations into account.
...
@@ -137,7 +137,7 @@ the following preparations into account.
-
[
Check indexes are present for foreign keys
](
migration_style_guide.md#adding-foreign-key-constraints
)
-
[
Check indexes are present for foreign keys
](
migration_style_guide.md#adding-foreign-key-constraints
)
-
Ensure that migrations execute in a transaction or only contain
-
Ensure that migrations execute in a transaction or only contain
concurrent index/foreign key helpers (with transactions disabled)
concurrent index/foreign key helpers (with transactions disabled)
-
Check consistency with
`db/s
chema.rb
`
and that migrations are
[
reversible
](
migration_style_guide.md#reversibility
)
-
Check consistency with
`db/s
tructure.sql
`
and that migrations are
[
reversible
](
migration_style_guide.md#reversibility
)
-
Check queries timing (If any): Queries executed in a migration
-
Check queries timing (If any): Queries executed in a migration
need to fit comfortably within
`15s`
- preferably much less than that - on GitLab.com.
need to fit comfortably within
`15s`
- preferably much less than that - on GitLab.com.
-
For column removals, make sure the column has been
[
ignored in a previous release
](
what_requires_downtime.md#dropping-columns
)
-
For column removals, make sure the column has been
[
ignored in a previous release
](
what_requires_downtime.md#dropping-columns
)
...
...
doc/development/omnibus.md
View file @
59eb9ad2
...
@@ -12,7 +12,7 @@ extra security. The Omnibus reconfigure script contains commands that give
...
@@ -12,7 +12,7 @@ extra security. The Omnibus reconfigure script contains commands that give
write access to the
`git`
user only where needed.
write access to the
`git`
user only where needed.
For example, the
`git`
user is allowed to write in the
`log/`
directory, in
For example, the
`git`
user is allowed to write in the
`log/`
directory, in
`public/uploads`
, and they are allowed to rewrite the
`db/s
chema.rb
`
file.
`public/uploads`
, and they are allowed to rewrite the
`db/s
tructure.sql
`
file.
In other cases, the reconfigure script tricks GitLab into not trying to write a
In other cases, the reconfigure script tricks GitLab into not trying to write a
file. For instance, GitLab will generate a
`.secret`
file if it cannot find one
file. For instance, GitLab will generate a
`.secret`
file if it cannot find one
...
...
doc/development/scalability.md
View file @
59eb9ad2
...
@@ -18,7 +18,7 @@ users. We will discuss each component below.
...
@@ -18,7 +18,7 @@ users. We will discuss each component below.
The PostgreSQL database holds all metadata for projects, issues, merge
The PostgreSQL database holds all metadata for projects, issues, merge
requests, users, etc. The schema is managed by the Rails application
requests, users, etc. The schema is managed by the Rails application
[
db/s
chema.rb
](
https://gitlab.com/gitlab-org/gitlab/blob/master/db/schema.rb
)
.
[
db/s
tructure.sql
](
https://gitlab.com/gitlab-org/gitlab/blob/master/db/structure.sql
)
.
GitLab Web/API servers and Sidekiq nodes talk directly to the database via a
GitLab Web/API servers and Sidekiq nodes talk directly to the database via a
Rails object relational model (ORM). Most SQL queries are accessed via this
Rails object relational model (ORM). Most SQL queries are accessed via this
...
...
doc/update/patch_versions.md
View file @
59eb9ad2
...
@@ -31,7 +31,7 @@ current version with `cat VERSION`).
...
@@ -31,7 +31,7 @@ current version with `cat VERSION`).
cd
/home/git/gitlab
cd
/home/git/gitlab
sudo
-u
git
-H
git fetch
--all
sudo
-u
git
-H
git fetch
--all
sudo
-u
git
-H
git checkout
--
Gemfile.lock db/s
chema.rb
locale
sudo
-u
git
-H
git checkout
--
Gemfile.lock db/s
tructure.sql
locale
sudo
-u
git
-H
git checkout LATEST_TAG
-b
LATEST_TAG
sudo
-u
git
-H
git checkout LATEST_TAG
-b
LATEST_TAG
```
```
...
...
doc/update/upgrading_from_source.md
View file @
59eb9ad2
...
@@ -165,7 +165,7 @@ sudo make prefix=/usr/local install
...
@@ -165,7 +165,7 @@ sudo make prefix=/usr/local install
cd
/home/git/gitlab
cd
/home/git/gitlab
sudo
-u
git
-H
git fetch
--all
--prune
sudo
-u
git
-H
git fetch
--all
--prune
sudo
-u
git
-H
git checkout
--
db/s
chema.rb
# local changes will be restored automatically
sudo
-u
git
-H
git checkout
--
db/s
tructure.sql
# local changes will be restored automatically
sudo
-u
git
-H
git checkout
--
locale
sudo
-u
git
-H
git checkout
--
locale
```
```
...
...
lib/gitlab/database/schema_cleaner.rb
0 → 100644
View file @
59eb9ad2
# frozen_string_literal: true
module
Gitlab
module
Database
class
SchemaCleaner
attr_reader
:original_schema
def
initialize
(
original_schema
)
@original_schema
=
original_schema
end
def
clean
(
io
)
structure
=
original_schema
.
dup
# Postgres compat fix for PG 9.6 (which doesn't support (AS datatype) syntax for sequences)
structure
.
gsub!
(
/CREATE SEQUENCE [^.]+\.\S+\n(\s+AS integer\n)/
)
{
|
m
|
m
.
gsub
(
Regexp
.
last_match
[
1
],
''
)
}
# Also a PG 9.6 compatibility fix, see below.
structure
.
gsub!
(
/^CREATE EXTENSION IF NOT EXISTS plpgsql.*/
,
''
)
structure
.
gsub!
(
/^COMMENT ON EXTENSION.*/
,
''
)
# Remove noise
structure
.
gsub!
(
/^SET.+/
,
''
)
structure
.
gsub!
(
/^SELECT pg_catalog\.set_config\('search_path'.+/
,
''
)
structure
.
gsub!
(
/^--.*/
,
"
\n
"
)
structure
.
gsub!
(
/\n{3,}/
,
"
\n\n
"
)
io
<<
"SET search_path=public;
\n\n
"
# Adding plpgsql explicitly is again a compatibility fix for PG 9.6
# In more recent versions of pg_dump, the extension isn't explicitly dumped anymore.
# We use PG 9.6 still on CI and for schema checks - here this is still the case.
io
<<
<<~
SQL
.
strip
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
SQL
io
<<
structure
nil
end
end
end
end
lib/gitlab/fake_application_settings.rb
View file @
59eb9ad2
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
# ActiveRecord access. We rely on the initial values being true or false to
# ActiveRecord access. We rely on the initial values being true or false to
# determine whether to define a predicate method because for a newly-added
# determine whether to define a predicate method because for a newly-added
# column that has not been migrated yet, there is no way to determine the
# column that has not been migrated yet, there is no way to determine the
# column type without parsing db/s
chema.rb
.
# column type without parsing db/s
tructure.sql
.
module
Gitlab
module
Gitlab
class
FakeApplicationSettings
<
OpenStruct
class
FakeApplicationSettings
<
OpenStruct
include
ApplicationSettingImplementation
include
ApplicationSettingImplementation
...
...
lib/tasks/gitlab/db.rake
View file @
59eb9ad2
...
@@ -50,7 +50,7 @@ namespace :gitlab do
...
@@ -50,7 +50,7 @@ namespace :gitlab do
else
else
# Add post-migrate paths to ensure we mark all migrations as up
# Add post-migrate paths to ensure we mark all migrations as up
Gitlab
::
Database
.
add_post_migrate_path_to_rails
(
force:
true
)
Gitlab
::
Database
.
add_post_migrate_path_to_rails
(
force:
true
)
Rake
::
Task
[
'db:s
chema
:load'
].
invoke
Rake
::
Task
[
'db:s
tructure
:load'
].
invoke
Rake
::
Task
[
'db:seed_fu'
].
invoke
Rake
::
Task
[
'db:seed_fu'
].
invoke
end
end
end
end
...
@@ -78,5 +78,20 @@ namespace :gitlab do
...
@@ -78,5 +78,20 @@ namespace :gitlab do
else
else
task
:setup_ee
task
:setup_ee
end
end
desc
'This adjusts and cleans db/structure.sql - it runs after db:structure:dump'
task
:clean_structure_sql
do
structure_file
=
'db/structure.sql'
schema
=
File
.
read
(
structure_file
)
File
.
open
(
structure_file
,
'wb+'
)
do
|
io
|
Gitlab
::
Database
::
SchemaCleaner
.
new
(
schema
).
clean
(
io
)
end
end
# Inform Rake that gitlab:schema:fix_structure_sql should be run every time rake db:structure:dump is run
Rake
::
Task
[
'db:structure:dump'
].
enhance
do
Rake
::
Task
[
'gitlab:db:clean_structure_sql'
].
invoke
end
end
end
end
end
scripts/schema_changed.sh
View file @
59eb9ad2
#!/bin/sh
#!/bin/sh
schema_changed
()
{
schema_changed
()
{
if
[
!
-z
"
$(
git diff
--name-only
--
db/s
chema.rb
)
"
]
;
then
if
[
!
-z
"
$(
git diff
--name-only
--
db/s
tructure.sql
)
"
]
;
then
printf
"db/s
chema.rb
after rake db:migrate:reset is different from one in the repository"
printf
"db/s
tructure.sql
after rake db:migrate:reset is different from one in the repository"
printf
"The diff is as follows:
\n
"
printf
"The diff is as follows:
\n
"
diff
=
$(
git diff
-p
--binary
--
db/s
chema.rb
)
diff
=
$(
git diff
-p
--binary
--
db/s
tructure.sql
)
printf
"%s"
"
$diff
"
printf
"%s"
"
$diff
"
exit
1
exit
1
else
else
printf
"db/s
chema.rb
after rake db:migrate:reset matches one in the repository"
printf
"db/s
tructure.sql
after rake db:migrate:reset matches one in the repository"
fi
fi
}
}
...
...
scripts/utils.sh
View file @
59eb9ad2
...
@@ -20,7 +20,7 @@ function setup_db_user_only() {
...
@@ -20,7 +20,7 @@ function setup_db_user_only() {
function
setup_db
()
{
function
setup_db
()
{
setup_db_user_only
setup_db_user_only
bundle
exec
rake db:drop db:create db:s
chema
:load db:migrate
bundle
exec
rake db:drop db:create db:s
tructure
:load db:migrate
bundle
exec
rake gitlab:db:setup_ee
bundle
exec
rake gitlab:db:setup_ee
}
}
...
...
spec/fixtures/gitlab/database/structure_example.sql
0 → 100644
View file @
59eb9ad2
SET
statement_timeout
=
0
;
SET
lock_timeout
=
0
;
SET
idle_in_transaction_session_timeout
=
0
;
SET
client_encoding
=
'UTF8'
;
SET
standard_conforming_strings
=
on
;
SELECT
pg_catalog
.
set_config
(
'search_path'
,
''
,
false
);
SET
check_function_bodies
=
false
;
SET
xmloption
=
content
;
SET
client_min_messages
=
warning
;
SET
row_security
=
off
;
--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--
CREATE
EXTENSION
IF
NOT
EXISTS
pg_trgm
WITH
SCHEMA
public
;
--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner: -
--
COMMENT
ON
EXTENSION
pg_trgm
IS
'text similarity measurement and index searching based on trigrams'
;
SET
default_tablespace
=
''
;
SET
default_with_oids
=
false
;
--
-- Name: abuse_reports; Type: TABLE; Schema: public; Owner: -
--
CREATE
TABLE
public
.
abuse_reports
(
id
integer
NOT
NULL
,
reporter_id
integer
,
user_id
integer
,
message
text
,
created_at
timestamp
without
time
zone
,
updated_at
timestamp
without
time
zone
,
message_html
text
,
cached_markdown_version
integer
);
--
-- Name: abuse_reports_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE
SEQUENCE
public
.
abuse_reports_id_seq
AS
integer
START
WITH
1
INCREMENT
BY
1
NO
MINVALUE
NO
MAXVALUE
CACHE
1
;
--
-- Name: abuse_reports id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER
TABLE
ONLY
public
.
abuse_reports
ALTER
COLUMN
id
SET
DEFAULT
nextval
(
'public.abuse_reports_id_seq'
::
regclass
);
--
--
-- Name: abuse_reports abuse_reports_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER
TABLE
ONLY
public
.
abuse_reports
ADD
CONSTRAINT
abuse_reports_pkey
PRIMARY
KEY
(
id
);
--
-- Name: index_abuse_reports_on_user_id; Type: INDEX; Schema: public; Owner: -
--
CREATE
INDEX
index_abuse_reports_on_user_id
ON
public
.
abuse_reports
USING
btree
(
user_id
);
INSERT
INTO
"schema_migrations"
(
version
)
VALUES
(
'20200305121159'
),
(
'20200306095654'
),
(
'20200306160521'
),
(
'20200306170211'
),
(
'20200306170321'
),
(
'20200306170531'
),
(
'20200309140540'
),
(
'20200309195209'
),
(
'20200309195710'
),
(
'20200310132654'
),
(
'20200310135823'
);
spec/fixtures/gitlab/database/structure_example_cleaned.sql
0 → 100644
View file @
59eb9ad2
SET
search_path
=
public
;
CREATE
EXTENSION
IF
NOT
EXISTS
plpgsql
WITH
SCHEMA
pg_catalog
;
CREATE
EXTENSION
IF
NOT
EXISTS
pg_trgm
WITH
SCHEMA
public
;
CREATE
TABLE
public
.
abuse_reports
(
id
integer
NOT
NULL
,
reporter_id
integer
,
user_id
integer
,
message
text
,
created_at
timestamp
without
time
zone
,
updated_at
timestamp
without
time
zone
,
message_html
text
,
cached_markdown_version
integer
);
CREATE
SEQUENCE
public
.
abuse_reports_id_seq
START
WITH
1
INCREMENT
BY
1
NO
MINVALUE
NO
MAXVALUE
CACHE
1
;
ALTER
TABLE
ONLY
public
.
abuse_reports
ALTER
COLUMN
id
SET
DEFAULT
nextval
(
'public.abuse_reports_id_seq'
::
regclass
);
ALTER
TABLE
ONLY
public
.
abuse_reports
ADD
CONSTRAINT
abuse_reports_pkey
PRIMARY
KEY
(
id
);
CREATE
INDEX
index_abuse_reports_on_user_id
ON
public
.
abuse_reports
USING
btree
(
user_id
);
INSERT
INTO
"schema_migrations"
(
version
)
VALUES
(
'20200305121159'
),
(
'20200306095654'
),
(
'20200306160521'
),
(
'20200306170211'
),
(
'20200306170321'
),
(
'20200306170531'
),
(
'20200309140540'
),
(
'20200309195209'
),
(
'20200309195710'
),
(
'20200310132654'
),
(
'20200310135823'
);
spec/lib/gitlab/danger/helper_spec.rb
View file @
59eb9ad2
...
@@ -229,6 +229,7 @@ describe Gitlab::Danger::Helper do
...
@@ -229,6 +229,7 @@ describe Gitlab::Danger::Helper do
'ee/FOO_VERSION'
|
:unknown
'ee/FOO_VERSION'
|
:unknown
'db/schema.rb'
|
:database
'db/schema.rb'
|
:database
'db/structure.sql'
|
:database
'db/migrate/foo'
|
:database
'db/migrate/foo'
|
:database
'db/post_migrate/foo'
|
:database
'db/post_migrate/foo'
|
:database
'ee/db/migrate/foo'
|
:database
'ee/db/migrate/foo'
|
:database
...
...
spec/lib/gitlab/database/schema_cleaner_spec.rb
0 → 100644
View file @
59eb9ad2
# frozen_string_literal: true
require
'spec_helper'
describe
Gitlab
::
Database
::
SchemaCleaner
do
let
(
:example_schema
)
{
fixture_file
(
File
.
join
(
'gitlab'
,
'database'
,
'structure_example.sql'
))
}
let
(
:io
)
{
StringIO
.
new
}
subject
do
described_class
.
new
(
example_schema
).
clean
(
io
)
io
.
string
end
it
'removes comments on extensions'
do
expect
(
subject
).
not_to
include
(
'COMMENT ON EXTENSION'
)
end
it
'includes the plpgsql extension'
do
expect
(
subject
).
to
include
(
'CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;'
)
end
it
'sets the search_path'
do
expect
(
subject
.
split
(
"
\n
"
).
first
).
to
eq
(
'SET search_path=public;'
)
end
it
'cleans up the full schema as expected (blackbox test with example)'
do
expected_schema
=
fixture_file
(
File
.
join
(
'gitlab'
,
'database'
,
'structure_example_cleaned.sql'
))
expect
(
subject
).
to
eq
(
expected_schema
)
end
end
spec/migrations/active_record/schema_spec.rb
View file @
59eb9ad2
...
@@ -2,27 +2,29 @@
...
@@ -2,27 +2,29 @@
require
'spec_helper'
require
'spec_helper'
# Check consistency of db/s
chema.rb
version, migrations' timestamps, and the latest migration timestamp
# Check consistency of db/s
tructure.sql
version, migrations' timestamps, and the latest migration timestamp
# stored in the database's schema_migrations table.
# stored in the database's schema_migrations table.
describe
ActiveRecord
::
Schema
,
schema: :latest
do
describe
ActiveRecord
::
Schema
,
schema: :latest
do
let
(
:
latest_migration_timestamp
)
do
let
(
:
all_migrations
)
do
migrations_paths
=
%w[db/migrate db/post_migrate]
migrations_paths
=
%w[db/migrate db/post_migrate]
.
map
{
|
path
|
Rails
.
root
.
join
(
*
path
,
'*'
)
}
.
map
{
|
path
|
Rails
.
root
.
join
(
*
path
,
'*'
)
}
migrations
=
Dir
[
*
migrations_paths
]
migrations
=
Dir
[
*
migrations_paths
]
migrations
.
map
{
|
migration
|
File
.
basename
(
migration
).
split
(
'_'
).
first
.
to_i
}.
max
migrations
.
map
{
|
migration
|
File
.
basename
(
migration
).
split
(
'_'
).
first
.
to_i
}.
sort
end
end
it
'> schema version equals last migration timestamp'
do
let
(
:latest_migration_timestamp
)
do
defined_schema_version
=
File
.
open
(
Rails
.
root
.
join
(
'db'
,
'schema.rb'
))
do
|
file
|
all_migrations
.
max
file
.
find
{
|
line
|
line
=~
/ActiveRecord::Schema.define/
}
end
.
match
(
/(\d{4}_\d{2}_\d{2}_\d{6})/
)[
0
].
to_i
expect
(
defined_schema_version
).
to
eq
(
latest_migration_timestamp
)
end
end
it
'> schema version should equal the latest migration timestamp stored in schema_migrations table'
do
it
'> schema version should equal the latest migration timestamp stored in schema_migrations table'
do
expect
(
latest_migration_timestamp
).
to
eq
(
ActiveRecord
::
Migrator
.
current_version
.
to_i
)
expect
(
latest_migration_timestamp
).
to
eq
(
ActiveRecord
::
Migrator
.
current_version
.
to_i
)
end
end
it
'the schema_migrations table contains all schema versions'
do
versions
=
ActiveRecord
::
Base
.
connection
.
execute
(
'SELECT version FROM schema_migrations ORDER BY version'
).
map
{
|
m
|
Integer
(
m
[
'version'
])
}
expect
(
versions
).
to
eq
(
all_migrations
)
end
end
end
spec/tasks/gitlab/backup_rake_spec.rb
View file @
59eb9ad2
...
@@ -23,11 +23,7 @@ describe 'gitlab:app namespace rake task' do
...
@@ -23,11 +23,7 @@ describe 'gitlab:app namespace rake task' do
end
end
before
(
:all
)
do
before
(
:all
)
do
Rake
.
application
.
rake_require
'tasks/gitlab/helpers'
Rails
.
application
.
load_tasks
Rake
.
application
.
rake_require
'tasks/gitlab/backup'
Rake
.
application
.
rake_require
'tasks/gitlab/shell'
Rake
.
application
.
rake_require
'tasks/gitlab/db'
Rake
.
application
.
rake_require
'tasks/cache'
# empty task as env is already loaded
# empty task as env is already loaded
Rake
::
Task
.
define_task
:environment
Rake
::
Task
.
define_task
:environment
...
...
spec/tasks/gitlab/db_rake_spec.rb
View file @
59eb9ad2
...
@@ -16,7 +16,7 @@ describe 'gitlab:db namespace rake task' do
...
@@ -16,7 +16,7 @@ describe 'gitlab:db namespace rake task' do
before
do
before
do
# Stub out db tasks
# Stub out db tasks
allow
(
Rake
::
Task
[
'db:migrate'
]).
to
receive
(
:invoke
).
and_return
(
true
)
allow
(
Rake
::
Task
[
'db:migrate'
]).
to
receive
(
:invoke
).
and_return
(
true
)
allow
(
Rake
::
Task
[
'db:s
chema
:load'
]).
to
receive
(
:invoke
).
and_return
(
true
)
allow
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
to
receive
(
:invoke
).
and_return
(
true
)
allow
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
).
and_return
(
true
)
allow
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
).
and_return
(
true
)
end
end
...
@@ -24,14 +24,14 @@ describe 'gitlab:db namespace rake task' do
...
@@ -24,14 +24,14 @@ describe 'gitlab:db namespace rake task' do
it
'invokes db:migrate when schema has already been loaded'
do
it
'invokes db:migrate when schema has already been loaded'
do
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
(
%w[table1 table2]
)
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
(
%w[table1 table2]
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
chema
:load'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
end
end
it
'invokes db:shema:load and db:seed_fu when schema is not loaded'
do
it
'invokes db:shema:load and db:seed_fu when schema is not loaded'
do
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([])
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([])
expect
(
Rake
::
Task
[
'db:s
chema
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
...
@@ -39,7 +39,7 @@ describe 'gitlab:db namespace rake task' do
...
@@ -39,7 +39,7 @@ describe 'gitlab:db namespace rake task' do
it
'invokes db:shema:load and db:seed_fu when there is only a single table present'
do
it
'invokes db:shema:load and db:seed_fu when there is only a single table present'
do
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([
'default'
])
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([
'default'
])
expect
(
Rake
::
Task
[
'db:s
chema
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
...
@@ -48,7 +48,7 @@ describe 'gitlab:db namespace rake task' do
...
@@ -48,7 +48,7 @@ describe 'gitlab:db namespace rake task' do
it
'does not invoke any other rake tasks during an error'
do
it
'does not invoke any other rake tasks during an error'
do
allow
(
ActiveRecord
::
Base
).
to
receive
(
:connection
).
and_raise
(
RuntimeError
,
'error'
)
allow
(
ActiveRecord
::
Base
).
to
receive
(
:connection
).
and_raise
(
RuntimeError
,
'error'
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
chema
:load'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
to
raise_error
(
RuntimeError
,
'error'
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
to
raise_error
(
RuntimeError
,
'error'
)
# unstub connection so that the database cleaner still works
# unstub connection so that the database cleaner still works
...
@@ -57,8 +57,8 @@ describe 'gitlab:db namespace rake task' do
...
@@ -57,8 +57,8 @@ describe 'gitlab:db namespace rake task' do
it
'does not invoke seed after a failed schema_load'
do
it
'does not invoke seed after a failed schema_load'
do
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([])
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([])
allow
(
Rake
::
Task
[
'db:s
chema
:load'
]).
to
receive
(
:invoke
).
and_raise
(
RuntimeError
,
'error'
)
allow
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
to
receive
(
:invoke
).
and_raise
(
RuntimeError
,
'error'
)
expect
(
Rake
::
Task
[
'db:s
chema
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
to
raise_error
(
RuntimeError
,
'error'
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
to
raise_error
(
RuntimeError
,
'error'
)
...
@@ -79,7 +79,7 @@ describe 'gitlab:db namespace rake task' do
...
@@ -79,7 +79,7 @@ describe 'gitlab:db namespace rake task' do
it
'adds post deployment migrations before schema load if the schema is not already loaded'
do
it
'adds post deployment migrations before schema load if the schema is not already loaded'
do
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([])
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
([])
expect
(
Gitlab
::
Database
).
to
receive
(
:add_post_migrate_path_to_rails
).
and_call_original
expect
(
Gitlab
::
Database
).
to
receive
(
:add_post_migrate_path_to_rails
).
and_call_original
expect
(
Rake
::
Task
[
'db:s
chema
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
not_to
receive
(
:invoke
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
...
@@ -90,7 +90,7 @@ describe 'gitlab:db namespace rake task' do
...
@@ -90,7 +90,7 @@ describe 'gitlab:db namespace rake task' do
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
(
%w[table1 table2]
)
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:tables
).
and_return
(
%w[table1 table2]
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:migrate'
]).
to
receive
(
:invoke
)
expect
(
Gitlab
::
Database
).
not_to
receive
(
:add_post_migrate_path_to_rails
)
expect
(
Gitlab
::
Database
).
not_to
receive
(
:add_post_migrate_path_to_rails
)
expect
(
Rake
::
Task
[
'db:s
chema
:load'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:s
tructure
:load'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
(
Rake
::
Task
[
'db:seed_fu'
]).
not_to
receive
(
:invoke
)
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
expect
{
run_rake_task
(
'gitlab:db:configure'
)
}.
not_to
raise_error
expect
(
rails_paths
[
'db/migrate'
].
include?
(
File
.
join
(
Rails
.
root
,
'db'
,
'post_migrate'
))).
to
be
(
false
)
expect
(
rails_paths
[
'db/migrate'
].
include?
(
File
.
join
(
Rails
.
root
,
'db'
,
'post_migrate'
))).
to
be
(
false
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment